1
0

evp_extra_test.c 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892
  1. /*
  2. * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* We need to use some deprecated APIs */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/err.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. #include <openssl/pem.h>
  21. #include <openssl/kdf.h>
  22. #include <openssl/provider.h>
  23. #include <openssl/core_names.h>
  24. #include <openssl/params.h>
  25. #include <openssl/param_build.h>
  26. #include <openssl/dsa.h>
  27. #include <openssl/dh.h>
  28. #include <openssl/aes.h>
  29. #include <openssl/decoder.h>
  30. #include <openssl/rsa.h>
  31. #include <openssl/engine.h>
  32. #include <openssl/proverr.h>
  33. #include "testutil.h"
  34. #include "internal/nelem.h"
  35. #include "internal/sizes.h"
  36. #include "crypto/evp.h"
  37. #include "fake_rsaprov.h"
  38. #ifdef STATIC_LEGACY
  39. OSSL_provider_init_fn ossl_legacy_provider_init;
  40. #endif
  41. static OSSL_LIB_CTX *testctx = NULL;
  42. static char *testpropq = NULL;
  43. static OSSL_PROVIDER *nullprov = NULL;
  44. static OSSL_PROVIDER *deflprov = NULL;
  45. static OSSL_PROVIDER *lgcyprov = NULL;
  46. /*
  47. * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
  48. * should never use this key anywhere but in an example.
  49. */
  50. static const unsigned char kExampleRSAKeyDER[] = {
  51. 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
  52. 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
  53. 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
  54. 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
  55. 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
  56. 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
  57. 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
  58. 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
  59. 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
  60. 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
  61. 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
  62. 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
  63. 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
  64. 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
  65. 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
  66. 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
  67. 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
  68. 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
  69. 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
  70. 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
  71. 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
  72. 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
  73. 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
  74. 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
  75. 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
  76. 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
  77. 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
  78. 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
  79. 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
  80. 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
  81. 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
  82. 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
  83. 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
  84. 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
  85. 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
  86. 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
  87. 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
  88. 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
  89. 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
  90. 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
  91. 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
  92. 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
  93. 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
  94. 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
  95. 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
  96. 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
  97. 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
  98. 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
  99. 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
  100. 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
  101. 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  102. };
  103. /*
  104. * kExampleDSAKeyDER is a DSA private key in ASN.1, DER format. Of course, you
  105. * should never use this key anywhere but in an example.
  106. */
  107. #ifndef OPENSSL_NO_DSA
  108. static const unsigned char kExampleDSAKeyDER[] = {
  109. 0x30, 0x82, 0x01, 0xba, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0x9a,
  110. 0x05, 0x6d, 0x33, 0xcd, 0x5d, 0x78, 0xa1, 0xbb, 0xcb, 0x7d, 0x5b, 0x8d,
  111. 0xb4, 0xcc, 0xbf, 0x03, 0x99, 0x64, 0xde, 0x38, 0x78, 0x06, 0x15, 0x2f,
  112. 0x86, 0x26, 0x77, 0xf3, 0xb1, 0x85, 0x00, 0xed, 0xfc, 0x28, 0x3a, 0x42,
  113. 0x4d, 0xab, 0xab, 0xdf, 0xbc, 0x9c, 0x16, 0xd0, 0x22, 0x50, 0xd1, 0x38,
  114. 0xdd, 0x3f, 0x64, 0x05, 0x9e, 0x68, 0x7a, 0x1e, 0xf1, 0x56, 0xbf, 0x1e,
  115. 0x2c, 0xc5, 0x97, 0x2a, 0xfe, 0x7a, 0x22, 0xdc, 0x6c, 0x68, 0xb8, 0x2e,
  116. 0x06, 0xdb, 0x41, 0xca, 0x98, 0xd8, 0x54, 0xc7, 0x64, 0x48, 0x24, 0x04,
  117. 0x20, 0xbc, 0x59, 0xe3, 0x6b, 0xea, 0x7e, 0xfc, 0x7e, 0xc5, 0x4e, 0xd4,
  118. 0xd8, 0x3a, 0xed, 0xcd, 0x5d, 0x99, 0xb8, 0x5c, 0xa2, 0x8b, 0xbb, 0x0b,
  119. 0xac, 0xe6, 0x8e, 0x25, 0x56, 0x22, 0x3a, 0x2d, 0x3a, 0x56, 0x41, 0x14,
  120. 0x1f, 0x1c, 0x8f, 0x53, 0x46, 0x13, 0x85, 0x02, 0x15, 0x00, 0x98, 0x7e,
  121. 0x92, 0x81, 0x88, 0xc7, 0x3f, 0x70, 0x49, 0x54, 0xf6, 0x76, 0xb4, 0xa3,
  122. 0x9e, 0x1d, 0x45, 0x98, 0x32, 0x7f, 0x02, 0x81, 0x80, 0x69, 0x4d, 0xef,
  123. 0x55, 0xff, 0x4d, 0x59, 0x2c, 0x01, 0xfa, 0x6a, 0x38, 0xe0, 0x70, 0x9f,
  124. 0x9e, 0x66, 0x8e, 0x3e, 0x8c, 0x52, 0x22, 0x9d, 0x15, 0x7e, 0x3c, 0xef,
  125. 0x4c, 0x7a, 0x61, 0x26, 0xe0, 0x2b, 0x81, 0x3f, 0xeb, 0xaf, 0x35, 0x38,
  126. 0x8d, 0xfe, 0xed, 0x46, 0xff, 0x5f, 0x03, 0x9b, 0x81, 0x92, 0xe7, 0x6f,
  127. 0x76, 0x4f, 0x1d, 0xd9, 0xbb, 0x89, 0xc9, 0x3e, 0xd9, 0x0b, 0xf9, 0xf4,
  128. 0x78, 0x11, 0x59, 0xc0, 0x1d, 0xcd, 0x0e, 0xa1, 0x6f, 0x15, 0xf1, 0x4d,
  129. 0xc1, 0xc9, 0x22, 0xed, 0x8d, 0xad, 0x67, 0xc5, 0x4b, 0x95, 0x93, 0x86,
  130. 0xa6, 0xaf, 0x8a, 0xee, 0x06, 0x89, 0x2f, 0x37, 0x7e, 0x64, 0xaa, 0xf6,
  131. 0xe7, 0xb1, 0x5a, 0x0a, 0x93, 0x95, 0x5d, 0x3e, 0x53, 0x9a, 0xde, 0x8a,
  132. 0xc2, 0x95, 0x45, 0x81, 0xbe, 0x5c, 0x2f, 0xc2, 0xb2, 0x92, 0x58, 0x19,
  133. 0x72, 0x80, 0xe9, 0x79, 0xa1, 0x02, 0x81, 0x80, 0x07, 0xd7, 0x62, 0xff,
  134. 0xdf, 0x1a, 0x3f, 0xed, 0x32, 0xd4, 0xd4, 0x88, 0x7b, 0x2c, 0x63, 0x7f,
  135. 0x97, 0xdc, 0x44, 0xd4, 0x84, 0xa2, 0xdd, 0x17, 0x16, 0x85, 0x13, 0xe0,
  136. 0xac, 0x51, 0x8d, 0x29, 0x1b, 0x75, 0x9a, 0xe4, 0xe3, 0x8a, 0x92, 0x69,
  137. 0x09, 0x03, 0xc5, 0x68, 0xae, 0x5e, 0x94, 0xfe, 0xc9, 0x92, 0x6c, 0x07,
  138. 0xb4, 0x1e, 0x64, 0x62, 0x87, 0xc6, 0xa4, 0xfd, 0x0d, 0x5f, 0xe5, 0xf9,
  139. 0x1b, 0x4f, 0x85, 0x5f, 0xae, 0xf3, 0x11, 0xe5, 0x18, 0xd4, 0x4d, 0x79,
  140. 0x9f, 0xc4, 0x79, 0x26, 0x04, 0x27, 0xf0, 0x0b, 0xee, 0x2b, 0x86, 0x9f,
  141. 0x86, 0x61, 0xe6, 0x51, 0xce, 0x04, 0x9b, 0x5d, 0x6b, 0x34, 0x43, 0x8c,
  142. 0x85, 0x3c, 0xf1, 0x51, 0x9b, 0x08, 0x23, 0x1b, 0xf5, 0x7e, 0x33, 0x12,
  143. 0xea, 0xab, 0x1f, 0xb7, 0x2d, 0xe2, 0x5f, 0xe6, 0x97, 0x99, 0xb5, 0x45,
  144. 0x16, 0x5b, 0xc3, 0x41, 0x02, 0x14, 0x61, 0xbf, 0x51, 0x60, 0xcf, 0xc8,
  145. 0xf1, 0x8c, 0x82, 0x97, 0xf2, 0xf4, 0x19, 0xba, 0x2b, 0xf3, 0x16, 0xbe,
  146. 0x40, 0x48
  147. };
  148. #endif
  149. /*
  150. * kExampleBadRSAKeyDER is an RSA private key in ASN.1, DER format. The private
  151. * components are not correct.
  152. */
  153. static const unsigned char kExampleBadRSAKeyDER[] = {
  154. 0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
  155. 0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef,
  156. 0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9,
  157. 0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4,
  158. 0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3,
  159. 0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05,
  160. 0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26,
  161. 0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0,
  162. 0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33,
  163. 0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66,
  164. 0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde,
  165. 0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa,
  166. 0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53,
  167. 0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c,
  168. 0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75,
  169. 0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7,
  170. 0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c,
  171. 0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a,
  172. 0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87,
  173. 0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd,
  174. 0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0,
  175. 0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18,
  176. 0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
  177. 0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5,
  178. 0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06,
  179. 0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0,
  180. 0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75,
  181. 0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1,
  182. 0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00,
  183. 0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd,
  184. 0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29,
  185. 0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5,
  186. 0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05,
  187. 0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e,
  188. 0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf,
  189. 0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d,
  190. 0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb,
  191. 0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5,
  192. 0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef,
  193. 0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9,
  194. 0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2,
  195. 0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7,
  196. 0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff,
  197. 0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99,
  198. 0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78,
  199. 0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb,
  200. 0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35,
  201. 0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46,
  202. 0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6,
  203. 0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f,
  204. 0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56,
  205. 0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d,
  206. 0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55,
  207. 0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20,
  208. 0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6,
  209. 0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81,
  210. 0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a,
  211. 0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e,
  212. 0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3,
  213. 0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1,
  214. 0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6,
  215. 0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77,
  216. 0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a,
  217. 0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d,
  218. 0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b,
  219. 0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a,
  220. 0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf,
  221. 0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28,
  222. 0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12,
  223. 0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd,
  224. 0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a,
  225. 0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5,
  226. 0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e,
  227. 0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b,
  228. 0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84,
  229. 0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e,
  230. 0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97,
  231. 0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81,
  232. 0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b,
  233. 0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6,
  234. 0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74,
  235. 0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e,
  236. 0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9,
  237. 0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98,
  238. 0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05,
  239. 0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef,
  240. 0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf,
  241. 0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69,
  242. 0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce,
  243. };
  244. /*
  245. * kExampleBad2RSAKeyDER is an RSA private key in ASN.1, DER format. All
  246. * values are 0.
  247. */
  248. static const unsigned char kExampleBad2RSAKeyDER[] = {
  249. 0x30, 0x1b, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02,
  250. 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02,
  251. 0x01, 0x00, 0x02, 0x01, 0x00
  252. };
  253. static const unsigned char kMsg[] = { 1, 2, 3, 4 };
  254. static const unsigned char kSignature[] = {
  255. 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
  256. 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
  257. 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
  258. 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
  259. 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
  260. 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
  261. 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
  262. 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
  263. 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
  264. 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
  265. 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
  266. };
  267. /*
  268. * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
  269. * PrivateKeyInfo.
  270. */
  271. static const unsigned char kExampleRSAKeyPKCS8[] = {
  272. 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  273. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
  274. 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
  275. 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
  276. 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
  277. 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
  278. 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
  279. 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
  280. 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
  281. 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
  282. 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
  283. 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
  284. 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
  285. 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
  286. 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
  287. 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
  288. 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
  289. 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
  290. 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
  291. 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
  292. 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
  293. 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
  294. 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
  295. 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
  296. 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
  297. 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
  298. 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
  299. 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
  300. 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
  301. 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
  302. 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
  303. 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
  304. 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
  305. 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
  306. 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
  307. 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
  308. 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
  309. 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
  310. 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
  311. 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
  312. 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
  313. 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
  314. 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
  315. 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
  316. 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
  317. 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
  318. 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
  319. 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
  320. 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
  321. 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
  322. 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
  323. 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
  324. 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
  325. };
  326. #ifndef OPENSSL_NO_EC
  327. /*
  328. * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
  329. * structure.
  330. */
  331. static const unsigned char kExampleECKeyDER[] = {
  332. 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
  333. 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
  334. 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
  335. 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
  336. 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
  337. 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
  338. 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
  339. 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
  340. 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
  341. 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
  342. 0xc1,
  343. };
  344. /*
  345. * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
  346. * structure. The private key is equal to the order and will fail to import
  347. */
  348. static const unsigned char kExampleBadECKeyDER[] = {
  349. 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
  350. 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
  351. 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
  352. 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  353. 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
  354. 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
  355. 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
  356. 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
  357. 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
  358. };
  359. /* prime256v1 */
  360. static const unsigned char kExampleECPubKeyDER[] = {
  361. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  362. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  363. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  364. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  365. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  366. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  367. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  368. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  369. };
  370. /*
  371. * kExampleBadECPubKeyDER is a sample EC public key with a wrong OID
  372. * 1.2.840.10045.2.2 instead of 1.2.840.10045.2.1 - EC Public Key
  373. */
  374. static const unsigned char kExampleBadECPubKeyDER[] = {
  375. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  376. 0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  377. 0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
  378. 0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
  379. 0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
  380. 0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
  381. 0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
  382. 0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
  383. };
  384. static const unsigned char pExampleECParamDER[] = {
  385. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
  386. };
  387. # ifndef OPENSSL_NO_ECX
  388. static const unsigned char kExampleED25519KeyDER[] = {
  389. 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,
  390. 0x04, 0x22, 0x04, 0x20, 0xba, 0x7b, 0xba, 0x20, 0x1b, 0x02, 0x75, 0x3a,
  391. 0xe8, 0x88, 0xfe, 0x00, 0xcd, 0x8b, 0xc6, 0xf4, 0x5c, 0x47, 0x09, 0x46,
  392. 0x66, 0xe4, 0x72, 0x85, 0x25, 0x26, 0x5e, 0x12, 0x33, 0x48, 0xf6, 0x50
  393. };
  394. static const unsigned char kExampleED25519PubKeyDER[] = {
  395. 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
  396. 0xf5, 0xc5, 0xeb, 0x52, 0x3e, 0x7d, 0x07, 0x86, 0xb2, 0x55, 0x07, 0x45,
  397. 0xef, 0x5b, 0x7c, 0x20, 0xe8, 0x66, 0x28, 0x30, 0x3c, 0x8a, 0x82, 0x40,
  398. 0x97, 0xa3, 0x08, 0xdc, 0x65, 0x80, 0x39, 0x29
  399. };
  400. # ifndef OPENSSL_NO_DEPRECATED_3_0
  401. static const unsigned char kExampleX25519KeyDER[] = {
  402. 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
  403. 0x04, 0x22, 0x04, 0x20, 0xa0, 0x24, 0x3a, 0x31, 0x24, 0xc3, 0x3f, 0xf6,
  404. 0x7b, 0x96, 0x0b, 0xd4, 0x8f, 0xd1, 0xee, 0x67, 0xf2, 0x9b, 0x88, 0xac,
  405. 0x50, 0xce, 0x97, 0x36, 0xdd, 0xaf, 0x25, 0xf6, 0x10, 0x34, 0x96, 0x6e
  406. };
  407. # endif
  408. # endif
  409. #endif
  410. /* kExampleDHKeyDER is a DH private key in ASN.1, DER format. */
  411. #ifndef OPENSSL_NO_DEPRECATED_3_0
  412. # ifndef OPENSSL_NO_DH
  413. static const unsigned char kExampleDHKeyDER[] = {
  414. 0x30, 0x82, 0x01, 0x21, 0x02, 0x01, 0x00, 0x30, 0x81, 0x95, 0x06, 0x09,
  415. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x03, 0x01, 0x30, 0x81, 0x87,
  416. 0x02, 0x81, 0x81, 0x00, 0xf7, 0x52, 0xc2, 0x68, 0xcc, 0x66, 0xc4, 0x8d,
  417. 0x03, 0x3f, 0xfa, 0x9c, 0x52, 0xd0, 0xd8, 0x33, 0xf2, 0xe1, 0xc9, 0x9e,
  418. 0xb7, 0xe7, 0x6e, 0x90, 0x97, 0xeb, 0x92, 0x91, 0x6a, 0x9a, 0x85, 0x63,
  419. 0x92, 0x79, 0xab, 0xb6, 0x3d, 0x23, 0x58, 0x5a, 0xe8, 0x45, 0x06, 0x81,
  420. 0x97, 0x77, 0xe1, 0xcc, 0x34, 0x4e, 0xae, 0x36, 0x80, 0xf2, 0xc4, 0x7f,
  421. 0x8a, 0x52, 0xb8, 0xdb, 0x58, 0xc8, 0x4b, 0x12, 0x4c, 0xf1, 0x4c, 0x53,
  422. 0xc1, 0x89, 0x39, 0x8d, 0xb6, 0x06, 0xd8, 0xea, 0x7f, 0x2d, 0x36, 0x53,
  423. 0x96, 0x29, 0xbe, 0xb6, 0x75, 0xfc, 0xe7, 0xf3, 0x36, 0xd6, 0xf4, 0x8f,
  424. 0x16, 0xa6, 0xc7, 0xec, 0x7b, 0xce, 0x42, 0x8d, 0x48, 0x2e, 0xb7, 0x74,
  425. 0x00, 0x11, 0x52, 0x61, 0xb4, 0x19, 0x35, 0xec, 0x5c, 0xe4, 0xbe, 0x34,
  426. 0xc6, 0x59, 0x64, 0x5e, 0x42, 0x61, 0x70, 0x54, 0xf4, 0xe9, 0x6b, 0x53,
  427. 0x02, 0x01, 0x02, 0x04, 0x81, 0x83, 0x02, 0x81, 0x80, 0x64, 0xc2, 0xe3,
  428. 0x09, 0x69, 0x37, 0x3c, 0xd2, 0x4a, 0xba, 0xc3, 0x78, 0x6a, 0x9b, 0x8a,
  429. 0x2a, 0xdb, 0xe7, 0xe6, 0xc0, 0xfa, 0x3a, 0xbe, 0x39, 0x67, 0xc0, 0xa9,
  430. 0x2a, 0xf0, 0x0a, 0xc1, 0x53, 0x1c, 0xdb, 0xfa, 0x1a, 0x26, 0x98, 0xb0,
  431. 0x8c, 0xc6, 0x06, 0x4a, 0xa2, 0x48, 0xd3, 0xa4, 0x3b, 0xbd, 0x05, 0x48,
  432. 0xea, 0x59, 0xdb, 0x18, 0xa4, 0xca, 0x66, 0xd9, 0x5d, 0xb8, 0x95, 0xd1,
  433. 0xeb, 0x97, 0x3d, 0x66, 0x97, 0x5c, 0x86, 0x8f, 0x7e, 0x90, 0xd3, 0x43,
  434. 0xd1, 0xa2, 0x0d, 0xcb, 0xe7, 0xeb, 0x90, 0xea, 0x09, 0x40, 0xb1, 0x6f,
  435. 0xf7, 0x4c, 0xf2, 0x41, 0x83, 0x1d, 0xd0, 0x76, 0xef, 0xaf, 0x55, 0x6f,
  436. 0x5d, 0xa9, 0xa3, 0x55, 0x81, 0x2a, 0xd1, 0x5d, 0x9d, 0x22, 0x77, 0x97,
  437. 0x83, 0xde, 0xad, 0xb6, 0x5d, 0x19, 0xc1, 0x53, 0xec, 0xfb, 0xaf, 0x06,
  438. 0x2e, 0x87, 0x2a, 0x0b, 0x7a
  439. };
  440. # endif
  441. #endif
  442. static const unsigned char kCFBDefaultKey[] = {
  443. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88,
  444. 0x09, 0xCF, 0x4F, 0x3C
  445. };
  446. static const unsigned char kGCMDefaultKey[32] = { 0 };
  447. static const unsigned char kGCMResetKey[] = {
  448. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94,
  449. 0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  450. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  451. };
  452. static const unsigned char iCFBIV[] = {
  453. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  454. 0x0C, 0x0D, 0x0E, 0x0F
  455. };
  456. static const unsigned char iGCMDefaultIV[12] = { 0 };
  457. static const unsigned char iGCMResetIV1[] = {
  458. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad
  459. };
  460. static const unsigned char iGCMResetIV2[] = {
  461. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88
  462. };
  463. static const unsigned char cfbPlaintext[] = {
  464. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11,
  465. 0x73, 0x93, 0x17, 0x2A
  466. };
  467. static const unsigned char cfbPlaintext_partial[] = {
  468. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11,
  469. 0x73, 0x93, 0x17, 0x2A, 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  470. };
  471. static const unsigned char gcmDefaultPlaintext[16] = { 0 };
  472. static const unsigned char gcmResetPlaintext[] = {
  473. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5,
  474. 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  475. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95,
  476. 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  477. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39
  478. };
  479. static const unsigned char cfbCiphertext[] = {
  480. 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8,
  481. 0xE8, 0x3C, 0xFB, 0x4A
  482. };
  483. static const unsigned char cfbCiphertext_partial[] = {
  484. 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8,
  485. 0xE8, 0x3C, 0xFB, 0x4A, 0x0D, 0x4A, 0x71, 0x82, 0x90, 0xF0, 0x9A, 0x35
  486. };
  487. static const unsigned char ofbCiphertext_partial[] = {
  488. 0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8,
  489. 0xE8, 0x3C, 0xFB, 0x4A, 0xB2, 0x65, 0x64, 0x38, 0x26, 0xD2, 0xBC, 0x09
  490. };
  491. static const unsigned char gcmDefaultCiphertext[] = {
  492. 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3,
  493. 0xba, 0xf3, 0x9d, 0x18
  494. };
  495. static const unsigned char gcmResetCiphertext1[] = {
  496. 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32, 0xae, 0x47, 0xc1, 0x3b,
  497. 0xf1, 0x98, 0x44, 0xcb, 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
  498. 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0, 0xfe, 0xb5, 0x82, 0xd3,
  499. 0x39, 0x34, 0xa4, 0xf0, 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
  500. 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99, 0xf4, 0x7c, 0x9b, 0x1f
  501. };
  502. static const unsigned char gcmResetCiphertext2[] = {
  503. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3,
  504. 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  505. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48,
  506. 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  507. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62
  508. };
  509. static const unsigned char gcmAAD[] = {
  510. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce,
  511. 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2
  512. };
  513. static const unsigned char gcmDefaultTag[] = {
  514. 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0, 0x26, 0x5b, 0x98, 0xb5,
  515. 0xd4, 0x8a, 0xb9, 0x19
  516. };
  517. static const unsigned char gcmResetTag1[] = {
  518. 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4, 0x5e, 0x45, 0x49, 0x13,
  519. 0xfe, 0x2e, 0xa8, 0xf2
  520. };
  521. static const unsigned char gcmResetTag2[] = {
  522. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68, 0xcd, 0xdf, 0x88, 0x53,
  523. 0xbb, 0x2d, 0x55, 0x1b
  524. };
  525. typedef struct APK_DATA_st {
  526. const unsigned char *kder;
  527. size_t size;
  528. const char *keytype;
  529. int evptype;
  530. int check;
  531. int pub_check;
  532. int param_check;
  533. int type; /* 0 for private, 1 for public, 2 for params */
  534. } APK_DATA;
  535. static APK_DATA keydata[] = {
  536. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA},
  537. {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), "RSA", EVP_PKEY_RSA},
  538. #ifndef OPENSSL_NO_EC
  539. {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC}
  540. #endif
  541. };
  542. static APK_DATA keycheckdata[] = {
  543. {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA, 1, 1, 1,
  544. 0},
  545. {kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), "RSA", EVP_PKEY_RSA,
  546. 0, 1, 1, 0},
  547. {kExampleBad2RSAKeyDER, sizeof(kExampleBad2RSAKeyDER), "RSA", EVP_PKEY_RSA,
  548. 0, 0, 1 /* Since there are no "params" in an RSA key this passes */, 0},
  549. #ifndef OPENSSL_NO_EC
  550. {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC, 1, 1, 1, 0},
  551. /* group is also associated in our pub key */
  552. {kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), "EC", EVP_PKEY_EC, 0, 1,
  553. 1, 1},
  554. {pExampleECParamDER, sizeof(pExampleECParamDER), "EC", EVP_PKEY_EC, 0, 0, 1,
  555. 2},
  556. # ifndef OPENSSL_NO_ECX
  557. {kExampleED25519KeyDER, sizeof(kExampleED25519KeyDER), "ED25519",
  558. EVP_PKEY_ED25519, 1, 1, 1, 0},
  559. {kExampleED25519PubKeyDER, sizeof(kExampleED25519PubKeyDER), "ED25519",
  560. EVP_PKEY_ED25519, 0, 1, 1, 1},
  561. # endif
  562. #endif
  563. };
  564. static EVP_PKEY *load_example_key(const char *keytype,
  565. const unsigned char *data, size_t data_len)
  566. {
  567. const unsigned char **pdata = &data;
  568. EVP_PKEY *pkey = NULL;
  569. OSSL_DECODER_CTX *dctx =
  570. OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, keytype, 0,
  571. testctx, testpropq);
  572. /* |pkey| will be NULL on error */
  573. (void)OSSL_DECODER_from_data(dctx, pdata, &data_len);
  574. OSSL_DECODER_CTX_free(dctx);
  575. return pkey;
  576. }
  577. static EVP_PKEY *load_example_rsa_key(void)
  578. {
  579. return load_example_key("RSA", kExampleRSAKeyDER,
  580. sizeof(kExampleRSAKeyDER));
  581. }
  582. #ifndef OPENSSL_NO_DSA
  583. static EVP_PKEY *load_example_dsa_key(void)
  584. {
  585. return load_example_key("DSA", kExampleDSAKeyDER,
  586. sizeof(kExampleDSAKeyDER));
  587. }
  588. #endif
  589. #ifndef OPENSSL_NO_EC
  590. static EVP_PKEY *load_example_ec_key(void)
  591. {
  592. return load_example_key("EC", kExampleECKeyDER,
  593. sizeof(kExampleECKeyDER));
  594. }
  595. #endif
  596. #ifndef OPENSSL_NO_DEPRECATED_3_0
  597. # ifndef OPENSSL_NO_DH
  598. static EVP_PKEY *load_example_dh_key(void)
  599. {
  600. return load_example_key("DH", kExampleDHKeyDER,
  601. sizeof(kExampleDHKeyDER));
  602. }
  603. # endif
  604. # ifndef OPENSSL_NO_ECX
  605. static EVP_PKEY *load_example_ed25519_key(void)
  606. {
  607. return load_example_key("ED25519", kExampleED25519KeyDER,
  608. sizeof(kExampleED25519KeyDER));
  609. }
  610. static EVP_PKEY *load_example_x25519_key(void)
  611. {
  612. return load_example_key("X25519", kExampleX25519KeyDER,
  613. sizeof(kExampleX25519KeyDER));
  614. }
  615. # endif
  616. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  617. static EVP_PKEY *load_example_hmac_key(void)
  618. {
  619. EVP_PKEY *pkey = NULL;
  620. unsigned char key[] = {
  621. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  622. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  623. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  624. };
  625. pkey = EVP_PKEY_new_raw_private_key_ex(testctx, "HMAC",
  626. NULL, key, sizeof(key));
  627. if (!TEST_ptr(pkey))
  628. return NULL;
  629. return pkey;
  630. }
  631. static int test_EVP_set_default_properties(void)
  632. {
  633. OSSL_LIB_CTX *ctx;
  634. EVP_MD *md = NULL;
  635. int res = 0;
  636. if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
  637. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
  638. goto err;
  639. EVP_MD_free(md);
  640. md = NULL;
  641. if (!TEST_true(EVP_set_default_properties(ctx, "provider=fizzbang"))
  642. || !TEST_ptr_null(md = EVP_MD_fetch(ctx, "sha256", NULL))
  643. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", "-provider")))
  644. goto err;
  645. EVP_MD_free(md);
  646. md = NULL;
  647. if (!TEST_true(EVP_set_default_properties(ctx, NULL))
  648. || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
  649. goto err;
  650. res = 1;
  651. err:
  652. EVP_MD_free(md);
  653. OSSL_LIB_CTX_free(ctx);
  654. return res;
  655. }
  656. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
  657. static EVP_PKEY *make_key_fromdata(char *keytype, OSSL_PARAM *params)
  658. {
  659. EVP_PKEY_CTX *pctx = NULL;
  660. EVP_PKEY *tmp_pkey = NULL, *pkey = NULL;
  661. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, keytype, testpropq)))
  662. goto err;
  663. /* Check that premature EVP_PKEY_CTX_set_params() fails gracefully */
  664. if (!TEST_int_eq(EVP_PKEY_CTX_set_params(pctx, params), 0)
  665. || !TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  666. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &tmp_pkey, EVP_PKEY_KEYPAIR,
  667. params), 0))
  668. goto err;
  669. if (!TEST_ptr(tmp_pkey))
  670. goto err;
  671. pkey = tmp_pkey;
  672. tmp_pkey = NULL;
  673. err:
  674. EVP_PKEY_free(tmp_pkey);
  675. EVP_PKEY_CTX_free(pctx);
  676. return pkey;
  677. }
  678. static int test_selection(EVP_PKEY *pkey, int selection)
  679. {
  680. int testresult = 0;
  681. int ret;
  682. BIO *bio = BIO_new(BIO_s_mem());
  683. ret = PEM_write_bio_PUBKEY(bio, pkey);
  684. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  685. if (!TEST_true(ret))
  686. goto err;
  687. } else {
  688. if (!TEST_false(ret))
  689. goto err;
  690. }
  691. ret = PEM_write_bio_PrivateKey_ex(bio, pkey, NULL, NULL, 0, NULL, NULL,
  692. testctx, NULL);
  693. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  694. if (!TEST_true(ret))
  695. goto err;
  696. } else {
  697. if (!TEST_false(ret))
  698. goto err;
  699. }
  700. testresult = 1;
  701. err:
  702. BIO_free(bio);
  703. return testresult;
  704. }
  705. #endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA || !OPENSSL_NO_EC */
  706. /*
  707. * Test combinations of private, public, missing and private + public key
  708. * params to ensure they are all accepted
  709. */
  710. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
  711. static int test_EVP_PKEY_ffc_priv_pub(char *keytype)
  712. {
  713. OSSL_PARAM_BLD *bld = NULL;
  714. OSSL_PARAM *params = NULL;
  715. EVP_PKEY *just_params = NULL;
  716. EVP_PKEY *params_and_priv = NULL;
  717. EVP_PKEY *params_and_pub = NULL;
  718. EVP_PKEY *params_and_keypair = NULL;
  719. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  720. int ret = 0;
  721. /*
  722. * Setup the parameters for our pkey object. For our purposes they don't
  723. * have to actually be *valid* parameters. We just need to set something.
  724. */
  725. if (!TEST_ptr(p = BN_new())
  726. || !TEST_ptr(q = BN_new())
  727. || !TEST_ptr(g = BN_new())
  728. || !TEST_ptr(pub = BN_new())
  729. || !TEST_ptr(priv = BN_new()))
  730. goto err;
  731. /* Test !priv and !pub */
  732. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  733. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  734. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  735. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g)))
  736. goto err;
  737. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  738. || !TEST_ptr(just_params = make_key_fromdata(keytype, params)))
  739. goto err;
  740. OSSL_PARAM_free(params);
  741. OSSL_PARAM_BLD_free(bld);
  742. params = NULL;
  743. bld = NULL;
  744. if (!test_selection(just_params, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
  745. || test_selection(just_params, OSSL_KEYMGMT_SELECT_KEYPAIR))
  746. goto err;
  747. /* Test priv and !pub */
  748. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  749. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  750. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  751. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  752. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  753. priv)))
  754. goto err;
  755. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  756. || !TEST_ptr(params_and_priv = make_key_fromdata(keytype, params)))
  757. goto err;
  758. OSSL_PARAM_free(params);
  759. OSSL_PARAM_BLD_free(bld);
  760. params = NULL;
  761. bld = NULL;
  762. if (!test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
  763. || test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
  764. goto err;
  765. /* Test !priv and pub */
  766. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  767. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  768. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  769. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  770. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  771. pub)))
  772. goto err;
  773. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  774. || !TEST_ptr(params_and_pub = make_key_fromdata(keytype, params)))
  775. goto err;
  776. OSSL_PARAM_free(params);
  777. OSSL_PARAM_BLD_free(bld);
  778. params = NULL;
  779. bld = NULL;
  780. if (!test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
  781. || test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
  782. goto err;
  783. /* Test priv and pub */
  784. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  785. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  786. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  787. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  788. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  789. pub))
  790. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  791. priv)))
  792. goto err;
  793. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  794. || !TEST_ptr(params_and_keypair = make_key_fromdata(keytype, params)))
  795. goto err;
  796. if (!test_selection(params_and_keypair, EVP_PKEY_KEYPAIR))
  797. goto err;
  798. ret = 1;
  799. err:
  800. OSSL_PARAM_free(params);
  801. OSSL_PARAM_BLD_free(bld);
  802. EVP_PKEY_free(just_params);
  803. EVP_PKEY_free(params_and_priv);
  804. EVP_PKEY_free(params_and_pub);
  805. EVP_PKEY_free(params_and_keypair);
  806. BN_free(p);
  807. BN_free(q);
  808. BN_free(g);
  809. BN_free(pub);
  810. BN_free(priv);
  811. return ret;
  812. }
  813. #endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA */
  814. /*
  815. * Test combinations of private, public, missing and private + public key
  816. * params to ensure they are all accepted for EC keys
  817. */
  818. #ifndef OPENSSL_NO_EC
  819. static unsigned char ec_priv[] = {
  820. 0xe9, 0x25, 0xf7, 0x66, 0x58, 0xa4, 0xdd, 0x99, 0x61, 0xe7, 0xe8, 0x23,
  821. 0x85, 0xc2, 0xe8, 0x33, 0x27, 0xc5, 0x5c, 0xeb, 0xdb, 0x43, 0x9f, 0xd5,
  822. 0xf2, 0x5a, 0x75, 0x55, 0xd0, 0x2e, 0x6d, 0x16
  823. };
  824. static unsigned char ec_pub[] = {
  825. 0x04, 0xad, 0x11, 0x90, 0x77, 0x4b, 0x46, 0xee, 0x72, 0x51, 0x15, 0x97,
  826. 0x4a, 0x6a, 0xa7, 0xaf, 0x59, 0xfa, 0x4b, 0xf2, 0x41, 0xc8, 0x3a, 0x81,
  827. 0x23, 0xb6, 0x90, 0x04, 0x6c, 0x67, 0x66, 0xd0, 0xdc, 0xf2, 0x15, 0x1d,
  828. 0x41, 0x61, 0xb7, 0x95, 0x85, 0x38, 0x5a, 0x84, 0x56, 0xe8, 0xb3, 0x0e,
  829. 0xf5, 0xc6, 0x5d, 0xa4, 0x54, 0x26, 0xb0, 0xf7, 0xa5, 0x4a, 0x33, 0xf1,
  830. 0x08, 0x09, 0xb8, 0xdb, 0x03
  831. };
  832. static int test_EC_priv_pub(void)
  833. {
  834. OSSL_PARAM_BLD *bld = NULL;
  835. OSSL_PARAM *params = NULL;
  836. EVP_PKEY *just_params = NULL;
  837. EVP_PKEY *params_and_priv = NULL;
  838. EVP_PKEY *params_and_pub = NULL;
  839. EVP_PKEY *params_and_keypair = NULL;
  840. BIGNUM *priv = NULL;
  841. int ret = 0;
  842. unsigned char *encoded = NULL;
  843. size_t len = 0;
  844. unsigned char buffer[128];
  845. /*
  846. * Setup the parameters for our pkey object. For our purposes they don't
  847. * have to actually be *valid* parameters. We just need to set something.
  848. */
  849. if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
  850. goto err;
  851. /* Test !priv and !pub */
  852. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  853. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  854. OSSL_PKEY_PARAM_GROUP_NAME,
  855. "P-256", 0)))
  856. goto err;
  857. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  858. || !TEST_ptr(just_params = make_key_fromdata("EC", params)))
  859. goto err;
  860. OSSL_PARAM_free(params);
  861. OSSL_PARAM_BLD_free(bld);
  862. params = NULL;
  863. bld = NULL;
  864. if (!test_selection(just_params, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
  865. || test_selection(just_params, OSSL_KEYMGMT_SELECT_KEYPAIR))
  866. goto err;
  867. /* Test priv and !pub */
  868. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  869. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  870. OSSL_PKEY_PARAM_GROUP_NAME,
  871. "P-256", 0))
  872. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  873. priv)))
  874. goto err;
  875. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  876. || !TEST_ptr(params_and_priv = make_key_fromdata("EC", params)))
  877. goto err;
  878. OSSL_PARAM_free(params);
  879. OSSL_PARAM_BLD_free(bld);
  880. params = NULL;
  881. bld = NULL;
  882. /*
  883. * We indicate only parameters here, in spite of having built a key that
  884. * has a private part, because the PEM_write_bio_PrivateKey_ex call is
  885. * expected to fail because it does not support exporting a private EC
  886. * key without a corresponding public key
  887. */
  888. if (!test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
  889. || test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
  890. goto err;
  891. /* Test !priv and pub */
  892. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  893. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  894. OSSL_PKEY_PARAM_GROUP_NAME,
  895. "P-256", 0))
  896. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  897. OSSL_PKEY_PARAM_PUB_KEY,
  898. ec_pub, sizeof(ec_pub))))
  899. goto err;
  900. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  901. || !TEST_ptr(params_and_pub = make_key_fromdata("EC", params)))
  902. goto err;
  903. OSSL_PARAM_free(params);
  904. OSSL_PARAM_BLD_free(bld);
  905. params = NULL;
  906. bld = NULL;
  907. if (!test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
  908. || test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
  909. goto err;
  910. /* Test priv and pub */
  911. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  912. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  913. OSSL_PKEY_PARAM_GROUP_NAME,
  914. "P-256", 0))
  915. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  916. OSSL_PKEY_PARAM_PUB_KEY,
  917. ec_pub, sizeof(ec_pub)))
  918. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  919. priv)))
  920. goto err;
  921. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  922. || !TEST_ptr(params_and_keypair = make_key_fromdata("EC", params)))
  923. goto err;
  924. if (!test_selection(params_and_keypair, EVP_PKEY_KEYPAIR))
  925. goto err;
  926. /* Try key equality */
  927. if (!TEST_int_gt(EVP_PKEY_parameters_eq(just_params, just_params), 0)
  928. || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_pub),
  929. 0)
  930. || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_priv),
  931. 0)
  932. || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_keypair),
  933. 0)
  934. || !TEST_int_gt(EVP_PKEY_eq(params_and_pub, params_and_pub), 0)
  935. || !TEST_int_gt(EVP_PKEY_eq(params_and_priv, params_and_priv), 0)
  936. || !TEST_int_gt(EVP_PKEY_eq(params_and_keypair, params_and_pub), 0)
  937. || !TEST_int_gt(EVP_PKEY_eq(params_and_keypair, params_and_priv), 0))
  938. goto err;
  939. /* Positive and negative testcase for EVP_PKEY_get1_encoded_public_key */
  940. if (!TEST_int_gt(EVP_PKEY_get1_encoded_public_key(params_and_pub, &encoded), 0))
  941. goto err;
  942. OPENSSL_free(encoded);
  943. encoded = NULL;
  944. if (!TEST_int_eq(EVP_PKEY_get1_encoded_public_key(just_params, &encoded), 0)) {
  945. OPENSSL_free(encoded);
  946. encoded = NULL;
  947. goto err;
  948. }
  949. /* Positive and negative testcase for EVP_PKEY_get_octet_string_param */
  950. if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
  951. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  952. buffer, sizeof(buffer), &len), 1)
  953. || !TEST_int_eq(len, 65))
  954. goto err;
  955. len = 0;
  956. if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
  957. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  958. NULL, 0, &len), 1)
  959. || !TEST_int_eq(len, 65))
  960. goto err;
  961. /* too-short buffer len*/
  962. if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
  963. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  964. buffer, 10, &len), 0))
  965. goto err;
  966. ret = 1;
  967. err:
  968. OSSL_PARAM_free(params);
  969. OSSL_PARAM_BLD_free(bld);
  970. EVP_PKEY_free(just_params);
  971. EVP_PKEY_free(params_and_priv);
  972. EVP_PKEY_free(params_and_pub);
  973. EVP_PKEY_free(params_and_keypair);
  974. BN_free(priv);
  975. return ret;
  976. }
  977. /* Also test that we can read the EC PUB affine coordinates */
  978. static int test_evp_get_ec_pub(void)
  979. {
  980. OSSL_PARAM_BLD *bld = NULL;
  981. OSSL_PARAM *params = NULL;
  982. unsigned char *pad = NULL;
  983. EVP_PKEY *keypair = NULL;
  984. BIGNUM *priv = NULL;
  985. BIGNUM *x = NULL;
  986. BIGNUM *y = NULL;
  987. int ret = 0;
  988. if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
  989. goto err;
  990. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
  991. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  992. OSSL_PKEY_PARAM_GROUP_NAME,
  993. "P-256", 0))
  994. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  995. OSSL_PKEY_PARAM_PUB_KEY,
  996. ec_pub, sizeof(ec_pub)))
  997. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  998. priv)))
  999. goto err;
  1000. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  1001. || !TEST_ptr(keypair = make_key_fromdata("EC", params)))
  1002. goto err;
  1003. if (!test_selection(keypair, EVP_PKEY_KEYPAIR))
  1004. goto err;
  1005. if (!EVP_PKEY_get_bn_param(keypair, OSSL_PKEY_PARAM_EC_PUB_X, &x)
  1006. || !EVP_PKEY_get_bn_param(keypair, OSSL_PKEY_PARAM_EC_PUB_Y, &y))
  1007. goto err;
  1008. if (!TEST_ptr(pad = OPENSSL_zalloc(sizeof(ec_pub))))
  1009. goto err;
  1010. pad[0] = ec_pub[0];
  1011. BN_bn2bin(x, &pad[1]);
  1012. BN_bn2bin(y, &pad[33]);
  1013. if (!TEST_true(memcmp(ec_pub, pad, sizeof(ec_pub)) == 0))
  1014. goto err;
  1015. ret = 1;
  1016. err:
  1017. OSSL_PARAM_free(params);
  1018. OSSL_PARAM_BLD_free(bld);
  1019. EVP_PKEY_free(keypair);
  1020. OPENSSL_free(pad);
  1021. BN_free(priv);
  1022. BN_free(x);
  1023. BN_free(y);
  1024. return ret;
  1025. }
  1026. /* Test that using a legacy EC key with only a private key in it works */
  1027. # ifndef OPENSSL_NO_DEPRECATED_3_0
  1028. static int test_EC_priv_only_legacy(void)
  1029. {
  1030. BIGNUM *priv = NULL;
  1031. int ret = 0;
  1032. EC_KEY *eckey = NULL;
  1033. EVP_PKEY *pkey = NULL, *dup_pk = NULL;
  1034. EVP_MD_CTX *ctx = NULL;
  1035. /* Create the low level EC_KEY */
  1036. if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
  1037. goto err;
  1038. eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  1039. if (!TEST_ptr(eckey))
  1040. goto err;
  1041. if (!TEST_true(EC_KEY_set_private_key(eckey, priv)))
  1042. goto err;
  1043. pkey = EVP_PKEY_new();
  1044. if (!TEST_ptr(pkey))
  1045. goto err;
  1046. if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  1047. goto err;
  1048. eckey = NULL;
  1049. for (;;) {
  1050. ret = 0;
  1051. ctx = EVP_MD_CTX_new();
  1052. if (!TEST_ptr(ctx))
  1053. goto err;
  1054. /*
  1055. * The EVP_DigestSignInit function should create the key on the
  1056. * provider side which is sufficient for this test.
  1057. */
  1058. if (!TEST_true(EVP_DigestSignInit_ex(ctx, NULL, NULL, testctx,
  1059. testpropq, pkey, NULL)))
  1060. goto err;
  1061. EVP_MD_CTX_free(ctx);
  1062. ctx = NULL;
  1063. if (dup_pk != NULL)
  1064. break;
  1065. if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)))
  1066. goto err;
  1067. /* EVP_PKEY_eq() returns -2 with missing public keys */
  1068. ret = TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), -2);
  1069. EVP_PKEY_free(pkey);
  1070. pkey = dup_pk;
  1071. if (!ret)
  1072. goto err;
  1073. }
  1074. ret = 1;
  1075. err:
  1076. EVP_MD_CTX_free(ctx);
  1077. EVP_PKEY_free(pkey);
  1078. EC_KEY_free(eckey);
  1079. BN_free(priv);
  1080. return ret;
  1081. }
  1082. static int test_evp_get_ec_pub_legacy(void)
  1083. {
  1084. OSSL_LIB_CTX *libctx = NULL;
  1085. unsigned char *pad = NULL;
  1086. EVP_PKEY *pkey = NULL;
  1087. EC_KEY *eckey = NULL;
  1088. BIGNUM *priv = NULL;
  1089. BIGNUM *x = NULL;
  1090. BIGNUM *y = NULL;
  1091. int ret = 0;
  1092. if (!TEST_ptr(libctx = OSSL_LIB_CTX_new()))
  1093. goto err;
  1094. /* Create the legacy key */
  1095. if (!TEST_ptr(eckey = EC_KEY_new_by_curve_name_ex(libctx, NULL,
  1096. NID_X9_62_prime256v1)))
  1097. goto err;
  1098. if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
  1099. goto err;
  1100. if (!TEST_true(EC_KEY_set_private_key(eckey, priv)))
  1101. goto err;
  1102. if (!TEST_ptr(x = BN_bin2bn(&ec_pub[1], 32, NULL)))
  1103. goto err;
  1104. if (!TEST_ptr(y = BN_bin2bn(&ec_pub[33], 32, NULL)))
  1105. goto err;
  1106. if (!TEST_true(EC_KEY_set_public_key_affine_coordinates(eckey, x, y)))
  1107. goto err;
  1108. if (!TEST_ptr(pkey = EVP_PKEY_new()))
  1109. goto err;
  1110. /* Transfer the legacy key */
  1111. if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
  1112. goto err;
  1113. eckey = NULL;
  1114. if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_X, &x))
  1115. || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, &y)))
  1116. goto err;
  1117. if (!TEST_ptr(pad = OPENSSL_zalloc(sizeof(ec_pub))))
  1118. goto err;
  1119. pad[0] = ec_pub[0];
  1120. BN_bn2bin(x, &pad[1]);
  1121. BN_bn2bin(y, &pad[33]);
  1122. if (!TEST_true(memcmp(ec_pub, pad, sizeof(ec_pub)) == 0))
  1123. goto err;
  1124. ret = 1;
  1125. err:
  1126. OSSL_LIB_CTX_free(libctx);
  1127. EVP_PKEY_free(pkey);
  1128. EC_KEY_free(eckey);
  1129. OPENSSL_free(pad);
  1130. BN_free(priv);
  1131. BN_free(x);
  1132. BN_free(y);
  1133. return ret;
  1134. }
  1135. # endif /* OPENSSL_NO_DEPRECATED_3_0 */
  1136. #endif /* OPENSSL_NO_EC */
  1137. static int test_EVP_PKEY_sign(int tst)
  1138. {
  1139. int ret = 0;
  1140. EVP_PKEY *pkey = NULL;
  1141. unsigned char *sig = NULL;
  1142. size_t sig_len = 0, shortsig_len = 1;
  1143. EVP_PKEY_CTX *ctx = NULL;
  1144. unsigned char tbs[] = {
  1145. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1146. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13
  1147. };
  1148. if (tst == 0) {
  1149. if (!TEST_ptr(pkey = load_example_rsa_key()))
  1150. goto out;
  1151. } else if (tst == 1) {
  1152. #ifndef OPENSSL_NO_DSA
  1153. if (!TEST_ptr(pkey = load_example_dsa_key()))
  1154. goto out;
  1155. #else
  1156. ret = 1;
  1157. goto out;
  1158. #endif
  1159. } else {
  1160. #ifndef OPENSSL_NO_EC
  1161. if (!TEST_ptr(pkey = load_example_ec_key()))
  1162. goto out;
  1163. #else
  1164. ret = 1;
  1165. goto out;
  1166. #endif
  1167. }
  1168. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, NULL);
  1169. if (!TEST_ptr(ctx)
  1170. || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0)
  1171. || !TEST_int_gt(EVP_PKEY_sign(ctx, NULL, &sig_len, tbs,
  1172. sizeof(tbs)), 0))
  1173. goto out;
  1174. sig = OPENSSL_malloc(sig_len);
  1175. if (!TEST_ptr(sig)
  1176. /* Test sending a signature buffer that is too short is rejected */
  1177. || !TEST_int_le(EVP_PKEY_sign(ctx, sig, &shortsig_len, tbs,
  1178. sizeof(tbs)), 0)
  1179. || !TEST_int_gt(EVP_PKEY_sign(ctx, sig, &sig_len, tbs, sizeof(tbs)),
  1180. 0)
  1181. /* Test the signature round-trips */
  1182. || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0)
  1183. || !TEST_int_gt(EVP_PKEY_verify(ctx, sig, sig_len, tbs, sizeof(tbs)),
  1184. 0))
  1185. goto out;
  1186. ret = 1;
  1187. out:
  1188. EVP_PKEY_CTX_free(ctx);
  1189. OPENSSL_free(sig);
  1190. EVP_PKEY_free(pkey);
  1191. return ret;
  1192. }
  1193. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1194. static int test_EVP_PKEY_sign_with_app_method(int tst)
  1195. {
  1196. int ret = 0;
  1197. EVP_PKEY *pkey = NULL;
  1198. RSA *rsa = NULL;
  1199. RSA_METHOD *rsa_meth = NULL;
  1200. #ifndef OPENSSL_NO_DSA
  1201. DSA *dsa = NULL;
  1202. DSA_METHOD *dsa_meth = NULL;
  1203. #endif
  1204. unsigned char *sig = NULL;
  1205. size_t sig_len = 0, shortsig_len = 1;
  1206. EVP_PKEY_CTX *ctx = NULL;
  1207. unsigned char tbs[] = {
  1208. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  1209. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13
  1210. };
  1211. if (tst == 0) {
  1212. if (!TEST_ptr(pkey = load_example_rsa_key()))
  1213. goto out;
  1214. if (!TEST_ptr(rsa_meth = RSA_meth_dup(RSA_get_default_method())))
  1215. goto out;
  1216. if (!TEST_ptr(rsa = EVP_PKEY_get1_RSA(pkey))
  1217. || !TEST_int_gt(RSA_set_method(rsa, rsa_meth), 0)
  1218. || !TEST_int_gt(EVP_PKEY_assign_RSA(pkey, rsa), 0))
  1219. goto out;
  1220. rsa = NULL; /* now owned by the pkey */
  1221. } else {
  1222. #ifndef OPENSSL_NO_DSA
  1223. if (!TEST_ptr(pkey = load_example_dsa_key()))
  1224. goto out;
  1225. if (!TEST_ptr(dsa_meth = DSA_meth_dup(DSA_get_default_method())))
  1226. goto out;
  1227. if (!TEST_ptr(dsa = EVP_PKEY_get1_DSA(pkey))
  1228. || !TEST_int_gt(DSA_set_method(dsa, dsa_meth), 0)
  1229. || !TEST_int_gt(EVP_PKEY_assign_DSA(pkey, dsa), 0))
  1230. goto out;
  1231. dsa = NULL; /* now owned by the pkey */
  1232. #else
  1233. ret = 1;
  1234. goto out;
  1235. #endif
  1236. }
  1237. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, NULL);
  1238. if (!TEST_ptr(ctx)
  1239. || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0)
  1240. || !TEST_int_gt(EVP_PKEY_sign(ctx, NULL, &sig_len, tbs,
  1241. sizeof(tbs)), 0))
  1242. goto out;
  1243. sig = OPENSSL_malloc(sig_len);
  1244. if (!TEST_ptr(sig)
  1245. /* Test sending a signature buffer that is too short is rejected */
  1246. || !TEST_int_le(EVP_PKEY_sign(ctx, sig, &shortsig_len, tbs,
  1247. sizeof(tbs)), 0)
  1248. || !TEST_int_gt(EVP_PKEY_sign(ctx, sig, &sig_len, tbs, sizeof(tbs)),
  1249. 0)
  1250. /* Test the signature round-trips */
  1251. || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0)
  1252. || !TEST_int_gt(EVP_PKEY_verify(ctx, sig, sig_len, tbs, sizeof(tbs)),
  1253. 0))
  1254. goto out;
  1255. ret = 1;
  1256. out:
  1257. EVP_PKEY_CTX_free(ctx);
  1258. OPENSSL_free(sig);
  1259. EVP_PKEY_free(pkey);
  1260. RSA_free(rsa);
  1261. RSA_meth_free(rsa_meth);
  1262. #ifndef OPENSSL_NO_DSA
  1263. DSA_free(dsa);
  1264. DSA_meth_free(dsa_meth);
  1265. #endif
  1266. return ret;
  1267. }
  1268. #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
  1269. /*
  1270. * n = 0 => test using legacy cipher
  1271. * n = 1 => test using fetched cipher
  1272. */
  1273. static int test_EVP_Enveloped(int n)
  1274. {
  1275. int ret = 0;
  1276. EVP_CIPHER_CTX *ctx = NULL;
  1277. EVP_PKEY *keypair = NULL;
  1278. unsigned char *kek = NULL;
  1279. unsigned char iv[EVP_MAX_IV_LENGTH];
  1280. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  1281. int len, kek_len, ciphertext_len, plaintext_len;
  1282. unsigned char ciphertext[32], plaintext[16];
  1283. EVP_CIPHER *type = NULL;
  1284. if (nullprov != NULL)
  1285. return TEST_skip("Test does not support a non-default library context");
  1286. if (n == 0)
  1287. type = (EVP_CIPHER *)EVP_aes_256_cbc();
  1288. else if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "AES-256-CBC",
  1289. testpropq)))
  1290. goto err;
  1291. if (!TEST_ptr(keypair = load_example_rsa_key())
  1292. || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_get_size(keypair)))
  1293. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  1294. || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,
  1295. &keypair, 1))
  1296. || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,
  1297. msg, sizeof(msg)))
  1298. || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,
  1299. &len)))
  1300. goto err;
  1301. ciphertext_len += len;
  1302. if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))
  1303. || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,
  1304. ciphertext, ciphertext_len))
  1305. || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))
  1306. goto err;
  1307. plaintext_len += len;
  1308. if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))
  1309. goto err;
  1310. ret = 1;
  1311. err:
  1312. if (n != 0)
  1313. EVP_CIPHER_free(type);
  1314. OPENSSL_free(kek);
  1315. EVP_PKEY_free(keypair);
  1316. EVP_CIPHER_CTX_free(ctx);
  1317. return ret;
  1318. }
  1319. /*
  1320. * Test 0: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, RSA)
  1321. * Test 1: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, DSA)
  1322. * Test 2: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, HMAC)
  1323. * Test 3: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, RSA)
  1324. * Test 4: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, DSA)
  1325. * Test 5: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch diegst, HMAC)
  1326. * Test 6: Use an MD BIO to do the Update calls instead (RSA)
  1327. * Test 7: Use an MD BIO to do the Update calls instead (DSA)
  1328. * Test 8: Use an MD BIO to do the Update calls instead (HMAC)
  1329. * Test 9: Use EVP_DigestSign (Implicit fetch digest, RSA, short sig)
  1330. * Test 10: Use EVP_DigestSign (Implicit fetch digest, DSA, short sig)
  1331. * Test 11: Use EVP_DigestSign (Implicit fetch digest, HMAC, short sig)
  1332. * Test 12: Use EVP_DigestSign (Implicit fetch digest, RSA)
  1333. * Test 13: Use EVP_DigestSign (Implicit fetch digest, DSA)
  1334. * Test 14: Use EVP_DigestSign (Implicit fetch digest, HMAC)
  1335. * Test 15-29: Same as above with reinitialization
  1336. */
  1337. static int test_EVP_DigestSignInit(int tst)
  1338. {
  1339. int ret = 0;
  1340. EVP_PKEY *pkey = NULL;
  1341. unsigned char *sig = NULL, *sig2 = NULL;
  1342. size_t sig_len = 0, sig2_len = 0, shortsig_len = 1;
  1343. EVP_MD_CTX *md_ctx = NULL, *md_ctx_verify = NULL;
  1344. EVP_MD_CTX *a_md_ctx = NULL, *a_md_ctx_verify = NULL;
  1345. BIO *mdbio = NULL, *membio = NULL;
  1346. size_t written;
  1347. const EVP_MD *md;
  1348. EVP_MD *mdexp = NULL;
  1349. int reinit = 0;
  1350. if (nullprov != NULL)
  1351. return TEST_skip("Test does not support a non-default library context");
  1352. if (tst >= 15) {
  1353. reinit = 1;
  1354. tst -= 15;
  1355. }
  1356. if (tst >= 6 && tst <= 8) {
  1357. membio = BIO_new(BIO_s_mem());
  1358. mdbio = BIO_new(BIO_f_md());
  1359. if (!TEST_ptr(membio) || !TEST_ptr(mdbio))
  1360. goto out;
  1361. BIO_push(mdbio, membio);
  1362. if (!TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx), 0))
  1363. goto out;
  1364. } else {
  1365. if (!TEST_ptr(a_md_ctx = md_ctx = EVP_MD_CTX_new())
  1366. || !TEST_ptr(a_md_ctx_verify = md_ctx_verify = EVP_MD_CTX_new()))
  1367. goto out;
  1368. }
  1369. if (tst % 3 == 0) {
  1370. if (!TEST_ptr(pkey = load_example_rsa_key()))
  1371. goto out;
  1372. } else if (tst % 3 == 1) {
  1373. #ifndef OPENSSL_NO_DSA
  1374. if (!TEST_ptr(pkey = load_example_dsa_key()))
  1375. goto out;
  1376. #else
  1377. ret = 1;
  1378. goto out;
  1379. #endif
  1380. } else {
  1381. if (!TEST_ptr(pkey = load_example_hmac_key()))
  1382. goto out;
  1383. }
  1384. if (tst >= 3 && tst <= 5)
  1385. md = mdexp = EVP_MD_fetch(NULL, "SHA256", NULL);
  1386. else
  1387. md = EVP_sha256();
  1388. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, md, NULL, pkey)))
  1389. goto out;
  1390. if (reinit && !TEST_true(EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, NULL)))
  1391. goto out;
  1392. if (tst >= 6 && tst <= 8) {
  1393. if (!BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written))
  1394. goto out;
  1395. } else if (tst < 6) {
  1396. if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  1397. goto out;
  1398. }
  1399. if (tst >= 9) {
  1400. /* Determine the size of the signature. */
  1401. if (!TEST_true(EVP_DigestSign(md_ctx, NULL, &sig_len, kMsg,
  1402. sizeof(kMsg)))
  1403. || !TEST_ptr(sig = OPENSSL_malloc(sig_len)))
  1404. goto out;
  1405. if (tst <= 11) {
  1406. /* Test that supply a short sig buffer fails */
  1407. if (!TEST_false(EVP_DigestSign(md_ctx, sig, &shortsig_len, kMsg,
  1408. sizeof(kMsg))))
  1409. goto out;
  1410. /*
  1411. * We end here because once EVP_DigestSign() has failed you should
  1412. * not call it again without re-initing the ctx
  1413. */
  1414. ret = 1;
  1415. goto out;
  1416. }
  1417. if (!TEST_true(EVP_DigestSign(md_ctx, sig, &sig_len, kMsg,
  1418. sizeof(kMsg))))
  1419. goto out;
  1420. } else {
  1421. /* Determine the size of the signature. */
  1422. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
  1423. || !TEST_ptr(sig = OPENSSL_malloc(sig_len))
  1424. /*
  1425. * Trying to create a signature with a deliberately short
  1426. * buffer should fail.
  1427. */
  1428. || !TEST_false(EVP_DigestSignFinal(md_ctx, sig, &shortsig_len))
  1429. || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  1430. goto out;
  1431. }
  1432. /*
  1433. * Ensure that the signature round-trips (Verification isn't supported for
  1434. * HMAC via EVP_DigestVerify*)
  1435. */
  1436. if (tst % 3 != 2) {
  1437. if (tst >= 6 && tst <= 8) {
  1438. if (!TEST_int_gt(BIO_reset(mdbio), 0)
  1439. || !TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx_verify), 0))
  1440. goto out;
  1441. }
  1442. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, md,
  1443. NULL, pkey)))
  1444. goto out;
  1445. if (tst >= 6 && tst <= 8) {
  1446. if (!TEST_true(BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written)))
  1447. goto out;
  1448. } else {
  1449. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg,
  1450. sizeof(kMsg))))
  1451. goto out;
  1452. }
  1453. if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
  1454. goto out;
  1455. /* Multiple calls to EVP_DigestVerifyFinal should work */
  1456. if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
  1457. goto out;
  1458. } else {
  1459. /*
  1460. * For HMAC a doubled call to DigestSignFinal should produce the same
  1461. * value as finalization should not happen.
  1462. */
  1463. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig2_len))
  1464. || !TEST_ptr(sig2 = OPENSSL_malloc(sig2_len))
  1465. || !TEST_true(EVP_DigestSignFinal(md_ctx, sig2, &sig2_len)))
  1466. goto out;
  1467. if (!TEST_mem_eq(sig, sig_len, sig2, sig2_len))
  1468. goto out;
  1469. }
  1470. ret = 1;
  1471. out:
  1472. BIO_free(membio);
  1473. BIO_free(mdbio);
  1474. EVP_MD_CTX_free(a_md_ctx);
  1475. EVP_MD_CTX_free(a_md_ctx_verify);
  1476. EVP_PKEY_free(pkey);
  1477. OPENSSL_free(sig);
  1478. OPENSSL_free(sig2);
  1479. EVP_MD_free(mdexp);
  1480. return ret;
  1481. }
  1482. static int test_EVP_DigestVerifyInit(void)
  1483. {
  1484. int ret = 0;
  1485. EVP_PKEY *pkey = NULL;
  1486. EVP_MD_CTX *md_ctx = NULL;
  1487. if (nullprov != NULL)
  1488. return TEST_skip("Test does not support a non-default library context");
  1489. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
  1490. || !TEST_ptr(pkey = load_example_rsa_key()))
  1491. goto out;
  1492. if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
  1493. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
  1494. || !TEST_int_gt(EVP_DigestVerifyFinal(md_ctx, kSignature,
  1495. sizeof(kSignature)), 0))
  1496. goto out;
  1497. /* test with reinitialization */
  1498. if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, NULL, NULL, NULL))
  1499. || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
  1500. || !TEST_int_gt(EVP_DigestVerifyFinal(md_ctx, kSignature,
  1501. sizeof(kSignature)), 0))
  1502. goto out;
  1503. ret = 1;
  1504. out:
  1505. EVP_MD_CTX_free(md_ctx);
  1506. EVP_PKEY_free(pkey);
  1507. return ret;
  1508. }
  1509. #ifndef OPENSSL_NO_SIPHASH
  1510. /* test SIPHASH MAC via EVP_PKEY with non-default parameters and reinit */
  1511. static int test_siphash_digestsign(void)
  1512. {
  1513. unsigned char key[16];
  1514. unsigned char buf[8], digest[8];
  1515. unsigned char expected[8] = {
  1516. 0x6d, 0x3e, 0x54, 0xc2, 0x2f, 0xf1, 0xfe, 0xe2
  1517. };
  1518. EVP_PKEY *pkey = NULL;
  1519. EVP_MD_CTX *mdctx = NULL;
  1520. EVP_PKEY_CTX *ctx = NULL;
  1521. int ret = 0;
  1522. size_t len = 8;
  1523. if (nullprov != NULL)
  1524. return TEST_skip("Test does not support a non-default library context");
  1525. memset(buf, 0, 8);
  1526. memset(key, 1, 16);
  1527. if (!TEST_ptr(pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_SIPHASH, NULL,
  1528. key, 16)))
  1529. goto out;
  1530. if (!TEST_ptr(mdctx = EVP_MD_CTX_create()))
  1531. goto out;
  1532. if (!TEST_true(EVP_DigestSignInit(mdctx, &ctx, NULL, NULL, pkey)))
  1533. goto out;
  1534. if (!TEST_int_eq(EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_SIGNCTX,
  1535. EVP_PKEY_CTRL_SET_DIGEST_SIZE,
  1536. 8, NULL), 1))
  1537. goto out;
  1538. /* reinitialize */
  1539. if (!TEST_true(EVP_DigestSignInit(mdctx, NULL, NULL, NULL, NULL)))
  1540. goto out;
  1541. if (!TEST_true(EVP_DigestSignUpdate(mdctx, buf, 8)))
  1542. goto out;
  1543. if (!TEST_true(EVP_DigestSignFinal(mdctx, digest, &len)))
  1544. goto out;
  1545. if (!TEST_mem_eq(digest, len, expected, sizeof(expected)))
  1546. goto out;
  1547. ret = 1;
  1548. out:
  1549. EVP_PKEY_free(pkey);
  1550. EVP_MD_CTX_free(mdctx);
  1551. return ret;
  1552. }
  1553. #endif
  1554. /*
  1555. * Test corner cases of EVP_DigestInit/Update/Final API call behavior.
  1556. */
  1557. static int test_EVP_Digest(void)
  1558. {
  1559. int ret = 0;
  1560. EVP_MD_CTX *md_ctx = NULL;
  1561. unsigned char md[EVP_MAX_MD_SIZE];
  1562. EVP_MD *sha256 = NULL;
  1563. EVP_MD *shake256 = NULL;
  1564. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  1565. goto out;
  1566. if (!TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", testpropq))
  1567. || !TEST_ptr(shake256 = EVP_MD_fetch(testctx, "shake256", testpropq)))
  1568. goto out;
  1569. if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
  1570. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  1571. || !TEST_true(EVP_DigestFinal(md_ctx, md, NULL))
  1572. /* EVP_DigestFinal resets the EVP_MD_CTX. */
  1573. || !TEST_ptr_eq(EVP_MD_CTX_get0_md(md_ctx), NULL))
  1574. goto out;
  1575. if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
  1576. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  1577. || !TEST_true(EVP_DigestFinal_ex(md_ctx, md, NULL))
  1578. /* EVP_DigestFinal_ex does not reset the EVP_MD_CTX. */
  1579. || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx))
  1580. /*
  1581. * EVP_DigestInit_ex with NULL type should work on
  1582. * pre-initialized context.
  1583. */
  1584. || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
  1585. goto out;
  1586. if (!TEST_true(EVP_DigestInit_ex(md_ctx, shake256, NULL))
  1587. || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
  1588. || !TEST_true(EVP_DigestFinalXOF(md_ctx, md, sizeof(md)))
  1589. /* EVP_DigestFinalXOF does not reset the EVP_MD_CTX. */
  1590. || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx))
  1591. || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
  1592. goto out;
  1593. ret = 1;
  1594. out:
  1595. EVP_MD_CTX_free(md_ctx);
  1596. EVP_MD_free(sha256);
  1597. EVP_MD_free(shake256);
  1598. return ret;
  1599. }
  1600. static int test_EVP_md_null(void)
  1601. {
  1602. int ret = 0;
  1603. EVP_MD_CTX *md_ctx = NULL;
  1604. const EVP_MD *md_null = EVP_md_null();
  1605. unsigned char md_value[EVP_MAX_MD_SIZE];
  1606. unsigned int md_len = sizeof(md_value);
  1607. if (nullprov != NULL)
  1608. return TEST_skip("Test does not support a non-default library context");
  1609. if (!TEST_ptr(md_null)
  1610. || !TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  1611. goto out;
  1612. if (!TEST_true(EVP_DigestInit_ex(md_ctx, md_null, NULL))
  1613. || !TEST_true(EVP_DigestUpdate(md_ctx, "test", 4))
  1614. || !TEST_true(EVP_DigestFinal_ex(md_ctx, md_value, &md_len)))
  1615. goto out;
  1616. if (!TEST_uint_eq(md_len, 0))
  1617. goto out;
  1618. ret = 1;
  1619. out:
  1620. EVP_MD_CTX_free(md_ctx);
  1621. return ret;
  1622. }
  1623. static int test_d2i_AutoPrivateKey(int i)
  1624. {
  1625. int ret = 0;
  1626. const unsigned char *p;
  1627. EVP_PKEY *pkey = NULL;
  1628. const APK_DATA *ak = &keydata[i];
  1629. const unsigned char *input = ak->kder;
  1630. size_t input_len = ak->size;
  1631. int expected_id = ak->evptype;
  1632. p = input;
  1633. if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
  1634. || !TEST_ptr_eq(p, input + input_len)
  1635. || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
  1636. goto done;
  1637. ret = 1;
  1638. done:
  1639. EVP_PKEY_free(pkey);
  1640. return ret;
  1641. }
  1642. #ifndef OPENSSL_NO_EC
  1643. static const unsigned char ec_public_sect163k1_validxy[] = {
  1644. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  1645. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  1646. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  1647. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b,
  1648. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  1649. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  1650. };
  1651. static const unsigned char ec_public_sect163k1_badx[] = {
  1652. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  1653. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  1654. 0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  1655. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b,
  1656. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  1657. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
  1658. };
  1659. static const unsigned char ec_public_sect163k1_bady[] = {
  1660. 0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  1661. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
  1662. 0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
  1663. 0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b,
  1664. 0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
  1665. 0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6
  1666. };
  1667. static struct ec_der_pub_keys_st {
  1668. const unsigned char *der;
  1669. size_t len;
  1670. int valid;
  1671. } ec_der_pub_keys[] = {
  1672. { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 },
  1673. { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 },
  1674. { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 },
  1675. };
  1676. /*
  1677. * Tests the range of the decoded EC char2 public point.
  1678. * See ec_GF2m_simple_oct2point().
  1679. */
  1680. static int test_invalide_ec_char2_pub_range_decode(int id)
  1681. {
  1682. int ret = 0;
  1683. EVP_PKEY *pkey;
  1684. pkey = load_example_key("EC", ec_der_pub_keys[id].der,
  1685. ec_der_pub_keys[id].len);
  1686. ret = (ec_der_pub_keys[id].valid && TEST_ptr(pkey))
  1687. || TEST_ptr_null(pkey);
  1688. EVP_PKEY_free(pkey);
  1689. return ret;
  1690. }
  1691. /* Tests loading a bad key in PKCS8 format */
  1692. static int test_EVP_PKCS82PKEY(void)
  1693. {
  1694. int ret = 0;
  1695. const unsigned char *derp = kExampleBadECKeyDER;
  1696. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  1697. EVP_PKEY *pkey = NULL;
  1698. if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp,
  1699. sizeof(kExampleBadECKeyDER))))
  1700. goto done;
  1701. if (!TEST_ptr_eq(derp,
  1702. kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)))
  1703. goto done;
  1704. if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf)))
  1705. goto done;
  1706. ret = 1;
  1707. done:
  1708. PKCS8_PRIV_KEY_INFO_free(p8inf);
  1709. EVP_PKEY_free(pkey);
  1710. return ret;
  1711. }
  1712. #endif
  1713. static int test_EVP_PKCS82PKEY_wrong_tag(void)
  1714. {
  1715. EVP_PKEY *pkey = NULL;
  1716. EVP_PKEY *pkey2 = NULL;
  1717. BIO *membio = NULL;
  1718. char *membuf = NULL;
  1719. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  1720. int ok = 0;
  1721. if (testctx != NULL)
  1722. /* test not supported with non-default context */
  1723. return 1;
  1724. if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
  1725. || !TEST_ptr(pkey = load_example_rsa_key())
  1726. || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
  1727. NULL, 0, NULL, NULL),
  1728. 0)
  1729. || !TEST_int_gt(BIO_get_mem_data(membio, &membuf), 0)
  1730. || !TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(membio, NULL))
  1731. || !TEST_ptr(pkey2 = EVP_PKCS82PKEY(p8inf))
  1732. || !TEST_int_eq(ERR_peek_last_error(), 0)) {
  1733. goto done;
  1734. }
  1735. ok = 1;
  1736. done:
  1737. EVP_PKEY_free(pkey);
  1738. EVP_PKEY_free(pkey2);
  1739. PKCS8_PRIV_KEY_INFO_free(p8inf);
  1740. BIO_free_all(membio);
  1741. return ok;
  1742. }
  1743. /* This uses kExampleRSAKeyDER and kExampleRSAKeyPKCS8 to verify encoding */
  1744. static int test_privatekey_to_pkcs8(void)
  1745. {
  1746. EVP_PKEY *pkey = NULL;
  1747. BIO *membio = NULL;
  1748. char *membuf = NULL;
  1749. long membuf_len = 0;
  1750. int ok = 0;
  1751. if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
  1752. || !TEST_ptr(pkey = load_example_rsa_key())
  1753. || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
  1754. NULL, 0, NULL, NULL),
  1755. 0)
  1756. || !TEST_int_gt(membuf_len = BIO_get_mem_data(membio, &membuf), 0)
  1757. || !TEST_ptr(membuf)
  1758. || !TEST_mem_eq(membuf, (size_t)membuf_len,
  1759. kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8))
  1760. /*
  1761. * We try to write PEM as well, just to see that it doesn't err, but
  1762. * assume that the result is correct.
  1763. */
  1764. || !TEST_int_gt(PEM_write_bio_PKCS8PrivateKey(membio, pkey, NULL,
  1765. NULL, 0, NULL, NULL),
  1766. 0))
  1767. goto done;
  1768. ok = 1;
  1769. done:
  1770. EVP_PKEY_free(pkey);
  1771. BIO_free_all(membio);
  1772. return ok;
  1773. }
  1774. #ifndef OPENSSL_NO_EC
  1775. static const struct {
  1776. int encoding;
  1777. const char *encoding_name;
  1778. } ec_encodings[] = {
  1779. { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
  1780. { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP }
  1781. };
  1782. static int ec_export_get_encoding_cb(const OSSL_PARAM params[], void *arg)
  1783. {
  1784. const OSSL_PARAM *p;
  1785. const char *enc_name = NULL;
  1786. int *enc = arg;
  1787. size_t i;
  1788. *enc = -1;
  1789. if (!TEST_ptr(p = OSSL_PARAM_locate_const(params,
  1790. OSSL_PKEY_PARAM_EC_ENCODING))
  1791. || !TEST_true(OSSL_PARAM_get_utf8_string_ptr(p, &enc_name)))
  1792. return 0;
  1793. for (i = 0; i < OSSL_NELEM(ec_encodings); i++) {
  1794. if (OPENSSL_strcasecmp(enc_name, ec_encodings[i].encoding_name) == 0) {
  1795. *enc = ec_encodings[i].encoding;
  1796. break;
  1797. }
  1798. }
  1799. return (*enc != -1);
  1800. }
  1801. static int test_EC_keygen_with_enc(int idx)
  1802. {
  1803. EVP_PKEY *params = NULL, *key = NULL;
  1804. EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
  1805. int enc;
  1806. int ret = 0;
  1807. enc = ec_encodings[idx].encoding;
  1808. /* Create key parameters */
  1809. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "EC", NULL))
  1810. || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
  1811. || !TEST_int_gt(EVP_PKEY_CTX_set_group_name(pctx, "P-256"), 0)
  1812. || !TEST_int_gt(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc), 0)
  1813. || !TEST_true(EVP_PKEY_paramgen(pctx, &params))
  1814. || !TEST_ptr(params))
  1815. goto done;
  1816. /* Create key */
  1817. if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx, params, NULL))
  1818. || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
  1819. || !TEST_true(EVP_PKEY_keygen(kctx, &key))
  1820. || !TEST_ptr(key))
  1821. goto done;
  1822. /* Check that the encoding got all the way into the key */
  1823. if (!TEST_true(evp_keymgmt_util_export(key, OSSL_KEYMGMT_SELECT_ALL,
  1824. ec_export_get_encoding_cb, &enc))
  1825. || !TEST_int_eq(enc, ec_encodings[idx].encoding))
  1826. goto done;
  1827. ret = 1;
  1828. done:
  1829. EVP_PKEY_free(key);
  1830. EVP_PKEY_free(params);
  1831. EVP_PKEY_CTX_free(kctx);
  1832. EVP_PKEY_CTX_free(pctx);
  1833. return ret;
  1834. }
  1835. #endif
  1836. #if !defined(OPENSSL_NO_SM2)
  1837. static int test_EVP_SM2_verify(void)
  1838. {
  1839. const char *pubkey =
  1840. "-----BEGIN PUBLIC KEY-----\n"
  1841. "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEp1KLWq1ZE2jmoAnnBJE1LBGxVr18\n"
  1842. "YvvqECWCpXfAQ9qUJ+UmthnUPf0iM3SaXKHe6PlLIDyNlWMWb9RUh/yU3g==\n"
  1843. "-----END PUBLIC KEY-----\n";
  1844. const char *msg = "message digest";
  1845. const char *id = "[email protected]";
  1846. const uint8_t signature[] = {
  1847. 0x30, 0x44, 0x02, 0x20, 0x5b, 0xdb, 0xab, 0x81, 0x4f, 0xbb,
  1848. 0x8b, 0x69, 0xb1, 0x05, 0x9c, 0x99, 0x3b, 0xb2, 0x45, 0x06,
  1849. 0x4a, 0x30, 0x15, 0x59, 0x84, 0xcd, 0xee, 0x30, 0x60, 0x36,
  1850. 0x57, 0x87, 0xef, 0x5c, 0xd0, 0xbe, 0x02, 0x20, 0x43, 0x8d,
  1851. 0x1f, 0xc7, 0x77, 0x72, 0x39, 0xbb, 0x72, 0xe1, 0xfd, 0x07,
  1852. 0x58, 0xd5, 0x82, 0xc8, 0x2d, 0xba, 0x3b, 0x2c, 0x46, 0x24,
  1853. 0xe3, 0x50, 0xff, 0x04, 0xc7, 0xa0, 0x71, 0x9f, 0xa4, 0x70
  1854. };
  1855. int rc = 0;
  1856. BIO *bio = NULL;
  1857. EVP_PKEY *pkey = NULL;
  1858. EVP_MD_CTX *mctx = NULL;
  1859. EVP_PKEY_CTX *pctx = NULL;
  1860. EVP_MD *sm3 = NULL;
  1861. bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
  1862. if (!TEST_true(bio != NULL))
  1863. goto done;
  1864. pkey = PEM_read_bio_PUBKEY_ex(bio, NULL, NULL, NULL, testctx, testpropq);
  1865. if (!TEST_true(pkey != NULL))
  1866. goto done;
  1867. if (!TEST_true(EVP_PKEY_is_a(pkey, "SM2")))
  1868. goto done;
  1869. if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
  1870. goto done;
  1871. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1872. goto done;
  1873. EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
  1874. if (!TEST_ptr(sm3 = EVP_MD_fetch(testctx, "sm3", testpropq)))
  1875. goto done;
  1876. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, sm3, NULL, pkey)))
  1877. goto done;
  1878. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, id, strlen(id)), 0))
  1879. goto done;
  1880. if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
  1881. goto done;
  1882. if (!TEST_int_gt(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature)), 0))
  1883. goto done;
  1884. rc = 1;
  1885. done:
  1886. BIO_free(bio);
  1887. EVP_PKEY_free(pkey);
  1888. EVP_PKEY_CTX_free(pctx);
  1889. EVP_MD_CTX_free(mctx);
  1890. EVP_MD_free(sm3);
  1891. return rc;
  1892. }
  1893. static int test_EVP_SM2(void)
  1894. {
  1895. int ret = 0;
  1896. EVP_PKEY *pkey = NULL;
  1897. EVP_PKEY *pkeyparams = NULL;
  1898. EVP_PKEY_CTX *pctx = NULL;
  1899. EVP_PKEY_CTX *kctx = NULL;
  1900. EVP_PKEY_CTX *sctx = NULL;
  1901. size_t sig_len = 0;
  1902. unsigned char *sig = NULL;
  1903. EVP_MD_CTX *md_ctx = NULL;
  1904. EVP_MD_CTX *md_ctx_verify = NULL;
  1905. EVP_PKEY_CTX *cctx = NULL;
  1906. EVP_MD *check_md = NULL;
  1907. uint8_t ciphertext[128];
  1908. size_t ctext_len = sizeof(ciphertext);
  1909. uint8_t plaintext[8];
  1910. size_t ptext_len = sizeof(plaintext);
  1911. uint8_t sm2_id[] = {1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r'};
  1912. OSSL_PARAM sparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
  1913. OSSL_PARAM gparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
  1914. int i;
  1915. char mdname[OSSL_MAX_NAME_SIZE];
  1916. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx,
  1917. "SM2", testpropq)))
  1918. goto done;
  1919. if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
  1920. goto done;
  1921. if (!TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2), 0))
  1922. goto done;
  1923. if (!TEST_true(EVP_PKEY_paramgen(pctx, &pkeyparams)))
  1924. goto done;
  1925. if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx,
  1926. pkeyparams, testpropq)))
  1927. goto done;
  1928. if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0))
  1929. goto done;
  1930. if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
  1931. goto done;
  1932. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  1933. goto done;
  1934. if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
  1935. goto done;
  1936. if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  1937. goto done;
  1938. EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
  1939. EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);
  1940. if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, "sm3", testpropq)))
  1941. goto done;
  1942. if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, check_md, NULL, pkey)))
  1943. goto done;
  1944. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  1945. goto done;
  1946. if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
  1947. goto done;
  1948. /* Determine the size of the signature. */
  1949. if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
  1950. goto done;
  1951. if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
  1952. goto done;
  1953. if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
  1954. goto done;
  1955. /* Ensure that the signature round-trips. */
  1956. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL,
  1957. pkey)))
  1958. goto done;
  1959. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
  1960. goto done;
  1961. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
  1962. goto done;
  1963. if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
  1964. goto done;
  1965. /*
  1966. * Try verify again with non-matching 0 length id but ensure that it can
  1967. * be set on the context and overrides the previous value.
  1968. */
  1969. if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL,
  1970. pkey)))
  1971. goto done;
  1972. if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, NULL, 0), 0))
  1973. goto done;
  1974. if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
  1975. goto done;
  1976. if (!TEST_int_eq(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
  1977. goto done;
  1978. /* now check encryption/decryption */
  1979. gparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
  1980. mdname, sizeof(mdname));
  1981. for (i = 0; i < 2; i++) {
  1982. const char *mdnames[] = {
  1983. #ifndef OPENSSL_NO_SM3
  1984. "SM3",
  1985. #else
  1986. NULL,
  1987. #endif
  1988. "SHA2-256" };
  1989. EVP_PKEY_CTX_free(cctx);
  1990. if (mdnames[i] == NULL)
  1991. continue;
  1992. sparams[0] =
  1993. OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
  1994. (char *)mdnames[i], 0);
  1995. if (!TEST_ptr(cctx = EVP_PKEY_CTX_new_from_pkey(testctx,
  1996. pkey, testpropq)))
  1997. goto done;
  1998. if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
  1999. goto done;
  2000. if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
  2001. goto done;
  2002. if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg,
  2003. sizeof(kMsg))))
  2004. goto done;
  2005. if (!TEST_int_gt(EVP_PKEY_decrypt_init(cctx), 0))
  2006. goto done;
  2007. if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
  2008. goto done;
  2009. if (!TEST_int_gt(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext,
  2010. ctext_len), 0))
  2011. goto done;
  2012. if (!TEST_true(EVP_PKEY_CTX_get_params(cctx, gparams)))
  2013. goto done;
  2014. /*
  2015. * Test we're still using the digest we think we are.
  2016. * Because of aliases, the easiest is to fetch the digest and
  2017. * check the name with EVP_MD_is_a().
  2018. */
  2019. EVP_MD_free(check_md);
  2020. if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, mdname, testpropq)))
  2021. goto done;
  2022. if (!TEST_true(EVP_MD_is_a(check_md, mdnames[i]))) {
  2023. TEST_info("Fetched md %s isn't %s", mdname, mdnames[i]);
  2024. goto done;
  2025. }
  2026. if (!TEST_true(ptext_len == sizeof(kMsg)))
  2027. goto done;
  2028. if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
  2029. goto done;
  2030. }
  2031. ret = 1;
  2032. done:
  2033. EVP_PKEY_CTX_free(pctx);
  2034. EVP_PKEY_CTX_free(kctx);
  2035. EVP_PKEY_CTX_free(sctx);
  2036. EVP_PKEY_CTX_free(cctx);
  2037. EVP_PKEY_free(pkey);
  2038. EVP_PKEY_free(pkeyparams);
  2039. EVP_MD_CTX_free(md_ctx);
  2040. EVP_MD_CTX_free(md_ctx_verify);
  2041. EVP_MD_free(check_md);
  2042. OPENSSL_free(sig);
  2043. return ret;
  2044. }
  2045. #endif
  2046. static struct keys_st {
  2047. int type;
  2048. char *priv;
  2049. char *pub;
  2050. } keys[] = {
  2051. {
  2052. EVP_PKEY_HMAC, "0123456789", NULL
  2053. },
  2054. {
  2055. EVP_PKEY_HMAC, "", NULL
  2056. #ifndef OPENSSL_NO_POLY1305
  2057. }, {
  2058. EVP_PKEY_POLY1305, "01234567890123456789012345678901", NULL
  2059. #endif
  2060. #ifndef OPENSSL_NO_SIPHASH
  2061. }, {
  2062. EVP_PKEY_SIPHASH, "0123456789012345", NULL
  2063. #endif
  2064. },
  2065. #ifndef OPENSSL_NO_ECX
  2066. {
  2067. EVP_PKEY_X25519, "01234567890123456789012345678901",
  2068. "abcdefghijklmnopqrstuvwxyzabcdef"
  2069. }, {
  2070. EVP_PKEY_ED25519, "01234567890123456789012345678901",
  2071. "abcdefghijklmnopqrstuvwxyzabcdef"
  2072. }, {
  2073. EVP_PKEY_X448,
  2074. "01234567890123456789012345678901234567890123456789012345",
  2075. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd"
  2076. }, {
  2077. EVP_PKEY_ED448,
  2078. "012345678901234567890123456789012345678901234567890123456",
  2079. "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde"
  2080. }
  2081. #endif
  2082. };
  2083. static int test_set_get_raw_keys_int(int tst, int pub, int uselibctx)
  2084. {
  2085. int ret = 0;
  2086. unsigned char buf[80];
  2087. unsigned char *in;
  2088. size_t inlen, len = 0, shortlen = 1;
  2089. EVP_PKEY *pkey;
  2090. /* Check if this algorithm supports public keys */
  2091. if (pub && keys[tst].pub == NULL)
  2092. return 1;
  2093. memset(buf, 0, sizeof(buf));
  2094. if (pub) {
  2095. #ifndef OPENSSL_NO_EC
  2096. inlen = strlen(keys[tst].pub);
  2097. in = (unsigned char *)keys[tst].pub;
  2098. if (uselibctx) {
  2099. pkey = EVP_PKEY_new_raw_public_key_ex(
  2100. testctx,
  2101. OBJ_nid2sn(keys[tst].type),
  2102. NULL,
  2103. in,
  2104. inlen);
  2105. } else {
  2106. pkey = EVP_PKEY_new_raw_public_key(keys[tst].type,
  2107. NULL,
  2108. in,
  2109. inlen);
  2110. }
  2111. #else
  2112. return 1;
  2113. #endif
  2114. } else {
  2115. inlen = strlen(keys[tst].priv);
  2116. in = (unsigned char *)keys[tst].priv;
  2117. if (uselibctx) {
  2118. pkey = EVP_PKEY_new_raw_private_key_ex(
  2119. testctx, OBJ_nid2sn(keys[tst].type),
  2120. NULL,
  2121. in,
  2122. inlen);
  2123. } else {
  2124. pkey = EVP_PKEY_new_raw_private_key(keys[tst].type,
  2125. NULL,
  2126. in,
  2127. inlen);
  2128. }
  2129. }
  2130. if (!TEST_ptr(pkey)
  2131. || !TEST_int_eq(EVP_PKEY_eq(pkey, pkey), 1)
  2132. || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len)))
  2133. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len)))
  2134. || !TEST_true(len == inlen))
  2135. goto done;
  2136. if (tst != 1) {
  2137. /*
  2138. * Test that supplying a buffer that is too small fails. Doesn't apply
  2139. * to HMAC with a zero length key
  2140. */
  2141. if ((!pub && !TEST_false(EVP_PKEY_get_raw_private_key(pkey, buf,
  2142. &shortlen)))
  2143. || (pub && !TEST_false(EVP_PKEY_get_raw_public_key(pkey, buf,
  2144. &shortlen))))
  2145. goto done;
  2146. }
  2147. if ((!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len)))
  2148. || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len)))
  2149. || !TEST_mem_eq(in, inlen, buf, len))
  2150. goto done;
  2151. ret = 1;
  2152. done:
  2153. EVP_PKEY_free(pkey);
  2154. return ret;
  2155. }
  2156. static int test_set_get_raw_keys(int tst)
  2157. {
  2158. return (nullprov != NULL || test_set_get_raw_keys_int(tst, 0, 0))
  2159. && test_set_get_raw_keys_int(tst, 0, 1)
  2160. && (nullprov != NULL || test_set_get_raw_keys_int(tst, 1, 0))
  2161. && test_set_get_raw_keys_int(tst, 1, 1);
  2162. }
  2163. #ifndef OPENSSL_NO_DEPRECATED_3_0
  2164. static int pkey_custom_check(EVP_PKEY *pkey)
  2165. {
  2166. return 0xbeef;
  2167. }
  2168. static int pkey_custom_pub_check(EVP_PKEY *pkey)
  2169. {
  2170. return 0xbeef;
  2171. }
  2172. static int pkey_custom_param_check(EVP_PKEY *pkey)
  2173. {
  2174. return 0xbeef;
  2175. }
  2176. static EVP_PKEY_METHOD *custom_pmeth;
  2177. #endif
  2178. static int test_EVP_PKEY_check(int i)
  2179. {
  2180. int ret = 0;
  2181. EVP_PKEY *pkey = NULL;
  2182. EVP_PKEY_CTX *ctx = NULL;
  2183. #ifndef OPENSSL_NO_DEPRECATED_3_0
  2184. EVP_PKEY_CTX *ctx2 = NULL;
  2185. #endif
  2186. const APK_DATA *ak = &keycheckdata[i];
  2187. const unsigned char *input = ak->kder;
  2188. size_t input_len = ak->size;
  2189. int expected_id = ak->evptype;
  2190. int expected_check = ak->check;
  2191. int expected_pub_check = ak->pub_check;
  2192. int expected_param_check = ak->param_check;
  2193. int type = ak->type;
  2194. if (!TEST_ptr(pkey = load_example_key(ak->keytype, input, input_len)))
  2195. goto done;
  2196. if (type == 0
  2197. && !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
  2198. goto done;
  2199. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
  2200. goto done;
  2201. if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check))
  2202. goto done;
  2203. if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check))
  2204. goto done;
  2205. if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check))
  2206. goto done;
  2207. #ifndef OPENSSL_NO_DEPRECATED_3_0
  2208. ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL);
  2209. /* assign the pkey directly, as an internal test */
  2210. EVP_PKEY_up_ref(pkey);
  2211. ctx2->pkey = pkey;
  2212. if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef))
  2213. goto done;
  2214. if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef))
  2215. goto done;
  2216. if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef))
  2217. goto done;
  2218. #endif
  2219. ret = 1;
  2220. done:
  2221. EVP_PKEY_CTX_free(ctx);
  2222. #ifndef OPENSSL_NO_DEPRECATED_3_0
  2223. EVP_PKEY_CTX_free(ctx2);
  2224. #endif
  2225. EVP_PKEY_free(pkey);
  2226. return ret;
  2227. }
  2228. #ifndef OPENSSL_NO_CMAC
  2229. static int get_cmac_val(EVP_PKEY *pkey, unsigned char *mac)
  2230. {
  2231. EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
  2232. const char msg[] = "Hello World";
  2233. size_t maclen = AES_BLOCK_SIZE;
  2234. int ret = 1;
  2235. if (!TEST_ptr(mdctx)
  2236. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, NULL, testctx,
  2237. testpropq, pkey, NULL))
  2238. || !TEST_true(EVP_DigestSignUpdate(mdctx, msg, sizeof(msg)))
  2239. || !TEST_true(EVP_DigestSignFinal(mdctx, mac, &maclen))
  2240. || !TEST_size_t_eq(maclen, AES_BLOCK_SIZE))
  2241. ret = 0;
  2242. EVP_MD_CTX_free(mdctx);
  2243. return ret;
  2244. }
  2245. static int test_CMAC_keygen(void)
  2246. {
  2247. static unsigned char key[] = {
  2248. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  2249. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  2250. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  2251. };
  2252. EVP_PKEY_CTX *kctx = NULL;
  2253. int ret = 0;
  2254. EVP_PKEY *pkey = NULL;
  2255. unsigned char mac[AES_BLOCK_SIZE];
  2256. # if !defined(OPENSSL_NO_DEPRECATED_3_0)
  2257. unsigned char mac2[AES_BLOCK_SIZE];
  2258. # endif
  2259. if (nullprov != NULL)
  2260. return TEST_skip("Test does not support a non-default library context");
  2261. /*
  2262. * This is a legacy method for CMACs, but should still work.
  2263. * This verifies that it works without an ENGINE.
  2264. */
  2265. kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_CMAC, NULL);
  2266. /* Test a CMAC key created using the "generated" method */
  2267. if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
  2268. || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
  2269. EVP_PKEY_CTRL_CIPHER,
  2270. 0, (void *)EVP_aes_256_cbc()), 0)
  2271. || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
  2272. EVP_PKEY_CTRL_SET_MAC_KEY,
  2273. sizeof(key), (void *)key), 0)
  2274. || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0)
  2275. || !TEST_ptr(pkey)
  2276. || !TEST_true(get_cmac_val(pkey, mac)))
  2277. goto done;
  2278. # if !defined(OPENSSL_NO_DEPRECATED_3_0)
  2279. EVP_PKEY_free(pkey);
  2280. /*
  2281. * Test a CMAC key using the direct method, and compare with the mac
  2282. * created above.
  2283. */
  2284. pkey = EVP_PKEY_new_CMAC_key(NULL, key, sizeof(key), EVP_aes_256_cbc());
  2285. if (!TEST_ptr(pkey)
  2286. || !TEST_true(get_cmac_val(pkey, mac2))
  2287. || !TEST_mem_eq(mac, sizeof(mac), mac2, sizeof(mac2)))
  2288. goto done;
  2289. # endif
  2290. ret = 1;
  2291. done:
  2292. EVP_PKEY_free(pkey);
  2293. EVP_PKEY_CTX_free(kctx);
  2294. return ret;
  2295. }
  2296. #endif
  2297. static int test_HKDF(void)
  2298. {
  2299. EVP_PKEY_CTX *pctx;
  2300. unsigned char out[20];
  2301. size_t outlen;
  2302. int i, ret = 0;
  2303. unsigned char salt[] = "0123456789";
  2304. unsigned char key[] = "012345678901234567890123456789";
  2305. unsigned char info[] = "infostring";
  2306. const unsigned char expected[] = {
  2307. 0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5,
  2308. 0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca
  2309. };
  2310. size_t expectedlen = sizeof(expected);
  2311. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
  2312. goto done;
  2313. /* We do this twice to test reuse of the EVP_PKEY_CTX */
  2314. for (i = 0; i < 2; i++) {
  2315. outlen = sizeof(out);
  2316. memset(out, 0, outlen);
  2317. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  2318. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  2319. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  2320. sizeof(salt) - 1), 0)
  2321. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  2322. sizeof(key) - 1), 0)
  2323. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  2324. sizeof(info) - 1), 0)
  2325. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  2326. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  2327. goto done;
  2328. }
  2329. ret = 1;
  2330. done:
  2331. EVP_PKEY_CTX_free(pctx);
  2332. return ret;
  2333. }
  2334. static int test_emptyikm_HKDF(void)
  2335. {
  2336. EVP_PKEY_CTX *pctx;
  2337. unsigned char out[20];
  2338. size_t outlen;
  2339. int ret = 0;
  2340. unsigned char salt[] = "9876543210";
  2341. unsigned char key[] = "";
  2342. unsigned char info[] = "stringinfo";
  2343. const unsigned char expected[] = {
  2344. 0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47,
  2345. 0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30
  2346. };
  2347. size_t expectedlen = sizeof(expected);
  2348. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
  2349. goto done;
  2350. outlen = sizeof(out);
  2351. memset(out, 0, outlen);
  2352. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  2353. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  2354. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  2355. sizeof(salt) - 1), 0)
  2356. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  2357. sizeof(key) - 1), 0)
  2358. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  2359. sizeof(info) - 1), 0)
  2360. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  2361. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  2362. goto done;
  2363. ret = 1;
  2364. done:
  2365. EVP_PKEY_CTX_free(pctx);
  2366. return ret;
  2367. }
  2368. static int test_empty_salt_info_HKDF(void)
  2369. {
  2370. EVP_PKEY_CTX *pctx;
  2371. unsigned char out[20];
  2372. size_t outlen;
  2373. int ret = 0;
  2374. unsigned char salt[] = "";
  2375. unsigned char fake[] = "0123456789";
  2376. unsigned char key[] = "012345678901234567890123456789";
  2377. unsigned char info[] = "";
  2378. const unsigned char expected[] = {
  2379. 0x67, 0x12, 0xf9, 0x27, 0x8a, 0x8a, 0x3a, 0x8f, 0x7d, 0x2c, 0xa3, 0x6a,
  2380. 0xaa, 0xe9, 0xb3, 0xb9, 0x52, 0x5f, 0xe0, 0x06,
  2381. };
  2382. size_t expectedlen = sizeof(expected);
  2383. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
  2384. goto done;
  2385. outlen = sizeof(out);
  2386. memset(out, 0, outlen);
  2387. if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
  2388. || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
  2389. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, fake,
  2390. sizeof(fake) - 1), 0)
  2391. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
  2392. sizeof(salt) - 1), 0)
  2393. || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
  2394. sizeof(key) - 1), 0)
  2395. || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
  2396. sizeof(info) - 1), 0)
  2397. || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
  2398. || !TEST_mem_eq(out, outlen, expected, expectedlen))
  2399. goto done;
  2400. ret = 1;
  2401. done:
  2402. EVP_PKEY_CTX_free(pctx);
  2403. return ret;
  2404. }
  2405. #ifndef OPENSSL_NO_EC
  2406. static int test_X509_PUBKEY_inplace(void)
  2407. {
  2408. int ret = 0;
  2409. X509_PUBKEY *xp = X509_PUBKEY_new_ex(testctx, testpropq);
  2410. const unsigned char *p = kExampleECPubKeyDER;
  2411. size_t input_len = sizeof(kExampleECPubKeyDER);
  2412. if (!TEST_ptr(xp))
  2413. goto done;
  2414. if (!TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len)))
  2415. goto done;
  2416. if (!TEST_ptr(X509_PUBKEY_get0(xp)))
  2417. goto done;
  2418. p = kExampleBadECPubKeyDER;
  2419. input_len = sizeof(kExampleBadECPubKeyDER);
  2420. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
  2421. goto done;
  2422. if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
  2423. goto done;
  2424. ret = 1;
  2425. done:
  2426. X509_PUBKEY_free(xp);
  2427. return ret;
  2428. }
  2429. static int test_X509_PUBKEY_dup(void)
  2430. {
  2431. int ret = 0;
  2432. X509_PUBKEY *xp = NULL, *xq = NULL;
  2433. const unsigned char *p = kExampleECPubKeyDER;
  2434. size_t input_len = sizeof(kExampleECPubKeyDER);
  2435. xp = X509_PUBKEY_new_ex(testctx, testpropq);
  2436. if (!TEST_ptr(xp)
  2437. || !TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len))
  2438. || !TEST_ptr(xq = X509_PUBKEY_dup(xp))
  2439. || !TEST_ptr_ne(xp, xq))
  2440. goto done;
  2441. if (!TEST_ptr(X509_PUBKEY_get0(xq))
  2442. || !TEST_ptr(X509_PUBKEY_get0(xp))
  2443. || !TEST_ptr_ne(X509_PUBKEY_get0(xq), X509_PUBKEY_get0(xp)))
  2444. goto done;
  2445. X509_PUBKEY_free(xq);
  2446. xq = NULL;
  2447. p = kExampleBadECPubKeyDER;
  2448. input_len = sizeof(kExampleBadECPubKeyDER);
  2449. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len))
  2450. || !TEST_ptr(xq = X509_PUBKEY_dup(xp)))
  2451. goto done;
  2452. X509_PUBKEY_free(xp);
  2453. xp = NULL;
  2454. if (!TEST_true(X509_PUBKEY_get0(xq) == NULL))
  2455. goto done;
  2456. ret = 1;
  2457. done:
  2458. X509_PUBKEY_free(xp);
  2459. X509_PUBKEY_free(xq);
  2460. return ret;
  2461. }
  2462. #endif /* OPENSSL_NO_EC */
  2463. /* Test getting and setting parameters on an EVP_PKEY_CTX */
  2464. static int test_EVP_PKEY_CTX_get_set_params(EVP_PKEY *pkey)
  2465. {
  2466. EVP_MD_CTX *mdctx = NULL;
  2467. EVP_PKEY_CTX *ctx = NULL;
  2468. const OSSL_PARAM *params;
  2469. OSSL_PARAM ourparams[2], *param = ourparams, *param_md;
  2470. int ret = 0;
  2471. const EVP_MD *md;
  2472. char mdname[OSSL_MAX_NAME_SIZE];
  2473. char ssl3ms[48];
  2474. /* Initialise a sign operation */
  2475. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq);
  2476. if (!TEST_ptr(ctx)
  2477. || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0))
  2478. goto err;
  2479. /*
  2480. * We should be able to query the parameters now.
  2481. */
  2482. params = EVP_PKEY_CTX_settable_params(ctx);
  2483. if (!TEST_ptr(params)
  2484. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  2485. OSSL_SIGNATURE_PARAM_DIGEST)))
  2486. goto err;
  2487. params = EVP_PKEY_CTX_gettable_params(ctx);
  2488. if (!TEST_ptr(params)
  2489. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  2490. OSSL_SIGNATURE_PARAM_ALGORITHM_ID))
  2491. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  2492. OSSL_SIGNATURE_PARAM_DIGEST)))
  2493. goto err;
  2494. /*
  2495. * Test getting and setting params via EVP_PKEY_CTX_set_params() and
  2496. * EVP_PKEY_CTX_get_params()
  2497. */
  2498. strcpy(mdname, "SHA512");
  2499. param_md = param;
  2500. *param++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  2501. mdname, 0);
  2502. *param++ = OSSL_PARAM_construct_end();
  2503. if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
  2504. goto err;
  2505. mdname[0] = '\0';
  2506. *param_md = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  2507. mdname, sizeof(mdname));
  2508. if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
  2509. || !TEST_str_eq(mdname, "SHA512"))
  2510. goto err;
  2511. /*
  2512. * Test the TEST_PKEY_CTX_set_signature_md() and
  2513. * TEST_PKEY_CTX_get_signature_md() functions
  2514. */
  2515. if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
  2516. || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
  2517. || !TEST_ptr_eq(md, EVP_sha256()))
  2518. goto err;
  2519. /*
  2520. * Test getting MD parameters via an associated EVP_PKEY_CTX
  2521. */
  2522. mdctx = EVP_MD_CTX_new();
  2523. if (!TEST_ptr(mdctx)
  2524. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", testctx, testpropq,
  2525. pkey, NULL)))
  2526. goto err;
  2527. /*
  2528. * We now have an EVP_MD_CTX with an EVP_PKEY_CTX inside it. We should be
  2529. * able to obtain the digest's settable parameters from the provider.
  2530. */
  2531. params = EVP_MD_CTX_settable_params(mdctx);
  2532. if (!TEST_ptr(params)
  2533. || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0)
  2534. /* The final key should be NULL */
  2535. || !TEST_ptr_null(params[1].key))
  2536. goto err;
  2537. param = ourparams;
  2538. memset(ssl3ms, 0, sizeof(ssl3ms));
  2539. *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  2540. ssl3ms, sizeof(ssl3ms));
  2541. *param++ = OSSL_PARAM_construct_end();
  2542. if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams)))
  2543. goto err;
  2544. ret = 1;
  2545. err:
  2546. EVP_MD_CTX_free(mdctx);
  2547. EVP_PKEY_CTX_free(ctx);
  2548. return ret;
  2549. }
  2550. #ifndef OPENSSL_NO_DSA
  2551. static int test_DSA_get_set_params(void)
  2552. {
  2553. OSSL_PARAM_BLD *bld = NULL;
  2554. OSSL_PARAM *params = NULL;
  2555. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  2556. EVP_PKEY_CTX *pctx = NULL;
  2557. EVP_PKEY *pkey = NULL;
  2558. int ret = 0;
  2559. /*
  2560. * Setup the parameters for our DSA object. For our purposes they don't
  2561. * have to actually be *valid* parameters. We just need to set something.
  2562. */
  2563. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "DSA", NULL))
  2564. || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
  2565. || !TEST_ptr(p = BN_new())
  2566. || !TEST_ptr(q = BN_new())
  2567. || !TEST_ptr(g = BN_new())
  2568. || !TEST_ptr(pub = BN_new())
  2569. || !TEST_ptr(priv = BN_new()))
  2570. goto err;
  2571. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  2572. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  2573. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  2574. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  2575. pub))
  2576. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  2577. priv)))
  2578. goto err;
  2579. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  2580. goto err;
  2581. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  2582. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  2583. params), 0))
  2584. goto err;
  2585. if (!TEST_ptr(pkey))
  2586. goto err;
  2587. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  2588. err:
  2589. EVP_PKEY_free(pkey);
  2590. EVP_PKEY_CTX_free(pctx);
  2591. OSSL_PARAM_free(params);
  2592. OSSL_PARAM_BLD_free(bld);
  2593. BN_free(p);
  2594. BN_free(q);
  2595. BN_free(g);
  2596. BN_free(pub);
  2597. BN_free(priv);
  2598. return ret;
  2599. }
  2600. /*
  2601. * Test combinations of private, public, missing and private + public key
  2602. * params to ensure they are all accepted
  2603. */
  2604. static int test_DSA_priv_pub(void)
  2605. {
  2606. return test_EVP_PKEY_ffc_priv_pub("DSA");
  2607. }
  2608. #endif /* !OPENSSL_NO_DSA */
  2609. static int test_RSA_get_set_params(void)
  2610. {
  2611. OSSL_PARAM_BLD *bld = NULL;
  2612. OSSL_PARAM *params = NULL;
  2613. BIGNUM *n = NULL, *e = NULL, *d = NULL;
  2614. EVP_PKEY_CTX *pctx = NULL;
  2615. EVP_PKEY *pkey = NULL;
  2616. int ret = 0;
  2617. /*
  2618. * Setup the parameters for our RSA object. For our purposes they don't
  2619. * have to actually be *valid* parameters. We just need to set something.
  2620. */
  2621. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", NULL))
  2622. || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
  2623. || !TEST_ptr(n = BN_new())
  2624. || !TEST_ptr(e = BN_new())
  2625. || !TEST_ptr(d = BN_new()))
  2626. goto err;
  2627. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
  2628. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
  2629. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)))
  2630. goto err;
  2631. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  2632. goto err;
  2633. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  2634. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  2635. params), 0))
  2636. goto err;
  2637. if (!TEST_ptr(pkey))
  2638. goto err;
  2639. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  2640. err:
  2641. EVP_PKEY_free(pkey);
  2642. EVP_PKEY_CTX_free(pctx);
  2643. OSSL_PARAM_free(params);
  2644. OSSL_PARAM_BLD_free(bld);
  2645. BN_free(n);
  2646. BN_free(e);
  2647. BN_free(d);
  2648. return ret;
  2649. }
  2650. static int test_RSA_OAEP_set_get_params(void)
  2651. {
  2652. int ret = 0;
  2653. EVP_PKEY *key = NULL;
  2654. EVP_PKEY_CTX *key_ctx = NULL;
  2655. if (nullprov != NULL)
  2656. return TEST_skip("Test does not support a non-default library context");
  2657. if (!TEST_ptr(key = load_example_rsa_key())
  2658. || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(0, key, 0)))
  2659. goto err;
  2660. {
  2661. int padding = RSA_PKCS1_OAEP_PADDING;
  2662. OSSL_PARAM params[4];
  2663. params[0] = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PAD_MODE, &padding);
  2664. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  2665. OSSL_DIGEST_NAME_SHA2_256, 0);
  2666. params[2] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
  2667. OSSL_DIGEST_NAME_SHA1, 0);
  2668. params[3] = OSSL_PARAM_construct_end();
  2669. if (!TEST_int_gt(EVP_PKEY_encrypt_init_ex(key_ctx, params),0))
  2670. goto err;
  2671. }
  2672. {
  2673. OSSL_PARAM params[3];
  2674. char oaepmd[30] = { '\0' };
  2675. char mgf1md[30] = { '\0' };
  2676. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  2677. oaepmd, sizeof(oaepmd));
  2678. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
  2679. mgf1md, sizeof(mgf1md));
  2680. params[2] = OSSL_PARAM_construct_end();
  2681. if (!TEST_true(EVP_PKEY_CTX_get_params(key_ctx, params)))
  2682. goto err;
  2683. if (!TEST_str_eq(oaepmd, OSSL_DIGEST_NAME_SHA2_256)
  2684. || !TEST_str_eq(mgf1md, OSSL_DIGEST_NAME_SHA1))
  2685. goto err;
  2686. }
  2687. ret = 1;
  2688. err:
  2689. EVP_PKEY_free(key);
  2690. EVP_PKEY_CTX_free(key_ctx);
  2691. return ret;
  2692. }
  2693. /* https://github.com/openssl/openssl/issues/21288 */
  2694. static int test_RSA_OAEP_set_null_label(void)
  2695. {
  2696. int ret = 0;
  2697. EVP_PKEY *key = NULL;
  2698. EVP_PKEY_CTX *key_ctx = NULL;
  2699. if (!TEST_ptr(key = load_example_rsa_key())
  2700. || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(testctx, key, NULL))
  2701. || !TEST_true(EVP_PKEY_encrypt_init(key_ctx)))
  2702. goto err;
  2703. if (!TEST_true(EVP_PKEY_CTX_set_rsa_padding(key_ctx, RSA_PKCS1_OAEP_PADDING)))
  2704. goto err;
  2705. if (!TEST_true(EVP_PKEY_CTX_set0_rsa_oaep_label(key_ctx, OPENSSL_strdup("foo"), 0)))
  2706. goto err;
  2707. if (!TEST_true(EVP_PKEY_CTX_set0_rsa_oaep_label(key_ctx, NULL, 0)))
  2708. goto err;
  2709. ret = 1;
  2710. err:
  2711. EVP_PKEY_free(key);
  2712. EVP_PKEY_CTX_free(key_ctx);
  2713. return ret;
  2714. }
  2715. #ifndef OPENSSL_NO_DEPRECATED_3_0
  2716. static int test_RSA_legacy(void)
  2717. {
  2718. int ret = 0;
  2719. BIGNUM *p = NULL;
  2720. BIGNUM *q = NULL;
  2721. BIGNUM *n = NULL;
  2722. BIGNUM *e = NULL;
  2723. BIGNUM *d = NULL;
  2724. const EVP_MD *md = EVP_sha256();
  2725. EVP_MD_CTX *ctx = NULL;
  2726. EVP_PKEY *pkey = NULL;
  2727. RSA *rsa = NULL;
  2728. if (nullprov != NULL)
  2729. return TEST_skip("Test does not support a non-default library context");
  2730. if (!TEST_ptr(p = BN_dup(BN_value_one()))
  2731. || !TEST_ptr(q = BN_dup(BN_value_one()))
  2732. || !TEST_ptr(n = BN_dup(BN_value_one()))
  2733. || !TEST_ptr(e = BN_dup(BN_value_one()))
  2734. || !TEST_ptr(d = BN_dup(BN_value_one())))
  2735. goto err;
  2736. if (!TEST_ptr(rsa = RSA_new())
  2737. || !TEST_ptr(pkey = EVP_PKEY_new())
  2738. || !TEST_ptr(ctx = EVP_MD_CTX_new()))
  2739. goto err;
  2740. if (!TEST_true(RSA_set0_factors(rsa, p, q)))
  2741. goto err;
  2742. p = NULL;
  2743. q = NULL;
  2744. if (!TEST_true(RSA_set0_key(rsa, n, e, d)))
  2745. goto err;
  2746. n = NULL;
  2747. e = NULL;
  2748. d = NULL;
  2749. if (!TEST_true(EVP_PKEY_assign_RSA(pkey, rsa)))
  2750. goto err;
  2751. rsa = NULL;
  2752. if (!TEST_true(EVP_DigestSignInit(ctx, NULL, md, NULL, pkey)))
  2753. goto err;
  2754. ret = 1;
  2755. err:
  2756. RSA_free(rsa);
  2757. EVP_MD_CTX_free(ctx);
  2758. EVP_PKEY_free(pkey);
  2759. BN_free(p);
  2760. BN_free(q);
  2761. BN_free(n);
  2762. BN_free(e);
  2763. BN_free(d);
  2764. return ret;
  2765. }
  2766. #endif
  2767. static int test_RSA_encrypt(void)
  2768. {
  2769. int ret = 0;
  2770. EVP_PKEY *pkey = NULL;
  2771. EVP_PKEY_CTX *pctx = NULL;
  2772. unsigned char *cbuf = NULL, *pbuf = NULL;
  2773. size_t clen = 0, plen = 0;
  2774. if (!TEST_ptr(pkey = load_example_rsa_key())
  2775. || !TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(testctx,
  2776. pkey, testpropq))
  2777. || !TEST_int_gt(EVP_PKEY_encrypt_init(pctx), 0)
  2778. || !TEST_int_gt(EVP_PKEY_encrypt(pctx, cbuf, &clen, kMsg, sizeof(kMsg)), 0)
  2779. || !TEST_ptr(cbuf = OPENSSL_malloc(clen))
  2780. || !TEST_int_gt(EVP_PKEY_encrypt(pctx, cbuf, &clen, kMsg, sizeof(kMsg)), 0))
  2781. goto done;
  2782. /* Require failure when the output buffer is too small */
  2783. plen = clen - 1;
  2784. if (!TEST_int_le(EVP_PKEY_encrypt(pctx, cbuf, &plen, kMsg, sizeof(kMsg)), 0))
  2785. goto done;
  2786. /* flush error stack */
  2787. TEST_openssl_errors();
  2788. /* Check decryption of encrypted result */
  2789. if (!TEST_int_gt(EVP_PKEY_decrypt_init(pctx), 0)
  2790. || !TEST_int_gt(EVP_PKEY_decrypt(pctx, pbuf, &plen, cbuf, clen), 0)
  2791. || !TEST_ptr(pbuf = OPENSSL_malloc(plen))
  2792. || !TEST_int_gt(EVP_PKEY_decrypt(pctx, pbuf, &plen, cbuf, clen), 0)
  2793. || !TEST_mem_eq(pbuf, plen, kMsg, sizeof(kMsg))
  2794. || !TEST_int_gt(EVP_PKEY_encrypt_init(pctx), 0))
  2795. goto done;
  2796. ret = 1;
  2797. done:
  2798. EVP_PKEY_CTX_free(pctx);
  2799. EVP_PKEY_free(pkey);
  2800. OPENSSL_free(cbuf);
  2801. OPENSSL_free(pbuf);
  2802. return ret;
  2803. }
  2804. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  2805. static int test_decrypt_null_chunks(void)
  2806. {
  2807. EVP_CIPHER_CTX* ctx = NULL;
  2808. EVP_CIPHER *cipher = NULL;
  2809. const unsigned char key[32] = {
  2810. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  2811. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  2812. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1
  2813. };
  2814. unsigned char iv[12] = {
  2815. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
  2816. };
  2817. unsigned char msg[] = "It was the best of times, it was the worst of times";
  2818. unsigned char ciphertext[80];
  2819. unsigned char plaintext[80];
  2820. /* We initialise tmp to a non zero value on purpose */
  2821. int ctlen, ptlen, tmp = 99;
  2822. int ret = 0;
  2823. const int enc_offset = 10, dec_offset = 20;
  2824. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "ChaCha20-Poly1305", testpropq))
  2825. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  2826. || !TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL,
  2827. key, iv))
  2828. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg,
  2829. enc_offset))
  2830. /* Deliberate add a zero length update */
  2831. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL,
  2832. 0))
  2833. || !TEST_int_eq(tmp, 0)
  2834. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp,
  2835. msg + enc_offset,
  2836. sizeof(msg) - enc_offset))
  2837. || !TEST_int_eq(ctlen += tmp, sizeof(msg))
  2838. || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp))
  2839. || !TEST_int_eq(tmp, 0))
  2840. goto err;
  2841. /* Deliberately initialise tmp to a non zero value */
  2842. tmp = 99;
  2843. if (!TEST_true(EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv))
  2844. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext,
  2845. dec_offset))
  2846. /*
  2847. * Deliberately add a zero length update. We also deliberately do
  2848. * this at a different offset than for encryption.
  2849. */
  2850. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL,
  2851. 0))
  2852. || !TEST_int_eq(tmp, 0)
  2853. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp,
  2854. ciphertext + dec_offset,
  2855. ctlen - dec_offset))
  2856. || !TEST_int_eq(ptlen += tmp, sizeof(msg))
  2857. || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp))
  2858. || !TEST_int_eq(tmp, 0)
  2859. || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen))
  2860. goto err;
  2861. ret = 1;
  2862. err:
  2863. EVP_CIPHER_CTX_free(ctx);
  2864. EVP_CIPHER_free(cipher);
  2865. return ret;
  2866. }
  2867. #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */
  2868. #ifndef OPENSSL_NO_DH
  2869. /*
  2870. * Test combinations of private, public, missing and private + public key
  2871. * params to ensure they are all accepted
  2872. */
  2873. static int test_DH_priv_pub(void)
  2874. {
  2875. return test_EVP_PKEY_ffc_priv_pub("DH");
  2876. }
  2877. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2878. static int test_EVP_PKEY_set1_DH(void)
  2879. {
  2880. DH *x942dh = NULL, *noqdh = NULL;
  2881. EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
  2882. int ret = 0;
  2883. BIGNUM *p, *g = NULL;
  2884. BIGNUM *pubkey = NULL;
  2885. unsigned char pub[2048 / 8];
  2886. size_t len = 0;
  2887. if (!TEST_ptr(p = BN_new())
  2888. || !TEST_ptr(g = BN_new())
  2889. || !TEST_ptr(pubkey = BN_new())
  2890. || !TEST_true(BN_set_word(p, 9999))
  2891. || !TEST_true(BN_set_word(g, 2))
  2892. || !TEST_true(BN_set_word(pubkey, 4321))
  2893. || !TEST_ptr(noqdh = DH_new())
  2894. || !TEST_true(DH_set0_pqg(noqdh, p, NULL, g))
  2895. || !TEST_true(DH_set0_key(noqdh, pubkey, NULL))
  2896. || !TEST_ptr(pubkey = BN_new())
  2897. || !TEST_true(BN_set_word(pubkey, 4321)))
  2898. goto err;
  2899. p = g = NULL;
  2900. x942dh = DH_get_2048_256();
  2901. pkey1 = EVP_PKEY_new();
  2902. pkey2 = EVP_PKEY_new();
  2903. if (!TEST_ptr(x942dh)
  2904. || !TEST_ptr(noqdh)
  2905. || !TEST_ptr(pkey1)
  2906. || !TEST_ptr(pkey2)
  2907. || !TEST_true(DH_set0_key(x942dh, pubkey, NULL)))
  2908. goto err;
  2909. pubkey = NULL;
  2910. if (!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh))
  2911. || !TEST_int_eq(EVP_PKEY_get_id(pkey1), EVP_PKEY_DHX))
  2912. goto err;
  2913. if (!TEST_true(EVP_PKEY_get_bn_param(pkey1, OSSL_PKEY_PARAM_PUB_KEY,
  2914. &pubkey))
  2915. || !TEST_ptr(pubkey))
  2916. goto err;
  2917. if (!TEST_true(EVP_PKEY_set1_DH(pkey2, noqdh))
  2918. || !TEST_int_eq(EVP_PKEY_get_id(pkey2), EVP_PKEY_DH))
  2919. goto err;
  2920. if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey2,
  2921. OSSL_PKEY_PARAM_PUB_KEY,
  2922. pub, sizeof(pub), &len))
  2923. || !TEST_size_t_ne(len, 0))
  2924. goto err;
  2925. ret = 1;
  2926. err:
  2927. BN_free(p);
  2928. BN_free(g);
  2929. BN_free(pubkey);
  2930. EVP_PKEY_free(pkey1);
  2931. EVP_PKEY_free(pkey2);
  2932. DH_free(x942dh);
  2933. DH_free(noqdh);
  2934. return ret;
  2935. }
  2936. # endif /* !OPENSSL_NO_DEPRECATED_3_0 */
  2937. #endif /* !OPENSSL_NO_DH */
  2938. /*
  2939. * We test what happens with an empty template. For the sake of this test,
  2940. * the template must be ignored, and we know that's the case for RSA keys
  2941. * (this might arguably be a misfeature, but that's what we currently do,
  2942. * even in provider code, since that's how the legacy RSA implementation
  2943. * does things)
  2944. */
  2945. static int test_keygen_with_empty_template(int n)
  2946. {
  2947. EVP_PKEY_CTX *ctx = NULL;
  2948. EVP_PKEY *pkey = NULL;
  2949. EVP_PKEY *tkey = NULL;
  2950. int ret = 0;
  2951. if (nullprov != NULL)
  2952. return TEST_skip("Test does not support a non-default library context");
  2953. switch (n) {
  2954. case 0:
  2955. /* We do test with no template at all as well */
  2956. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)))
  2957. goto err;
  2958. break;
  2959. case 1:
  2960. /* Here we create an empty RSA key that serves as our template */
  2961. if (!TEST_ptr(tkey = EVP_PKEY_new())
  2962. || !TEST_true(EVP_PKEY_set_type(tkey, EVP_PKEY_RSA))
  2963. || !TEST_ptr(ctx = EVP_PKEY_CTX_new(tkey, NULL)))
  2964. goto err;
  2965. break;
  2966. }
  2967. if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  2968. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0))
  2969. goto err;
  2970. ret = 1;
  2971. err:
  2972. EVP_PKEY_CTX_free(ctx);
  2973. EVP_PKEY_free(pkey);
  2974. EVP_PKEY_free(tkey);
  2975. return ret;
  2976. }
  2977. /*
  2978. * Test that we fail if we attempt to use an algorithm that is not available
  2979. * in the current library context (unless we are using an algorithm that
  2980. * should be made available via legacy codepaths).
  2981. *
  2982. * 0: RSA
  2983. * 1: SM2
  2984. */
  2985. static int test_pkey_ctx_fail_without_provider(int tst)
  2986. {
  2987. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  2988. OSSL_PROVIDER *tmpnullprov = NULL;
  2989. EVP_PKEY_CTX *pctx = NULL;
  2990. const char *keytype = NULL;
  2991. int expect_null = 0;
  2992. int ret = 0;
  2993. if (!TEST_ptr(tmpctx))
  2994. goto err;
  2995. tmpnullprov = OSSL_PROVIDER_load(tmpctx, "null");
  2996. if (!TEST_ptr(tmpnullprov))
  2997. goto err;
  2998. /*
  2999. * We check for certain algos in the null provider.
  3000. * If an algo is expected to have a provider keymgmt, constructing an
  3001. * EVP_PKEY_CTX is expected to fail (return NULL).
  3002. * Otherwise, if it's expected to have legacy support, constructing an
  3003. * EVP_PKEY_CTX is expected to succeed (return non-NULL).
  3004. */
  3005. switch (tst) {
  3006. case 0:
  3007. keytype = "RSA";
  3008. expect_null = 1;
  3009. break;
  3010. case 1:
  3011. keytype = "SM2";
  3012. expect_null = 1;
  3013. #ifdef OPENSSL_NO_EC
  3014. TEST_info("EC disable, skipping SM2 check...");
  3015. goto end;
  3016. #endif
  3017. #ifdef OPENSSL_NO_SM2
  3018. TEST_info("SM2 disable, skipping SM2 check...");
  3019. goto end;
  3020. #endif
  3021. break;
  3022. default:
  3023. TEST_error("No test for case %d", tst);
  3024. goto err;
  3025. }
  3026. pctx = EVP_PKEY_CTX_new_from_name(tmpctx, keytype, "");
  3027. if (expect_null ? !TEST_ptr_null(pctx) : !TEST_ptr(pctx))
  3028. goto err;
  3029. #if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SM2)
  3030. end:
  3031. #endif
  3032. ret = 1;
  3033. err:
  3034. EVP_PKEY_CTX_free(pctx);
  3035. OSSL_PROVIDER_unload(tmpnullprov);
  3036. OSSL_LIB_CTX_free(tmpctx);
  3037. return ret;
  3038. }
  3039. static int test_rand_agglomeration(void)
  3040. {
  3041. EVP_RAND *rand;
  3042. EVP_RAND_CTX *ctx;
  3043. OSSL_PARAM params[3], *p = params;
  3044. int res;
  3045. unsigned int step = 7;
  3046. static unsigned char seed[] = "It does not matter how slowly you go "
  3047. "as long as you do not stop.";
  3048. unsigned char out[sizeof(seed)];
  3049. if (!TEST_int_ne(sizeof(seed) % step, 0)
  3050. || !TEST_ptr(rand = EVP_RAND_fetch(testctx, "TEST-RAND", testpropq)))
  3051. return 0;
  3052. ctx = EVP_RAND_CTX_new(rand, NULL);
  3053. EVP_RAND_free(rand);
  3054. if (!TEST_ptr(ctx))
  3055. return 0;
  3056. memset(out, 0, sizeof(out));
  3057. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  3058. seed, sizeof(seed));
  3059. *p++ = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_MAX_REQUEST, &step);
  3060. *p = OSSL_PARAM_construct_end();
  3061. res = TEST_true(EVP_RAND_CTX_set_params(ctx, params))
  3062. && TEST_true(EVP_RAND_generate(ctx, out, sizeof(out), 0, 1, NULL, 0))
  3063. && TEST_mem_eq(seed, sizeof(seed), out, sizeof(out));
  3064. EVP_RAND_CTX_free(ctx);
  3065. return res;
  3066. }
  3067. /*
  3068. * Test that we correctly return the original or "running" IV after
  3069. * an encryption operation.
  3070. * Run multiple times for some different relevant algorithms/modes.
  3071. */
  3072. static int test_evp_iv_aes(int idx)
  3073. {
  3074. int ret = 0;
  3075. EVP_CIPHER_CTX *ctx = NULL;
  3076. unsigned char key[16] = {0x4c, 0x43, 0xdb, 0xdd, 0x42, 0x73, 0x47, 0xd1,
  3077. 0xe5, 0x62, 0x7d, 0xcd, 0x4d, 0x76, 0x4d, 0x57};
  3078. unsigned char init_iv[EVP_MAX_IV_LENGTH] =
  3079. {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  3080. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34};
  3081. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  3082. 9, 10, 11, 12, 13, 14, 15, 16 };
  3083. unsigned char ciphertext[32], oiv[16], iv[16];
  3084. unsigned char *ref_iv;
  3085. unsigned char cbc_state[16] = {0x10, 0x2f, 0x05, 0xcc, 0xc2, 0x55, 0x72, 0xb9,
  3086. 0x88, 0xe6, 0x4a, 0x17, 0x10, 0x74, 0x22, 0x5e};
  3087. unsigned char ofb_state[16] = {0x76, 0xe6, 0x66, 0x61, 0xd0, 0x8a, 0xe4, 0x64,
  3088. 0xdd, 0x66, 0xbf, 0x00, 0xf0, 0xe3, 0x6f, 0xfd};
  3089. unsigned char cfb_state[16] = {0x77, 0xe4, 0x65, 0x65, 0xd5, 0x8c, 0xe3, 0x6c,
  3090. 0xd4, 0x6c, 0xb4, 0x0c, 0xfd, 0xed, 0x60, 0xed};
  3091. unsigned char gcm_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  3092. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  3093. unsigned char ccm_state[7] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98};
  3094. #ifndef OPENSSL_NO_OCB
  3095. unsigned char ocb_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  3096. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  3097. #endif
  3098. int len = sizeof(ciphertext);
  3099. size_t ivlen, ref_len;
  3100. const EVP_CIPHER *type = NULL;
  3101. int iv_reset = 0;
  3102. if (nullprov != NULL && idx < 6)
  3103. return TEST_skip("Test does not support a non-default library context");
  3104. switch (idx) {
  3105. case 0:
  3106. type = EVP_aes_128_cbc();
  3107. /* FALLTHROUGH */
  3108. case 6:
  3109. type = (type != NULL) ? type :
  3110. EVP_CIPHER_fetch(testctx, "aes-128-cbc", testpropq);
  3111. ref_iv = cbc_state;
  3112. ref_len = sizeof(cbc_state);
  3113. iv_reset = 1;
  3114. break;
  3115. case 1:
  3116. type = EVP_aes_128_ofb();
  3117. /* FALLTHROUGH */
  3118. case 7:
  3119. type = (type != NULL) ? type :
  3120. EVP_CIPHER_fetch(testctx, "aes-128-ofb", testpropq);
  3121. ref_iv = ofb_state;
  3122. ref_len = sizeof(ofb_state);
  3123. iv_reset = 1;
  3124. break;
  3125. case 2:
  3126. type = EVP_aes_128_cfb();
  3127. /* FALLTHROUGH */
  3128. case 8:
  3129. type = (type != NULL) ? type :
  3130. EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq);
  3131. ref_iv = cfb_state;
  3132. ref_len = sizeof(cfb_state);
  3133. iv_reset = 1;
  3134. break;
  3135. case 3:
  3136. type = EVP_aes_128_gcm();
  3137. /* FALLTHROUGH */
  3138. case 9:
  3139. type = (type != NULL) ? type :
  3140. EVP_CIPHER_fetch(testctx, "aes-128-gcm", testpropq);
  3141. ref_iv = gcm_state;
  3142. ref_len = sizeof(gcm_state);
  3143. break;
  3144. case 4:
  3145. type = EVP_aes_128_ccm();
  3146. /* FALLTHROUGH */
  3147. case 10:
  3148. type = (type != NULL) ? type :
  3149. EVP_CIPHER_fetch(testctx, "aes-128-ccm", testpropq);
  3150. ref_iv = ccm_state;
  3151. ref_len = sizeof(ccm_state);
  3152. break;
  3153. #ifdef OPENSSL_NO_OCB
  3154. case 5:
  3155. case 11:
  3156. return 1;
  3157. #else
  3158. case 5:
  3159. type = EVP_aes_128_ocb();
  3160. /* FALLTHROUGH */
  3161. case 11:
  3162. type = (type != NULL) ? type :
  3163. EVP_CIPHER_fetch(testctx, "aes-128-ocb", testpropq);
  3164. ref_iv = ocb_state;
  3165. ref_len = sizeof(ocb_state);
  3166. break;
  3167. #endif
  3168. default:
  3169. return 0;
  3170. }
  3171. if (!TEST_ptr(type)
  3172. || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
  3173. || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
  3174. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
  3175. (int)sizeof(msg)))
  3176. || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
  3177. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
  3178. || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
  3179. goto err;
  3180. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  3181. if (!TEST_int_gt(ivlen, 0))
  3182. goto err;
  3183. if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
  3184. || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
  3185. goto err;
  3186. /* CBC, OFB, and CFB modes: the updated iv must be reset after reinit */
  3187. if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
  3188. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
  3189. goto err;
  3190. if (iv_reset) {
  3191. if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
  3192. goto err;
  3193. } else {
  3194. if (!TEST_mem_eq(ref_iv, ivlen, iv, ivlen))
  3195. goto err;
  3196. }
  3197. ret = 1;
  3198. err:
  3199. EVP_CIPHER_CTX_free(ctx);
  3200. if (idx >= 6)
  3201. EVP_CIPHER_free((EVP_CIPHER *)type);
  3202. return ret;
  3203. }
  3204. #ifndef OPENSSL_NO_DES
  3205. static int test_evp_iv_des(int idx)
  3206. {
  3207. int ret = 0;
  3208. EVP_CIPHER_CTX *ctx = NULL;
  3209. static const unsigned char key[24] = {
  3210. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  3211. 0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86,
  3212. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  3213. };
  3214. static const unsigned char init_iv[8] = {
  3215. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  3216. };
  3217. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  3218. 9, 10, 11, 12, 13, 14, 15, 16 };
  3219. unsigned char ciphertext[32], oiv[8], iv[8];
  3220. unsigned const char *ref_iv;
  3221. static const unsigned char cbc_state_des[8] = {
  3222. 0x4f, 0xa3, 0x85, 0xcd, 0x8b, 0xf3, 0x06, 0x2a
  3223. };
  3224. static const unsigned char cbc_state_3des[8] = {
  3225. 0x35, 0x27, 0x7d, 0x65, 0x6c, 0xfb, 0x50, 0xd9
  3226. };
  3227. static const unsigned char ofb_state_des[8] = {
  3228. 0xa7, 0x0d, 0x1d, 0x45, 0xf9, 0x96, 0x3f, 0x2c
  3229. };
  3230. static const unsigned char ofb_state_3des[8] = {
  3231. 0xab, 0x16, 0x24, 0xbb, 0x5b, 0xac, 0xed, 0x5e
  3232. };
  3233. static const unsigned char cfb_state_des[8] = {
  3234. 0x91, 0xeb, 0x6d, 0x29, 0x4b, 0x08, 0xbd, 0x73
  3235. };
  3236. static const unsigned char cfb_state_3des[8] = {
  3237. 0x34, 0xdd, 0xfb, 0x47, 0x33, 0x1c, 0x61, 0xf7
  3238. };
  3239. int len = sizeof(ciphertext);
  3240. size_t ivlen, ref_len;
  3241. EVP_CIPHER *type = NULL;
  3242. if (lgcyprov == NULL && idx < 3)
  3243. return TEST_skip("Test requires legacy provider to be loaded");
  3244. switch (idx) {
  3245. case 0:
  3246. type = EVP_CIPHER_fetch(testctx, "des-cbc", testpropq);
  3247. ref_iv = cbc_state_des;
  3248. ref_len = sizeof(cbc_state_des);
  3249. break;
  3250. case 1:
  3251. type = EVP_CIPHER_fetch(testctx, "des-ofb", testpropq);
  3252. ref_iv = ofb_state_des;
  3253. ref_len = sizeof(ofb_state_des);
  3254. break;
  3255. case 2:
  3256. type = EVP_CIPHER_fetch(testctx, "des-cfb", testpropq);
  3257. ref_iv = cfb_state_des;
  3258. ref_len = sizeof(cfb_state_des);
  3259. break;
  3260. case 3:
  3261. type = EVP_CIPHER_fetch(testctx, "des-ede3-cbc", testpropq);
  3262. ref_iv = cbc_state_3des;
  3263. ref_len = sizeof(cbc_state_3des);
  3264. break;
  3265. case 4:
  3266. type = EVP_CIPHER_fetch(testctx, "des-ede3-ofb", testpropq);
  3267. ref_iv = ofb_state_3des;
  3268. ref_len = sizeof(ofb_state_3des);
  3269. break;
  3270. case 5:
  3271. type = EVP_CIPHER_fetch(testctx, "des-ede3-cfb", testpropq);
  3272. ref_iv = cfb_state_3des;
  3273. ref_len = sizeof(cfb_state_3des);
  3274. break;
  3275. default:
  3276. return 0;
  3277. }
  3278. if (!TEST_ptr(type)
  3279. || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
  3280. || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
  3281. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
  3282. (int)sizeof(msg)))
  3283. || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
  3284. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
  3285. || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
  3286. goto err;
  3287. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  3288. if (!TEST_int_gt(ivlen, 0))
  3289. goto err;
  3290. if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
  3291. || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
  3292. goto err;
  3293. if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
  3294. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
  3295. goto err;
  3296. if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
  3297. goto err;
  3298. ret = 1;
  3299. err:
  3300. EVP_CIPHER_CTX_free(ctx);
  3301. EVP_CIPHER_free(type);
  3302. return ret;
  3303. }
  3304. #endif
  3305. #ifndef OPENSSL_NO_BF
  3306. static int test_evp_bf_default_keylen(int idx)
  3307. {
  3308. int ret = 0;
  3309. static const char *algos[4] = {
  3310. "bf-ecb", "bf-cbc", "bf-cfb", "bf-ofb"
  3311. };
  3312. int ivlen[4] = { 0, 8, 8, 8 };
  3313. EVP_CIPHER *cipher = NULL;
  3314. if (lgcyprov == NULL)
  3315. return TEST_skip("Test requires legacy provider to be loaded");
  3316. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, algos[idx], testpropq))
  3317. || !TEST_int_eq(EVP_CIPHER_get_key_length(cipher), 16)
  3318. || !TEST_int_eq(EVP_CIPHER_get_iv_length(cipher), ivlen[idx]))
  3319. goto err;
  3320. ret = 1;
  3321. err:
  3322. EVP_CIPHER_free(cipher);
  3323. return ret;
  3324. }
  3325. #endif
  3326. #ifndef OPENSSL_NO_EC
  3327. static int ecpub_nids[] = {
  3328. NID_brainpoolP256r1, NID_X9_62_prime256v1,
  3329. NID_secp384r1, NID_secp521r1,
  3330. # ifndef OPENSSL_NO_EC2M
  3331. NID_sect233k1, NID_sect233r1, NID_sect283r1,
  3332. NID_sect409k1, NID_sect409r1, NID_sect571k1, NID_sect571r1,
  3333. # endif
  3334. NID_brainpoolP384r1, NID_brainpoolP512r1
  3335. };
  3336. static int test_ecpub(int idx)
  3337. {
  3338. int ret = 0, len, savelen;
  3339. int nid;
  3340. unsigned char buf[1024];
  3341. unsigned char *p;
  3342. EVP_PKEY *pkey = NULL;
  3343. EVP_PKEY_CTX *ctx = NULL;
  3344. # ifndef OPENSSL_NO_DEPRECATED_3_0
  3345. const unsigned char *q;
  3346. EVP_PKEY *pkey2 = NULL;
  3347. EC_KEY *ec = NULL;
  3348. # endif
  3349. if (nullprov != NULL)
  3350. return TEST_skip("Test does not support a non-default library context");
  3351. nid = ecpub_nids[idx];
  3352. ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  3353. if (!TEST_ptr(ctx)
  3354. || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  3355. || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid), 0)
  3356. || !TEST_true(EVP_PKEY_keygen(ctx, &pkey)))
  3357. goto done;
  3358. len = i2d_PublicKey(pkey, NULL);
  3359. savelen = len;
  3360. if (!TEST_int_ge(len, 1)
  3361. || !TEST_int_lt(len, 1024))
  3362. goto done;
  3363. p = buf;
  3364. len = i2d_PublicKey(pkey, &p);
  3365. if (!TEST_int_ge(len, 1)
  3366. || !TEST_int_eq(len, savelen))
  3367. goto done;
  3368. # ifndef OPENSSL_NO_DEPRECATED_3_0
  3369. /* Now try to decode the just-created DER. */
  3370. q = buf;
  3371. if (!TEST_ptr((pkey2 = EVP_PKEY_new()))
  3372. || !TEST_ptr((ec = EC_KEY_new_by_curve_name(nid)))
  3373. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey2, ec)))
  3374. goto done;
  3375. /* EC_KEY ownership transferred */
  3376. ec = NULL;
  3377. if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_EC, &pkey2, &q, savelen)))
  3378. goto done;
  3379. /* The keys should match. */
  3380. if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
  3381. goto done;
  3382. # endif
  3383. ret = 1;
  3384. done:
  3385. EVP_PKEY_CTX_free(ctx);
  3386. EVP_PKEY_free(pkey);
  3387. # ifndef OPENSSL_NO_DEPRECATED_3_0
  3388. EVP_PKEY_free(pkey2);
  3389. EC_KEY_free(ec);
  3390. # endif
  3391. return ret;
  3392. }
  3393. #endif
  3394. static int test_EVP_rsa_pss_with_keygen_bits(void)
  3395. {
  3396. int ret = 0;
  3397. EVP_PKEY_CTX *ctx = NULL;
  3398. EVP_PKEY *pkey = NULL;
  3399. EVP_MD *md;
  3400. md = EVP_MD_fetch(testctx, "sha256", testpropq);
  3401. ret = TEST_ptr(md)
  3402. && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA-PSS", testpropq)))
  3403. && TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  3404. && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 512), 0)
  3405. && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md), 0)
  3406. && TEST_true(EVP_PKEY_keygen(ctx, &pkey));
  3407. EVP_MD_free(md);
  3408. EVP_PKEY_free(pkey);
  3409. EVP_PKEY_CTX_free(ctx);
  3410. return ret;
  3411. }
  3412. static int test_EVP_rsa_pss_set_saltlen(void)
  3413. {
  3414. int ret = 0;
  3415. EVP_PKEY *pkey = NULL;
  3416. EVP_PKEY_CTX *pkey_ctx = NULL;
  3417. EVP_MD *sha256 = NULL;
  3418. EVP_MD_CTX *sha256_ctx = NULL;
  3419. int saltlen = 9999; /* buggy EVP_PKEY_CTX_get_rsa_pss_saltlen() didn't update this */
  3420. const int test_value = 32;
  3421. ret = TEST_ptr(pkey = load_example_rsa_key())
  3422. && TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", NULL))
  3423. && TEST_ptr(sha256_ctx = EVP_MD_CTX_new())
  3424. && TEST_true(EVP_DigestSignInit(sha256_ctx, &pkey_ctx, sha256, NULL, pkey))
  3425. && TEST_true(EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING))
  3426. && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, test_value), 0)
  3427. && TEST_int_gt(EVP_PKEY_CTX_get_rsa_pss_saltlen(pkey_ctx, &saltlen), 0)
  3428. && TEST_int_eq(saltlen, test_value);
  3429. EVP_MD_CTX_free(sha256_ctx);
  3430. EVP_PKEY_free(pkey);
  3431. EVP_MD_free(sha256);
  3432. return ret;
  3433. }
  3434. static int success = 1;
  3435. static void md_names(const char *name, void *vctx)
  3436. {
  3437. OSSL_LIB_CTX *ctx = (OSSL_LIB_CTX *)vctx;
  3438. /* Force a namemap update */
  3439. EVP_CIPHER *aes128 = EVP_CIPHER_fetch(ctx, "AES-128-CBC", NULL);
  3440. if (!TEST_ptr(aes128))
  3441. success = 0;
  3442. EVP_CIPHER_free(aes128);
  3443. }
  3444. /*
  3445. * Test that changing the namemap in a user callback works in a names_do_all
  3446. * function.
  3447. */
  3448. static int test_names_do_all(void)
  3449. {
  3450. /* We use a custom libctx so that we know the state of the namemap */
  3451. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  3452. EVP_MD *sha256 = NULL;
  3453. int testresult = 0;
  3454. if (!TEST_ptr(ctx))
  3455. goto err;
  3456. sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
  3457. if (!TEST_ptr(sha256))
  3458. goto err;
  3459. /*
  3460. * We loop through all the names for a given digest. This should still work
  3461. * even if the namemap changes part way through.
  3462. */
  3463. if (!TEST_true(EVP_MD_names_do_all(sha256, md_names, ctx)))
  3464. goto err;
  3465. if (!TEST_true(success))
  3466. goto err;
  3467. testresult = 1;
  3468. err:
  3469. EVP_MD_free(sha256);
  3470. OSSL_LIB_CTX_free(ctx);
  3471. return testresult;
  3472. }
  3473. typedef struct {
  3474. const char *cipher;
  3475. const unsigned char *key;
  3476. const unsigned char *iv;
  3477. const unsigned char *input;
  3478. const unsigned char *expected;
  3479. const unsigned char *tag;
  3480. size_t ivlen; /* 0 if we do not need to set a specific IV len */
  3481. size_t inlen;
  3482. size_t expectedlen;
  3483. size_t taglen;
  3484. int keyfirst;
  3485. int initenc;
  3486. int finalenc;
  3487. } EVP_INIT_TEST_st;
  3488. static const EVP_INIT_TEST_st evp_init_tests[] = {
  3489. {
  3490. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
  3491. cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
  3492. 0, 1, 0, 1
  3493. },
  3494. {
  3495. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
  3496. gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  3497. sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
  3498. sizeof(gcmDefaultTag), 1, 0, 1
  3499. },
  3500. {
  3501. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
  3502. cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
  3503. 0, 0, 0, 1
  3504. },
  3505. {
  3506. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
  3507. gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  3508. sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
  3509. sizeof(gcmDefaultTag), 0, 0, 1
  3510. },
  3511. {
  3512. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
  3513. cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
  3514. 0, 1, 1, 0
  3515. },
  3516. {
  3517. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
  3518. gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  3519. sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
  3520. sizeof(gcmDefaultTag), 1, 1, 0
  3521. },
  3522. {
  3523. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
  3524. cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
  3525. 0, 0, 1, 0
  3526. },
  3527. {
  3528. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
  3529. gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  3530. sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
  3531. sizeof(gcmDefaultTag), 0, 1, 0
  3532. }
  3533. };
  3534. /* use same key, iv and plaintext for cfb and ofb */
  3535. static const EVP_INIT_TEST_st evp_reinit_tests[] = {
  3536. {
  3537. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext_partial,
  3538. cfbCiphertext_partial, NULL, 0, sizeof(cfbPlaintext_partial),
  3539. sizeof(cfbCiphertext_partial), 0, 0, 1, 0
  3540. },
  3541. {
  3542. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext_partial,
  3543. cfbPlaintext_partial, NULL, 0, sizeof(cfbCiphertext_partial),
  3544. sizeof(cfbPlaintext_partial), 0, 0, 0, 0
  3545. },
  3546. {
  3547. "aes-128-ofb", kCFBDefaultKey, iCFBIV, cfbPlaintext_partial,
  3548. ofbCiphertext_partial, NULL, 0, sizeof(cfbPlaintext_partial),
  3549. sizeof(ofbCiphertext_partial), 0, 0, 1, 0
  3550. },
  3551. {
  3552. "aes-128-ofb", kCFBDefaultKey, iCFBIV, ofbCiphertext_partial,
  3553. cfbPlaintext_partial, NULL, 0, sizeof(ofbCiphertext_partial),
  3554. sizeof(cfbPlaintext_partial), 0, 0, 0, 0
  3555. },
  3556. };
  3557. static int evp_init_seq_set_iv(EVP_CIPHER_CTX *ctx, const EVP_INIT_TEST_st *t)
  3558. {
  3559. int res = 0;
  3560. if (t->ivlen != 0) {
  3561. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen, NULL), 0))
  3562. goto err;
  3563. }
  3564. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv, -1)))
  3565. goto err;
  3566. res = 1;
  3567. err:
  3568. return res;
  3569. }
  3570. /*
  3571. * Test step-wise cipher initialization via EVP_CipherInit_ex where the
  3572. * arguments are given one at a time and a final adjustment to the enc
  3573. * parameter sets the correct operation.
  3574. */
  3575. static int test_evp_init_seq(int idx)
  3576. {
  3577. int outlen1, outlen2;
  3578. int testresult = 0;
  3579. unsigned char outbuf[1024];
  3580. unsigned char tag[16];
  3581. const EVP_INIT_TEST_st *t = &evp_init_tests[idx];
  3582. EVP_CIPHER_CTX *ctx = NULL;
  3583. EVP_CIPHER *type = NULL;
  3584. size_t taglen = sizeof(tag);
  3585. char *errmsg = NULL;
  3586. ctx = EVP_CIPHER_CTX_new();
  3587. if (ctx == NULL) {
  3588. errmsg = "CTX_ALLOC";
  3589. goto err;
  3590. }
  3591. if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq))) {
  3592. errmsg = "CIPHER_FETCH";
  3593. goto err;
  3594. }
  3595. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, t->initenc))) {
  3596. errmsg = "EMPTY_ENC_INIT";
  3597. goto err;
  3598. }
  3599. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  3600. errmsg = "PADDING";
  3601. goto err;
  3602. }
  3603. if (t->keyfirst && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
  3604. errmsg = "KEY_INIT (before iv)";
  3605. goto err;
  3606. }
  3607. if (!evp_init_seq_set_iv(ctx, t)) {
  3608. errmsg = "IV_INIT";
  3609. goto err;
  3610. }
  3611. if (t->keyfirst == 0 && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
  3612. errmsg = "KEY_INIT (after iv)";
  3613. goto err;
  3614. }
  3615. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, t->finalenc))) {
  3616. errmsg = "FINAL_ENC_INIT";
  3617. goto err;
  3618. }
  3619. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  3620. errmsg = "CIPHER_UPDATE";
  3621. goto err;
  3622. }
  3623. if (t->finalenc == 0 && t->tag != NULL) {
  3624. /* Set expected tag */
  3625. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  3626. t->taglen, (void *)t->tag), 0)) {
  3627. errmsg = "SET_TAG";
  3628. goto err;
  3629. }
  3630. }
  3631. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3632. errmsg = "CIPHER_FINAL";
  3633. goto err;
  3634. }
  3635. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  3636. errmsg = "WRONG_RESULT";
  3637. goto err;
  3638. }
  3639. if (t->finalenc != 0 && t->tag != NULL) {
  3640. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
  3641. errmsg = "GET_TAG";
  3642. goto err;
  3643. }
  3644. if (!TEST_mem_eq(t->tag, t->taglen, tag, taglen)) {
  3645. errmsg = "TAG_ERROR";
  3646. goto err;
  3647. }
  3648. }
  3649. testresult = 1;
  3650. err:
  3651. if (errmsg != NULL)
  3652. TEST_info("evp_init_test %d: %s", idx, errmsg);
  3653. EVP_CIPHER_CTX_free(ctx);
  3654. EVP_CIPHER_free(type);
  3655. return testresult;
  3656. }
  3657. /*
  3658. * Test re-initialization of cipher context without changing key or iv.
  3659. * The result of both iteration should be the same.
  3660. */
  3661. static int test_evp_reinit_seq(int idx)
  3662. {
  3663. int outlen1, outlen2, outlen_final;
  3664. int testresult = 0;
  3665. unsigned char outbuf1[1024];
  3666. unsigned char outbuf2[1024];
  3667. const EVP_INIT_TEST_st *t = &evp_reinit_tests[idx];
  3668. EVP_CIPHER_CTX *ctx = NULL;
  3669. EVP_CIPHER *type = NULL;
  3670. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  3671. || !TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq))
  3672. /* setup cipher context */
  3673. || !TEST_true(EVP_CipherInit_ex2(ctx, type, t->key, t->iv, t->initenc, NULL))
  3674. /* first iteration */
  3675. || !TEST_true(EVP_CipherUpdate(ctx, outbuf1, &outlen1, t->input, t->inlen))
  3676. || !TEST_true(EVP_CipherFinal_ex(ctx, outbuf1, &outlen_final))
  3677. /* check test results iteration 1 */
  3678. || !TEST_mem_eq(t->expected, t->expectedlen, outbuf1, outlen1 + outlen_final)
  3679. /* now re-init the context (same cipher, key and iv) */
  3680. || !TEST_true(EVP_CipherInit_ex2(ctx, NULL, NULL, NULL, -1, NULL))
  3681. /* second iteration */
  3682. || !TEST_true(EVP_CipherUpdate(ctx, outbuf2, &outlen2, t->input, t->inlen))
  3683. || !TEST_true(EVP_CipherFinal_ex(ctx, outbuf2, &outlen_final))
  3684. /* check test results iteration 2 */
  3685. || !TEST_mem_eq(t->expected, t->expectedlen, outbuf2, outlen2 + outlen_final))
  3686. goto err;
  3687. testresult = 1;
  3688. err:
  3689. EVP_CIPHER_CTX_free(ctx);
  3690. EVP_CIPHER_free(type);
  3691. return testresult;
  3692. }
  3693. typedef struct {
  3694. const unsigned char *input;
  3695. const unsigned char *expected;
  3696. size_t inlen;
  3697. size_t expectedlen;
  3698. int enc;
  3699. } EVP_RESET_TEST_st;
  3700. static const EVP_RESET_TEST_st evp_reset_tests[] = {
  3701. {
  3702. cfbPlaintext, cfbCiphertext,
  3703. sizeof(cfbPlaintext), sizeof(cfbCiphertext), 1
  3704. },
  3705. {
  3706. cfbCiphertext, cfbPlaintext,
  3707. sizeof(cfbCiphertext), sizeof(cfbPlaintext), 0
  3708. }
  3709. };
  3710. /*
  3711. * Test a reset of a cipher via EVP_CipherInit_ex after the cipher has already
  3712. * been used.
  3713. */
  3714. static int test_evp_reset(int idx)
  3715. {
  3716. const EVP_RESET_TEST_st *t = &evp_reset_tests[idx];
  3717. int outlen1, outlen2;
  3718. int testresult = 0;
  3719. unsigned char outbuf[1024];
  3720. EVP_CIPHER_CTX *ctx = NULL;
  3721. EVP_CIPHER *type = NULL;
  3722. char *errmsg = NULL;
  3723. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  3724. errmsg = "CTX_ALLOC";
  3725. goto err;
  3726. }
  3727. if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq))) {
  3728. errmsg = "CIPHER_FETCH";
  3729. goto err;
  3730. }
  3731. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
  3732. errmsg = "CIPHER_INIT";
  3733. goto err;
  3734. }
  3735. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  3736. errmsg = "PADDING";
  3737. goto err;
  3738. }
  3739. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  3740. errmsg = "CIPHER_UPDATE";
  3741. goto err;
  3742. }
  3743. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3744. errmsg = "CIPHER_FINAL";
  3745. goto err;
  3746. }
  3747. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  3748. errmsg = "WRONG_RESULT";
  3749. goto err;
  3750. }
  3751. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1))) {
  3752. errmsg = "CIPHER_REINIT";
  3753. goto err;
  3754. }
  3755. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  3756. errmsg = "CIPHER_UPDATE (reinit)";
  3757. goto err;
  3758. }
  3759. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3760. errmsg = "CIPHER_FINAL (reinit)";
  3761. goto err;
  3762. }
  3763. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  3764. errmsg = "WRONG_RESULT (reinit)";
  3765. goto err;
  3766. }
  3767. testresult = 1;
  3768. err:
  3769. if (errmsg != NULL)
  3770. TEST_info("test_evp_reset %d: %s", idx, errmsg);
  3771. EVP_CIPHER_CTX_free(ctx);
  3772. EVP_CIPHER_free(type);
  3773. return testresult;
  3774. }
  3775. typedef struct {
  3776. const char *cipher;
  3777. int enc;
  3778. } EVP_UPDATED_IV_TEST_st;
  3779. static const EVP_UPDATED_IV_TEST_st evp_updated_iv_tests[] = {
  3780. {
  3781. "aes-128-cfb", 1
  3782. },
  3783. {
  3784. "aes-128-cfb", 0
  3785. },
  3786. {
  3787. "aes-128-cfb1", 1
  3788. },
  3789. {
  3790. "aes-128-cfb1", 0
  3791. },
  3792. {
  3793. "aes-128-cfb8", 1
  3794. },
  3795. {
  3796. "aes-128-cfb8", 0
  3797. },
  3798. {
  3799. "aes-128-ofb", 1
  3800. },
  3801. {
  3802. "aes-128-ofb", 0
  3803. },
  3804. {
  3805. "aes-128-ctr", 1
  3806. },
  3807. {
  3808. "aes-128-ctr", 0
  3809. },
  3810. {
  3811. "aes-128-cbc", 1
  3812. },
  3813. {
  3814. "aes-128-cbc", 0
  3815. }
  3816. };
  3817. /*
  3818. * Test that the IV in the context is updated during a crypto operation for CFB
  3819. * and OFB.
  3820. */
  3821. static int test_evp_updated_iv(int idx)
  3822. {
  3823. const EVP_UPDATED_IV_TEST_st *t = &evp_updated_iv_tests[idx];
  3824. int outlen1, outlen2;
  3825. int testresult = 0;
  3826. unsigned char outbuf[1024];
  3827. EVP_CIPHER_CTX *ctx = NULL;
  3828. EVP_CIPHER *type = NULL;
  3829. unsigned char updated_iv[EVP_MAX_IV_LENGTH];
  3830. int iv_len;
  3831. char *errmsg = NULL;
  3832. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  3833. errmsg = "CTX_ALLOC";
  3834. goto err;
  3835. }
  3836. if ((type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq)) == NULL) {
  3837. TEST_info("cipher %s not supported, skipping", t->cipher);
  3838. goto ok;
  3839. }
  3840. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
  3841. errmsg = "CIPHER_INIT";
  3842. goto err;
  3843. }
  3844. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  3845. errmsg = "PADDING";
  3846. goto err;
  3847. }
  3848. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, cfbPlaintext, sizeof(cfbPlaintext)))) {
  3849. errmsg = "CIPHER_UPDATE";
  3850. goto err;
  3851. }
  3852. if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, updated_iv, sizeof(updated_iv)))) {
  3853. errmsg = "CIPHER_CTX_GET_UPDATED_IV";
  3854. goto err;
  3855. }
  3856. iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
  3857. if (!TEST_int_ge(iv_len,0)) {
  3858. errmsg = "CIPHER_CTX_GET_IV_LEN";
  3859. goto err;
  3860. }
  3861. if (!TEST_mem_ne(iCFBIV, sizeof(iCFBIV), updated_iv, iv_len)) {
  3862. errmsg = "IV_NOT_UPDATED";
  3863. goto err;
  3864. }
  3865. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3866. errmsg = "CIPHER_FINAL";
  3867. goto err;
  3868. }
  3869. ok:
  3870. testresult = 1;
  3871. err:
  3872. if (errmsg != NULL)
  3873. TEST_info("test_evp_updated_iv %d: %s", idx, errmsg);
  3874. EVP_CIPHER_CTX_free(ctx);
  3875. EVP_CIPHER_free(type);
  3876. return testresult;
  3877. }
  3878. typedef struct {
  3879. const unsigned char *iv1;
  3880. const unsigned char *iv2;
  3881. const unsigned char *expected1;
  3882. const unsigned char *expected2;
  3883. const unsigned char *tag1;
  3884. const unsigned char *tag2;
  3885. size_t ivlen1;
  3886. size_t ivlen2;
  3887. size_t expectedlen1;
  3888. size_t expectedlen2;
  3889. } TEST_GCM_IV_REINIT_st;
  3890. static const TEST_GCM_IV_REINIT_st gcm_reinit_tests[] = {
  3891. {
  3892. iGCMResetIV1, iGCMResetIV2, gcmResetCiphertext1, gcmResetCiphertext2,
  3893. gcmResetTag1, gcmResetTag2, sizeof(iGCMResetIV1), sizeof(iGCMResetIV2),
  3894. sizeof(gcmResetCiphertext1), sizeof(gcmResetCiphertext2)
  3895. },
  3896. {
  3897. iGCMResetIV2, iGCMResetIV1, gcmResetCiphertext2, gcmResetCiphertext1,
  3898. gcmResetTag2, gcmResetTag1, sizeof(iGCMResetIV2), sizeof(iGCMResetIV1),
  3899. sizeof(gcmResetCiphertext2), sizeof(gcmResetCiphertext1)
  3900. }
  3901. };
  3902. static int test_gcm_reinit(int idx)
  3903. {
  3904. int outlen1, outlen2, outlen3;
  3905. int testresult = 0;
  3906. unsigned char outbuf[1024];
  3907. unsigned char tag[16];
  3908. const TEST_GCM_IV_REINIT_st *t = &gcm_reinit_tests[idx];
  3909. EVP_CIPHER_CTX *ctx = NULL;
  3910. EVP_CIPHER *type = NULL;
  3911. size_t taglen = sizeof(tag);
  3912. char *errmsg = NULL;
  3913. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  3914. errmsg = "CTX_ALLOC";
  3915. goto err;
  3916. }
  3917. if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-256-gcm", testpropq))) {
  3918. errmsg = "CIPHER_FETCH";
  3919. goto err;
  3920. }
  3921. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, 1))) {
  3922. errmsg = "ENC_INIT";
  3923. goto err;
  3924. }
  3925. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen1, NULL), 0)) {
  3926. errmsg = "SET_IVLEN1";
  3927. goto err;
  3928. }
  3929. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, kGCMResetKey, t->iv1, 1))) {
  3930. errmsg = "SET_IV1";
  3931. goto err;
  3932. }
  3933. if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
  3934. errmsg = "AAD1";
  3935. goto err;
  3936. }
  3937. EVP_CIPHER_CTX_set_padding(ctx, 0);
  3938. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
  3939. sizeof(gcmResetPlaintext)))) {
  3940. errmsg = "CIPHER_UPDATE1";
  3941. goto err;
  3942. }
  3943. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3944. errmsg = "CIPHER_FINAL1";
  3945. goto err;
  3946. }
  3947. if (!TEST_mem_eq(t->expected1, t->expectedlen1, outbuf, outlen1 + outlen2)) {
  3948. errmsg = "WRONG_RESULT1";
  3949. goto err;
  3950. }
  3951. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
  3952. errmsg = "GET_TAG1";
  3953. goto err;
  3954. }
  3955. if (!TEST_mem_eq(t->tag1, taglen, tag, taglen)) {
  3956. errmsg = "TAG_ERROR1";
  3957. goto err;
  3958. }
  3959. /* Now reinit */
  3960. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen2, NULL), 0)) {
  3961. errmsg = "SET_IVLEN2";
  3962. goto err;
  3963. }
  3964. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv2, -1))) {
  3965. errmsg = "SET_IV2";
  3966. goto err;
  3967. }
  3968. if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
  3969. errmsg = "AAD2";
  3970. goto err;
  3971. }
  3972. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
  3973. sizeof(gcmResetPlaintext)))) {
  3974. errmsg = "CIPHER_UPDATE2";
  3975. goto err;
  3976. }
  3977. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3978. errmsg = "CIPHER_FINAL2";
  3979. goto err;
  3980. }
  3981. if (!TEST_mem_eq(t->expected2, t->expectedlen2, outbuf, outlen1 + outlen2)) {
  3982. errmsg = "WRONG_RESULT2";
  3983. goto err;
  3984. }
  3985. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
  3986. errmsg = "GET_TAG2";
  3987. goto err;
  3988. }
  3989. if (!TEST_mem_eq(t->tag2, taglen, tag, taglen)) {
  3990. errmsg = "TAG_ERROR2";
  3991. goto err;
  3992. }
  3993. testresult = 1;
  3994. err:
  3995. if (errmsg != NULL)
  3996. TEST_info("evp_init_test %d: %s", idx, errmsg);
  3997. EVP_CIPHER_CTX_free(ctx);
  3998. EVP_CIPHER_free(type);
  3999. return testresult;
  4000. }
  4001. static const char *ivlen_change_ciphers[] = {
  4002. "AES-256-GCM",
  4003. #ifndef OPENSSL_NO_OCB
  4004. "AES-256-OCB",
  4005. #endif
  4006. "AES-256-CCM"
  4007. };
  4008. /* Negative test for ivlen change after iv being set */
  4009. static int test_ivlen_change(int idx)
  4010. {
  4011. int outlen;
  4012. int res = 0;
  4013. unsigned char outbuf[1024];
  4014. static const unsigned char iv[] = {
  4015. 0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  4016. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34
  4017. };
  4018. EVP_CIPHER_CTX *ctx = NULL;
  4019. EVP_CIPHER *ciph = NULL;
  4020. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  4021. size_t ivlen = 13; /* non-default IV length */
  4022. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  4023. goto err;
  4024. if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, ivlen_change_ciphers[idx],
  4025. testpropq)))
  4026. goto err;
  4027. if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, kGCMDefaultKey, iv, 1)))
  4028. goto err;
  4029. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
  4030. sizeof(gcmDefaultPlaintext))))
  4031. goto err;
  4032. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  4033. &ivlen);
  4034. if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params)))
  4035. goto err;
  4036. ERR_set_mark();
  4037. if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
  4038. sizeof(gcmDefaultPlaintext)))) {
  4039. ERR_clear_last_mark();
  4040. goto err;
  4041. }
  4042. ERR_pop_to_mark();
  4043. res = 1;
  4044. err:
  4045. EVP_CIPHER_CTX_free(ctx);
  4046. EVP_CIPHER_free(ciph);
  4047. return res;
  4048. }
  4049. static const char *keylen_change_ciphers[] = {
  4050. #ifndef OPENSSL_NO_BF
  4051. "BF-ECB",
  4052. #endif
  4053. #ifndef OPENSSL_NO_CAST
  4054. "CAST5-ECB",
  4055. #endif
  4056. #ifndef OPENSSL_NO_RC2
  4057. "RC2-ECB",
  4058. #endif
  4059. #ifndef OPENSSL_NO_RC4
  4060. "RC4",
  4061. #endif
  4062. #ifndef OPENSSL_NO_RC5
  4063. "RC5-ECB",
  4064. #endif
  4065. NULL
  4066. };
  4067. /* Negative test for keylen change after key was set */
  4068. static int test_keylen_change(int idx)
  4069. {
  4070. int outlen;
  4071. int res = 0;
  4072. unsigned char outbuf[1024];
  4073. static const unsigned char key[] = {
  4074. 0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  4075. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34
  4076. };
  4077. EVP_CIPHER_CTX *ctx = NULL;
  4078. EVP_CIPHER *ciph = NULL;
  4079. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  4080. size_t keylen = 12; /* non-default key length */
  4081. if (lgcyprov == NULL)
  4082. return TEST_skip("Test requires legacy provider to be loaded");
  4083. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  4084. goto err;
  4085. if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, keylen_change_ciphers[idx],
  4086. testpropq)))
  4087. goto err;
  4088. if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, key, NULL, 1)))
  4089. goto err;
  4090. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
  4091. sizeof(gcmDefaultPlaintext))))
  4092. goto err;
  4093. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
  4094. &keylen);
  4095. if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params)))
  4096. goto err;
  4097. ERR_set_mark();
  4098. if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
  4099. sizeof(gcmDefaultPlaintext)))) {
  4100. ERR_clear_last_mark();
  4101. goto err;
  4102. }
  4103. ERR_pop_to_mark();
  4104. res = 1;
  4105. err:
  4106. EVP_CIPHER_CTX_free(ctx);
  4107. EVP_CIPHER_free(ciph);
  4108. return res;
  4109. }
  4110. #ifndef OPENSSL_NO_DEPRECATED_3_0
  4111. static EVP_PKEY_METHOD *custom_pmeth = NULL;
  4112. static const EVP_PKEY_METHOD *orig_pmeth = NULL;
  4113. # define EVP_PKEY_CTRL_MY_COMMAND 9999
  4114. static int custom_pmeth_init(EVP_PKEY_CTX *ctx)
  4115. {
  4116. int (*pinit)(EVP_PKEY_CTX *ctx);
  4117. EVP_PKEY_meth_get_init(orig_pmeth, &pinit);
  4118. return pinit(ctx);
  4119. }
  4120. static void custom_pmeth_cleanup(EVP_PKEY_CTX *ctx)
  4121. {
  4122. void (*pcleanup)(EVP_PKEY_CTX *ctx);
  4123. EVP_PKEY_meth_get_cleanup(orig_pmeth, &pcleanup);
  4124. pcleanup(ctx);
  4125. }
  4126. static int custom_pmeth_sign(EVP_PKEY_CTX *ctx, unsigned char *out,
  4127. size_t *outlen, const unsigned char *in,
  4128. size_t inlen)
  4129. {
  4130. int (*psign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  4131. const unsigned char *tbs, size_t tbslen);
  4132. EVP_PKEY_meth_get_sign(orig_pmeth, NULL, &psign);
  4133. return psign(ctx, out, outlen, in, inlen);
  4134. }
  4135. static int custom_pmeth_digestsign(EVP_MD_CTX *ctx, unsigned char *sig,
  4136. size_t *siglen, const unsigned char *tbs,
  4137. size_t tbslen)
  4138. {
  4139. int (*pdigestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  4140. const unsigned char *tbs, size_t tbslen);
  4141. EVP_PKEY_meth_get_digestsign(orig_pmeth, &pdigestsign);
  4142. return pdigestsign(ctx, sig, siglen, tbs, tbslen);
  4143. }
  4144. static int custom_pmeth_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  4145. size_t *keylen)
  4146. {
  4147. int (*pderive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
  4148. EVP_PKEY_meth_get_derive(orig_pmeth, NULL, &pderive);
  4149. return pderive(ctx, key, keylen);
  4150. }
  4151. static int custom_pmeth_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
  4152. {
  4153. int (*pcopy)(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
  4154. EVP_PKEY_meth_get_copy(orig_pmeth, &pcopy);
  4155. return pcopy(dst, src);
  4156. }
  4157. static int ctrl_called;
  4158. static int custom_pmeth_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  4159. {
  4160. int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  4161. EVP_PKEY_meth_get_ctrl(orig_pmeth, &pctrl, NULL);
  4162. if (type == EVP_PKEY_CTRL_MY_COMMAND) {
  4163. ctrl_called = 1;
  4164. return 1;
  4165. }
  4166. return pctrl(ctx, type, p1, p2);
  4167. }
  4168. static int test_custom_pmeth(int idx)
  4169. {
  4170. EVP_PKEY_CTX *pctx = NULL;
  4171. EVP_MD_CTX *ctx = NULL;
  4172. EVP_PKEY *pkey = NULL;
  4173. int id, orig_id, orig_flags;
  4174. int testresult = 0;
  4175. size_t reslen;
  4176. unsigned char *res = NULL;
  4177. unsigned char msg[] = { 'H', 'e', 'l', 'l', 'o' };
  4178. const EVP_MD *md = EVP_sha256();
  4179. int doderive = 0;
  4180. ctrl_called = 0;
  4181. /* We call deprecated APIs so this test doesn't support a custom libctx */
  4182. if (testctx != NULL)
  4183. return 1;
  4184. switch (idx) {
  4185. case 0:
  4186. case 6:
  4187. id = EVP_PKEY_RSA;
  4188. pkey = load_example_rsa_key();
  4189. break;
  4190. case 1:
  4191. case 7:
  4192. # ifndef OPENSSL_NO_DSA
  4193. id = EVP_PKEY_DSA;
  4194. pkey = load_example_dsa_key();
  4195. break;
  4196. # else
  4197. return 1;
  4198. # endif
  4199. case 2:
  4200. case 8:
  4201. # ifndef OPENSSL_NO_EC
  4202. id = EVP_PKEY_EC;
  4203. pkey = load_example_ec_key();
  4204. break;
  4205. # else
  4206. return 1;
  4207. # endif
  4208. case 3:
  4209. case 9:
  4210. # ifndef OPENSSL_NO_ECX
  4211. id = EVP_PKEY_ED25519;
  4212. md = NULL;
  4213. pkey = load_example_ed25519_key();
  4214. break;
  4215. # else
  4216. return 1;
  4217. # endif
  4218. case 4:
  4219. case 10:
  4220. # ifndef OPENSSL_NO_DH
  4221. id = EVP_PKEY_DH;
  4222. doderive = 1;
  4223. pkey = load_example_dh_key();
  4224. break;
  4225. # else
  4226. return 1;
  4227. # endif
  4228. case 5:
  4229. case 11:
  4230. # ifndef OPENSSL_NO_ECX
  4231. id = EVP_PKEY_X25519;
  4232. doderive = 1;
  4233. pkey = load_example_x25519_key();
  4234. break;
  4235. # else
  4236. return 1;
  4237. # endif
  4238. default:
  4239. TEST_error("Should not happen");
  4240. goto err;
  4241. }
  4242. if (!TEST_ptr(pkey))
  4243. goto err;
  4244. if (idx < 6) {
  4245. if (!TEST_true(evp_pkey_is_provided(pkey)))
  4246. goto err;
  4247. } else {
  4248. EVP_PKEY *tmp = pkey;
  4249. /* Convert to a legacy key */
  4250. pkey = EVP_PKEY_new();
  4251. if (!TEST_ptr(pkey)) {
  4252. pkey = tmp;
  4253. goto err;
  4254. }
  4255. if (!TEST_true(evp_pkey_copy_downgraded(&pkey, tmp))) {
  4256. EVP_PKEY_free(tmp);
  4257. goto err;
  4258. }
  4259. EVP_PKEY_free(tmp);
  4260. if (!TEST_true(evp_pkey_is_legacy(pkey)))
  4261. goto err;
  4262. }
  4263. if (!TEST_ptr(orig_pmeth = EVP_PKEY_meth_find(id))
  4264. || !TEST_ptr(pkey))
  4265. goto err;
  4266. EVP_PKEY_meth_get0_info(&orig_id, &orig_flags, orig_pmeth);
  4267. if (!TEST_int_eq(orig_id, id)
  4268. || !TEST_ptr(custom_pmeth = EVP_PKEY_meth_new(id, orig_flags)))
  4269. goto err;
  4270. if (id == EVP_PKEY_ED25519) {
  4271. EVP_PKEY_meth_set_digestsign(custom_pmeth, custom_pmeth_digestsign);
  4272. } if (id == EVP_PKEY_DH || id == EVP_PKEY_X25519) {
  4273. EVP_PKEY_meth_set_derive(custom_pmeth, NULL, custom_pmeth_derive);
  4274. } else {
  4275. EVP_PKEY_meth_set_sign(custom_pmeth, NULL, custom_pmeth_sign);
  4276. }
  4277. if (id != EVP_PKEY_ED25519 && id != EVP_PKEY_X25519) {
  4278. EVP_PKEY_meth_set_init(custom_pmeth, custom_pmeth_init);
  4279. EVP_PKEY_meth_set_cleanup(custom_pmeth, custom_pmeth_cleanup);
  4280. EVP_PKEY_meth_set_copy(custom_pmeth, custom_pmeth_copy);
  4281. }
  4282. EVP_PKEY_meth_set_ctrl(custom_pmeth, custom_pmeth_ctrl, NULL);
  4283. if (!TEST_true(EVP_PKEY_meth_add0(custom_pmeth)))
  4284. goto err;
  4285. if (doderive) {
  4286. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  4287. if (!TEST_ptr(pctx)
  4288. || !TEST_int_eq(EVP_PKEY_derive_init(pctx), 1)
  4289. || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1,
  4290. EVP_PKEY_CTRL_MY_COMMAND, 0, NULL),
  4291. 1)
  4292. || !TEST_int_eq(ctrl_called, 1)
  4293. || !TEST_int_ge(EVP_PKEY_derive_set_peer(pctx, pkey), 1)
  4294. || !TEST_int_ge(EVP_PKEY_derive(pctx, NULL, &reslen), 1)
  4295. || !TEST_ptr(res = OPENSSL_malloc(reslen))
  4296. || !TEST_int_ge(EVP_PKEY_derive(pctx, res, &reslen), 1))
  4297. goto err;
  4298. } else {
  4299. ctx = EVP_MD_CTX_new();
  4300. reslen = EVP_PKEY_size(pkey);
  4301. res = OPENSSL_malloc(reslen);
  4302. if (!TEST_ptr(ctx)
  4303. || !TEST_ptr(res)
  4304. || !TEST_true(EVP_DigestSignInit(ctx, &pctx, md, NULL, pkey))
  4305. || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1,
  4306. EVP_PKEY_CTRL_MY_COMMAND, 0, NULL),
  4307. 1)
  4308. || !TEST_int_eq(ctrl_called, 1))
  4309. goto err;
  4310. if (id == EVP_PKEY_ED25519) {
  4311. if (!TEST_true(EVP_DigestSign(ctx, res, &reslen, msg, sizeof(msg))))
  4312. goto err;
  4313. } else {
  4314. if (!TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg)))
  4315. || !TEST_true(EVP_DigestSignFinal(ctx, res, &reslen)))
  4316. goto err;
  4317. }
  4318. }
  4319. testresult = 1;
  4320. err:
  4321. OPENSSL_free(res);
  4322. EVP_MD_CTX_free(ctx);
  4323. if (doderive)
  4324. EVP_PKEY_CTX_free(pctx);
  4325. EVP_PKEY_free(pkey);
  4326. EVP_PKEY_meth_remove(custom_pmeth);
  4327. EVP_PKEY_meth_free(custom_pmeth);
  4328. custom_pmeth = NULL;
  4329. return testresult;
  4330. }
  4331. static int test_evp_md_cipher_meth(void)
  4332. {
  4333. EVP_MD *md = EVP_MD_meth_dup(EVP_sha256());
  4334. EVP_CIPHER *ciph = EVP_CIPHER_meth_dup(EVP_aes_128_cbc());
  4335. int testresult = 0;
  4336. if (!TEST_ptr(md) || !TEST_ptr(ciph))
  4337. goto err;
  4338. testresult = 1;
  4339. err:
  4340. EVP_MD_meth_free(md);
  4341. EVP_CIPHER_meth_free(ciph);
  4342. return testresult;
  4343. }
  4344. typedef struct {
  4345. int data;
  4346. } custom_dgst_ctx;
  4347. static int custom_md_init_called = 0;
  4348. static int custom_md_cleanup_called = 0;
  4349. static int custom_md_init(EVP_MD_CTX *ctx)
  4350. {
  4351. custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
  4352. if (p == NULL)
  4353. return 0;
  4354. custom_md_init_called++;
  4355. return 1;
  4356. }
  4357. static int custom_md_cleanup(EVP_MD_CTX *ctx)
  4358. {
  4359. custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
  4360. if (p == NULL)
  4361. /* Nothing to do */
  4362. return 1;
  4363. custom_md_cleanup_called++;
  4364. return 1;
  4365. }
  4366. static int test_custom_md_meth(void)
  4367. {
  4368. ASN1_OBJECT *o = NULL;
  4369. EVP_MD_CTX *mdctx = NULL;
  4370. EVP_MD *tmp = NULL;
  4371. char mess[] = "Test Message\n";
  4372. unsigned char md_value[EVP_MAX_MD_SIZE];
  4373. unsigned int md_len;
  4374. int testresult = 0;
  4375. int nid;
  4376. /*
  4377. * We are testing deprecated functions. We don't support a non-default
  4378. * library context in this test.
  4379. */
  4380. if (testctx != NULL)
  4381. return TEST_skip("Non-default libctx");
  4382. custom_md_init_called = custom_md_cleanup_called = 0;
  4383. nid = OBJ_create("1.3.6.1.4.1.16604.998866.1", "custom-md", "custom-md");
  4384. if (!TEST_int_ne(nid, NID_undef))
  4385. goto err;
  4386. tmp = EVP_MD_meth_new(nid, NID_undef);
  4387. if (!TEST_ptr(tmp))
  4388. goto err;
  4389. if (!TEST_true(EVP_MD_meth_set_init(tmp, custom_md_init))
  4390. || !TEST_true(EVP_MD_meth_set_cleanup(tmp, custom_md_cleanup))
  4391. || !TEST_true(EVP_MD_meth_set_app_datasize(tmp,
  4392. sizeof(custom_dgst_ctx))))
  4393. goto err;
  4394. mdctx = EVP_MD_CTX_new();
  4395. if (!TEST_ptr(mdctx)
  4396. /*
  4397. * Initing our custom md and then initing another md should
  4398. * result in the init and cleanup functions of the custom md
  4399. * being called.
  4400. */
  4401. || !TEST_true(EVP_DigestInit_ex(mdctx, tmp, NULL))
  4402. || !TEST_true(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL))
  4403. || !TEST_true(EVP_DigestUpdate(mdctx, mess, strlen(mess)))
  4404. || !TEST_true(EVP_DigestFinal_ex(mdctx, md_value, &md_len))
  4405. || !TEST_int_eq(custom_md_init_called, 1)
  4406. || !TEST_int_eq(custom_md_cleanup_called, 1))
  4407. goto err;
  4408. if (!TEST_int_eq(OBJ_create("1.3.6.1.4.1.16604.998866.1",
  4409. "custom-md", "custom-md"), NID_undef)
  4410. || !TEST_int_eq(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OBJ)
  4411. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), OBJ_R_OID_EXISTS))
  4412. goto err;
  4413. o = ASN1_OBJECT_create(nid, (unsigned char *)
  4414. "\53\6\1\4\1\201\201\134\274\373\122\1", 12,
  4415. "custom-md", "custom-md");
  4416. if (!TEST_int_eq(OBJ_add_object(o), nid))
  4417. goto err;
  4418. testresult = 1;
  4419. err:
  4420. ASN1_OBJECT_free(o);
  4421. EVP_MD_CTX_free(mdctx);
  4422. EVP_MD_meth_free(tmp);
  4423. return testresult;
  4424. }
  4425. typedef struct {
  4426. int data;
  4427. } custom_ciph_ctx;
  4428. static int custom_ciph_init_called = 0;
  4429. static int custom_ciph_cleanup_called = 0;
  4430. static int custom_ciph_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  4431. const unsigned char *iv, int enc)
  4432. {
  4433. custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
  4434. if (p == NULL)
  4435. return 0;
  4436. custom_ciph_init_called++;
  4437. return 1;
  4438. }
  4439. static int custom_ciph_cleanup(EVP_CIPHER_CTX *ctx)
  4440. {
  4441. custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
  4442. if (p == NULL)
  4443. /* Nothing to do */
  4444. return 1;
  4445. custom_ciph_cleanup_called++;
  4446. return 1;
  4447. }
  4448. static int test_custom_ciph_meth(void)
  4449. {
  4450. EVP_CIPHER_CTX *ciphctx = NULL;
  4451. EVP_CIPHER *tmp = NULL;
  4452. int testresult = 0;
  4453. int nid;
  4454. /*
  4455. * We are testing deprecated functions. We don't support a non-default
  4456. * library context in this test.
  4457. */
  4458. if (testctx != NULL)
  4459. return TEST_skip("Non-default libctx");
  4460. custom_ciph_init_called = custom_ciph_cleanup_called = 0;
  4461. nid = OBJ_create("1.3.6.1.4.1.16604.998866.2", "custom-ciph", "custom-ciph");
  4462. if (!TEST_int_ne(nid, NID_undef))
  4463. goto err;
  4464. tmp = EVP_CIPHER_meth_new(nid, 16, 16);
  4465. if (!TEST_ptr(tmp))
  4466. goto err;
  4467. if (!TEST_true(EVP_CIPHER_meth_set_init(tmp, custom_ciph_init))
  4468. || !TEST_true(EVP_CIPHER_meth_set_flags(tmp, EVP_CIPH_ALWAYS_CALL_INIT))
  4469. || !TEST_true(EVP_CIPHER_meth_set_cleanup(tmp, custom_ciph_cleanup))
  4470. || !TEST_true(EVP_CIPHER_meth_set_impl_ctx_size(tmp,
  4471. sizeof(custom_ciph_ctx))))
  4472. goto err;
  4473. ciphctx = EVP_CIPHER_CTX_new();
  4474. if (!TEST_ptr(ciphctx)
  4475. /*
  4476. * Initing our custom cipher and then initing another cipher
  4477. * should result in the init and cleanup functions of the custom
  4478. * cipher being called.
  4479. */
  4480. || !TEST_true(EVP_CipherInit_ex(ciphctx, tmp, NULL, NULL, NULL, 1))
  4481. || !TEST_true(EVP_CipherInit_ex(ciphctx, EVP_aes_128_cbc(), NULL,
  4482. NULL, NULL, 1))
  4483. || !TEST_int_eq(custom_ciph_init_called, 1)
  4484. || !TEST_int_eq(custom_ciph_cleanup_called, 1))
  4485. goto err;
  4486. testresult = 1;
  4487. err:
  4488. EVP_CIPHER_CTX_free(ciphctx);
  4489. EVP_CIPHER_meth_free(tmp);
  4490. return testresult;
  4491. }
  4492. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  4493. /* Test we can create a signature keys with an associated ENGINE */
  4494. static int test_signatures_with_engine(int tst)
  4495. {
  4496. ENGINE *e;
  4497. const char *engine_id = "dasync";
  4498. EVP_PKEY *pkey = NULL;
  4499. const unsigned char badcmackey[] = { 0x00, 0x01 };
  4500. const unsigned char cmackey[] = {
  4501. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4502. 0x0c, 0x0d, 0x0e, 0x0f
  4503. };
  4504. const unsigned char ed25519key[] = {
  4505. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4506. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  4507. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  4508. };
  4509. const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  4510. int testresult = 0;
  4511. EVP_MD_CTX *ctx = NULL;
  4512. unsigned char *mac = NULL;
  4513. size_t maclen = 0;
  4514. int ret;
  4515. # ifdef OPENSSL_NO_CMAC
  4516. /* Skip CMAC tests in a no-cmac build */
  4517. if (tst <= 1)
  4518. return 1;
  4519. # endif
  4520. # ifdef OPENSSL_NO_ECX
  4521. /* Skip ECX tests in a no-ecx build */
  4522. if (tst == 2)
  4523. return 1;
  4524. # endif
  4525. if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
  4526. return 0;
  4527. if (!TEST_true(ENGINE_init(e))) {
  4528. ENGINE_free(e);
  4529. return 0;
  4530. }
  4531. switch (tst) {
  4532. case 0:
  4533. pkey = EVP_PKEY_new_CMAC_key(e, cmackey, sizeof(cmackey),
  4534. EVP_aes_128_cbc());
  4535. break;
  4536. case 1:
  4537. pkey = EVP_PKEY_new_CMAC_key(e, badcmackey, sizeof(badcmackey),
  4538. EVP_aes_128_cbc());
  4539. break;
  4540. case 2:
  4541. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, e, ed25519key,
  4542. sizeof(ed25519key));
  4543. break;
  4544. default:
  4545. TEST_error("Invalid test case");
  4546. goto err;
  4547. }
  4548. if (!TEST_ptr(pkey))
  4549. goto err;
  4550. if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
  4551. goto err;
  4552. ret = EVP_DigestSignInit(ctx, NULL, tst == 2 ? NULL : EVP_sha256(), NULL,
  4553. pkey);
  4554. if (tst == 0) {
  4555. if (!TEST_true(ret))
  4556. goto err;
  4557. if (!TEST_true(EVP_DigestSignUpdate(ctx, msg, sizeof(msg)))
  4558. || !TEST_true(EVP_DigestSignFinal(ctx, NULL, &maclen)))
  4559. goto err;
  4560. if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
  4561. goto err;
  4562. if (!TEST_true(EVP_DigestSignFinal(ctx, mac, &maclen)))
  4563. goto err;
  4564. } else {
  4565. /* We used a bad key. We expect a failure here */
  4566. if (!TEST_false(ret))
  4567. goto err;
  4568. }
  4569. testresult = 1;
  4570. err:
  4571. EVP_MD_CTX_free(ctx);
  4572. OPENSSL_free(mac);
  4573. EVP_PKEY_free(pkey);
  4574. ENGINE_finish(e);
  4575. ENGINE_free(e);
  4576. return testresult;
  4577. }
  4578. static int test_cipher_with_engine(void)
  4579. {
  4580. ENGINE *e;
  4581. const char *engine_id = "dasync";
  4582. const unsigned char keyiv[] = {
  4583. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4584. 0x0c, 0x0d, 0x0e, 0x0f
  4585. };
  4586. const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  4587. int testresult = 0;
  4588. EVP_CIPHER_CTX *ctx = NULL, *ctx2 = NULL;
  4589. unsigned char buf[AES_BLOCK_SIZE];
  4590. int len = 0;
  4591. if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
  4592. return 0;
  4593. if (!TEST_true(ENGINE_init(e))) {
  4594. ENGINE_free(e);
  4595. return 0;
  4596. }
  4597. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  4598. || !TEST_ptr(ctx2 = EVP_CIPHER_CTX_new()))
  4599. goto err;
  4600. if (!TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), e, keyiv, keyiv)))
  4601. goto err;
  4602. /* Copy the ctx, and complete the operation with the new ctx */
  4603. if (!TEST_true(EVP_CIPHER_CTX_copy(ctx2, ctx)))
  4604. goto err;
  4605. if (!TEST_true(EVP_EncryptUpdate(ctx2, buf, &len, msg, sizeof(msg)))
  4606. || !TEST_true(EVP_EncryptFinal_ex(ctx2, buf + len, &len)))
  4607. goto err;
  4608. testresult = 1;
  4609. err:
  4610. EVP_CIPHER_CTX_free(ctx);
  4611. EVP_CIPHER_CTX_free(ctx2);
  4612. ENGINE_finish(e);
  4613. ENGINE_free(e);
  4614. return testresult;
  4615. }
  4616. # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
  4617. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  4618. #ifndef OPENSSL_NO_ECX
  4619. static int ecxnids[] = {
  4620. NID_X25519,
  4621. NID_X448,
  4622. NID_ED25519,
  4623. NID_ED448
  4624. };
  4625. /* Test that creating ECX keys with a short private key fails as expected */
  4626. static int test_ecx_short_keys(int tst)
  4627. {
  4628. unsigned char ecxkeydata = 1;
  4629. EVP_PKEY *pkey;
  4630. pkey = EVP_PKEY_new_raw_private_key_ex(testctx, OBJ_nid2sn(ecxnids[tst]),
  4631. NULL, &ecxkeydata, 1);
  4632. if (!TEST_ptr_null(pkey)) {
  4633. EVP_PKEY_free(pkey);
  4634. return 0;
  4635. }
  4636. return 1;
  4637. }
  4638. #endif
  4639. typedef enum OPTION_choice {
  4640. OPT_ERR = -1,
  4641. OPT_EOF = 0,
  4642. OPT_CONTEXT,
  4643. OPT_TEST_ENUM
  4644. } OPTION_CHOICE;
  4645. const OPTIONS *test_get_options(void)
  4646. {
  4647. static const OPTIONS options[] = {
  4648. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  4649. { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" },
  4650. { NULL }
  4651. };
  4652. return options;
  4653. }
  4654. #ifndef OPENSSL_NO_ECX
  4655. /* Test that trying to sign with a public key errors out gracefully */
  4656. static int test_ecx_not_private_key(int tst)
  4657. {
  4658. EVP_PKEY *pkey = NULL;
  4659. const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  4660. int testresult = 0;
  4661. EVP_MD_CTX *ctx = NULL;
  4662. unsigned char *mac = NULL;
  4663. size_t maclen = 0;
  4664. unsigned char *pubkey;
  4665. size_t pubkeylen;
  4666. switch (keys[tst].type) {
  4667. case NID_X25519:
  4668. case NID_X448:
  4669. return TEST_skip("signing not supported for X25519/X448");
  4670. }
  4671. /* Check if this algorithm supports public keys */
  4672. if (keys[tst].pub == NULL)
  4673. return TEST_skip("no public key present");
  4674. pubkey = (unsigned char *)keys[tst].pub;
  4675. pubkeylen = strlen(keys[tst].pub);
  4676. pkey = EVP_PKEY_new_raw_public_key_ex(testctx, OBJ_nid2sn(keys[tst].type),
  4677. NULL, pubkey, pubkeylen);
  4678. if (!TEST_ptr(pkey))
  4679. goto err;
  4680. if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
  4681. goto err;
  4682. if (EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey) != 1)
  4683. goto check_err;
  4684. if (EVP_DigestSign(ctx, NULL, &maclen, msg, sizeof(msg)) != 1)
  4685. goto check_err;
  4686. if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
  4687. goto err;
  4688. if (!TEST_int_eq(EVP_DigestSign(ctx, mac, &maclen, msg, sizeof(msg)), 0))
  4689. goto err;
  4690. check_err:
  4691. /*
  4692. * Currently only EVP_DigestSign will throw PROV_R_NOT_A_PRIVATE_KEY,
  4693. * but we relax the check to allow error also thrown by
  4694. * EVP_DigestSignInit and EVP_DigestSign.
  4695. */
  4696. if (ERR_GET_REASON(ERR_peek_error()) == PROV_R_NOT_A_PRIVATE_KEY) {
  4697. testresult = 1;
  4698. ERR_clear_error();
  4699. }
  4700. err:
  4701. EVP_MD_CTX_free(ctx);
  4702. OPENSSL_free(mac);
  4703. EVP_PKEY_free(pkey);
  4704. return testresult;
  4705. }
  4706. #endif /* OPENSSL_NO_ECX */
  4707. static int test_sign_continuation(void)
  4708. {
  4709. OSSL_PROVIDER *fake_rsa = NULL;
  4710. int testresult = 0;
  4711. EVP_PKEY *pkey = NULL;
  4712. EVP_PKEY_CTX *pctx = NULL;
  4713. EVP_MD_CTX *mctx = NULL;
  4714. const char sigbuf[] = "To Be Signed";
  4715. unsigned char signature[256];
  4716. size_t siglen = 256;
  4717. static int nodupnum = 1;
  4718. static const OSSL_PARAM nodup_params[] = {
  4719. OSSL_PARAM_int("NO_DUP", &nodupnum),
  4720. OSSL_PARAM_END
  4721. };
  4722. if (!TEST_ptr(fake_rsa = fake_rsa_start(testctx)))
  4723. return 0;
  4724. /* Construct a pkey using precise propq to use our provider */
  4725. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA",
  4726. "provider=fake-rsa"))
  4727. || !TEST_true(EVP_PKEY_fromdata_init(pctx))
  4728. || !TEST_true(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, NULL))
  4729. || !TEST_ptr(pkey))
  4730. goto end;
  4731. /* First test it continues (classic behavior) */
  4732. if (!TEST_ptr(mctx = EVP_MD_CTX_new())
  4733. || !TEST_true(EVP_DigestSignInit_ex(mctx, NULL, NULL, testctx,
  4734. NULL, pkey, NULL))
  4735. || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
  4736. || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen))
  4737. || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
  4738. || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen)))
  4739. goto end;
  4740. EVP_MD_CTX_free(mctx);
  4741. /* try again but failing the continuation */
  4742. if (!TEST_ptr(mctx = EVP_MD_CTX_new())
  4743. || !TEST_true(EVP_DigestSignInit_ex(mctx, NULL, NULL, testctx,
  4744. NULL, pkey, nodup_params))
  4745. || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
  4746. || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen))
  4747. || !TEST_false(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
  4748. || !TEST_false(EVP_DigestSignFinal(mctx, signature, &siglen)))
  4749. goto end;
  4750. testresult = 1;
  4751. end:
  4752. EVP_MD_CTX_free(mctx);
  4753. EVP_PKEY_free(pkey);
  4754. EVP_PKEY_CTX_free(pctx);
  4755. fake_rsa_finish(fake_rsa);
  4756. return testresult;
  4757. }
  4758. static int aes_gcm_encrypt(const unsigned char *gcm_key, size_t gcm_key_s,
  4759. const unsigned char *gcm_iv, size_t gcm_ivlen,
  4760. const unsigned char *gcm_pt, size_t gcm_pt_s,
  4761. const unsigned char *gcm_aad, size_t gcm_aad_s,
  4762. const unsigned char *gcm_ct, size_t gcm_ct_s,
  4763. const unsigned char *gcm_tag, size_t gcm_tag_s)
  4764. {
  4765. int ret = 0;
  4766. EVP_CIPHER_CTX *ctx;
  4767. EVP_CIPHER *cipher = NULL;
  4768. int outlen, tmplen;
  4769. unsigned char outbuf[1024];
  4770. unsigned char outtag[16];
  4771. OSSL_PARAM params[2] = {
  4772. OSSL_PARAM_END, OSSL_PARAM_END
  4773. };
  4774. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  4775. || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM", "")))
  4776. goto err;
  4777. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  4778. &gcm_ivlen);
  4779. if (!TEST_true(EVP_EncryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
  4780. || (gcm_aad != NULL
  4781. && !TEST_true(EVP_EncryptUpdate(ctx, NULL, &outlen,
  4782. gcm_aad, gcm_aad_s)))
  4783. || !TEST_true(EVP_EncryptUpdate(ctx, outbuf, &outlen,
  4784. gcm_pt, gcm_pt_s))
  4785. || !TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &tmplen)))
  4786. goto err;
  4787. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  4788. outtag, sizeof(outtag));
  4789. if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx, params))
  4790. || !TEST_mem_eq(outbuf, outlen, gcm_ct, gcm_ct_s)
  4791. || !TEST_mem_eq(outtag, gcm_tag_s, gcm_tag, gcm_tag_s))
  4792. goto err;
  4793. ret = 1;
  4794. err:
  4795. EVP_CIPHER_free(cipher);
  4796. EVP_CIPHER_CTX_free(ctx);
  4797. return ret;
  4798. }
  4799. static int aes_gcm_decrypt(const unsigned char *gcm_key, size_t gcm_key_s,
  4800. const unsigned char *gcm_iv, size_t gcm_ivlen,
  4801. const unsigned char *gcm_pt, size_t gcm_pt_s,
  4802. const unsigned char *gcm_aad, size_t gcm_aad_s,
  4803. const unsigned char *gcm_ct, size_t gcm_ct_s,
  4804. const unsigned char *gcm_tag, size_t gcm_tag_s)
  4805. {
  4806. int ret = 0;
  4807. EVP_CIPHER_CTX *ctx;
  4808. EVP_CIPHER *cipher = NULL;
  4809. int outlen;
  4810. unsigned char outbuf[1024];
  4811. OSSL_PARAM params[2] = {
  4812. OSSL_PARAM_END, OSSL_PARAM_END
  4813. };
  4814. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  4815. goto err;
  4816. if ((cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM", "")) == NULL)
  4817. goto err;
  4818. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  4819. &gcm_ivlen);
  4820. if (!TEST_true(EVP_DecryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
  4821. || (gcm_aad != NULL
  4822. && !TEST_true(EVP_DecryptUpdate(ctx, NULL, &outlen,
  4823. gcm_aad, gcm_aad_s)))
  4824. || !TEST_true(EVP_DecryptUpdate(ctx, outbuf, &outlen,
  4825. gcm_ct, gcm_ct_s))
  4826. || !TEST_mem_eq(outbuf, outlen, gcm_pt, gcm_pt_s))
  4827. goto err;
  4828. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  4829. (void*)gcm_tag, gcm_tag_s);
  4830. if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params))
  4831. ||!TEST_true(EVP_DecryptFinal_ex(ctx, outbuf, &outlen)))
  4832. goto err;
  4833. ret = 1;
  4834. err:
  4835. EVP_CIPHER_free(cipher);
  4836. EVP_CIPHER_CTX_free(ctx);
  4837. return ret;
  4838. }
  4839. static int test_aes_gcm_ivlen_change_cve_2023_5363(void)
  4840. {
  4841. /* AES-GCM test data obtained from NIST public test vectors */
  4842. static const unsigned char gcm_key[] = {
  4843. 0xd0, 0xc2, 0x67, 0xc1, 0x9f, 0x30, 0xd8, 0x0b, 0x89, 0x14, 0xbb, 0xbf,
  4844. 0xb7, 0x2f, 0x73, 0xb8, 0xd3, 0xcd, 0x5f, 0x6a, 0x78, 0x70, 0x15, 0x84,
  4845. 0x8a, 0x7b, 0x30, 0xe3, 0x8f, 0x16, 0xf1, 0x8b,
  4846. };
  4847. static const unsigned char gcm_iv[] = {
  4848. 0xb6, 0xdc, 0xda, 0x95, 0xac, 0x99, 0x77, 0x76, 0x25, 0xae, 0x87, 0xf8,
  4849. 0xa3, 0xa9, 0xdd, 0x64, 0xd7, 0x9b, 0xbd, 0x5f, 0x4a, 0x0e, 0x54, 0xca,
  4850. 0x1a, 0x9f, 0xa2, 0xe3, 0xf4, 0x5f, 0x5f, 0xc2, 0xce, 0xa7, 0xb6, 0x14,
  4851. 0x12, 0x6f, 0xf0, 0xaf, 0xfd, 0x3e, 0x17, 0x35, 0x6e, 0xa0, 0x16, 0x09,
  4852. 0xdd, 0xa1, 0x3f, 0xd8, 0xdd, 0xf3, 0xdf, 0x4f, 0xcb, 0x18, 0x49, 0xb8,
  4853. 0xb3, 0x69, 0x2c, 0x5d, 0x4f, 0xad, 0x30, 0x91, 0x08, 0xbc, 0xbe, 0x24,
  4854. 0x01, 0x0f, 0xbe, 0x9c, 0xfb, 0x4f, 0x5d, 0x19, 0x7f, 0x4c, 0x53, 0xb0,
  4855. 0x95, 0x90, 0xac, 0x7b, 0x1f, 0x7b, 0xa0, 0x99, 0xe1, 0xf3, 0x48, 0x54,
  4856. 0xd0, 0xfc, 0xa9, 0xcc, 0x91, 0xf8, 0x1f, 0x9b, 0x6c, 0x9a, 0xe0, 0xdc,
  4857. 0x63, 0xea, 0x7d, 0x2a, 0x4a, 0x7d, 0xa5, 0xed, 0x68, 0x57, 0x27, 0x6b,
  4858. 0x68, 0xe0, 0xf2, 0xb8, 0x51, 0x50, 0x8d, 0x3d,
  4859. };
  4860. static const unsigned char gcm_pt[] = {
  4861. 0xb8, 0xb6, 0x88, 0x36, 0x44, 0xe2, 0x34, 0xdf, 0x24, 0x32, 0x91, 0x07,
  4862. 0x4f, 0xe3, 0x6f, 0x81,
  4863. };
  4864. static const unsigned char gcm_ct[] = {
  4865. 0xff, 0x4f, 0xb3, 0xf3, 0xf9, 0xa2, 0x51, 0xd4, 0x82, 0xc2, 0xbe, 0xf3,
  4866. 0xe2, 0xd0, 0xec, 0xed,
  4867. };
  4868. static const unsigned char gcm_tag[] = {
  4869. 0xbd, 0x06, 0x38, 0x09, 0xf7, 0xe1, 0xc4, 0x72, 0x0e, 0xf2, 0xea, 0x63,
  4870. 0xdb, 0x99, 0x6c, 0x21,
  4871. };
  4872. return aes_gcm_encrypt(gcm_key, sizeof(gcm_key), gcm_iv, sizeof(gcm_iv),
  4873. gcm_pt, sizeof(gcm_pt), NULL, 0,
  4874. gcm_ct, sizeof(gcm_ct), gcm_tag, sizeof(gcm_tag))
  4875. && aes_gcm_decrypt(gcm_key, sizeof(gcm_key), gcm_iv, sizeof(gcm_iv),
  4876. gcm_pt, sizeof(gcm_pt), NULL, 0,
  4877. gcm_ct, sizeof(gcm_ct), gcm_tag, sizeof(gcm_tag));
  4878. }
  4879. #ifndef OPENSSL_NO_RC4
  4880. static int rc4_encrypt(const unsigned char *rc4_key, size_t rc4_key_s,
  4881. const unsigned char *rc4_pt, size_t rc4_pt_s,
  4882. const unsigned char *rc4_ct, size_t rc4_ct_s)
  4883. {
  4884. int ret = 0;
  4885. EVP_CIPHER_CTX *ctx;
  4886. EVP_CIPHER *cipher = NULL;
  4887. int outlen, tmplen;
  4888. unsigned char outbuf[1024];
  4889. OSSL_PARAM params[2] = {
  4890. OSSL_PARAM_END, OSSL_PARAM_END
  4891. };
  4892. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  4893. || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "RC4", "")))
  4894. goto err;
  4895. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
  4896. &rc4_key_s);
  4897. if (!TEST_true(EVP_EncryptInit_ex2(ctx, cipher, rc4_key, NULL, params))
  4898. || !TEST_true(EVP_EncryptUpdate(ctx, outbuf, &outlen,
  4899. rc4_pt, rc4_pt_s))
  4900. || !TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &tmplen)))
  4901. goto err;
  4902. if (!TEST_mem_eq(outbuf, outlen, rc4_ct, rc4_ct_s))
  4903. goto err;
  4904. ret = 1;
  4905. err:
  4906. EVP_CIPHER_free(cipher);
  4907. EVP_CIPHER_CTX_free(ctx);
  4908. return ret;
  4909. }
  4910. static int rc4_decrypt(const unsigned char *rc4_key, size_t rc4_key_s,
  4911. const unsigned char *rc4_pt, size_t rc4_pt_s,
  4912. const unsigned char *rc4_ct, size_t rc4_ct_s)
  4913. {
  4914. int ret = 0;
  4915. EVP_CIPHER_CTX *ctx;
  4916. EVP_CIPHER *cipher = NULL;
  4917. int outlen;
  4918. unsigned char outbuf[1024];
  4919. OSSL_PARAM params[2] = {
  4920. OSSL_PARAM_END, OSSL_PARAM_END
  4921. };
  4922. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  4923. goto err;
  4924. if ((cipher = EVP_CIPHER_fetch(testctx, "RC4", "")) == NULL)
  4925. goto err;
  4926. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
  4927. &rc4_key_s);
  4928. if (!TEST_true(EVP_DecryptInit_ex2(ctx, cipher, rc4_key, NULL, params))
  4929. || !TEST_true(EVP_DecryptUpdate(ctx, outbuf, &outlen,
  4930. rc4_ct, rc4_ct_s))
  4931. || !TEST_mem_eq(outbuf, outlen, rc4_pt, rc4_pt_s))
  4932. goto err;
  4933. ret = 1;
  4934. err:
  4935. EVP_CIPHER_free(cipher);
  4936. EVP_CIPHER_CTX_free(ctx);
  4937. return ret;
  4938. }
  4939. static int test_aes_rc4_keylen_change_cve_2023_5363(void)
  4940. {
  4941. /* RC4 test data obtained from RFC 6229 */
  4942. static const struct {
  4943. unsigned char key[5];
  4944. unsigned char padding[11];
  4945. } rc4_key = {
  4946. { /* Five bytes of key material */
  4947. 0x83, 0x32, 0x22, 0x77, 0x2a,
  4948. },
  4949. { /* Random padding to 16 bytes */
  4950. 0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a, 0xaa, 0x32, 0x91
  4951. }
  4952. };
  4953. static const unsigned char rc4_pt[] = {
  4954. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4955. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  4956. };
  4957. static const unsigned char rc4_ct[] = {
  4958. 0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a,
  4959. 0x2e, 0x87, 0x9e, 0x92, 0xa4, 0x97, 0xef, 0xda
  4960. };
  4961. if (lgcyprov == NULL)
  4962. return TEST_skip("Test requires legacy provider to be loaded");
  4963. return rc4_encrypt(rc4_key.key, sizeof(rc4_key.key),
  4964. rc4_pt, sizeof(rc4_pt), rc4_ct, sizeof(rc4_ct))
  4965. && rc4_decrypt(rc4_key.key, sizeof(rc4_key.key),
  4966. rc4_pt, sizeof(rc4_pt), rc4_ct, sizeof(rc4_ct));
  4967. }
  4968. #endif
  4969. static int test_invalid_ctx_for_digest(void)
  4970. {
  4971. int ret;
  4972. EVP_MD_CTX *mdctx;
  4973. mdctx = EVP_MD_CTX_new();
  4974. if (!TEST_ptr(mdctx))
  4975. return 0;
  4976. if (!TEST_int_eq(EVP_DigestUpdate(mdctx, "test", sizeof("test") - 1), 0))
  4977. ret = 0;
  4978. else
  4979. ret = 1;
  4980. EVP_MD_CTX_free(mdctx);
  4981. return ret;
  4982. }
  4983. int setup_tests(void)
  4984. {
  4985. OPTION_CHOICE o;
  4986. while ((o = opt_next()) != OPT_EOF) {
  4987. switch (o) {
  4988. case OPT_CONTEXT:
  4989. /* Set up an alternate library context */
  4990. testctx = OSSL_LIB_CTX_new();
  4991. if (!TEST_ptr(testctx))
  4992. return 0;
  4993. #ifdef STATIC_LEGACY
  4994. /*
  4995. * This test is always statically linked against libcrypto. We must not
  4996. * attempt to load legacy.so that might be dynamically linked against
  4997. * libcrypto. Instead we use a built-in version of the legacy provider.
  4998. */
  4999. if (!OSSL_PROVIDER_add_builtin(testctx, "legacy", ossl_legacy_provider_init))
  5000. return 0;
  5001. #endif
  5002. /* Swap the libctx to test non-default context only */
  5003. nullprov = OSSL_PROVIDER_load(NULL, "null");
  5004. deflprov = OSSL_PROVIDER_load(testctx, "default");
  5005. #ifndef OPENSSL_SYS_TANDEM
  5006. lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
  5007. #endif
  5008. break;
  5009. case OPT_TEST_CASES:
  5010. break;
  5011. default:
  5012. return 0;
  5013. }
  5014. }
  5015. ADD_TEST(test_EVP_set_default_properties);
  5016. ADD_ALL_TESTS(test_EVP_DigestSignInit, 30);
  5017. ADD_TEST(test_EVP_DigestVerifyInit);
  5018. #ifndef OPENSSL_NO_SIPHASH
  5019. ADD_TEST(test_siphash_digestsign);
  5020. #endif
  5021. ADD_TEST(test_EVP_Digest);
  5022. ADD_TEST(test_EVP_md_null);
  5023. ADD_ALL_TESTS(test_EVP_PKEY_sign, 3);
  5024. #ifndef OPENSSL_NO_DEPRECATED_3_0
  5025. ADD_ALL_TESTS(test_EVP_PKEY_sign_with_app_method, 2);
  5026. #endif
  5027. ADD_ALL_TESTS(test_EVP_Enveloped, 2);
  5028. ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
  5029. ADD_TEST(test_privatekey_to_pkcs8);
  5030. ADD_TEST(test_EVP_PKCS82PKEY_wrong_tag);
  5031. #ifndef OPENSSL_NO_EC
  5032. ADD_TEST(test_EVP_PKCS82PKEY);
  5033. #endif
  5034. #ifndef OPENSSL_NO_EC
  5035. ADD_ALL_TESTS(test_EC_keygen_with_enc, OSSL_NELEM(ec_encodings));
  5036. #endif
  5037. #if !defined(OPENSSL_NO_SM2)
  5038. ADD_TEST(test_EVP_SM2);
  5039. ADD_TEST(test_EVP_SM2_verify);
  5040. #endif
  5041. ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
  5042. #ifndef OPENSSL_NO_DEPRECATED_3_0
  5043. custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
  5044. if (!TEST_ptr(custom_pmeth))
  5045. return 0;
  5046. EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
  5047. EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
  5048. EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
  5049. if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
  5050. return 0;
  5051. #endif
  5052. ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
  5053. #ifndef OPENSSL_NO_CMAC
  5054. ADD_TEST(test_CMAC_keygen);
  5055. #endif
  5056. ADD_TEST(test_HKDF);
  5057. ADD_TEST(test_emptyikm_HKDF);
  5058. ADD_TEST(test_empty_salt_info_HKDF);
  5059. #ifndef OPENSSL_NO_EC
  5060. ADD_TEST(test_X509_PUBKEY_inplace);
  5061. ADD_TEST(test_X509_PUBKEY_dup);
  5062. ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
  5063. OSSL_NELEM(ec_der_pub_keys));
  5064. #endif
  5065. #ifndef OPENSSL_NO_DSA
  5066. ADD_TEST(test_DSA_get_set_params);
  5067. ADD_TEST(test_DSA_priv_pub);
  5068. #endif
  5069. ADD_TEST(test_RSA_get_set_params);
  5070. ADD_TEST(test_RSA_OAEP_set_get_params);
  5071. ADD_TEST(test_RSA_OAEP_set_null_label);
  5072. ADD_TEST(test_RSA_encrypt);
  5073. #ifndef OPENSSL_NO_DEPRECATED_3_0
  5074. ADD_TEST(test_RSA_legacy);
  5075. #endif
  5076. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  5077. ADD_TEST(test_decrypt_null_chunks);
  5078. #endif
  5079. #ifndef OPENSSL_NO_DH
  5080. ADD_TEST(test_DH_priv_pub);
  5081. # ifndef OPENSSL_NO_DEPRECATED_3_0
  5082. ADD_TEST(test_EVP_PKEY_set1_DH);
  5083. # endif
  5084. #endif
  5085. #ifndef OPENSSL_NO_EC
  5086. ADD_TEST(test_EC_priv_pub);
  5087. ADD_TEST(test_evp_get_ec_pub);
  5088. # ifndef OPENSSL_NO_DEPRECATED_3_0
  5089. ADD_TEST(test_EC_priv_only_legacy);
  5090. ADD_TEST(test_evp_get_ec_pub_legacy);
  5091. # endif
  5092. #endif
  5093. ADD_ALL_TESTS(test_keygen_with_empty_template, 2);
  5094. ADD_ALL_TESTS(test_pkey_ctx_fail_without_provider, 2);
  5095. ADD_TEST(test_rand_agglomeration);
  5096. ADD_ALL_TESTS(test_evp_iv_aes, 12);
  5097. #ifndef OPENSSL_NO_DES
  5098. ADD_ALL_TESTS(test_evp_iv_des, 6);
  5099. #endif
  5100. #ifndef OPENSSL_NO_BF
  5101. ADD_ALL_TESTS(test_evp_bf_default_keylen, 4);
  5102. #endif
  5103. ADD_TEST(test_EVP_rsa_pss_with_keygen_bits);
  5104. ADD_TEST(test_EVP_rsa_pss_set_saltlen);
  5105. #ifndef OPENSSL_NO_EC
  5106. ADD_ALL_TESTS(test_ecpub, OSSL_NELEM(ecpub_nids));
  5107. #endif
  5108. ADD_TEST(test_names_do_all);
  5109. ADD_ALL_TESTS(test_evp_init_seq, OSSL_NELEM(evp_init_tests));
  5110. ADD_ALL_TESTS(test_evp_reset, OSSL_NELEM(evp_reset_tests));
  5111. ADD_ALL_TESTS(test_evp_reinit_seq, OSSL_NELEM(evp_reinit_tests));
  5112. ADD_ALL_TESTS(test_gcm_reinit, OSSL_NELEM(gcm_reinit_tests));
  5113. ADD_ALL_TESTS(test_evp_updated_iv, OSSL_NELEM(evp_updated_iv_tests));
  5114. ADD_ALL_TESTS(test_ivlen_change, OSSL_NELEM(ivlen_change_ciphers));
  5115. if (OSSL_NELEM(keylen_change_ciphers) - 1 > 0)
  5116. ADD_ALL_TESTS(test_keylen_change, OSSL_NELEM(keylen_change_ciphers) - 1);
  5117. #ifndef OPENSSL_NO_DEPRECATED_3_0
  5118. ADD_ALL_TESTS(test_custom_pmeth, 12);
  5119. ADD_TEST(test_evp_md_cipher_meth);
  5120. ADD_TEST(test_custom_md_meth);
  5121. ADD_TEST(test_custom_ciph_meth);
  5122. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  5123. /* Tests only support the default libctx */
  5124. if (testctx == NULL) {
  5125. # ifndef OPENSSL_NO_EC
  5126. ADD_ALL_TESTS(test_signatures_with_engine, 3);
  5127. # else
  5128. ADD_ALL_TESTS(test_signatures_with_engine, 2);
  5129. # endif
  5130. ADD_TEST(test_cipher_with_engine);
  5131. }
  5132. # endif
  5133. #endif
  5134. #ifndef OPENSSL_NO_ECX
  5135. ADD_ALL_TESTS(test_ecx_short_keys, OSSL_NELEM(ecxnids));
  5136. ADD_ALL_TESTS(test_ecx_not_private_key, OSSL_NELEM(keys));
  5137. #endif
  5138. ADD_TEST(test_sign_continuation);
  5139. /* Test cases for CVE-2023-5363 */
  5140. ADD_TEST(test_aes_gcm_ivlen_change_cve_2023_5363);
  5141. #ifndef OPENSSL_NO_RC4
  5142. ADD_TEST(test_aes_rc4_keylen_change_cve_2023_5363);
  5143. #endif
  5144. ADD_TEST(test_invalid_ctx_for_digest);
  5145. return 1;
  5146. }
  5147. void cleanup_tests(void)
  5148. {
  5149. OSSL_PROVIDER_unload(nullprov);
  5150. OSSL_PROVIDER_unload(deflprov);
  5151. #ifndef OPENSSL_SYS_TANDEM
  5152. OSSL_PROVIDER_unload(lgcyprov);
  5153. #endif
  5154. OSSL_LIB_CTX_free(testctx);
  5155. }