evp_extra_test.c 190 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767
  1. /*
  2. * Copyright 2015-2023 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. #ifndef OPENSSL_NO_EC
  2367. static int test_X509_PUBKEY_inplace(void)
  2368. {
  2369. int ret = 0;
  2370. X509_PUBKEY *xp = X509_PUBKEY_new_ex(testctx, testpropq);
  2371. const unsigned char *p = kExampleECPubKeyDER;
  2372. size_t input_len = sizeof(kExampleECPubKeyDER);
  2373. if (!TEST_ptr(xp))
  2374. goto done;
  2375. if (!TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len)))
  2376. goto done;
  2377. if (!TEST_ptr(X509_PUBKEY_get0(xp)))
  2378. goto done;
  2379. p = kExampleBadECPubKeyDER;
  2380. input_len = sizeof(kExampleBadECPubKeyDER);
  2381. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
  2382. goto done;
  2383. if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
  2384. goto done;
  2385. ret = 1;
  2386. done:
  2387. X509_PUBKEY_free(xp);
  2388. return ret;
  2389. }
  2390. static int test_X509_PUBKEY_dup(void)
  2391. {
  2392. int ret = 0;
  2393. X509_PUBKEY *xp = NULL, *xq = NULL;
  2394. const unsigned char *p = kExampleECPubKeyDER;
  2395. size_t input_len = sizeof(kExampleECPubKeyDER);
  2396. xp = X509_PUBKEY_new_ex(testctx, testpropq);
  2397. if (!TEST_ptr(xp)
  2398. || !TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len))
  2399. || !TEST_ptr(xq = X509_PUBKEY_dup(xp))
  2400. || !TEST_ptr_ne(xp, xq))
  2401. goto done;
  2402. if (!TEST_ptr(X509_PUBKEY_get0(xq))
  2403. || !TEST_ptr(X509_PUBKEY_get0(xp))
  2404. || !TEST_ptr_ne(X509_PUBKEY_get0(xq), X509_PUBKEY_get0(xp)))
  2405. goto done;
  2406. X509_PUBKEY_free(xq);
  2407. xq = NULL;
  2408. p = kExampleBadECPubKeyDER;
  2409. input_len = sizeof(kExampleBadECPubKeyDER);
  2410. if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len))
  2411. || !TEST_ptr(xq = X509_PUBKEY_dup(xp)))
  2412. goto done;
  2413. X509_PUBKEY_free(xp);
  2414. xp = NULL;
  2415. if (!TEST_true(X509_PUBKEY_get0(xq) == NULL))
  2416. goto done;
  2417. ret = 1;
  2418. done:
  2419. X509_PUBKEY_free(xp);
  2420. X509_PUBKEY_free(xq);
  2421. return ret;
  2422. }
  2423. #endif /* OPENSSL_NO_EC */
  2424. /* Test getting and setting parameters on an EVP_PKEY_CTX */
  2425. static int test_EVP_PKEY_CTX_get_set_params(EVP_PKEY *pkey)
  2426. {
  2427. EVP_MD_CTX *mdctx = NULL;
  2428. EVP_PKEY_CTX *ctx = NULL;
  2429. const OSSL_PARAM *params;
  2430. OSSL_PARAM ourparams[2], *param = ourparams, *param_md;
  2431. int ret = 0;
  2432. const EVP_MD *md;
  2433. char mdname[OSSL_MAX_NAME_SIZE];
  2434. char ssl3ms[48];
  2435. /* Initialise a sign operation */
  2436. ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq);
  2437. if (!TEST_ptr(ctx)
  2438. || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0))
  2439. goto err;
  2440. /*
  2441. * We should be able to query the parameters now.
  2442. */
  2443. params = EVP_PKEY_CTX_settable_params(ctx);
  2444. if (!TEST_ptr(params)
  2445. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  2446. OSSL_SIGNATURE_PARAM_DIGEST)))
  2447. goto err;
  2448. params = EVP_PKEY_CTX_gettable_params(ctx);
  2449. if (!TEST_ptr(params)
  2450. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  2451. OSSL_SIGNATURE_PARAM_ALGORITHM_ID))
  2452. || !TEST_ptr(OSSL_PARAM_locate_const(params,
  2453. OSSL_SIGNATURE_PARAM_DIGEST)))
  2454. goto err;
  2455. /*
  2456. * Test getting and setting params via EVP_PKEY_CTX_set_params() and
  2457. * EVP_PKEY_CTX_get_params()
  2458. */
  2459. strcpy(mdname, "SHA512");
  2460. param_md = param;
  2461. *param++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  2462. mdname, 0);
  2463. *param++ = OSSL_PARAM_construct_end();
  2464. if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
  2465. goto err;
  2466. mdname[0] = '\0';
  2467. *param_md = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  2468. mdname, sizeof(mdname));
  2469. if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
  2470. || !TEST_str_eq(mdname, "SHA512"))
  2471. goto err;
  2472. /*
  2473. * Test the TEST_PKEY_CTX_set_signature_md() and
  2474. * TEST_PKEY_CTX_get_signature_md() functions
  2475. */
  2476. if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
  2477. || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
  2478. || !TEST_ptr_eq(md, EVP_sha256()))
  2479. goto err;
  2480. /*
  2481. * Test getting MD parameters via an associated EVP_PKEY_CTX
  2482. */
  2483. mdctx = EVP_MD_CTX_new();
  2484. if (!TEST_ptr(mdctx)
  2485. || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", testctx, testpropq,
  2486. pkey, NULL)))
  2487. goto err;
  2488. /*
  2489. * We now have an EVP_MD_CTX with an EVP_PKEY_CTX inside it. We should be
  2490. * able to obtain the digest's settable parameters from the provider.
  2491. */
  2492. params = EVP_MD_CTX_settable_params(mdctx);
  2493. if (!TEST_ptr(params)
  2494. || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0)
  2495. /* The final key should be NULL */
  2496. || !TEST_ptr_null(params[1].key))
  2497. goto err;
  2498. param = ourparams;
  2499. memset(ssl3ms, 0, sizeof(ssl3ms));
  2500. *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
  2501. ssl3ms, sizeof(ssl3ms));
  2502. *param++ = OSSL_PARAM_construct_end();
  2503. if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams)))
  2504. goto err;
  2505. ret = 1;
  2506. err:
  2507. EVP_MD_CTX_free(mdctx);
  2508. EVP_PKEY_CTX_free(ctx);
  2509. return ret;
  2510. }
  2511. #ifndef OPENSSL_NO_DSA
  2512. static int test_DSA_get_set_params(void)
  2513. {
  2514. OSSL_PARAM_BLD *bld = NULL;
  2515. OSSL_PARAM *params = NULL;
  2516. BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
  2517. EVP_PKEY_CTX *pctx = NULL;
  2518. EVP_PKEY *pkey = NULL;
  2519. int ret = 0;
  2520. /*
  2521. * Setup the parameters for our DSA object. For our purposes they don't
  2522. * have to actually be *valid* parameters. We just need to set something.
  2523. */
  2524. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "DSA", NULL))
  2525. || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
  2526. || !TEST_ptr(p = BN_new())
  2527. || !TEST_ptr(q = BN_new())
  2528. || !TEST_ptr(g = BN_new())
  2529. || !TEST_ptr(pub = BN_new())
  2530. || !TEST_ptr(priv = BN_new()))
  2531. goto err;
  2532. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
  2533. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
  2534. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  2535. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
  2536. pub))
  2537. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
  2538. priv)))
  2539. goto err;
  2540. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  2541. goto err;
  2542. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  2543. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  2544. params), 0))
  2545. goto err;
  2546. if (!TEST_ptr(pkey))
  2547. goto err;
  2548. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  2549. err:
  2550. EVP_PKEY_free(pkey);
  2551. EVP_PKEY_CTX_free(pctx);
  2552. OSSL_PARAM_free(params);
  2553. OSSL_PARAM_BLD_free(bld);
  2554. BN_free(p);
  2555. BN_free(q);
  2556. BN_free(g);
  2557. BN_free(pub);
  2558. BN_free(priv);
  2559. return ret;
  2560. }
  2561. /*
  2562. * Test combinations of private, public, missing and private + public key
  2563. * params to ensure they are all accepted
  2564. */
  2565. static int test_DSA_priv_pub(void)
  2566. {
  2567. return test_EVP_PKEY_ffc_priv_pub("DSA");
  2568. }
  2569. #endif /* !OPENSSL_NO_DSA */
  2570. static int test_RSA_get_set_params(void)
  2571. {
  2572. OSSL_PARAM_BLD *bld = NULL;
  2573. OSSL_PARAM *params = NULL;
  2574. BIGNUM *n = NULL, *e = NULL, *d = NULL;
  2575. EVP_PKEY_CTX *pctx = NULL;
  2576. EVP_PKEY *pkey = NULL;
  2577. int ret = 0;
  2578. /*
  2579. * Setup the parameters for our RSA object. For our purposes they don't
  2580. * have to actually be *valid* parameters. We just need to set something.
  2581. */
  2582. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", NULL))
  2583. || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
  2584. || !TEST_ptr(n = BN_new())
  2585. || !TEST_ptr(e = BN_new())
  2586. || !TEST_ptr(d = BN_new()))
  2587. goto err;
  2588. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
  2589. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
  2590. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)))
  2591. goto err;
  2592. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
  2593. goto err;
  2594. if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  2595. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
  2596. params), 0))
  2597. goto err;
  2598. if (!TEST_ptr(pkey))
  2599. goto err;
  2600. ret = test_EVP_PKEY_CTX_get_set_params(pkey);
  2601. err:
  2602. EVP_PKEY_free(pkey);
  2603. EVP_PKEY_CTX_free(pctx);
  2604. OSSL_PARAM_free(params);
  2605. OSSL_PARAM_BLD_free(bld);
  2606. BN_free(n);
  2607. BN_free(e);
  2608. BN_free(d);
  2609. return ret;
  2610. }
  2611. static int test_RSA_OAEP_set_get_params(void)
  2612. {
  2613. int ret = 0;
  2614. EVP_PKEY *key = NULL;
  2615. EVP_PKEY_CTX *key_ctx = NULL;
  2616. if (nullprov != NULL)
  2617. return TEST_skip("Test does not support a non-default library context");
  2618. if (!TEST_ptr(key = load_example_rsa_key())
  2619. || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(0, key, 0)))
  2620. goto err;
  2621. {
  2622. int padding = RSA_PKCS1_OAEP_PADDING;
  2623. OSSL_PARAM params[4];
  2624. params[0] = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PAD_MODE, &padding);
  2625. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  2626. OSSL_DIGEST_NAME_SHA2_256, 0);
  2627. params[2] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
  2628. OSSL_DIGEST_NAME_SHA1, 0);
  2629. params[3] = OSSL_PARAM_construct_end();
  2630. if (!TEST_int_gt(EVP_PKEY_encrypt_init_ex(key_ctx, params),0))
  2631. goto err;
  2632. }
  2633. {
  2634. OSSL_PARAM params[3];
  2635. char oaepmd[30] = { '\0' };
  2636. char mgf1md[30] = { '\0' };
  2637. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  2638. oaepmd, sizeof(oaepmd));
  2639. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
  2640. mgf1md, sizeof(mgf1md));
  2641. params[2] = OSSL_PARAM_construct_end();
  2642. if (!TEST_true(EVP_PKEY_CTX_get_params(key_ctx, params)))
  2643. goto err;
  2644. if (!TEST_str_eq(oaepmd, OSSL_DIGEST_NAME_SHA2_256)
  2645. || !TEST_str_eq(mgf1md, OSSL_DIGEST_NAME_SHA1))
  2646. goto err;
  2647. }
  2648. ret = 1;
  2649. err:
  2650. EVP_PKEY_free(key);
  2651. EVP_PKEY_CTX_free(key_ctx);
  2652. return ret;
  2653. }
  2654. /* https://github.com/openssl/openssl/issues/21288 */
  2655. static int test_RSA_OAEP_set_null_label(void)
  2656. {
  2657. int ret = 0;
  2658. EVP_PKEY *key = NULL;
  2659. EVP_PKEY_CTX *key_ctx = NULL;
  2660. if (!TEST_ptr(key = load_example_rsa_key())
  2661. || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(testctx, key, NULL))
  2662. || !TEST_true(EVP_PKEY_encrypt_init(key_ctx)))
  2663. goto err;
  2664. if (!TEST_true(EVP_PKEY_CTX_set_rsa_padding(key_ctx, RSA_PKCS1_OAEP_PADDING)))
  2665. goto err;
  2666. if (!TEST_true(EVP_PKEY_CTX_set0_rsa_oaep_label(key_ctx, OPENSSL_strdup("foo"), 0)))
  2667. goto err;
  2668. if (!TEST_true(EVP_PKEY_CTX_set0_rsa_oaep_label(key_ctx, NULL, 0)))
  2669. goto err;
  2670. ret = 1;
  2671. err:
  2672. EVP_PKEY_free(key);
  2673. EVP_PKEY_CTX_free(key_ctx);
  2674. return ret;
  2675. }
  2676. #ifndef OPENSSL_NO_DEPRECATED_3_0
  2677. static int test_RSA_legacy(void)
  2678. {
  2679. int ret = 0;
  2680. BIGNUM *p = NULL;
  2681. BIGNUM *q = NULL;
  2682. BIGNUM *n = NULL;
  2683. BIGNUM *e = NULL;
  2684. BIGNUM *d = NULL;
  2685. const EVP_MD *md = EVP_sha256();
  2686. EVP_MD_CTX *ctx = NULL;
  2687. EVP_PKEY *pkey = NULL;
  2688. RSA *rsa = NULL;
  2689. if (nullprov != NULL)
  2690. return TEST_skip("Test does not support a non-default library context");
  2691. if (!TEST_ptr(p = BN_dup(BN_value_one()))
  2692. || !TEST_ptr(q = BN_dup(BN_value_one()))
  2693. || !TEST_ptr(n = BN_dup(BN_value_one()))
  2694. || !TEST_ptr(e = BN_dup(BN_value_one()))
  2695. || !TEST_ptr(d = BN_dup(BN_value_one())))
  2696. goto err;
  2697. if (!TEST_ptr(rsa = RSA_new())
  2698. || !TEST_ptr(pkey = EVP_PKEY_new())
  2699. || !TEST_ptr(ctx = EVP_MD_CTX_new()))
  2700. goto err;
  2701. if (!TEST_true(RSA_set0_factors(rsa, p, q)))
  2702. goto err;
  2703. p = NULL;
  2704. q = NULL;
  2705. if (!TEST_true(RSA_set0_key(rsa, n, e, d)))
  2706. goto err;
  2707. n = NULL;
  2708. e = NULL;
  2709. d = NULL;
  2710. if (!TEST_true(EVP_PKEY_assign_RSA(pkey, rsa)))
  2711. goto err;
  2712. rsa = NULL;
  2713. if (!TEST_true(EVP_DigestSignInit(ctx, NULL, md, NULL, pkey)))
  2714. goto err;
  2715. ret = 1;
  2716. err:
  2717. RSA_free(rsa);
  2718. EVP_MD_CTX_free(ctx);
  2719. EVP_PKEY_free(pkey);
  2720. BN_free(p);
  2721. BN_free(q);
  2722. BN_free(n);
  2723. BN_free(e);
  2724. BN_free(d);
  2725. return ret;
  2726. }
  2727. #endif
  2728. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  2729. static int test_decrypt_null_chunks(void)
  2730. {
  2731. EVP_CIPHER_CTX* ctx = NULL;
  2732. EVP_CIPHER *cipher = NULL;
  2733. const unsigned char key[32] = {
  2734. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  2735. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  2736. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1
  2737. };
  2738. unsigned char iv[12] = {
  2739. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
  2740. };
  2741. unsigned char msg[] = "It was the best of times, it was the worst of times";
  2742. unsigned char ciphertext[80];
  2743. unsigned char plaintext[80];
  2744. /* We initialise tmp to a non zero value on purpose */
  2745. int ctlen, ptlen, tmp = 99;
  2746. int ret = 0;
  2747. const int enc_offset = 10, dec_offset = 20;
  2748. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "ChaCha20-Poly1305", testpropq))
  2749. || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  2750. || !TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL,
  2751. key, iv))
  2752. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg,
  2753. enc_offset))
  2754. /* Deliberate add a zero length update */
  2755. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL,
  2756. 0))
  2757. || !TEST_int_eq(tmp, 0)
  2758. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp,
  2759. msg + enc_offset,
  2760. sizeof(msg) - enc_offset))
  2761. || !TEST_int_eq(ctlen += tmp, sizeof(msg))
  2762. || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp))
  2763. || !TEST_int_eq(tmp, 0))
  2764. goto err;
  2765. /* Deliberately initialise tmp to a non zero value */
  2766. tmp = 99;
  2767. if (!TEST_true(EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv))
  2768. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext,
  2769. dec_offset))
  2770. /*
  2771. * Deliberately add a zero length update. We also deliberately do
  2772. * this at a different offset than for encryption.
  2773. */
  2774. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL,
  2775. 0))
  2776. || !TEST_int_eq(tmp, 0)
  2777. || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp,
  2778. ciphertext + dec_offset,
  2779. ctlen - dec_offset))
  2780. || !TEST_int_eq(ptlen += tmp, sizeof(msg))
  2781. || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp))
  2782. || !TEST_int_eq(tmp, 0)
  2783. || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen))
  2784. goto err;
  2785. ret = 1;
  2786. err:
  2787. EVP_CIPHER_CTX_free(ctx);
  2788. EVP_CIPHER_free(cipher);
  2789. return ret;
  2790. }
  2791. #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */
  2792. #ifndef OPENSSL_NO_DH
  2793. /*
  2794. * Test combinations of private, public, missing and private + public key
  2795. * params to ensure they are all accepted
  2796. */
  2797. static int test_DH_priv_pub(void)
  2798. {
  2799. return test_EVP_PKEY_ffc_priv_pub("DH");
  2800. }
  2801. # ifndef OPENSSL_NO_DEPRECATED_3_0
  2802. static int test_EVP_PKEY_set1_DH(void)
  2803. {
  2804. DH *x942dh = NULL, *noqdh = NULL;
  2805. EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
  2806. int ret = 0;
  2807. BIGNUM *p, *g = NULL;
  2808. BIGNUM *pubkey = NULL;
  2809. unsigned char pub[2048 / 8];
  2810. size_t len = 0;
  2811. if (!TEST_ptr(p = BN_new())
  2812. || !TEST_ptr(g = BN_new())
  2813. || !TEST_ptr(pubkey = BN_new())
  2814. || !TEST_true(BN_set_word(p, 9999))
  2815. || !TEST_true(BN_set_word(g, 2))
  2816. || !TEST_true(BN_set_word(pubkey, 4321))
  2817. || !TEST_ptr(noqdh = DH_new())
  2818. || !TEST_true(DH_set0_pqg(noqdh, p, NULL, g))
  2819. || !TEST_true(DH_set0_key(noqdh, pubkey, NULL))
  2820. || !TEST_ptr(pubkey = BN_new())
  2821. || !TEST_true(BN_set_word(pubkey, 4321)))
  2822. goto err;
  2823. p = g = NULL;
  2824. x942dh = DH_get_2048_256();
  2825. pkey1 = EVP_PKEY_new();
  2826. pkey2 = EVP_PKEY_new();
  2827. if (!TEST_ptr(x942dh)
  2828. || !TEST_ptr(noqdh)
  2829. || !TEST_ptr(pkey1)
  2830. || !TEST_ptr(pkey2)
  2831. || !TEST_true(DH_set0_key(x942dh, pubkey, NULL)))
  2832. goto err;
  2833. pubkey = NULL;
  2834. if (!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh))
  2835. || !TEST_int_eq(EVP_PKEY_get_id(pkey1), EVP_PKEY_DHX))
  2836. goto err;
  2837. if (!TEST_true(EVP_PKEY_get_bn_param(pkey1, OSSL_PKEY_PARAM_PUB_KEY,
  2838. &pubkey))
  2839. || !TEST_ptr(pubkey))
  2840. goto err;
  2841. if (!TEST_true(EVP_PKEY_set1_DH(pkey2, noqdh))
  2842. || !TEST_int_eq(EVP_PKEY_get_id(pkey2), EVP_PKEY_DH))
  2843. goto err;
  2844. if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey2,
  2845. OSSL_PKEY_PARAM_PUB_KEY,
  2846. pub, sizeof(pub), &len))
  2847. || !TEST_size_t_ne(len, 0))
  2848. goto err;
  2849. ret = 1;
  2850. err:
  2851. BN_free(p);
  2852. BN_free(g);
  2853. BN_free(pubkey);
  2854. EVP_PKEY_free(pkey1);
  2855. EVP_PKEY_free(pkey2);
  2856. DH_free(x942dh);
  2857. DH_free(noqdh);
  2858. return ret;
  2859. }
  2860. # endif /* !OPENSSL_NO_DEPRECATED_3_0 */
  2861. #endif /* !OPENSSL_NO_DH */
  2862. /*
  2863. * We test what happens with an empty template. For the sake of this test,
  2864. * the template must be ignored, and we know that's the case for RSA keys
  2865. * (this might arguably be a misfeature, but that's what we currently do,
  2866. * even in provider code, since that's how the legacy RSA implementation
  2867. * does things)
  2868. */
  2869. static int test_keygen_with_empty_template(int n)
  2870. {
  2871. EVP_PKEY_CTX *ctx = NULL;
  2872. EVP_PKEY *pkey = NULL;
  2873. EVP_PKEY *tkey = NULL;
  2874. int ret = 0;
  2875. if (nullprov != NULL)
  2876. return TEST_skip("Test does not support a non-default library context");
  2877. switch (n) {
  2878. case 0:
  2879. /* We do test with no template at all as well */
  2880. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)))
  2881. goto err;
  2882. break;
  2883. case 1:
  2884. /* Here we create an empty RSA key that serves as our template */
  2885. if (!TEST_ptr(tkey = EVP_PKEY_new())
  2886. || !TEST_true(EVP_PKEY_set_type(tkey, EVP_PKEY_RSA))
  2887. || !TEST_ptr(ctx = EVP_PKEY_CTX_new(tkey, NULL)))
  2888. goto err;
  2889. break;
  2890. }
  2891. if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  2892. || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0))
  2893. goto err;
  2894. ret = 1;
  2895. err:
  2896. EVP_PKEY_CTX_free(ctx);
  2897. EVP_PKEY_free(pkey);
  2898. EVP_PKEY_free(tkey);
  2899. return ret;
  2900. }
  2901. /*
  2902. * Test that we fail if we attempt to use an algorithm that is not available
  2903. * in the current library context (unless we are using an algorithm that
  2904. * should be made available via legacy codepaths).
  2905. *
  2906. * 0: RSA
  2907. * 1: SM2
  2908. */
  2909. static int test_pkey_ctx_fail_without_provider(int tst)
  2910. {
  2911. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  2912. OSSL_PROVIDER *tmpnullprov = NULL;
  2913. EVP_PKEY_CTX *pctx = NULL;
  2914. const char *keytype = NULL;
  2915. int expect_null = 0;
  2916. int ret = 0;
  2917. if (!TEST_ptr(tmpctx))
  2918. goto err;
  2919. tmpnullprov = OSSL_PROVIDER_load(tmpctx, "null");
  2920. if (!TEST_ptr(tmpnullprov))
  2921. goto err;
  2922. /*
  2923. * We check for certain algos in the null provider.
  2924. * If an algo is expected to have a provider keymgmt, constructing an
  2925. * EVP_PKEY_CTX is expected to fail (return NULL).
  2926. * Otherwise, if it's expected to have legacy support, constructing an
  2927. * EVP_PKEY_CTX is expected to succeed (return non-NULL).
  2928. */
  2929. switch (tst) {
  2930. case 0:
  2931. keytype = "RSA";
  2932. expect_null = 1;
  2933. break;
  2934. case 1:
  2935. keytype = "SM2";
  2936. expect_null = 1;
  2937. #ifdef OPENSSL_NO_EC
  2938. TEST_info("EC disable, skipping SM2 check...");
  2939. goto end;
  2940. #endif
  2941. #ifdef OPENSSL_NO_SM2
  2942. TEST_info("SM2 disable, skipping SM2 check...");
  2943. goto end;
  2944. #endif
  2945. break;
  2946. default:
  2947. TEST_error("No test for case %d", tst);
  2948. goto err;
  2949. }
  2950. pctx = EVP_PKEY_CTX_new_from_name(tmpctx, keytype, "");
  2951. if (expect_null ? !TEST_ptr_null(pctx) : !TEST_ptr(pctx))
  2952. goto err;
  2953. #if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SM2)
  2954. end:
  2955. #endif
  2956. ret = 1;
  2957. err:
  2958. EVP_PKEY_CTX_free(pctx);
  2959. OSSL_PROVIDER_unload(tmpnullprov);
  2960. OSSL_LIB_CTX_free(tmpctx);
  2961. return ret;
  2962. }
  2963. static int test_rand_agglomeration(void)
  2964. {
  2965. EVP_RAND *rand;
  2966. EVP_RAND_CTX *ctx;
  2967. OSSL_PARAM params[3], *p = params;
  2968. int res;
  2969. unsigned int step = 7;
  2970. static unsigned char seed[] = "It does not matter how slowly you go "
  2971. "as long as you do not stop.";
  2972. unsigned char out[sizeof(seed)];
  2973. if (!TEST_int_ne(sizeof(seed) % step, 0)
  2974. || !TEST_ptr(rand = EVP_RAND_fetch(testctx, "TEST-RAND", testpropq)))
  2975. return 0;
  2976. ctx = EVP_RAND_CTX_new(rand, NULL);
  2977. EVP_RAND_free(rand);
  2978. if (!TEST_ptr(ctx))
  2979. return 0;
  2980. memset(out, 0, sizeof(out));
  2981. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  2982. seed, sizeof(seed));
  2983. *p++ = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_MAX_REQUEST, &step);
  2984. *p = OSSL_PARAM_construct_end();
  2985. res = TEST_true(EVP_RAND_CTX_set_params(ctx, params))
  2986. && TEST_true(EVP_RAND_generate(ctx, out, sizeof(out), 0, 1, NULL, 0))
  2987. && TEST_mem_eq(seed, sizeof(seed), out, sizeof(out));
  2988. EVP_RAND_CTX_free(ctx);
  2989. return res;
  2990. }
  2991. /*
  2992. * Test that we correctly return the original or "running" IV after
  2993. * an encryption operation.
  2994. * Run multiple times for some different relevant algorithms/modes.
  2995. */
  2996. static int test_evp_iv_aes(int idx)
  2997. {
  2998. int ret = 0;
  2999. EVP_CIPHER_CTX *ctx = NULL;
  3000. unsigned char key[16] = {0x4c, 0x43, 0xdb, 0xdd, 0x42, 0x73, 0x47, 0xd1,
  3001. 0xe5, 0x62, 0x7d, 0xcd, 0x4d, 0x76, 0x4d, 0x57};
  3002. unsigned char init_iv[EVP_MAX_IV_LENGTH] =
  3003. {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  3004. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34};
  3005. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  3006. 9, 10, 11, 12, 13, 14, 15, 16 };
  3007. unsigned char ciphertext[32], oiv[16], iv[16];
  3008. unsigned char *ref_iv;
  3009. unsigned char cbc_state[16] = {0x10, 0x2f, 0x05, 0xcc, 0xc2, 0x55, 0x72, 0xb9,
  3010. 0x88, 0xe6, 0x4a, 0x17, 0x10, 0x74, 0x22, 0x5e};
  3011. unsigned char ofb_state[16] = {0x76, 0xe6, 0x66, 0x61, 0xd0, 0x8a, 0xe4, 0x64,
  3012. 0xdd, 0x66, 0xbf, 0x00, 0xf0, 0xe3, 0x6f, 0xfd};
  3013. unsigned char cfb_state[16] = {0x77, 0xe4, 0x65, 0x65, 0xd5, 0x8c, 0xe3, 0x6c,
  3014. 0xd4, 0x6c, 0xb4, 0x0c, 0xfd, 0xed, 0x60, 0xed};
  3015. unsigned char gcm_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  3016. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  3017. unsigned char ccm_state[7] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98};
  3018. #ifndef OPENSSL_NO_OCB
  3019. unsigned char ocb_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
  3020. 0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
  3021. #endif
  3022. int len = sizeof(ciphertext);
  3023. size_t ivlen, ref_len;
  3024. const EVP_CIPHER *type = NULL;
  3025. int iv_reset = 0;
  3026. if (nullprov != NULL && idx < 6)
  3027. return TEST_skip("Test does not support a non-default library context");
  3028. switch (idx) {
  3029. case 0:
  3030. type = EVP_aes_128_cbc();
  3031. /* FALLTHROUGH */
  3032. case 6:
  3033. type = (type != NULL) ? type :
  3034. EVP_CIPHER_fetch(testctx, "aes-128-cbc", testpropq);
  3035. ref_iv = cbc_state;
  3036. ref_len = sizeof(cbc_state);
  3037. iv_reset = 1;
  3038. break;
  3039. case 1:
  3040. type = EVP_aes_128_ofb();
  3041. /* FALLTHROUGH */
  3042. case 7:
  3043. type = (type != NULL) ? type :
  3044. EVP_CIPHER_fetch(testctx, "aes-128-ofb", testpropq);
  3045. ref_iv = ofb_state;
  3046. ref_len = sizeof(ofb_state);
  3047. iv_reset = 1;
  3048. break;
  3049. case 2:
  3050. type = EVP_aes_128_cfb();
  3051. /* FALLTHROUGH */
  3052. case 8:
  3053. type = (type != NULL) ? type :
  3054. EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq);
  3055. ref_iv = cfb_state;
  3056. ref_len = sizeof(cfb_state);
  3057. iv_reset = 1;
  3058. break;
  3059. case 3:
  3060. type = EVP_aes_128_gcm();
  3061. /* FALLTHROUGH */
  3062. case 9:
  3063. type = (type != NULL) ? type :
  3064. EVP_CIPHER_fetch(testctx, "aes-128-gcm", testpropq);
  3065. ref_iv = gcm_state;
  3066. ref_len = sizeof(gcm_state);
  3067. break;
  3068. case 4:
  3069. type = EVP_aes_128_ccm();
  3070. /* FALLTHROUGH */
  3071. case 10:
  3072. type = (type != NULL) ? type :
  3073. EVP_CIPHER_fetch(testctx, "aes-128-ccm", testpropq);
  3074. ref_iv = ccm_state;
  3075. ref_len = sizeof(ccm_state);
  3076. break;
  3077. #ifdef OPENSSL_NO_OCB
  3078. case 5:
  3079. case 11:
  3080. return 1;
  3081. #else
  3082. case 5:
  3083. type = EVP_aes_128_ocb();
  3084. /* FALLTHROUGH */
  3085. case 11:
  3086. type = (type != NULL) ? type :
  3087. EVP_CIPHER_fetch(testctx, "aes-128-ocb", testpropq);
  3088. ref_iv = ocb_state;
  3089. ref_len = sizeof(ocb_state);
  3090. break;
  3091. #endif
  3092. default:
  3093. return 0;
  3094. }
  3095. if (!TEST_ptr(type)
  3096. || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
  3097. || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
  3098. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
  3099. (int)sizeof(msg)))
  3100. || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
  3101. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
  3102. || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
  3103. goto err;
  3104. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  3105. if (!TEST_int_gt(ivlen, 0))
  3106. goto err;
  3107. if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
  3108. || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
  3109. goto err;
  3110. /* CBC, OFB, and CFB modes: the updated iv must be reset after reinit */
  3111. if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
  3112. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
  3113. goto err;
  3114. if (iv_reset) {
  3115. if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
  3116. goto err;
  3117. } else {
  3118. if (!TEST_mem_eq(ref_iv, ivlen, iv, ivlen))
  3119. goto err;
  3120. }
  3121. ret = 1;
  3122. err:
  3123. EVP_CIPHER_CTX_free(ctx);
  3124. if (idx >= 6)
  3125. EVP_CIPHER_free((EVP_CIPHER *)type);
  3126. return ret;
  3127. }
  3128. #ifndef OPENSSL_NO_DES
  3129. static int test_evp_iv_des(int idx)
  3130. {
  3131. int ret = 0;
  3132. EVP_CIPHER_CTX *ctx = NULL;
  3133. static const unsigned char key[24] = {
  3134. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  3135. 0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86,
  3136. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  3137. };
  3138. static const unsigned char init_iv[8] = {
  3139. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  3140. };
  3141. static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
  3142. 9, 10, 11, 12, 13, 14, 15, 16 };
  3143. unsigned char ciphertext[32], oiv[8], iv[8];
  3144. unsigned const char *ref_iv;
  3145. static const unsigned char cbc_state_des[8] = {
  3146. 0x4f, 0xa3, 0x85, 0xcd, 0x8b, 0xf3, 0x06, 0x2a
  3147. };
  3148. static const unsigned char cbc_state_3des[8] = {
  3149. 0x35, 0x27, 0x7d, 0x65, 0x6c, 0xfb, 0x50, 0xd9
  3150. };
  3151. static const unsigned char ofb_state_des[8] = {
  3152. 0xa7, 0x0d, 0x1d, 0x45, 0xf9, 0x96, 0x3f, 0x2c
  3153. };
  3154. static const unsigned char ofb_state_3des[8] = {
  3155. 0xab, 0x16, 0x24, 0xbb, 0x5b, 0xac, 0xed, 0x5e
  3156. };
  3157. static const unsigned char cfb_state_des[8] = {
  3158. 0x91, 0xeb, 0x6d, 0x29, 0x4b, 0x08, 0xbd, 0x73
  3159. };
  3160. static const unsigned char cfb_state_3des[8] = {
  3161. 0x34, 0xdd, 0xfb, 0x47, 0x33, 0x1c, 0x61, 0xf7
  3162. };
  3163. int len = sizeof(ciphertext);
  3164. size_t ivlen, ref_len;
  3165. EVP_CIPHER *type = NULL;
  3166. if (lgcyprov == NULL && idx < 3)
  3167. return TEST_skip("Test requires legacy provider to be loaded");
  3168. switch (idx) {
  3169. case 0:
  3170. type = EVP_CIPHER_fetch(testctx, "des-cbc", testpropq);
  3171. ref_iv = cbc_state_des;
  3172. ref_len = sizeof(cbc_state_des);
  3173. break;
  3174. case 1:
  3175. type = EVP_CIPHER_fetch(testctx, "des-ofb", testpropq);
  3176. ref_iv = ofb_state_des;
  3177. ref_len = sizeof(ofb_state_des);
  3178. break;
  3179. case 2:
  3180. type = EVP_CIPHER_fetch(testctx, "des-cfb", testpropq);
  3181. ref_iv = cfb_state_des;
  3182. ref_len = sizeof(cfb_state_des);
  3183. break;
  3184. case 3:
  3185. type = EVP_CIPHER_fetch(testctx, "des-ede3-cbc", testpropq);
  3186. ref_iv = cbc_state_3des;
  3187. ref_len = sizeof(cbc_state_3des);
  3188. break;
  3189. case 4:
  3190. type = EVP_CIPHER_fetch(testctx, "des-ede3-ofb", testpropq);
  3191. ref_iv = ofb_state_3des;
  3192. ref_len = sizeof(ofb_state_3des);
  3193. break;
  3194. case 5:
  3195. type = EVP_CIPHER_fetch(testctx, "des-ede3-cfb", testpropq);
  3196. ref_iv = cfb_state_3des;
  3197. ref_len = sizeof(cfb_state_3des);
  3198. break;
  3199. default:
  3200. return 0;
  3201. }
  3202. if (!TEST_ptr(type)
  3203. || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
  3204. || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
  3205. || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
  3206. (int)sizeof(msg)))
  3207. || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
  3208. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
  3209. || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
  3210. goto err;
  3211. ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
  3212. if (!TEST_int_gt(ivlen, 0))
  3213. goto err;
  3214. if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
  3215. || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
  3216. goto err;
  3217. if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
  3218. || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
  3219. goto err;
  3220. if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
  3221. goto err;
  3222. ret = 1;
  3223. err:
  3224. EVP_CIPHER_CTX_free(ctx);
  3225. EVP_CIPHER_free(type);
  3226. return ret;
  3227. }
  3228. #endif
  3229. #ifndef OPENSSL_NO_BF
  3230. static int test_evp_bf_default_keylen(int idx)
  3231. {
  3232. int ret = 0;
  3233. static const char *algos[4] = {
  3234. "bf-ecb", "bf-cbc", "bf-cfb", "bf-ofb"
  3235. };
  3236. int ivlen[4] = { 0, 8, 8, 8 };
  3237. EVP_CIPHER *cipher = NULL;
  3238. if (lgcyprov == NULL)
  3239. return TEST_skip("Test requires legacy provider to be loaded");
  3240. if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, algos[idx], testpropq))
  3241. || !TEST_int_eq(EVP_CIPHER_get_key_length(cipher), 16)
  3242. || !TEST_int_eq(EVP_CIPHER_get_iv_length(cipher), ivlen[idx]))
  3243. goto err;
  3244. ret = 1;
  3245. err:
  3246. EVP_CIPHER_free(cipher);
  3247. return ret;
  3248. }
  3249. #endif
  3250. #ifndef OPENSSL_NO_EC
  3251. static int ecpub_nids[] = {
  3252. NID_brainpoolP256r1, NID_X9_62_prime256v1,
  3253. NID_secp384r1, NID_secp521r1,
  3254. # ifndef OPENSSL_NO_EC2M
  3255. NID_sect233k1, NID_sect233r1, NID_sect283r1,
  3256. NID_sect409k1, NID_sect409r1, NID_sect571k1, NID_sect571r1,
  3257. # endif
  3258. NID_brainpoolP384r1, NID_brainpoolP512r1
  3259. };
  3260. static int test_ecpub(int idx)
  3261. {
  3262. int ret = 0, len, savelen;
  3263. int nid;
  3264. unsigned char buf[1024];
  3265. unsigned char *p;
  3266. EVP_PKEY *pkey = NULL;
  3267. EVP_PKEY_CTX *ctx = NULL;
  3268. # ifndef OPENSSL_NO_DEPRECATED_3_0
  3269. const unsigned char *q;
  3270. EVP_PKEY *pkey2 = NULL;
  3271. EC_KEY *ec = NULL;
  3272. # endif
  3273. if (nullprov != NULL)
  3274. return TEST_skip("Test does not support a non-default library context");
  3275. nid = ecpub_nids[idx];
  3276. ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  3277. if (!TEST_ptr(ctx)
  3278. || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  3279. || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid), 0)
  3280. || !TEST_true(EVP_PKEY_keygen(ctx, &pkey)))
  3281. goto done;
  3282. len = i2d_PublicKey(pkey, NULL);
  3283. savelen = len;
  3284. if (!TEST_int_ge(len, 1)
  3285. || !TEST_int_lt(len, 1024))
  3286. goto done;
  3287. p = buf;
  3288. len = i2d_PublicKey(pkey, &p);
  3289. if (!TEST_int_ge(len, 1)
  3290. || !TEST_int_eq(len, savelen))
  3291. goto done;
  3292. # ifndef OPENSSL_NO_DEPRECATED_3_0
  3293. /* Now try to decode the just-created DER. */
  3294. q = buf;
  3295. if (!TEST_ptr((pkey2 = EVP_PKEY_new()))
  3296. || !TEST_ptr((ec = EC_KEY_new_by_curve_name(nid)))
  3297. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey2, ec)))
  3298. goto done;
  3299. /* EC_KEY ownership transferred */
  3300. ec = NULL;
  3301. if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_EC, &pkey2, &q, savelen)))
  3302. goto done;
  3303. /* The keys should match. */
  3304. if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
  3305. goto done;
  3306. # endif
  3307. ret = 1;
  3308. done:
  3309. EVP_PKEY_CTX_free(ctx);
  3310. EVP_PKEY_free(pkey);
  3311. # ifndef OPENSSL_NO_DEPRECATED_3_0
  3312. EVP_PKEY_free(pkey2);
  3313. EC_KEY_free(ec);
  3314. # endif
  3315. return ret;
  3316. }
  3317. #endif
  3318. static int test_EVP_rsa_pss_with_keygen_bits(void)
  3319. {
  3320. int ret = 0;
  3321. EVP_PKEY_CTX *ctx = NULL;
  3322. EVP_PKEY *pkey = NULL;
  3323. EVP_MD *md;
  3324. md = EVP_MD_fetch(testctx, "sha256", testpropq);
  3325. ret = TEST_ptr(md)
  3326. && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA-PSS", testpropq)))
  3327. && TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
  3328. && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 512), 0)
  3329. && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md), 0)
  3330. && TEST_true(EVP_PKEY_keygen(ctx, &pkey));
  3331. EVP_MD_free(md);
  3332. EVP_PKEY_free(pkey);
  3333. EVP_PKEY_CTX_free(ctx);
  3334. return ret;
  3335. }
  3336. static int test_EVP_rsa_pss_set_saltlen(void)
  3337. {
  3338. int ret = 0;
  3339. EVP_PKEY *pkey = NULL;
  3340. EVP_PKEY_CTX *pkey_ctx = NULL;
  3341. EVP_MD *sha256 = NULL;
  3342. EVP_MD_CTX *sha256_ctx = NULL;
  3343. int saltlen = 9999; /* buggy EVP_PKEY_CTX_get_rsa_pss_saltlen() didn't update this */
  3344. const int test_value = 32;
  3345. ret = TEST_ptr(pkey = load_example_rsa_key())
  3346. && TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", NULL))
  3347. && TEST_ptr(sha256_ctx = EVP_MD_CTX_new())
  3348. && TEST_true(EVP_DigestSignInit(sha256_ctx, &pkey_ctx, sha256, NULL, pkey))
  3349. && TEST_true(EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING))
  3350. && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, test_value), 0)
  3351. && TEST_int_gt(EVP_PKEY_CTX_get_rsa_pss_saltlen(pkey_ctx, &saltlen), 0)
  3352. && TEST_int_eq(saltlen, test_value);
  3353. EVP_MD_CTX_free(sha256_ctx);
  3354. EVP_PKEY_free(pkey);
  3355. EVP_MD_free(sha256);
  3356. return ret;
  3357. }
  3358. static int success = 1;
  3359. static void md_names(const char *name, void *vctx)
  3360. {
  3361. OSSL_LIB_CTX *ctx = (OSSL_LIB_CTX *)vctx;
  3362. /* Force a namemap update */
  3363. EVP_CIPHER *aes128 = EVP_CIPHER_fetch(ctx, "AES-128-CBC", NULL);
  3364. if (!TEST_ptr(aes128))
  3365. success = 0;
  3366. EVP_CIPHER_free(aes128);
  3367. }
  3368. /*
  3369. * Test that changing the namemap in a user callback works in a names_do_all
  3370. * function.
  3371. */
  3372. static int test_names_do_all(void)
  3373. {
  3374. /* We use a custom libctx so that we know the state of the namemap */
  3375. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  3376. EVP_MD *sha256 = NULL;
  3377. int testresult = 0;
  3378. if (!TEST_ptr(ctx))
  3379. goto err;
  3380. sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
  3381. if (!TEST_ptr(sha256))
  3382. goto err;
  3383. /*
  3384. * We loop through all the names for a given digest. This should still work
  3385. * even if the namemap changes part way through.
  3386. */
  3387. if (!TEST_true(EVP_MD_names_do_all(sha256, md_names, ctx)))
  3388. goto err;
  3389. if (!TEST_true(success))
  3390. goto err;
  3391. testresult = 1;
  3392. err:
  3393. EVP_MD_free(sha256);
  3394. OSSL_LIB_CTX_free(ctx);
  3395. return testresult;
  3396. }
  3397. typedef struct {
  3398. const char *cipher;
  3399. const unsigned char *key;
  3400. const unsigned char *iv;
  3401. const unsigned char *input;
  3402. const unsigned char *expected;
  3403. const unsigned char *tag;
  3404. size_t ivlen; /* 0 if we do not need to set a specific IV len */
  3405. size_t inlen;
  3406. size_t expectedlen;
  3407. size_t taglen;
  3408. int keyfirst;
  3409. int initenc;
  3410. int finalenc;
  3411. } EVP_INIT_TEST_st;
  3412. static const EVP_INIT_TEST_st evp_init_tests[] = {
  3413. {
  3414. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
  3415. cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
  3416. 0, 1, 0, 1
  3417. },
  3418. {
  3419. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
  3420. gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  3421. sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
  3422. sizeof(gcmDefaultTag), 1, 0, 1
  3423. },
  3424. {
  3425. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
  3426. cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
  3427. 0, 0, 0, 1
  3428. },
  3429. {
  3430. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
  3431. gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  3432. sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
  3433. sizeof(gcmDefaultTag), 0, 0, 1
  3434. },
  3435. {
  3436. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
  3437. cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
  3438. 0, 1, 1, 0
  3439. },
  3440. {
  3441. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
  3442. gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  3443. sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
  3444. sizeof(gcmDefaultTag), 1, 1, 0
  3445. },
  3446. {
  3447. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
  3448. cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
  3449. 0, 0, 1, 0
  3450. },
  3451. {
  3452. "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
  3453. gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
  3454. sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
  3455. sizeof(gcmDefaultTag), 0, 1, 0
  3456. }
  3457. };
  3458. /* use same key, iv and plaintext for cfb and ofb */
  3459. static const EVP_INIT_TEST_st evp_reinit_tests[] = {
  3460. {
  3461. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext_partial,
  3462. cfbCiphertext_partial, NULL, 0, sizeof(cfbPlaintext_partial),
  3463. sizeof(cfbCiphertext_partial), 0, 0, 1, 0
  3464. },
  3465. {
  3466. "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext_partial,
  3467. cfbPlaintext_partial, NULL, 0, sizeof(cfbCiphertext_partial),
  3468. sizeof(cfbPlaintext_partial), 0, 0, 0, 0
  3469. },
  3470. {
  3471. "aes-128-ofb", kCFBDefaultKey, iCFBIV, cfbPlaintext_partial,
  3472. ofbCiphertext_partial, NULL, 0, sizeof(cfbPlaintext_partial),
  3473. sizeof(ofbCiphertext_partial), 0, 0, 1, 0
  3474. },
  3475. {
  3476. "aes-128-ofb", kCFBDefaultKey, iCFBIV, ofbCiphertext_partial,
  3477. cfbPlaintext_partial, NULL, 0, sizeof(ofbCiphertext_partial),
  3478. sizeof(cfbPlaintext_partial), 0, 0, 0, 0
  3479. },
  3480. };
  3481. static int evp_init_seq_set_iv(EVP_CIPHER_CTX *ctx, const EVP_INIT_TEST_st *t)
  3482. {
  3483. int res = 0;
  3484. if (t->ivlen != 0) {
  3485. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen, NULL), 0))
  3486. goto err;
  3487. }
  3488. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv, -1)))
  3489. goto err;
  3490. res = 1;
  3491. err:
  3492. return res;
  3493. }
  3494. /*
  3495. * Test step-wise cipher initialization via EVP_CipherInit_ex where the
  3496. * arguments are given one at a time and a final adjustment to the enc
  3497. * parameter sets the correct operation.
  3498. */
  3499. static int test_evp_init_seq(int idx)
  3500. {
  3501. int outlen1, outlen2;
  3502. int testresult = 0;
  3503. unsigned char outbuf[1024];
  3504. unsigned char tag[16];
  3505. const EVP_INIT_TEST_st *t = &evp_init_tests[idx];
  3506. EVP_CIPHER_CTX *ctx = NULL;
  3507. EVP_CIPHER *type = NULL;
  3508. size_t taglen = sizeof(tag);
  3509. char *errmsg = NULL;
  3510. ctx = EVP_CIPHER_CTX_new();
  3511. if (ctx == NULL) {
  3512. errmsg = "CTX_ALLOC";
  3513. goto err;
  3514. }
  3515. if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq))) {
  3516. errmsg = "CIPHER_FETCH";
  3517. goto err;
  3518. }
  3519. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, t->initenc))) {
  3520. errmsg = "EMPTY_ENC_INIT";
  3521. goto err;
  3522. }
  3523. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  3524. errmsg = "PADDING";
  3525. goto err;
  3526. }
  3527. if (t->keyfirst && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
  3528. errmsg = "KEY_INIT (before iv)";
  3529. goto err;
  3530. }
  3531. if (!evp_init_seq_set_iv(ctx, t)) {
  3532. errmsg = "IV_INIT";
  3533. goto err;
  3534. }
  3535. if (t->keyfirst == 0 && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
  3536. errmsg = "KEY_INIT (after iv)";
  3537. goto err;
  3538. }
  3539. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, t->finalenc))) {
  3540. errmsg = "FINAL_ENC_INIT";
  3541. goto err;
  3542. }
  3543. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  3544. errmsg = "CIPHER_UPDATE";
  3545. goto err;
  3546. }
  3547. if (t->finalenc == 0 && t->tag != NULL) {
  3548. /* Set expected tag */
  3549. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  3550. t->taglen, (void *)t->tag), 0)) {
  3551. errmsg = "SET_TAG";
  3552. goto err;
  3553. }
  3554. }
  3555. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3556. errmsg = "CIPHER_FINAL";
  3557. goto err;
  3558. }
  3559. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  3560. errmsg = "WRONG_RESULT";
  3561. goto err;
  3562. }
  3563. if (t->finalenc != 0 && t->tag != NULL) {
  3564. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
  3565. errmsg = "GET_TAG";
  3566. goto err;
  3567. }
  3568. if (!TEST_mem_eq(t->tag, t->taglen, tag, taglen)) {
  3569. errmsg = "TAG_ERROR";
  3570. goto err;
  3571. }
  3572. }
  3573. testresult = 1;
  3574. err:
  3575. if (errmsg != NULL)
  3576. TEST_info("evp_init_test %d: %s", idx, errmsg);
  3577. EVP_CIPHER_CTX_free(ctx);
  3578. EVP_CIPHER_free(type);
  3579. return testresult;
  3580. }
  3581. /*
  3582. * Test re-initialization of cipher context without changing key or iv.
  3583. * The result of both iteration should be the same.
  3584. */
  3585. static int test_evp_reinit_seq(int idx)
  3586. {
  3587. int outlen1, outlen2, outlen_final;
  3588. int testresult = 0;
  3589. unsigned char outbuf1[1024];
  3590. unsigned char outbuf2[1024];
  3591. const EVP_INIT_TEST_st *t = &evp_reinit_tests[idx];
  3592. EVP_CIPHER_CTX *ctx = NULL;
  3593. EVP_CIPHER *type = NULL;
  3594. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  3595. || !TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq))
  3596. /* setup cipher context */
  3597. || !TEST_true(EVP_CipherInit_ex2(ctx, type, t->key, t->iv, t->initenc, NULL))
  3598. /* first iteration */
  3599. || !TEST_true(EVP_CipherUpdate(ctx, outbuf1, &outlen1, t->input, t->inlen))
  3600. || !TEST_true(EVP_CipherFinal_ex(ctx, outbuf1, &outlen_final))
  3601. /* check test results iteration 1 */
  3602. || !TEST_mem_eq(t->expected, t->expectedlen, outbuf1, outlen1 + outlen_final)
  3603. /* now re-init the context (same cipher, key and iv) */
  3604. || !TEST_true(EVP_CipherInit_ex2(ctx, NULL, NULL, NULL, -1, NULL))
  3605. /* second iteration */
  3606. || !TEST_true(EVP_CipherUpdate(ctx, outbuf2, &outlen2, t->input, t->inlen))
  3607. || !TEST_true(EVP_CipherFinal_ex(ctx, outbuf2, &outlen_final))
  3608. /* check test results iteration 2 */
  3609. || !TEST_mem_eq(t->expected, t->expectedlen, outbuf2, outlen2 + outlen_final))
  3610. goto err;
  3611. testresult = 1;
  3612. err:
  3613. EVP_CIPHER_CTX_free(ctx);
  3614. EVP_CIPHER_free(type);
  3615. return testresult;
  3616. }
  3617. typedef struct {
  3618. const unsigned char *input;
  3619. const unsigned char *expected;
  3620. size_t inlen;
  3621. size_t expectedlen;
  3622. int enc;
  3623. } EVP_RESET_TEST_st;
  3624. static const EVP_RESET_TEST_st evp_reset_tests[] = {
  3625. {
  3626. cfbPlaintext, cfbCiphertext,
  3627. sizeof(cfbPlaintext), sizeof(cfbCiphertext), 1
  3628. },
  3629. {
  3630. cfbCiphertext, cfbPlaintext,
  3631. sizeof(cfbCiphertext), sizeof(cfbPlaintext), 0
  3632. }
  3633. };
  3634. /*
  3635. * Test a reset of a cipher via EVP_CipherInit_ex after the cipher has already
  3636. * been used.
  3637. */
  3638. static int test_evp_reset(int idx)
  3639. {
  3640. const EVP_RESET_TEST_st *t = &evp_reset_tests[idx];
  3641. int outlen1, outlen2;
  3642. int testresult = 0;
  3643. unsigned char outbuf[1024];
  3644. EVP_CIPHER_CTX *ctx = NULL;
  3645. EVP_CIPHER *type = NULL;
  3646. char *errmsg = NULL;
  3647. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  3648. errmsg = "CTX_ALLOC";
  3649. goto err;
  3650. }
  3651. if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq))) {
  3652. errmsg = "CIPHER_FETCH";
  3653. goto err;
  3654. }
  3655. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
  3656. errmsg = "CIPHER_INIT";
  3657. goto err;
  3658. }
  3659. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  3660. errmsg = "PADDING";
  3661. goto err;
  3662. }
  3663. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  3664. errmsg = "CIPHER_UPDATE";
  3665. goto err;
  3666. }
  3667. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3668. errmsg = "CIPHER_FINAL";
  3669. goto err;
  3670. }
  3671. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  3672. errmsg = "WRONG_RESULT";
  3673. goto err;
  3674. }
  3675. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1))) {
  3676. errmsg = "CIPHER_REINIT";
  3677. goto err;
  3678. }
  3679. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
  3680. errmsg = "CIPHER_UPDATE (reinit)";
  3681. goto err;
  3682. }
  3683. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3684. errmsg = "CIPHER_FINAL (reinit)";
  3685. goto err;
  3686. }
  3687. if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
  3688. errmsg = "WRONG_RESULT (reinit)";
  3689. goto err;
  3690. }
  3691. testresult = 1;
  3692. err:
  3693. if (errmsg != NULL)
  3694. TEST_info("test_evp_reset %d: %s", idx, errmsg);
  3695. EVP_CIPHER_CTX_free(ctx);
  3696. EVP_CIPHER_free(type);
  3697. return testresult;
  3698. }
  3699. typedef struct {
  3700. const char *cipher;
  3701. int enc;
  3702. } EVP_UPDATED_IV_TEST_st;
  3703. static const EVP_UPDATED_IV_TEST_st evp_updated_iv_tests[] = {
  3704. {
  3705. "aes-128-cfb", 1
  3706. },
  3707. {
  3708. "aes-128-cfb", 0
  3709. },
  3710. {
  3711. "aes-128-cfb1", 1
  3712. },
  3713. {
  3714. "aes-128-cfb1", 0
  3715. },
  3716. {
  3717. "aes-128-cfb8", 1
  3718. },
  3719. {
  3720. "aes-128-cfb8", 0
  3721. },
  3722. {
  3723. "aes-128-ofb", 1
  3724. },
  3725. {
  3726. "aes-128-ofb", 0
  3727. },
  3728. {
  3729. "aes-128-ctr", 1
  3730. },
  3731. {
  3732. "aes-128-ctr", 0
  3733. },
  3734. {
  3735. "aes-128-cbc", 1
  3736. },
  3737. {
  3738. "aes-128-cbc", 0
  3739. }
  3740. };
  3741. /*
  3742. * Test that the IV in the context is updated during a crypto operation for CFB
  3743. * and OFB.
  3744. */
  3745. static int test_evp_updated_iv(int idx)
  3746. {
  3747. const EVP_UPDATED_IV_TEST_st *t = &evp_updated_iv_tests[idx];
  3748. int outlen1, outlen2;
  3749. int testresult = 0;
  3750. unsigned char outbuf[1024];
  3751. EVP_CIPHER_CTX *ctx = NULL;
  3752. EVP_CIPHER *type = NULL;
  3753. unsigned char updated_iv[EVP_MAX_IV_LENGTH];
  3754. int iv_len;
  3755. char *errmsg = NULL;
  3756. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  3757. errmsg = "CTX_ALLOC";
  3758. goto err;
  3759. }
  3760. if ((type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq)) == NULL) {
  3761. TEST_info("cipher %s not supported, skipping", t->cipher);
  3762. goto ok;
  3763. }
  3764. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
  3765. errmsg = "CIPHER_INIT";
  3766. goto err;
  3767. }
  3768. if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
  3769. errmsg = "PADDING";
  3770. goto err;
  3771. }
  3772. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, cfbPlaintext, sizeof(cfbPlaintext)))) {
  3773. errmsg = "CIPHER_UPDATE";
  3774. goto err;
  3775. }
  3776. if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, updated_iv, sizeof(updated_iv)))) {
  3777. errmsg = "CIPHER_CTX_GET_UPDATED_IV";
  3778. goto err;
  3779. }
  3780. iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
  3781. if (!TEST_int_ge(iv_len,0)) {
  3782. errmsg = "CIPHER_CTX_GET_IV_LEN";
  3783. goto err;
  3784. }
  3785. if (!TEST_mem_ne(iCFBIV, sizeof(iCFBIV), updated_iv, iv_len)) {
  3786. errmsg = "IV_NOT_UPDATED";
  3787. goto err;
  3788. }
  3789. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3790. errmsg = "CIPHER_FINAL";
  3791. goto err;
  3792. }
  3793. ok:
  3794. testresult = 1;
  3795. err:
  3796. if (errmsg != NULL)
  3797. TEST_info("test_evp_updated_iv %d: %s", idx, errmsg);
  3798. EVP_CIPHER_CTX_free(ctx);
  3799. EVP_CIPHER_free(type);
  3800. return testresult;
  3801. }
  3802. typedef struct {
  3803. const unsigned char *iv1;
  3804. const unsigned char *iv2;
  3805. const unsigned char *expected1;
  3806. const unsigned char *expected2;
  3807. const unsigned char *tag1;
  3808. const unsigned char *tag2;
  3809. size_t ivlen1;
  3810. size_t ivlen2;
  3811. size_t expectedlen1;
  3812. size_t expectedlen2;
  3813. } TEST_GCM_IV_REINIT_st;
  3814. static const TEST_GCM_IV_REINIT_st gcm_reinit_tests[] = {
  3815. {
  3816. iGCMResetIV1, iGCMResetIV2, gcmResetCiphertext1, gcmResetCiphertext2,
  3817. gcmResetTag1, gcmResetTag2, sizeof(iGCMResetIV1), sizeof(iGCMResetIV2),
  3818. sizeof(gcmResetCiphertext1), sizeof(gcmResetCiphertext2)
  3819. },
  3820. {
  3821. iGCMResetIV2, iGCMResetIV1, gcmResetCiphertext2, gcmResetCiphertext1,
  3822. gcmResetTag2, gcmResetTag1, sizeof(iGCMResetIV2), sizeof(iGCMResetIV1),
  3823. sizeof(gcmResetCiphertext2), sizeof(gcmResetCiphertext1)
  3824. }
  3825. };
  3826. static int test_gcm_reinit(int idx)
  3827. {
  3828. int outlen1, outlen2, outlen3;
  3829. int testresult = 0;
  3830. unsigned char outbuf[1024];
  3831. unsigned char tag[16];
  3832. const TEST_GCM_IV_REINIT_st *t = &gcm_reinit_tests[idx];
  3833. EVP_CIPHER_CTX *ctx = NULL;
  3834. EVP_CIPHER *type = NULL;
  3835. size_t taglen = sizeof(tag);
  3836. char *errmsg = NULL;
  3837. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
  3838. errmsg = "CTX_ALLOC";
  3839. goto err;
  3840. }
  3841. if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-256-gcm", testpropq))) {
  3842. errmsg = "CIPHER_FETCH";
  3843. goto err;
  3844. }
  3845. if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, 1))) {
  3846. errmsg = "ENC_INIT";
  3847. goto err;
  3848. }
  3849. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen1, NULL), 0)) {
  3850. errmsg = "SET_IVLEN1";
  3851. goto err;
  3852. }
  3853. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, kGCMResetKey, t->iv1, 1))) {
  3854. errmsg = "SET_IV1";
  3855. goto err;
  3856. }
  3857. if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
  3858. errmsg = "AAD1";
  3859. goto err;
  3860. }
  3861. EVP_CIPHER_CTX_set_padding(ctx, 0);
  3862. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
  3863. sizeof(gcmResetPlaintext)))) {
  3864. errmsg = "CIPHER_UPDATE1";
  3865. goto err;
  3866. }
  3867. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3868. errmsg = "CIPHER_FINAL1";
  3869. goto err;
  3870. }
  3871. if (!TEST_mem_eq(t->expected1, t->expectedlen1, outbuf, outlen1 + outlen2)) {
  3872. errmsg = "WRONG_RESULT1";
  3873. goto err;
  3874. }
  3875. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
  3876. errmsg = "GET_TAG1";
  3877. goto err;
  3878. }
  3879. if (!TEST_mem_eq(t->tag1, taglen, tag, taglen)) {
  3880. errmsg = "TAG_ERROR1";
  3881. goto err;
  3882. }
  3883. /* Now reinit */
  3884. if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen2, NULL), 0)) {
  3885. errmsg = "SET_IVLEN2";
  3886. goto err;
  3887. }
  3888. if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv2, -1))) {
  3889. errmsg = "SET_IV2";
  3890. goto err;
  3891. }
  3892. if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
  3893. errmsg = "AAD2";
  3894. goto err;
  3895. }
  3896. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
  3897. sizeof(gcmResetPlaintext)))) {
  3898. errmsg = "CIPHER_UPDATE2";
  3899. goto err;
  3900. }
  3901. if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
  3902. errmsg = "CIPHER_FINAL2";
  3903. goto err;
  3904. }
  3905. if (!TEST_mem_eq(t->expected2, t->expectedlen2, outbuf, outlen1 + outlen2)) {
  3906. errmsg = "WRONG_RESULT2";
  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_TAG2";
  3911. goto err;
  3912. }
  3913. if (!TEST_mem_eq(t->tag2, taglen, tag, taglen)) {
  3914. errmsg = "TAG_ERROR2";
  3915. goto err;
  3916. }
  3917. testresult = 1;
  3918. err:
  3919. if (errmsg != NULL)
  3920. TEST_info("evp_init_test %d: %s", idx, errmsg);
  3921. EVP_CIPHER_CTX_free(ctx);
  3922. EVP_CIPHER_free(type);
  3923. return testresult;
  3924. }
  3925. static const char *ivlen_change_ciphers[] = {
  3926. "AES-256-GCM",
  3927. #ifndef OPENSSL_NO_OCB
  3928. "AES-256-OCB",
  3929. #endif
  3930. "AES-256-CCM"
  3931. };
  3932. /* Negative test for ivlen change after iv being set */
  3933. static int test_ivlen_change(int idx)
  3934. {
  3935. int outlen;
  3936. int res = 0;
  3937. unsigned char outbuf[1024];
  3938. static const unsigned char iv[] = {
  3939. 0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  3940. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34
  3941. };
  3942. EVP_CIPHER_CTX *ctx = NULL;
  3943. EVP_CIPHER *ciph = NULL;
  3944. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  3945. size_t ivlen = 13; /* non-default IV length */
  3946. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  3947. goto err;
  3948. if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, ivlen_change_ciphers[idx],
  3949. testpropq)))
  3950. goto err;
  3951. if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, kGCMDefaultKey, iv, 1)))
  3952. goto err;
  3953. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
  3954. sizeof(gcmDefaultPlaintext))))
  3955. goto err;
  3956. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  3957. &ivlen);
  3958. if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params)))
  3959. goto err;
  3960. ERR_set_mark();
  3961. if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
  3962. sizeof(gcmDefaultPlaintext)))) {
  3963. ERR_clear_last_mark();
  3964. goto err;
  3965. }
  3966. ERR_pop_to_mark();
  3967. res = 1;
  3968. err:
  3969. EVP_CIPHER_CTX_free(ctx);
  3970. EVP_CIPHER_free(ciph);
  3971. return res;
  3972. }
  3973. static const char *keylen_change_ciphers[] = {
  3974. #ifndef OPENSSL_NO_BF
  3975. "BF-ECB",
  3976. #endif
  3977. #ifndef OPENSSL_NO_CAST
  3978. "CAST5-ECB",
  3979. #endif
  3980. #ifndef OPENSSL_NO_RC2
  3981. "RC2-ECB",
  3982. #endif
  3983. #ifndef OPENSSL_NO_RC4
  3984. "RC4",
  3985. #endif
  3986. #ifndef OPENSSL_NO_RC5
  3987. "RC5-ECB",
  3988. #endif
  3989. NULL
  3990. };
  3991. /* Negative test for keylen change after key was set */
  3992. static int test_keylen_change(int idx)
  3993. {
  3994. int outlen;
  3995. int res = 0;
  3996. unsigned char outbuf[1024];
  3997. static const unsigned char key[] = {
  3998. 0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
  3999. 0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34
  4000. };
  4001. EVP_CIPHER_CTX *ctx = NULL;
  4002. EVP_CIPHER *ciph = NULL;
  4003. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  4004. size_t keylen = 12; /* non-default key length */
  4005. if (lgcyprov == NULL)
  4006. return TEST_skip("Test requires legacy provider to be loaded");
  4007. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
  4008. goto err;
  4009. if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, keylen_change_ciphers[idx],
  4010. testpropq)))
  4011. goto err;
  4012. if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, key, NULL, 1)))
  4013. goto err;
  4014. if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
  4015. sizeof(gcmDefaultPlaintext))))
  4016. goto err;
  4017. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
  4018. &keylen);
  4019. if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params)))
  4020. goto err;
  4021. ERR_set_mark();
  4022. if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
  4023. sizeof(gcmDefaultPlaintext)))) {
  4024. ERR_clear_last_mark();
  4025. goto err;
  4026. }
  4027. ERR_pop_to_mark();
  4028. res = 1;
  4029. err:
  4030. EVP_CIPHER_CTX_free(ctx);
  4031. EVP_CIPHER_free(ciph);
  4032. return res;
  4033. }
  4034. #ifndef OPENSSL_NO_DEPRECATED_3_0
  4035. static EVP_PKEY_METHOD *custom_pmeth = NULL;
  4036. static const EVP_PKEY_METHOD *orig_pmeth = NULL;
  4037. # define EVP_PKEY_CTRL_MY_COMMAND 9999
  4038. static int custom_pmeth_init(EVP_PKEY_CTX *ctx)
  4039. {
  4040. int (*pinit)(EVP_PKEY_CTX *ctx);
  4041. EVP_PKEY_meth_get_init(orig_pmeth, &pinit);
  4042. return pinit(ctx);
  4043. }
  4044. static void custom_pmeth_cleanup(EVP_PKEY_CTX *ctx)
  4045. {
  4046. void (*pcleanup)(EVP_PKEY_CTX *ctx);
  4047. EVP_PKEY_meth_get_cleanup(orig_pmeth, &pcleanup);
  4048. pcleanup(ctx);
  4049. }
  4050. static int custom_pmeth_sign(EVP_PKEY_CTX *ctx, unsigned char *out,
  4051. size_t *outlen, const unsigned char *in,
  4052. size_t inlen)
  4053. {
  4054. int (*psign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  4055. const unsigned char *tbs, size_t tbslen);
  4056. EVP_PKEY_meth_get_sign(orig_pmeth, NULL, &psign);
  4057. return psign(ctx, out, outlen, in, inlen);
  4058. }
  4059. static int custom_pmeth_digestsign(EVP_MD_CTX *ctx, unsigned char *sig,
  4060. size_t *siglen, const unsigned char *tbs,
  4061. size_t tbslen)
  4062. {
  4063. int (*pdigestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  4064. const unsigned char *tbs, size_t tbslen);
  4065. EVP_PKEY_meth_get_digestsign(orig_pmeth, &pdigestsign);
  4066. return pdigestsign(ctx, sig, siglen, tbs, tbslen);
  4067. }
  4068. static int custom_pmeth_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  4069. size_t *keylen)
  4070. {
  4071. int (*pderive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
  4072. EVP_PKEY_meth_get_derive(orig_pmeth, NULL, &pderive);
  4073. return pderive(ctx, key, keylen);
  4074. }
  4075. static int custom_pmeth_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
  4076. {
  4077. int (*pcopy)(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
  4078. EVP_PKEY_meth_get_copy(orig_pmeth, &pcopy);
  4079. return pcopy(dst, src);
  4080. }
  4081. static int ctrl_called;
  4082. static int custom_pmeth_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  4083. {
  4084. int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
  4085. EVP_PKEY_meth_get_ctrl(orig_pmeth, &pctrl, NULL);
  4086. if (type == EVP_PKEY_CTRL_MY_COMMAND) {
  4087. ctrl_called = 1;
  4088. return 1;
  4089. }
  4090. return pctrl(ctx, type, p1, p2);
  4091. }
  4092. static int test_custom_pmeth(int idx)
  4093. {
  4094. EVP_PKEY_CTX *pctx = NULL;
  4095. EVP_MD_CTX *ctx = NULL;
  4096. EVP_PKEY *pkey = NULL;
  4097. int id, orig_id, orig_flags;
  4098. int testresult = 0;
  4099. size_t reslen;
  4100. unsigned char *res = NULL;
  4101. unsigned char msg[] = { 'H', 'e', 'l', 'l', 'o' };
  4102. const EVP_MD *md = EVP_sha256();
  4103. int doderive = 0;
  4104. ctrl_called = 0;
  4105. /* We call deprecated APIs so this test doesn't support a custom libctx */
  4106. if (testctx != NULL)
  4107. return 1;
  4108. switch (idx) {
  4109. case 0:
  4110. case 6:
  4111. id = EVP_PKEY_RSA;
  4112. pkey = load_example_rsa_key();
  4113. break;
  4114. case 1:
  4115. case 7:
  4116. # ifndef OPENSSL_NO_DSA
  4117. id = EVP_PKEY_DSA;
  4118. pkey = load_example_dsa_key();
  4119. break;
  4120. # else
  4121. return 1;
  4122. # endif
  4123. case 2:
  4124. case 8:
  4125. # ifndef OPENSSL_NO_EC
  4126. id = EVP_PKEY_EC;
  4127. pkey = load_example_ec_key();
  4128. break;
  4129. # else
  4130. return 1;
  4131. # endif
  4132. case 3:
  4133. case 9:
  4134. # ifndef OPENSSL_NO_ECX
  4135. id = EVP_PKEY_ED25519;
  4136. md = NULL;
  4137. pkey = load_example_ed25519_key();
  4138. break;
  4139. # else
  4140. return 1;
  4141. # endif
  4142. case 4:
  4143. case 10:
  4144. # ifndef OPENSSL_NO_DH
  4145. id = EVP_PKEY_DH;
  4146. doderive = 1;
  4147. pkey = load_example_dh_key();
  4148. break;
  4149. # else
  4150. return 1;
  4151. # endif
  4152. case 5:
  4153. case 11:
  4154. # ifndef OPENSSL_NO_ECX
  4155. id = EVP_PKEY_X25519;
  4156. doderive = 1;
  4157. pkey = load_example_x25519_key();
  4158. break;
  4159. # else
  4160. return 1;
  4161. # endif
  4162. default:
  4163. TEST_error("Should not happen");
  4164. goto err;
  4165. }
  4166. if (!TEST_ptr(pkey))
  4167. goto err;
  4168. if (idx < 6) {
  4169. if (!TEST_true(evp_pkey_is_provided(pkey)))
  4170. goto err;
  4171. } else {
  4172. EVP_PKEY *tmp = pkey;
  4173. /* Convert to a legacy key */
  4174. pkey = EVP_PKEY_new();
  4175. if (!TEST_ptr(pkey)) {
  4176. pkey = tmp;
  4177. goto err;
  4178. }
  4179. if (!TEST_true(evp_pkey_copy_downgraded(&pkey, tmp))) {
  4180. EVP_PKEY_free(tmp);
  4181. goto err;
  4182. }
  4183. EVP_PKEY_free(tmp);
  4184. if (!TEST_true(evp_pkey_is_legacy(pkey)))
  4185. goto err;
  4186. }
  4187. if (!TEST_ptr(orig_pmeth = EVP_PKEY_meth_find(id))
  4188. || !TEST_ptr(pkey))
  4189. goto err;
  4190. EVP_PKEY_meth_get0_info(&orig_id, &orig_flags, orig_pmeth);
  4191. if (!TEST_int_eq(orig_id, id)
  4192. || !TEST_ptr(custom_pmeth = EVP_PKEY_meth_new(id, orig_flags)))
  4193. goto err;
  4194. if (id == EVP_PKEY_ED25519) {
  4195. EVP_PKEY_meth_set_digestsign(custom_pmeth, custom_pmeth_digestsign);
  4196. } if (id == EVP_PKEY_DH || id == EVP_PKEY_X25519) {
  4197. EVP_PKEY_meth_set_derive(custom_pmeth, NULL, custom_pmeth_derive);
  4198. } else {
  4199. EVP_PKEY_meth_set_sign(custom_pmeth, NULL, custom_pmeth_sign);
  4200. }
  4201. if (id != EVP_PKEY_ED25519 && id != EVP_PKEY_X25519) {
  4202. EVP_PKEY_meth_set_init(custom_pmeth, custom_pmeth_init);
  4203. EVP_PKEY_meth_set_cleanup(custom_pmeth, custom_pmeth_cleanup);
  4204. EVP_PKEY_meth_set_copy(custom_pmeth, custom_pmeth_copy);
  4205. }
  4206. EVP_PKEY_meth_set_ctrl(custom_pmeth, custom_pmeth_ctrl, NULL);
  4207. if (!TEST_true(EVP_PKEY_meth_add0(custom_pmeth)))
  4208. goto err;
  4209. if (doderive) {
  4210. pctx = EVP_PKEY_CTX_new(pkey, NULL);
  4211. if (!TEST_ptr(pctx)
  4212. || !TEST_int_eq(EVP_PKEY_derive_init(pctx), 1)
  4213. || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1,
  4214. EVP_PKEY_CTRL_MY_COMMAND, 0, NULL),
  4215. 1)
  4216. || !TEST_int_eq(ctrl_called, 1)
  4217. || !TEST_int_ge(EVP_PKEY_derive_set_peer(pctx, pkey), 1)
  4218. || !TEST_int_ge(EVP_PKEY_derive(pctx, NULL, &reslen), 1)
  4219. || !TEST_ptr(res = OPENSSL_malloc(reslen))
  4220. || !TEST_int_ge(EVP_PKEY_derive(pctx, res, &reslen), 1))
  4221. goto err;
  4222. } else {
  4223. ctx = EVP_MD_CTX_new();
  4224. reslen = EVP_PKEY_size(pkey);
  4225. res = OPENSSL_malloc(reslen);
  4226. if (!TEST_ptr(ctx)
  4227. || !TEST_ptr(res)
  4228. || !TEST_true(EVP_DigestSignInit(ctx, &pctx, md, NULL, pkey))
  4229. || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1,
  4230. EVP_PKEY_CTRL_MY_COMMAND, 0, NULL),
  4231. 1)
  4232. || !TEST_int_eq(ctrl_called, 1))
  4233. goto err;
  4234. if (id == EVP_PKEY_ED25519) {
  4235. if (!TEST_true(EVP_DigestSign(ctx, res, &reslen, msg, sizeof(msg))))
  4236. goto err;
  4237. } else {
  4238. if (!TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg)))
  4239. || !TEST_true(EVP_DigestSignFinal(ctx, res, &reslen)))
  4240. goto err;
  4241. }
  4242. }
  4243. testresult = 1;
  4244. err:
  4245. OPENSSL_free(res);
  4246. EVP_MD_CTX_free(ctx);
  4247. if (doderive)
  4248. EVP_PKEY_CTX_free(pctx);
  4249. EVP_PKEY_free(pkey);
  4250. EVP_PKEY_meth_remove(custom_pmeth);
  4251. EVP_PKEY_meth_free(custom_pmeth);
  4252. custom_pmeth = NULL;
  4253. return testresult;
  4254. }
  4255. static int test_evp_md_cipher_meth(void)
  4256. {
  4257. EVP_MD *md = EVP_MD_meth_dup(EVP_sha256());
  4258. EVP_CIPHER *ciph = EVP_CIPHER_meth_dup(EVP_aes_128_cbc());
  4259. int testresult = 0;
  4260. if (!TEST_ptr(md) || !TEST_ptr(ciph))
  4261. goto err;
  4262. testresult = 1;
  4263. err:
  4264. EVP_MD_meth_free(md);
  4265. EVP_CIPHER_meth_free(ciph);
  4266. return testresult;
  4267. }
  4268. typedef struct {
  4269. int data;
  4270. } custom_dgst_ctx;
  4271. static int custom_md_init_called = 0;
  4272. static int custom_md_cleanup_called = 0;
  4273. static int custom_md_init(EVP_MD_CTX *ctx)
  4274. {
  4275. custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
  4276. if (p == NULL)
  4277. return 0;
  4278. custom_md_init_called++;
  4279. return 1;
  4280. }
  4281. static int custom_md_cleanup(EVP_MD_CTX *ctx)
  4282. {
  4283. custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
  4284. if (p == NULL)
  4285. /* Nothing to do */
  4286. return 1;
  4287. custom_md_cleanup_called++;
  4288. return 1;
  4289. }
  4290. static int test_custom_md_meth(void)
  4291. {
  4292. EVP_MD_CTX *mdctx = NULL;
  4293. EVP_MD *tmp = NULL;
  4294. char mess[] = "Test Message\n";
  4295. unsigned char md_value[EVP_MAX_MD_SIZE];
  4296. unsigned int md_len;
  4297. int testresult = 0;
  4298. int nid;
  4299. /*
  4300. * We are testing deprecated functions. We don't support a non-default
  4301. * library context in this test.
  4302. */
  4303. if (testctx != NULL)
  4304. return TEST_skip("Non-default libctx");
  4305. custom_md_init_called = custom_md_cleanup_called = 0;
  4306. nid = OBJ_create("1.3.6.1.4.1.16604.998866.1", "custom-md", "custom-md");
  4307. if (!TEST_int_ne(nid, NID_undef))
  4308. goto err;
  4309. tmp = EVP_MD_meth_new(nid, NID_undef);
  4310. if (!TEST_ptr(tmp))
  4311. goto err;
  4312. if (!TEST_true(EVP_MD_meth_set_init(tmp, custom_md_init))
  4313. || !TEST_true(EVP_MD_meth_set_cleanup(tmp, custom_md_cleanup))
  4314. || !TEST_true(EVP_MD_meth_set_app_datasize(tmp,
  4315. sizeof(custom_dgst_ctx))))
  4316. goto err;
  4317. mdctx = EVP_MD_CTX_new();
  4318. if (!TEST_ptr(mdctx)
  4319. /*
  4320. * Initing our custom md and then initing another md should
  4321. * result in the init and cleanup functions of the custom md
  4322. * being called.
  4323. */
  4324. || !TEST_true(EVP_DigestInit_ex(mdctx, tmp, NULL))
  4325. || !TEST_true(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL))
  4326. || !TEST_true(EVP_DigestUpdate(mdctx, mess, strlen(mess)))
  4327. || !TEST_true(EVP_DigestFinal_ex(mdctx, md_value, &md_len))
  4328. || !TEST_int_eq(custom_md_init_called, 1)
  4329. || !TEST_int_eq(custom_md_cleanup_called, 1))
  4330. goto err;
  4331. testresult = 1;
  4332. err:
  4333. EVP_MD_CTX_free(mdctx);
  4334. EVP_MD_meth_free(tmp);
  4335. return testresult;
  4336. }
  4337. typedef struct {
  4338. int data;
  4339. } custom_ciph_ctx;
  4340. static int custom_ciph_init_called = 0;
  4341. static int custom_ciph_cleanup_called = 0;
  4342. static int custom_ciph_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  4343. const unsigned char *iv, int enc)
  4344. {
  4345. custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
  4346. if (p == NULL)
  4347. return 0;
  4348. custom_ciph_init_called++;
  4349. return 1;
  4350. }
  4351. static int custom_ciph_cleanup(EVP_CIPHER_CTX *ctx)
  4352. {
  4353. custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
  4354. if (p == NULL)
  4355. /* Nothing to do */
  4356. return 1;
  4357. custom_ciph_cleanup_called++;
  4358. return 1;
  4359. }
  4360. static int test_custom_ciph_meth(void)
  4361. {
  4362. EVP_CIPHER_CTX *ciphctx = NULL;
  4363. EVP_CIPHER *tmp = NULL;
  4364. int testresult = 0;
  4365. int nid;
  4366. /*
  4367. * We are testing deprecated functions. We don't support a non-default
  4368. * library context in this test.
  4369. */
  4370. if (testctx != NULL)
  4371. return TEST_skip("Non-default libctx");
  4372. custom_ciph_init_called = custom_ciph_cleanup_called = 0;
  4373. nid = OBJ_create("1.3.6.1.4.1.16604.998866.2", "custom-ciph", "custom-ciph");
  4374. if (!TEST_int_ne(nid, NID_undef))
  4375. goto err;
  4376. tmp = EVP_CIPHER_meth_new(nid, 16, 16);
  4377. if (!TEST_ptr(tmp))
  4378. goto err;
  4379. if (!TEST_true(EVP_CIPHER_meth_set_init(tmp, custom_ciph_init))
  4380. || !TEST_true(EVP_CIPHER_meth_set_flags(tmp, EVP_CIPH_ALWAYS_CALL_INIT))
  4381. || !TEST_true(EVP_CIPHER_meth_set_cleanup(tmp, custom_ciph_cleanup))
  4382. || !TEST_true(EVP_CIPHER_meth_set_impl_ctx_size(tmp,
  4383. sizeof(custom_ciph_ctx))))
  4384. goto err;
  4385. ciphctx = EVP_CIPHER_CTX_new();
  4386. if (!TEST_ptr(ciphctx)
  4387. /*
  4388. * Initing our custom cipher and then initing another cipher
  4389. * should result in the init and cleanup functions of the custom
  4390. * cipher being called.
  4391. */
  4392. || !TEST_true(EVP_CipherInit_ex(ciphctx, tmp, NULL, NULL, NULL, 1))
  4393. || !TEST_true(EVP_CipherInit_ex(ciphctx, EVP_aes_128_cbc(), NULL,
  4394. NULL, NULL, 1))
  4395. || !TEST_int_eq(custom_ciph_init_called, 1)
  4396. || !TEST_int_eq(custom_ciph_cleanup_called, 1))
  4397. goto err;
  4398. testresult = 1;
  4399. err:
  4400. EVP_CIPHER_CTX_free(ciphctx);
  4401. EVP_CIPHER_meth_free(tmp);
  4402. return testresult;
  4403. }
  4404. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  4405. /* Test we can create a signature keys with an associated ENGINE */
  4406. static int test_signatures_with_engine(int tst)
  4407. {
  4408. ENGINE *e;
  4409. const char *engine_id = "dasync";
  4410. EVP_PKEY *pkey = NULL;
  4411. const unsigned char badcmackey[] = { 0x00, 0x01 };
  4412. const unsigned char cmackey[] = {
  4413. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4414. 0x0c, 0x0d, 0x0e, 0x0f
  4415. };
  4416. const unsigned char ed25519key[] = {
  4417. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4418. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  4419. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  4420. };
  4421. const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  4422. int testresult = 0;
  4423. EVP_MD_CTX *ctx = NULL;
  4424. unsigned char *mac = NULL;
  4425. size_t maclen = 0;
  4426. int ret;
  4427. # ifdef OPENSSL_NO_CMAC
  4428. /* Skip CMAC tests in a no-cmac build */
  4429. if (tst <= 1)
  4430. return 1;
  4431. # endif
  4432. # ifdef OPENSSL_NO_ECX
  4433. /* Skip ECX tests in a no-ecx build */
  4434. if (tst == 2)
  4435. return 1;
  4436. # endif
  4437. if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
  4438. return 0;
  4439. if (!TEST_true(ENGINE_init(e))) {
  4440. ENGINE_free(e);
  4441. return 0;
  4442. }
  4443. switch (tst) {
  4444. case 0:
  4445. pkey = EVP_PKEY_new_CMAC_key(e, cmackey, sizeof(cmackey),
  4446. EVP_aes_128_cbc());
  4447. break;
  4448. case 1:
  4449. pkey = EVP_PKEY_new_CMAC_key(e, badcmackey, sizeof(badcmackey),
  4450. EVP_aes_128_cbc());
  4451. break;
  4452. case 2:
  4453. pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, e, ed25519key,
  4454. sizeof(ed25519key));
  4455. break;
  4456. default:
  4457. TEST_error("Invalid test case");
  4458. goto err;
  4459. }
  4460. if (!TEST_ptr(pkey))
  4461. goto err;
  4462. if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
  4463. goto err;
  4464. ret = EVP_DigestSignInit(ctx, NULL, tst == 2 ? NULL : EVP_sha256(), NULL,
  4465. pkey);
  4466. if (tst == 0) {
  4467. if (!TEST_true(ret))
  4468. goto err;
  4469. if (!TEST_true(EVP_DigestSignUpdate(ctx, msg, sizeof(msg)))
  4470. || !TEST_true(EVP_DigestSignFinal(ctx, NULL, &maclen)))
  4471. goto err;
  4472. if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
  4473. goto err;
  4474. if (!TEST_true(EVP_DigestSignFinal(ctx, mac, &maclen)))
  4475. goto err;
  4476. } else {
  4477. /* We used a bad key. We expect a failure here */
  4478. if (!TEST_false(ret))
  4479. goto err;
  4480. }
  4481. testresult = 1;
  4482. err:
  4483. EVP_MD_CTX_free(ctx);
  4484. OPENSSL_free(mac);
  4485. EVP_PKEY_free(pkey);
  4486. ENGINE_finish(e);
  4487. ENGINE_free(e);
  4488. return testresult;
  4489. }
  4490. static int test_cipher_with_engine(void)
  4491. {
  4492. ENGINE *e;
  4493. const char *engine_id = "dasync";
  4494. const unsigned char keyiv[] = {
  4495. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4496. 0x0c, 0x0d, 0x0e, 0x0f
  4497. };
  4498. const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  4499. int testresult = 0;
  4500. EVP_CIPHER_CTX *ctx = NULL, *ctx2 = NULL;
  4501. unsigned char buf[AES_BLOCK_SIZE];
  4502. int len = 0;
  4503. if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
  4504. return 0;
  4505. if (!TEST_true(ENGINE_init(e))) {
  4506. ENGINE_free(e);
  4507. return 0;
  4508. }
  4509. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  4510. || !TEST_ptr(ctx2 = EVP_CIPHER_CTX_new()))
  4511. goto err;
  4512. if (!TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), e, keyiv, keyiv)))
  4513. goto err;
  4514. /* Copy the ctx, and complete the operation with the new ctx */
  4515. if (!TEST_true(EVP_CIPHER_CTX_copy(ctx2, ctx)))
  4516. goto err;
  4517. if (!TEST_true(EVP_EncryptUpdate(ctx2, buf, &len, msg, sizeof(msg)))
  4518. || !TEST_true(EVP_EncryptFinal_ex(ctx2, buf + len, &len)))
  4519. goto err;
  4520. testresult = 1;
  4521. err:
  4522. EVP_CIPHER_CTX_free(ctx);
  4523. EVP_CIPHER_CTX_free(ctx2);
  4524. ENGINE_finish(e);
  4525. ENGINE_free(e);
  4526. return testresult;
  4527. }
  4528. # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
  4529. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  4530. #ifndef OPENSSL_NO_ECX
  4531. static int ecxnids[] = {
  4532. NID_X25519,
  4533. NID_X448,
  4534. NID_ED25519,
  4535. NID_ED448
  4536. };
  4537. /* Test that creating ECX keys with a short private key fails as expected */
  4538. static int test_ecx_short_keys(int tst)
  4539. {
  4540. unsigned char ecxkeydata = 1;
  4541. EVP_PKEY *pkey;
  4542. pkey = EVP_PKEY_new_raw_private_key_ex(testctx, OBJ_nid2sn(ecxnids[tst]),
  4543. NULL, &ecxkeydata, 1);
  4544. if (!TEST_ptr_null(pkey)) {
  4545. EVP_PKEY_free(pkey);
  4546. return 0;
  4547. }
  4548. return 1;
  4549. }
  4550. #endif
  4551. typedef enum OPTION_choice {
  4552. OPT_ERR = -1,
  4553. OPT_EOF = 0,
  4554. OPT_CONTEXT,
  4555. OPT_TEST_ENUM
  4556. } OPTION_CHOICE;
  4557. const OPTIONS *test_get_options(void)
  4558. {
  4559. static const OPTIONS options[] = {
  4560. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  4561. { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" },
  4562. { NULL }
  4563. };
  4564. return options;
  4565. }
  4566. #ifndef OPENSSL_NO_ECX
  4567. /* Test that trying to sign with a public key errors out gracefully */
  4568. static int test_ecx_not_private_key(int tst)
  4569. {
  4570. EVP_PKEY *pkey = NULL;
  4571. const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
  4572. int testresult = 0;
  4573. EVP_MD_CTX *ctx = NULL;
  4574. unsigned char *mac = NULL;
  4575. size_t maclen = 0;
  4576. unsigned char *pubkey;
  4577. size_t pubkeylen;
  4578. switch (keys[tst].type) {
  4579. case NID_X25519:
  4580. case NID_X448:
  4581. return TEST_skip("signing not supported for X25519/X448");
  4582. }
  4583. /* Check if this algorithm supports public keys */
  4584. if (keys[tst].pub == NULL)
  4585. return TEST_skip("no public key present");
  4586. pubkey = (unsigned char *)keys[tst].pub;
  4587. pubkeylen = strlen(keys[tst].pub);
  4588. pkey = EVP_PKEY_new_raw_public_key_ex(testctx, OBJ_nid2sn(keys[tst].type),
  4589. NULL, pubkey, pubkeylen);
  4590. if (!TEST_ptr(pkey))
  4591. goto err;
  4592. if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
  4593. goto err;
  4594. if (EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey) != 1)
  4595. goto check_err;
  4596. if (EVP_DigestSign(ctx, NULL, &maclen, msg, sizeof(msg)) != 1)
  4597. goto check_err;
  4598. if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
  4599. goto err;
  4600. if (!TEST_int_eq(EVP_DigestSign(ctx, mac, &maclen, msg, sizeof(msg)), 0))
  4601. goto err;
  4602. check_err:
  4603. /*
  4604. * Currently only EVP_DigestSign will throw PROV_R_NOT_A_PRIVATE_KEY,
  4605. * but we relax the check to allow error also thrown by
  4606. * EVP_DigestSignInit and EVP_DigestSign.
  4607. */
  4608. if (ERR_GET_REASON(ERR_peek_error()) == PROV_R_NOT_A_PRIVATE_KEY) {
  4609. testresult = 1;
  4610. ERR_clear_error();
  4611. }
  4612. err:
  4613. EVP_MD_CTX_free(ctx);
  4614. OPENSSL_free(mac);
  4615. EVP_PKEY_free(pkey);
  4616. return testresult;
  4617. }
  4618. #endif /* OPENSSL_NO_ECX */
  4619. static int test_sign_continuation(void)
  4620. {
  4621. OSSL_PROVIDER *fake_rsa = NULL;
  4622. int testresult = 0;
  4623. EVP_PKEY *pkey = NULL;
  4624. EVP_PKEY_CTX *pctx = NULL;
  4625. EVP_MD_CTX *mctx = NULL;
  4626. const char sigbuf[] = "To Be Signed";
  4627. unsigned char signature[256];
  4628. size_t siglen = 256;
  4629. static int nodupnum = 1;
  4630. static const OSSL_PARAM nodup_params[] = {
  4631. OSSL_PARAM_int("NO_DUP", &nodupnum),
  4632. OSSL_PARAM_END
  4633. };
  4634. if (!TEST_ptr(fake_rsa = fake_rsa_start(testctx)))
  4635. return 0;
  4636. /* Construct a pkey using precise propq to use our provider */
  4637. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA",
  4638. "provider=fake-rsa"))
  4639. || !TEST_true(EVP_PKEY_fromdata_init(pctx))
  4640. || !TEST_true(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, NULL))
  4641. || !TEST_ptr(pkey))
  4642. goto end;
  4643. /* First test it continues (classic behavior) */
  4644. if (!TEST_ptr(mctx = EVP_MD_CTX_new())
  4645. || !TEST_true(EVP_DigestSignInit_ex(mctx, NULL, NULL, testctx,
  4646. NULL, pkey, NULL))
  4647. || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
  4648. || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen))
  4649. || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
  4650. || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen)))
  4651. goto end;
  4652. EVP_MD_CTX_free(mctx);
  4653. /* try again but failing the continuation */
  4654. if (!TEST_ptr(mctx = EVP_MD_CTX_new())
  4655. || !TEST_true(EVP_DigestSignInit_ex(mctx, NULL, NULL, testctx,
  4656. NULL, pkey, nodup_params))
  4657. || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
  4658. || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen))
  4659. || !TEST_false(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
  4660. || !TEST_false(EVP_DigestSignFinal(mctx, signature, &siglen)))
  4661. goto end;
  4662. testresult = 1;
  4663. end:
  4664. EVP_MD_CTX_free(mctx);
  4665. EVP_PKEY_free(pkey);
  4666. EVP_PKEY_CTX_free(pctx);
  4667. fake_rsa_finish(fake_rsa);
  4668. return testresult;
  4669. }
  4670. static int aes_gcm_encrypt(const unsigned char *gcm_key, size_t gcm_key_s,
  4671. const unsigned char *gcm_iv, size_t gcm_ivlen,
  4672. const unsigned char *gcm_pt, size_t gcm_pt_s,
  4673. const unsigned char *gcm_aad, size_t gcm_aad_s,
  4674. const unsigned char *gcm_ct, size_t gcm_ct_s,
  4675. const unsigned char *gcm_tag, size_t gcm_tag_s)
  4676. {
  4677. int ret = 0;
  4678. EVP_CIPHER_CTX *ctx;
  4679. EVP_CIPHER *cipher = NULL;
  4680. int outlen, tmplen;
  4681. unsigned char outbuf[1024];
  4682. unsigned char outtag[16];
  4683. OSSL_PARAM params[2] = {
  4684. OSSL_PARAM_END, OSSL_PARAM_END
  4685. };
  4686. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  4687. || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM", "")))
  4688. goto err;
  4689. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  4690. &gcm_ivlen);
  4691. if (!TEST_true(EVP_EncryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
  4692. || (gcm_aad != NULL
  4693. && !TEST_true(EVP_EncryptUpdate(ctx, NULL, &outlen,
  4694. gcm_aad, gcm_aad_s)))
  4695. || !TEST_true(EVP_EncryptUpdate(ctx, outbuf, &outlen,
  4696. gcm_pt, gcm_pt_s))
  4697. || !TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &tmplen)))
  4698. goto err;
  4699. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  4700. outtag, sizeof(outtag));
  4701. if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx, params))
  4702. || !TEST_mem_eq(outbuf, outlen, gcm_ct, gcm_ct_s)
  4703. || !TEST_mem_eq(outtag, gcm_tag_s, gcm_tag, gcm_tag_s))
  4704. goto err;
  4705. ret = 1;
  4706. err:
  4707. EVP_CIPHER_free(cipher);
  4708. EVP_CIPHER_CTX_free(ctx);
  4709. return ret;
  4710. }
  4711. static int aes_gcm_decrypt(const unsigned char *gcm_key, size_t gcm_key_s,
  4712. const unsigned char *gcm_iv, size_t gcm_ivlen,
  4713. const unsigned char *gcm_pt, size_t gcm_pt_s,
  4714. const unsigned char *gcm_aad, size_t gcm_aad_s,
  4715. const unsigned char *gcm_ct, size_t gcm_ct_s,
  4716. const unsigned char *gcm_tag, size_t gcm_tag_s)
  4717. {
  4718. int ret = 0;
  4719. EVP_CIPHER_CTX *ctx;
  4720. EVP_CIPHER *cipher = NULL;
  4721. int outlen;
  4722. unsigned char outbuf[1024];
  4723. OSSL_PARAM params[2] = {
  4724. OSSL_PARAM_END, OSSL_PARAM_END
  4725. };
  4726. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  4727. goto err;
  4728. if ((cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM", "")) == NULL)
  4729. goto err;
  4730. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
  4731. &gcm_ivlen);
  4732. if (!TEST_true(EVP_DecryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
  4733. || (gcm_aad != NULL
  4734. && !TEST_true(EVP_DecryptUpdate(ctx, NULL, &outlen,
  4735. gcm_aad, gcm_aad_s)))
  4736. || !TEST_true(EVP_DecryptUpdate(ctx, outbuf, &outlen,
  4737. gcm_ct, gcm_ct_s))
  4738. || !TEST_mem_eq(outbuf, outlen, gcm_pt, gcm_pt_s))
  4739. goto err;
  4740. params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
  4741. (void*)gcm_tag, gcm_tag_s);
  4742. if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params))
  4743. ||!TEST_true(EVP_DecryptFinal_ex(ctx, outbuf, &outlen)))
  4744. goto err;
  4745. ret = 1;
  4746. err:
  4747. EVP_CIPHER_free(cipher);
  4748. EVP_CIPHER_CTX_free(ctx);
  4749. return ret;
  4750. }
  4751. static int test_aes_gcm_ivlen_change_cve_2023_5363(void)
  4752. {
  4753. /* AES-GCM test data obtained from NIST public test vectors */
  4754. static const unsigned char gcm_key[] = {
  4755. 0xd0, 0xc2, 0x67, 0xc1, 0x9f, 0x30, 0xd8, 0x0b, 0x89, 0x14, 0xbb, 0xbf,
  4756. 0xb7, 0x2f, 0x73, 0xb8, 0xd3, 0xcd, 0x5f, 0x6a, 0x78, 0x70, 0x15, 0x84,
  4757. 0x8a, 0x7b, 0x30, 0xe3, 0x8f, 0x16, 0xf1, 0x8b,
  4758. };
  4759. static const unsigned char gcm_iv[] = {
  4760. 0xb6, 0xdc, 0xda, 0x95, 0xac, 0x99, 0x77, 0x76, 0x25, 0xae, 0x87, 0xf8,
  4761. 0xa3, 0xa9, 0xdd, 0x64, 0xd7, 0x9b, 0xbd, 0x5f, 0x4a, 0x0e, 0x54, 0xca,
  4762. 0x1a, 0x9f, 0xa2, 0xe3, 0xf4, 0x5f, 0x5f, 0xc2, 0xce, 0xa7, 0xb6, 0x14,
  4763. 0x12, 0x6f, 0xf0, 0xaf, 0xfd, 0x3e, 0x17, 0x35, 0x6e, 0xa0, 0x16, 0x09,
  4764. 0xdd, 0xa1, 0x3f, 0xd8, 0xdd, 0xf3, 0xdf, 0x4f, 0xcb, 0x18, 0x49, 0xb8,
  4765. 0xb3, 0x69, 0x2c, 0x5d, 0x4f, 0xad, 0x30, 0x91, 0x08, 0xbc, 0xbe, 0x24,
  4766. 0x01, 0x0f, 0xbe, 0x9c, 0xfb, 0x4f, 0x5d, 0x19, 0x7f, 0x4c, 0x53, 0xb0,
  4767. 0x95, 0x90, 0xac, 0x7b, 0x1f, 0x7b, 0xa0, 0x99, 0xe1, 0xf3, 0x48, 0x54,
  4768. 0xd0, 0xfc, 0xa9, 0xcc, 0x91, 0xf8, 0x1f, 0x9b, 0x6c, 0x9a, 0xe0, 0xdc,
  4769. 0x63, 0xea, 0x7d, 0x2a, 0x4a, 0x7d, 0xa5, 0xed, 0x68, 0x57, 0x27, 0x6b,
  4770. 0x68, 0xe0, 0xf2, 0xb8, 0x51, 0x50, 0x8d, 0x3d,
  4771. };
  4772. static const unsigned char gcm_pt[] = {
  4773. 0xb8, 0xb6, 0x88, 0x36, 0x44, 0xe2, 0x34, 0xdf, 0x24, 0x32, 0x91, 0x07,
  4774. 0x4f, 0xe3, 0x6f, 0x81,
  4775. };
  4776. static const unsigned char gcm_ct[] = {
  4777. 0xff, 0x4f, 0xb3, 0xf3, 0xf9, 0xa2, 0x51, 0xd4, 0x82, 0xc2, 0xbe, 0xf3,
  4778. 0xe2, 0xd0, 0xec, 0xed,
  4779. };
  4780. static const unsigned char gcm_tag[] = {
  4781. 0xbd, 0x06, 0x38, 0x09, 0xf7, 0xe1, 0xc4, 0x72, 0x0e, 0xf2, 0xea, 0x63,
  4782. 0xdb, 0x99, 0x6c, 0x21,
  4783. };
  4784. return aes_gcm_encrypt(gcm_key, sizeof(gcm_key), gcm_iv, sizeof(gcm_iv),
  4785. gcm_pt, sizeof(gcm_pt), NULL, 0,
  4786. gcm_ct, sizeof(gcm_ct), gcm_tag, sizeof(gcm_tag))
  4787. && aes_gcm_decrypt(gcm_key, sizeof(gcm_key), gcm_iv, sizeof(gcm_iv),
  4788. gcm_pt, sizeof(gcm_pt), NULL, 0,
  4789. gcm_ct, sizeof(gcm_ct), gcm_tag, sizeof(gcm_tag));
  4790. }
  4791. #ifndef OPENSSL_NO_RC4
  4792. static int rc4_encrypt(const unsigned char *rc4_key, size_t rc4_key_s,
  4793. const unsigned char *rc4_pt, size_t rc4_pt_s,
  4794. const unsigned char *rc4_ct, size_t rc4_ct_s)
  4795. {
  4796. int ret = 0;
  4797. EVP_CIPHER_CTX *ctx;
  4798. EVP_CIPHER *cipher = NULL;
  4799. int outlen, tmplen;
  4800. unsigned char outbuf[1024];
  4801. OSSL_PARAM params[2] = {
  4802. OSSL_PARAM_END, OSSL_PARAM_END
  4803. };
  4804. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  4805. || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "RC4", "")))
  4806. goto err;
  4807. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
  4808. &rc4_key_s);
  4809. if (!TEST_true(EVP_EncryptInit_ex2(ctx, cipher, rc4_key, NULL, params))
  4810. || !TEST_true(EVP_EncryptUpdate(ctx, outbuf, &outlen,
  4811. rc4_pt, rc4_pt_s))
  4812. || !TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &tmplen)))
  4813. goto err;
  4814. if (!TEST_mem_eq(outbuf, outlen, rc4_ct, rc4_ct_s))
  4815. goto err;
  4816. ret = 1;
  4817. err:
  4818. EVP_CIPHER_free(cipher);
  4819. EVP_CIPHER_CTX_free(ctx);
  4820. return ret;
  4821. }
  4822. static int rc4_decrypt(const unsigned char *rc4_key, size_t rc4_key_s,
  4823. const unsigned char *rc4_pt, size_t rc4_pt_s,
  4824. const unsigned char *rc4_ct, size_t rc4_ct_s)
  4825. {
  4826. int ret = 0;
  4827. EVP_CIPHER_CTX *ctx;
  4828. EVP_CIPHER *cipher = NULL;
  4829. int outlen;
  4830. unsigned char outbuf[1024];
  4831. OSSL_PARAM params[2] = {
  4832. OSSL_PARAM_END, OSSL_PARAM_END
  4833. };
  4834. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  4835. goto err;
  4836. if ((cipher = EVP_CIPHER_fetch(testctx, "RC4", "")) == NULL)
  4837. goto err;
  4838. params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
  4839. &rc4_key_s);
  4840. if (!TEST_true(EVP_DecryptInit_ex2(ctx, cipher, rc4_key, NULL, params))
  4841. || !TEST_true(EVP_DecryptUpdate(ctx, outbuf, &outlen,
  4842. rc4_ct, rc4_ct_s))
  4843. || !TEST_mem_eq(outbuf, outlen, rc4_pt, rc4_pt_s))
  4844. goto err;
  4845. ret = 1;
  4846. err:
  4847. EVP_CIPHER_free(cipher);
  4848. EVP_CIPHER_CTX_free(ctx);
  4849. return ret;
  4850. }
  4851. static int test_aes_rc4_keylen_change_cve_2023_5363(void)
  4852. {
  4853. /* RC4 test data obtained from RFC 6229 */
  4854. static const struct {
  4855. unsigned char key[5];
  4856. unsigned char padding[11];
  4857. } rc4_key = {
  4858. { /* Five bytes of key material */
  4859. 0x83, 0x32, 0x22, 0x77, 0x2a,
  4860. },
  4861. { /* Random padding to 16 bytes */
  4862. 0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a, 0xaa, 0x32, 0x91
  4863. }
  4864. };
  4865. static const unsigned char rc4_pt[] = {
  4866. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  4867. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  4868. };
  4869. static const unsigned char rc4_ct[] = {
  4870. 0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a,
  4871. 0x2e, 0x87, 0x9e, 0x92, 0xa4, 0x97, 0xef, 0xda
  4872. };
  4873. if (lgcyprov == NULL)
  4874. return TEST_skip("Test requires legacy provider to be loaded");
  4875. return rc4_encrypt(rc4_key.key, sizeof(rc4_key.key),
  4876. rc4_pt, sizeof(rc4_pt), rc4_ct, sizeof(rc4_ct))
  4877. && rc4_decrypt(rc4_key.key, sizeof(rc4_key.key),
  4878. rc4_pt, sizeof(rc4_pt), rc4_ct, sizeof(rc4_ct));
  4879. }
  4880. #endif
  4881. int setup_tests(void)
  4882. {
  4883. OPTION_CHOICE o;
  4884. while ((o = opt_next()) != OPT_EOF) {
  4885. switch (o) {
  4886. case OPT_CONTEXT:
  4887. /* Set up an alternate library context */
  4888. testctx = OSSL_LIB_CTX_new();
  4889. if (!TEST_ptr(testctx))
  4890. return 0;
  4891. #ifdef STATIC_LEGACY
  4892. /*
  4893. * This test is always statically linked against libcrypto. We must not
  4894. * attempt to load legacy.so that might be dynamically linked against
  4895. * libcrypto. Instead we use a built-in version of the legacy provider.
  4896. */
  4897. if (!OSSL_PROVIDER_add_builtin(testctx, "legacy", ossl_legacy_provider_init))
  4898. return 0;
  4899. #endif
  4900. /* Swap the libctx to test non-default context only */
  4901. nullprov = OSSL_PROVIDER_load(NULL, "null");
  4902. deflprov = OSSL_PROVIDER_load(testctx, "default");
  4903. #ifndef OPENSSL_SYS_TANDEM
  4904. lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
  4905. #endif
  4906. break;
  4907. case OPT_TEST_CASES:
  4908. break;
  4909. default:
  4910. return 0;
  4911. }
  4912. }
  4913. ADD_TEST(test_EVP_set_default_properties);
  4914. ADD_ALL_TESTS(test_EVP_DigestSignInit, 30);
  4915. ADD_TEST(test_EVP_DigestVerifyInit);
  4916. #ifndef OPENSSL_NO_SIPHASH
  4917. ADD_TEST(test_siphash_digestsign);
  4918. #endif
  4919. ADD_TEST(test_EVP_Digest);
  4920. ADD_TEST(test_EVP_md_null);
  4921. ADD_ALL_TESTS(test_EVP_PKEY_sign, 3);
  4922. #ifndef OPENSSL_NO_DEPRECATED_3_0
  4923. ADD_ALL_TESTS(test_EVP_PKEY_sign_with_app_method, 2);
  4924. #endif
  4925. ADD_ALL_TESTS(test_EVP_Enveloped, 2);
  4926. ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
  4927. ADD_TEST(test_privatekey_to_pkcs8);
  4928. ADD_TEST(test_EVP_PKCS82PKEY_wrong_tag);
  4929. #ifndef OPENSSL_NO_EC
  4930. ADD_TEST(test_EVP_PKCS82PKEY);
  4931. #endif
  4932. #ifndef OPENSSL_NO_EC
  4933. ADD_ALL_TESTS(test_EC_keygen_with_enc, OSSL_NELEM(ec_encodings));
  4934. #endif
  4935. #if !defined(OPENSSL_NO_SM2)
  4936. ADD_TEST(test_EVP_SM2);
  4937. ADD_TEST(test_EVP_SM2_verify);
  4938. #endif
  4939. ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
  4940. #ifndef OPENSSL_NO_DEPRECATED_3_0
  4941. custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
  4942. if (!TEST_ptr(custom_pmeth))
  4943. return 0;
  4944. EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
  4945. EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
  4946. EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
  4947. if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
  4948. return 0;
  4949. #endif
  4950. ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
  4951. #ifndef OPENSSL_NO_CMAC
  4952. ADD_TEST(test_CMAC_keygen);
  4953. #endif
  4954. ADD_TEST(test_HKDF);
  4955. ADD_TEST(test_emptyikm_HKDF);
  4956. #ifndef OPENSSL_NO_EC
  4957. ADD_TEST(test_X509_PUBKEY_inplace);
  4958. ADD_TEST(test_X509_PUBKEY_dup);
  4959. ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
  4960. OSSL_NELEM(ec_der_pub_keys));
  4961. #endif
  4962. #ifndef OPENSSL_NO_DSA
  4963. ADD_TEST(test_DSA_get_set_params);
  4964. ADD_TEST(test_DSA_priv_pub);
  4965. #endif
  4966. ADD_TEST(test_RSA_get_set_params);
  4967. ADD_TEST(test_RSA_OAEP_set_get_params);
  4968. ADD_TEST(test_RSA_OAEP_set_null_label);
  4969. #ifndef OPENSSL_NO_DEPRECATED_3_0
  4970. ADD_TEST(test_RSA_legacy);
  4971. #endif
  4972. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  4973. ADD_TEST(test_decrypt_null_chunks);
  4974. #endif
  4975. #ifndef OPENSSL_NO_DH
  4976. ADD_TEST(test_DH_priv_pub);
  4977. # ifndef OPENSSL_NO_DEPRECATED_3_0
  4978. ADD_TEST(test_EVP_PKEY_set1_DH);
  4979. # endif
  4980. #endif
  4981. #ifndef OPENSSL_NO_EC
  4982. ADD_TEST(test_EC_priv_pub);
  4983. ADD_TEST(test_evp_get_ec_pub);
  4984. # ifndef OPENSSL_NO_DEPRECATED_3_0
  4985. ADD_TEST(test_EC_priv_only_legacy);
  4986. ADD_TEST(test_evp_get_ec_pub_legacy);
  4987. # endif
  4988. #endif
  4989. ADD_ALL_TESTS(test_keygen_with_empty_template, 2);
  4990. ADD_ALL_TESTS(test_pkey_ctx_fail_without_provider, 2);
  4991. ADD_TEST(test_rand_agglomeration);
  4992. ADD_ALL_TESTS(test_evp_iv_aes, 12);
  4993. #ifndef OPENSSL_NO_DES
  4994. ADD_ALL_TESTS(test_evp_iv_des, 6);
  4995. #endif
  4996. #ifndef OPENSSL_NO_BF
  4997. ADD_ALL_TESTS(test_evp_bf_default_keylen, 4);
  4998. #endif
  4999. ADD_TEST(test_EVP_rsa_pss_with_keygen_bits);
  5000. ADD_TEST(test_EVP_rsa_pss_set_saltlen);
  5001. #ifndef OPENSSL_NO_EC
  5002. ADD_ALL_TESTS(test_ecpub, OSSL_NELEM(ecpub_nids));
  5003. #endif
  5004. ADD_TEST(test_names_do_all);
  5005. ADD_ALL_TESTS(test_evp_init_seq, OSSL_NELEM(evp_init_tests));
  5006. ADD_ALL_TESTS(test_evp_reset, OSSL_NELEM(evp_reset_tests));
  5007. ADD_ALL_TESTS(test_evp_reinit_seq, OSSL_NELEM(evp_reinit_tests));
  5008. ADD_ALL_TESTS(test_gcm_reinit, OSSL_NELEM(gcm_reinit_tests));
  5009. ADD_ALL_TESTS(test_evp_updated_iv, OSSL_NELEM(evp_updated_iv_tests));
  5010. ADD_ALL_TESTS(test_ivlen_change, OSSL_NELEM(ivlen_change_ciphers));
  5011. if (OSSL_NELEM(keylen_change_ciphers) - 1 > 0)
  5012. ADD_ALL_TESTS(test_keylen_change, OSSL_NELEM(keylen_change_ciphers) - 1);
  5013. #ifndef OPENSSL_NO_DEPRECATED_3_0
  5014. ADD_ALL_TESTS(test_custom_pmeth, 12);
  5015. ADD_TEST(test_evp_md_cipher_meth);
  5016. ADD_TEST(test_custom_md_meth);
  5017. ADD_TEST(test_custom_ciph_meth);
  5018. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  5019. /* Tests only support the default libctx */
  5020. if (testctx == NULL) {
  5021. # ifndef OPENSSL_NO_EC
  5022. ADD_ALL_TESTS(test_signatures_with_engine, 3);
  5023. # else
  5024. ADD_ALL_TESTS(test_signatures_with_engine, 2);
  5025. # endif
  5026. ADD_TEST(test_cipher_with_engine);
  5027. }
  5028. # endif
  5029. #endif
  5030. #ifndef OPENSSL_NO_ECX
  5031. ADD_ALL_TESTS(test_ecx_short_keys, OSSL_NELEM(ecxnids));
  5032. ADD_ALL_TESTS(test_ecx_not_private_key, OSSL_NELEM(keys));
  5033. #endif
  5034. ADD_TEST(test_sign_continuation);
  5035. /* Test cases for CVE-2023-5363 */
  5036. ADD_TEST(test_aes_gcm_ivlen_change_cve_2023_5363);
  5037. #ifndef OPENSSL_NO_RC4
  5038. ADD_TEST(test_aes_rc4_keylen_change_cve_2023_5363);
  5039. #endif
  5040. return 1;
  5041. }
  5042. void cleanup_tests(void)
  5043. {
  5044. OSSL_PROVIDER_unload(nullprov);
  5045. OSSL_PROVIDER_unload(deflprov);
  5046. #ifndef OPENSSL_SYS_TANDEM
  5047. OSSL_PROVIDER_unload(lgcyprov);
  5048. #endif
  5049. OSSL_LIB_CTX_free(testctx);
  5050. }