evp_extra_test.c 191 KB

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