evp.c 311 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407
  1. /* evp.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #if !defined(WOLFSSL_EVP_INCLUDED)
  26. #ifndef WOLFSSL_IGNORE_FILE_WARN
  27. #warning evp.c does not need to be compiled separately from ssl.c
  28. #endif
  29. #elif defined(WOLFCRYPT_ONLY)
  30. #else
  31. #if defined(OPENSSL_EXTRA)
  32. #if !defined(HAVE_PKCS7) && \
  33. ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  34. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST))
  35. #include <wolfssl/wolfcrypt/aes.h>
  36. #endif
  37. #include <wolfssl/openssl/ecdsa.h>
  38. #include <wolfssl/openssl/evp.h>
  39. #include <wolfssl/openssl/kdf.h>
  40. #include <wolfssl/wolfcrypt/integer.h>
  41. #ifndef NO_AES
  42. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  43. #ifdef WOLFSSL_AES_128
  44. static const char EVP_AES_128_CBC[] = "AES-128-CBC";
  45. #endif
  46. #ifdef WOLFSSL_AES_192
  47. static const char EVP_AES_192_CBC[] = "AES-192-CBC";
  48. #endif
  49. #ifdef WOLFSSL_AES_256
  50. static const char EVP_AES_256_CBC[] = "AES-256-CBC";
  51. #endif
  52. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  53. #ifdef WOLFSSL_AES_OFB
  54. #ifdef WOLFSSL_AES_128
  55. static const char EVP_AES_128_OFB[] = "AES-128-OFB";
  56. #endif
  57. #ifdef WOLFSSL_AES_192
  58. static const char EVP_AES_192_OFB[] = "AES-192-OFB";
  59. #endif
  60. #ifdef WOLFSSL_AES_256
  61. static const char EVP_AES_256_OFB[] = "AES-256-OFB";
  62. #endif
  63. #endif /* WOLFSSL_AES_OFB */
  64. #ifdef WOLFSSL_AES_XTS
  65. #ifdef WOLFSSL_AES_128
  66. static const char EVP_AES_128_XTS[] = "AES-128-XTS";
  67. #endif
  68. #ifdef WOLFSSL_AES_256
  69. static const char EVP_AES_256_XTS[] = "AES-256-XTS";
  70. #endif
  71. #endif /* WOLFSSL_AES_XTS */
  72. #ifdef WOLFSSL_AES_CFB
  73. #ifdef WOLFSSL_AES_128
  74. static const char EVP_AES_128_CFB1[] = "AES-128-CFB1";
  75. #endif
  76. #ifdef WOLFSSL_AES_192
  77. static const char EVP_AES_192_CFB1[] = "AES-192-CFB1";
  78. #endif
  79. #ifdef WOLFSSL_AES_256
  80. static const char EVP_AES_256_CFB1[] = "AES-256-CFB1";
  81. #endif
  82. #ifdef WOLFSSL_AES_128
  83. static const char EVP_AES_128_CFB8[] = "AES-128-CFB8";
  84. #endif
  85. #ifdef WOLFSSL_AES_192
  86. static const char EVP_AES_192_CFB8[] = "AES-192-CFB8";
  87. #endif
  88. #ifdef WOLFSSL_AES_256
  89. static const char EVP_AES_256_CFB8[] = "AES-256-CFB8";
  90. #endif
  91. #ifdef WOLFSSL_AES_128
  92. static const char EVP_AES_128_CFB128[] = "AES-128-CFB128";
  93. #endif
  94. #ifdef WOLFSSL_AES_192
  95. static const char EVP_AES_192_CFB128[] = "AES-192-CFB128";
  96. #endif
  97. #ifdef WOLFSSL_AES_256
  98. static const char EVP_AES_256_CFB128[] = "AES-256-CFB128";
  99. #endif
  100. #endif /* WOLFSSL_AES_CFB */
  101. #ifdef HAVE_AESGCM
  102. #ifdef WOLFSSL_AES_128
  103. static const char EVP_AES_128_GCM[] = "AES-128-GCM";
  104. #endif
  105. #ifdef WOLFSSL_AES_192
  106. static const char EVP_AES_192_GCM[] = "AES-192-GCM";
  107. #endif
  108. #ifdef WOLFSSL_AES_256
  109. static const char EVP_AES_256_GCM[] = "AES-256-GCM";
  110. #endif
  111. #endif /* HAVE_AESGCM */
  112. #ifdef WOLFSSL_AES_COUNTER
  113. #ifdef WOLFSSL_AES_128
  114. static const char EVP_AES_128_CTR[] = "AES-128-CTR";
  115. #endif
  116. #ifdef WOLFSSL_AES_192
  117. static const char EVP_AES_192_CTR[] = "AES-192-CTR";
  118. #endif
  119. #ifdef WOLFSSL_AES_256
  120. static const char EVP_AES_256_CTR[] = "AES-256-CTR";
  121. #endif
  122. #endif
  123. #ifdef HAVE_AES_ECB
  124. #ifdef WOLFSSL_AES_128
  125. static const char EVP_AES_128_ECB[] = "AES-128-ECB";
  126. #endif
  127. #ifdef WOLFSSL_AES_192
  128. static const char EVP_AES_192_ECB[] = "AES-192-ECB";
  129. #endif
  130. #ifdef WOLFSSL_AES_256
  131. static const char EVP_AES_256_ECB[] = "AES-256-ECB";
  132. #endif
  133. #endif
  134. #endif
  135. #ifndef NO_DES3
  136. static const char EVP_DES_CBC[] = "DES-CBC";
  137. static const char EVP_DES_ECB[] = "DES-ECB";
  138. static const char EVP_DES_EDE3_CBC[] = "DES-EDE3-CBC";
  139. static const char EVP_DES_EDE3_ECB[] = "DES-EDE3-ECB";
  140. #endif
  141. #ifndef NO_RC4
  142. static const char EVP_ARC4[] = "ARC4";
  143. #endif
  144. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  145. static const char EVP_CHACHA20_POLY1305[] = "CHACHA20-POLY1305";
  146. #endif
  147. static const char EVP_NULL[] = "NULL";
  148. #define EVP_CIPHER_TYPE_MATCHES(x, y) (XSTRCMP(x,y) == 0)
  149. #define EVP_PKEY_PRINT_LINE_WIDTH_MAX 80
  150. #define EVP_PKEY_PRINT_DIGITS_PER_LINE 15
  151. static unsigned int cipherType(const WOLFSSL_EVP_CIPHER *cipher);
  152. static enum wc_HashType EvpMd2MacType(const WOLFSSL_EVP_MD *md);
  153. /* Getter function for cipher key length
  154. *
  155. * c WOLFSSL_EVP_CIPHER structure to get key length from
  156. *
  157. * NOTE: OpenSSL_add_all_ciphers() should be called first before using this
  158. * function
  159. *
  160. * Returns size of key in bytes
  161. */
  162. int wolfSSL_EVP_Cipher_key_length(const WOLFSSL_EVP_CIPHER* c)
  163. {
  164. WOLFSSL_ENTER("wolfSSL_EVP_Cipher_key_length");
  165. if (c == NULL) {
  166. return 0;
  167. }
  168. switch (cipherType(c)) {
  169. #if !defined(NO_AES)
  170. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  171. case AES_128_CBC_TYPE: return 16;
  172. case AES_192_CBC_TYPE: return 24;
  173. case AES_256_CBC_TYPE: return 32;
  174. #endif
  175. #if defined(WOLFSSL_AES_CFB)
  176. case AES_128_CFB1_TYPE: return 16;
  177. case AES_192_CFB1_TYPE: return 24;
  178. case AES_256_CFB1_TYPE: return 32;
  179. case AES_128_CFB8_TYPE: return 16;
  180. case AES_192_CFB8_TYPE: return 24;
  181. case AES_256_CFB8_TYPE: return 32;
  182. case AES_128_CFB128_TYPE: return 16;
  183. case AES_192_CFB128_TYPE: return 24;
  184. case AES_256_CFB128_TYPE: return 32;
  185. #endif
  186. #if defined(WOLFSSL_AES_OFB)
  187. case AES_128_OFB_TYPE: return 16;
  188. case AES_192_OFB_TYPE: return 24;
  189. case AES_256_OFB_TYPE: return 32;
  190. #endif
  191. #if defined(WOLFSSL_AES_XTS)
  192. /* Two keys for XTS. */
  193. case AES_128_XTS_TYPE: return 16 * 2;
  194. case AES_256_XTS_TYPE: return 32 * 2;
  195. #endif
  196. #if defined(HAVE_AESGCM)
  197. case AES_128_GCM_TYPE: return 16;
  198. case AES_192_GCM_TYPE: return 24;
  199. case AES_256_GCM_TYPE: return 32;
  200. #endif
  201. #if defined(WOLFSSL_AES_COUNTER)
  202. case AES_128_CTR_TYPE: return 16;
  203. case AES_192_CTR_TYPE: return 24;
  204. case AES_256_CTR_TYPE: return 32;
  205. #endif
  206. #if defined(HAVE_AES_ECB)
  207. case AES_128_ECB_TYPE: return 16;
  208. case AES_192_ECB_TYPE: return 24;
  209. case AES_256_ECB_TYPE: return 32;
  210. #endif
  211. #endif /* !NO_AES */
  212. #ifndef NO_DES3
  213. case DES_CBC_TYPE: return 8;
  214. case DES_EDE3_CBC_TYPE: return 24;
  215. case DES_ECB_TYPE: return 8;
  216. case DES_EDE3_ECB_TYPE: return 24;
  217. #endif
  218. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  219. case CHACHA20_POLY1305_TYPE: return 32;
  220. #endif
  221. default:
  222. return 0;
  223. }
  224. }
  225. int wolfSSL_EVP_EncryptInit(WOLFSSL_EVP_CIPHER_CTX* ctx,
  226. const WOLFSSL_EVP_CIPHER* type,
  227. const unsigned char* key,
  228. const unsigned char* iv)
  229. {
  230. return wolfSSL_EVP_CipherInit(ctx, type, (byte*)key, (byte*)iv, 1);
  231. }
  232. int wolfSSL_EVP_EncryptInit_ex(WOLFSSL_EVP_CIPHER_CTX* ctx,
  233. const WOLFSSL_EVP_CIPHER* type,
  234. WOLFSSL_ENGINE *impl,
  235. const unsigned char* key,
  236. const unsigned char* iv)
  237. {
  238. (void) impl;
  239. return wolfSSL_EVP_CipherInit(ctx, type, (byte*)key, (byte*)iv, 1);
  240. }
  241. int wolfSSL_EVP_DecryptInit(WOLFSSL_EVP_CIPHER_CTX* ctx,
  242. const WOLFSSL_EVP_CIPHER* type,
  243. const unsigned char* key,
  244. const unsigned char* iv)
  245. {
  246. WOLFSSL_ENTER("wolfSSL_EVP_CipherInit");
  247. return wolfSSL_EVP_CipherInit(ctx, type, (byte*)key, (byte*)iv, 0);
  248. }
  249. int wolfSSL_EVP_DecryptInit_ex(WOLFSSL_EVP_CIPHER_CTX* ctx,
  250. const WOLFSSL_EVP_CIPHER* type,
  251. WOLFSSL_ENGINE *impl,
  252. const unsigned char* key,
  253. const unsigned char* iv)
  254. {
  255. (void) impl;
  256. WOLFSSL_ENTER("wolfSSL_EVP_DecryptInit");
  257. return wolfSSL_EVP_CipherInit(ctx, type, (byte*)key, (byte*)iv, 0);
  258. }
  259. WOLFSSL_EVP_CIPHER_CTX *wolfSSL_EVP_CIPHER_CTX_new(void)
  260. {
  261. WOLFSSL_EVP_CIPHER_CTX *ctx = (WOLFSSL_EVP_CIPHER_CTX*)XMALLOC(sizeof *ctx,
  262. NULL, DYNAMIC_TYPE_TMP_BUFFER);
  263. if (ctx) {
  264. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_new");
  265. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  266. }
  267. return ctx;
  268. }
  269. void wolfSSL_EVP_CIPHER_CTX_free(WOLFSSL_EVP_CIPHER_CTX *ctx)
  270. {
  271. if (ctx) {
  272. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_free");
  273. wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  274. XFREE(ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  275. }
  276. }
  277. int wolfSSL_EVP_CIPHER_CTX_reset(WOLFSSL_EVP_CIPHER_CTX *ctx)
  278. {
  279. int ret = WOLFSSL_FAILURE;
  280. if (ctx != NULL) {
  281. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_reset");
  282. wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
  283. ret = WOLFSSL_SUCCESS;
  284. }
  285. return ret;
  286. }
  287. unsigned long wolfSSL_EVP_CIPHER_CTX_mode(const WOLFSSL_EVP_CIPHER_CTX *ctx)
  288. {
  289. if (ctx == NULL) return 0;
  290. return ctx->flags & WOLFSSL_EVP_CIPH_MODE;
  291. }
  292. unsigned long wolfSSL_EVP_CIPHER_CTX_flags(const WOLFSSL_EVP_CIPHER_CTX *ctx)
  293. {
  294. if (ctx == NULL) return 0;
  295. return ctx->flags;
  296. }
  297. int wolfSSL_EVP_EncryptFinal(WOLFSSL_EVP_CIPHER_CTX *ctx,
  298. unsigned char *out, int *outl)
  299. {
  300. if (ctx && ctx->enc) {
  301. WOLFSSL_ENTER("wolfSSL_EVP_EncryptFinal");
  302. return wolfSSL_EVP_CipherFinal(ctx, out, outl);
  303. }
  304. else
  305. return WOLFSSL_FAILURE;
  306. }
  307. int wolfSSL_EVP_CipherInit_ex(WOLFSSL_EVP_CIPHER_CTX* ctx,
  308. const WOLFSSL_EVP_CIPHER* type,
  309. WOLFSSL_ENGINE *impl,
  310. const unsigned char* key,
  311. const unsigned char* iv,
  312. int enc)
  313. {
  314. (void)impl;
  315. return wolfSSL_EVP_CipherInit(ctx, type, key, iv, enc);
  316. }
  317. int wolfSSL_EVP_EncryptFinal_ex(WOLFSSL_EVP_CIPHER_CTX *ctx,
  318. unsigned char *out, int *outl)
  319. {
  320. if (ctx && ctx->enc) {
  321. WOLFSSL_ENTER("wolfSSL_EVP_EncryptFinal_ex");
  322. return wolfSSL_EVP_CipherFinal(ctx, out, outl);
  323. }
  324. else
  325. return WOLFSSL_FAILURE;
  326. }
  327. int wolfSSL_EVP_DecryptFinal(WOLFSSL_EVP_CIPHER_CTX *ctx,
  328. unsigned char *out, int *outl)
  329. {
  330. if (ctx && !ctx->enc) {
  331. WOLFSSL_ENTER("wolfSSL_EVP_DecryptFinal");
  332. return wolfSSL_EVP_CipherFinal(ctx, out, outl);
  333. }
  334. else {
  335. return WOLFSSL_FAILURE;
  336. }
  337. }
  338. int wolfSSL_EVP_DecryptFinal_ex(WOLFSSL_EVP_CIPHER_CTX *ctx,
  339. unsigned char *out, int *outl)
  340. {
  341. if (ctx && !ctx->enc) {
  342. WOLFSSL_ENTER("wolfSSL_EVP_DecryptFinal_ex");
  343. return wolfSSL_EVP_CipherFinal(ctx, out, outl);
  344. }
  345. else {
  346. return WOLFSSL_FAILURE;
  347. }
  348. }
  349. int wolfSSL_EVP_DigestInit_ex(WOLFSSL_EVP_MD_CTX* ctx,
  350. const WOLFSSL_EVP_MD* type,
  351. WOLFSSL_ENGINE *impl)
  352. {
  353. (void) impl;
  354. WOLFSSL_ENTER("wolfSSL_EVP_DigestInit_ex");
  355. return wolfSSL_EVP_DigestInit(ctx, type);
  356. }
  357. #ifdef DEBUG_WOLFSSL_EVP
  358. #define PRINT_BUF(b, sz) { int _i; for(_i=0; _i<(sz); _i++) { \
  359. printf("%02x(%c),", (b)[_i], (b)[_i]); if ((_i+1)%8==0)printf("\n");}}
  360. #else
  361. #define PRINT_BUF(b, sz)
  362. #endif
  363. static int fillBuff(WOLFSSL_EVP_CIPHER_CTX *ctx, const unsigned char *in, int sz)
  364. {
  365. int fill;
  366. if (sz > 0) {
  367. if ((sz+ctx->bufUsed) > ctx->block_size) {
  368. fill = ctx->block_size - ctx->bufUsed;
  369. } else {
  370. fill = sz;
  371. }
  372. XMEMCPY(&(ctx->buf[ctx->bufUsed]), in, fill);
  373. ctx->bufUsed += fill;
  374. return fill;
  375. } else return 0;
  376. }
  377. static int evpCipherBlock(WOLFSSL_EVP_CIPHER_CTX *ctx,
  378. unsigned char *out,
  379. const unsigned char *in, int inl)
  380. {
  381. int ret = 0;
  382. switch (ctx->cipherType) {
  383. #if !defined(NO_AES)
  384. #if defined(HAVE_AES_CBC)
  385. case AES_128_CBC_TYPE:
  386. case AES_192_CBC_TYPE:
  387. case AES_256_CBC_TYPE:
  388. if (ctx->enc)
  389. ret = wc_AesCbcEncrypt(&ctx->cipher.aes, out, in, inl);
  390. else
  391. ret = wc_AesCbcDecrypt(&ctx->cipher.aes, out, in, inl);
  392. break;
  393. #endif
  394. #if defined(WOLFSSL_AES_COUNTER)
  395. case AES_128_CTR_TYPE:
  396. case AES_192_CTR_TYPE:
  397. case AES_256_CTR_TYPE:
  398. ret = wc_AesCtrEncrypt(&ctx->cipher.aes, out, in, inl);
  399. break;
  400. #endif
  401. #if defined(HAVE_AES_ECB)
  402. case AES_128_ECB_TYPE:
  403. case AES_192_ECB_TYPE:
  404. case AES_256_ECB_TYPE:
  405. if (ctx->enc)
  406. ret = wc_AesEcbEncrypt(&ctx->cipher.aes, out, in, inl);
  407. else
  408. ret = wc_AesEcbDecrypt(&ctx->cipher.aes, out, in, inl);
  409. break;
  410. #endif
  411. #if defined(WOLFSSL_AES_OFB)
  412. case AES_128_OFB_TYPE:
  413. case AES_192_OFB_TYPE:
  414. case AES_256_OFB_TYPE:
  415. if (ctx->enc)
  416. ret = wc_AesOfbEncrypt(&ctx->cipher.aes, out, in, inl);
  417. else
  418. ret = wc_AesOfbDecrypt(&ctx->cipher.aes, out, in, inl);
  419. break;
  420. #endif
  421. #if defined(WOLFSSL_AES_CFB)
  422. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  423. case AES_128_CFB1_TYPE:
  424. case AES_192_CFB1_TYPE:
  425. case AES_256_CFB1_TYPE:
  426. if (ctx->enc)
  427. ret = wc_AesCfb1Encrypt(&ctx->cipher.aes, out, in,
  428. inl * WOLFSSL_BIT_SIZE);
  429. else
  430. ret = wc_AesCfb1Decrypt(&ctx->cipher.aes, out, in,
  431. inl * WOLFSSL_BIT_SIZE);
  432. break;
  433. case AES_128_CFB8_TYPE:
  434. case AES_192_CFB8_TYPE:
  435. case AES_256_CFB8_TYPE:
  436. if (ctx->enc)
  437. ret = wc_AesCfb8Encrypt(&ctx->cipher.aes, out, in, inl);
  438. else
  439. ret = wc_AesCfb8Decrypt(&ctx->cipher.aes, out, in, inl);
  440. break;
  441. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  442. case AES_128_CFB128_TYPE:
  443. case AES_192_CFB128_TYPE:
  444. case AES_256_CFB128_TYPE:
  445. if (ctx->enc)
  446. ret = wc_AesCfbEncrypt(&ctx->cipher.aes, out, in, inl);
  447. else
  448. ret = wc_AesCfbDecrypt(&ctx->cipher.aes, out, in, inl);
  449. break;
  450. #endif
  451. #if defined(WOLFSSL_AES_XTS)
  452. case AES_128_XTS_TYPE:
  453. case AES_256_XTS_TYPE:
  454. if (ctx->enc)
  455. ret = wc_AesXtsEncrypt(&ctx->cipher.xts, out, in, inl,
  456. ctx->iv, ctx->ivSz);
  457. else
  458. ret = wc_AesXtsDecrypt(&ctx->cipher.xts, out, in, inl,
  459. ctx->iv, ctx->ivSz);
  460. break;
  461. #endif
  462. #endif /* !NO_AES */
  463. #ifndef NO_DES3
  464. case DES_CBC_TYPE:
  465. if (ctx->enc)
  466. ret = wc_Des_CbcEncrypt(&ctx->cipher.des, out, in, inl);
  467. else
  468. ret = wc_Des_CbcDecrypt(&ctx->cipher.des, out, in, inl);
  469. break;
  470. case DES_EDE3_CBC_TYPE:
  471. if (ctx->enc)
  472. ret = wc_Des3_CbcEncrypt(&ctx->cipher.des3, out, in, inl);
  473. else
  474. ret = wc_Des3_CbcDecrypt(&ctx->cipher.des3, out, in, inl);
  475. break;
  476. #if defined(WOLFSSL_DES_ECB)
  477. case DES_ECB_TYPE:
  478. ret = wc_Des_EcbEncrypt(&ctx->cipher.des, out, in, inl);
  479. break;
  480. case DES_EDE3_ECB_TYPE:
  481. ret = wc_Des3_EcbEncrypt(&ctx->cipher.des3, out, in, inl);
  482. break;
  483. #endif
  484. #endif
  485. #ifndef NO_RC4
  486. case ARC4_TYPE:
  487. wc_Arc4Process(&ctx->cipher.arc4, out, in, inl);
  488. break;
  489. #endif
  490. default:
  491. ret = WOLFSSL_FAILURE;
  492. }
  493. (void)in;
  494. (void)inl;
  495. (void)out;
  496. return (ret == 0) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  497. }
  498. #if defined(HAVE_AESGCM)
  499. #ifndef WOLFSSL_AESGCM_STREAM
  500. static int wolfSSL_EVP_CipherUpdate_GCM_AAD(WOLFSSL_EVP_CIPHER_CTX *ctx,
  501. const unsigned char *in, int inl) {
  502. if (in && inl > 0) {
  503. byte* tmp = (byte*)XREALLOC(ctx->gcmAuthIn,
  504. ctx->gcmAuthInSz + inl, NULL, DYNAMIC_TYPE_OPENSSL);
  505. if (tmp) {
  506. ctx->gcmAuthIn = tmp;
  507. XMEMCPY(ctx->gcmAuthIn + ctx->gcmAuthInSz, in, inl);
  508. ctx->gcmAuthInSz += inl;
  509. }
  510. else {
  511. WOLFSSL_MSG("realloc error");
  512. return MEMORY_E;
  513. }
  514. }
  515. return 0;
  516. }
  517. #endif /* WOLFSSL_AESGCM_STREAM */
  518. static int wolfSSL_EVP_CipherUpdate_GCM(WOLFSSL_EVP_CIPHER_CTX *ctx,
  519. unsigned char *out, int *outl,
  520. const unsigned char *in, int inl)
  521. {
  522. #ifndef WOLFSSL_AESGCM_STREAM
  523. int ret = 0;
  524. *outl = inl;
  525. if (out) {
  526. /* Buffer input for one-shot API */
  527. if (inl > 0) {
  528. byte* tmp;
  529. tmp = (byte*)XREALLOC(ctx->gcmBuffer,
  530. ctx->gcmBufferLen + inl, NULL,
  531. DYNAMIC_TYPE_OPENSSL);
  532. if (tmp) {
  533. XMEMCPY(tmp + ctx->gcmBufferLen, in, inl);
  534. ctx->gcmBufferLen += inl;
  535. ctx->gcmBuffer = tmp;
  536. *outl = 0;
  537. }
  538. else {
  539. ret = MEMORY_E;
  540. }
  541. }
  542. }
  543. else {
  544. ret = wolfSSL_EVP_CipherUpdate_GCM_AAD(ctx, in, inl);
  545. }
  546. if (ret != 0) {
  547. *outl = 0;
  548. return WOLFSSL_FAILURE;
  549. }
  550. return WOLFSSL_SUCCESS;
  551. #else
  552. int ret;
  553. /* When out is NULL then this is AAD. */
  554. if (out == NULL) {
  555. if (ctx->enc) {
  556. ret = wc_AesGcmEncryptUpdate(&ctx->cipher.aes, NULL, NULL, 0, in,
  557. inl);
  558. }
  559. else {
  560. ret = wc_AesGcmDecryptUpdate(&ctx->cipher.aes, NULL, NULL, 0, in,
  561. inl);
  562. }
  563. }
  564. /* When out is not NULL then this is plaintext/cipher text. */
  565. else {
  566. if (ctx->enc) {
  567. ret = wc_AesGcmEncryptUpdate(&ctx->cipher.aes, out, in, inl, NULL,
  568. 0);
  569. }
  570. else {
  571. ret = wc_AesGcmDecryptUpdate(&ctx->cipher.aes, out, in, inl, NULL,
  572. 0);
  573. }
  574. }
  575. *outl = inl;
  576. if (ret == 0) {
  577. ret = WOLFSSL_SUCCESS;
  578. }
  579. else {
  580. ret = WOLFSSL_FAILURE;
  581. }
  582. return ret;
  583. #endif /* WOLFSSL_AESGCM_STREAM */
  584. }
  585. #endif /* HAVE_AESGCM */
  586. /* returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure */
  587. int wolfSSL_EVP_CipherUpdate(WOLFSSL_EVP_CIPHER_CTX *ctx,
  588. unsigned char *out, int *outl,
  589. const unsigned char *in, int inl)
  590. {
  591. int blocks;
  592. int fill;
  593. WOLFSSL_ENTER("wolfSSL_EVP_CipherUpdate");
  594. if (inl == 0 && in == NULL ) {
  595. /* Nothing to do in this case. Just return. */
  596. return WOLFSSL_SUCCESS;
  597. }
  598. if ((ctx == NULL) || (inl < 0) || (outl == NULL) || (in == NULL)) {
  599. WOLFSSL_MSG("Bad argument");
  600. return WOLFSSL_FAILURE;
  601. }
  602. *outl = 0;
  603. switch (ctx->cipherType) {
  604. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  605. case AES_128_GCM_TYPE:
  606. case AES_192_GCM_TYPE:
  607. case AES_256_GCM_TYPE:
  608. /* if out == NULL, in/inl contains the additional authenticated data * for GCM */
  609. return wolfSSL_EVP_CipherUpdate_GCM(ctx, out, outl, in, inl);
  610. #endif /* !defined(NO_AES) && defined(HAVE_AESGCM) */
  611. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  612. case CHACHA20_POLY1305_TYPE:
  613. if (out == NULL) {
  614. if (wc_ChaCha20Poly1305_UpdateAad(&ctx->cipher.chachaPoly, in,
  615. inl) != 0) {
  616. WOLFSSL_MSG("wc_ChaCha20Poly1305_UpdateAad failed");
  617. return WOLFSSL_FAILURE;
  618. }
  619. else {
  620. *outl = inl;
  621. return WOLFSSL_SUCCESS;
  622. }
  623. }
  624. else {
  625. if (wc_ChaCha20Poly1305_UpdateData(&ctx->cipher.chachaPoly, in,
  626. out, inl) != 0) {
  627. WOLFSSL_MSG("wc_ChaCha20Poly1305_UpdateData failed");
  628. return WOLFSSL_FAILURE;
  629. }
  630. else {
  631. *outl = inl;
  632. return WOLFSSL_SUCCESS;
  633. }
  634. }
  635. #endif
  636. default:
  637. /* fall-through */
  638. break;
  639. }
  640. if (out == NULL) {
  641. return WOLFSSL_FAILURE;
  642. }
  643. /* if(inl == 0)wolfSSL_EVP_CipherUpdate_GCM to get tag */
  644. if (inl == 0) {
  645. return WOLFSSL_SUCCESS;
  646. }
  647. if (ctx->bufUsed > 0) { /* concatenate them if there is anything */
  648. fill = fillBuff(ctx, in, inl);
  649. inl -= fill;
  650. in += fill;
  651. }
  652. /* check if the buff is full, and if so flash it out */
  653. if (ctx->bufUsed == ctx->block_size) {
  654. byte* output = out;
  655. /* During decryption we save the last block to check padding on Final.
  656. * Update the last block stored if one has already been stored */
  657. if (ctx->enc == 0) {
  658. if (ctx->lastUsed == 1) {
  659. XMEMCPY(out, ctx->lastBlock, ctx->block_size);
  660. *outl+= ctx->block_size;
  661. out += ctx->block_size;
  662. }
  663. output = ctx->lastBlock; /* redirect output to last block buffer */
  664. ctx->lastUsed = 1;
  665. }
  666. PRINT_BUF(ctx->buf, ctx->block_size);
  667. if (evpCipherBlock(ctx, output, ctx->buf, ctx->block_size) == 0) {
  668. return WOLFSSL_FAILURE;
  669. }
  670. PRINT_BUF(out, ctx->block_size);
  671. ctx->bufUsed = 0;
  672. /* if doing encryption update the new output block, decryption will
  673. * always have the last block saved for when Final is called */
  674. if ((ctx->enc != 0)) {
  675. *outl+= ctx->block_size;
  676. out += ctx->block_size;
  677. }
  678. }
  679. blocks = inl / ctx->block_size;
  680. if (blocks > 0) {
  681. /* During decryption we save the last block to check padding on Final.
  682. * Update the last block stored if one has already been stored */
  683. if ((ctx->enc == 0) && (ctx->lastUsed == 1)) {
  684. PRINT_BUF(ctx->lastBlock, ctx->block_size);
  685. XMEMCPY(out, ctx->lastBlock, ctx->block_size);
  686. *outl += ctx->block_size;
  687. out += ctx->block_size;
  688. ctx->lastUsed = 0;
  689. }
  690. /* process blocks */
  691. if (evpCipherBlock(ctx, out, in, blocks * ctx->block_size) == 0) {
  692. return WOLFSSL_FAILURE;
  693. }
  694. PRINT_BUF(in, ctx->block_size*blocks);
  695. PRINT_BUF(out,ctx->block_size*blocks);
  696. inl -= ctx->block_size * blocks;
  697. in += ctx->block_size * blocks;
  698. if (ctx->enc == 0) {
  699. if ((ctx->flags & WOLFSSL_EVP_CIPH_NO_PADDING) ||
  700. (ctx->block_size == 1)) {
  701. ctx->lastUsed = 0;
  702. *outl += ctx->block_size * blocks;
  703. } else {
  704. /* in the case of decryption and padding, store the last block
  705. * here in order to verify the padding when Final is called */
  706. if (inl == 0) { /* if not 0 then we know leftovers are checked*/
  707. ctx->lastUsed = 1;
  708. blocks = blocks - 1; /* save last block to check padding in
  709. * EVP_CipherFinal call */
  710. XMEMCPY(ctx->lastBlock, &out[ctx->block_size * blocks],
  711. ctx->block_size);
  712. }
  713. *outl += ctx->block_size * blocks;
  714. }
  715. } else {
  716. *outl += ctx->block_size * blocks;
  717. }
  718. }
  719. if (inl > 0) {
  720. /* put fraction into buff */
  721. fillBuff(ctx, in, inl);
  722. /* no increase of outl */
  723. }
  724. (void)out; /* silence warning in case not read */
  725. return WOLFSSL_SUCCESS;
  726. }
  727. static void padBlock(WOLFSSL_EVP_CIPHER_CTX *ctx)
  728. {
  729. int i;
  730. for (i = ctx->bufUsed; i < ctx->block_size; i++)
  731. ctx->buf[i] = (byte)(ctx->block_size - ctx->bufUsed);
  732. }
  733. static int checkPad(WOLFSSL_EVP_CIPHER_CTX *ctx, unsigned char *buff)
  734. {
  735. int i;
  736. int n;
  737. n = buff[ctx->block_size-1];
  738. if (n > ctx->block_size) return -1;
  739. for (i = 0; i < n; i++) {
  740. if (buff[ctx->block_size-i-1] != n)
  741. return -1;
  742. }
  743. return ctx->block_size - n;
  744. }
  745. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) \
  746. || FIPS_VERSION_GE(2,0))
  747. static WC_INLINE void IncCtr(byte* ctr, word32 ctrSz)
  748. {
  749. int i;
  750. for (i = ctrSz-1; i >= 0; i--) {
  751. if (++ctr[i])
  752. break;
  753. }
  754. }
  755. #endif /* HAVE_AESGCM && ((!HAVE_FIPS && !HAVE_SELFTEST) ||
  756. * HAVE_FIPS_VERSION >= 2 */
  757. int wolfSSL_EVP_CipherFinal(WOLFSSL_EVP_CIPHER_CTX *ctx, unsigned char *out,
  758. int *outl)
  759. {
  760. int fl;
  761. int ret = WOLFSSL_SUCCESS;
  762. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) \
  763. || FIPS_VERSION_GE(2,0))
  764. byte tmp = 0;
  765. #endif
  766. if (!ctx || !outl)
  767. return WOLFSSL_FAILURE;
  768. WOLFSSL_ENTER("wolfSSL_EVP_CipherFinal");
  769. switch (ctx->cipherType) {
  770. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) \
  771. || FIPS_VERSION_GE(2,0))
  772. case AES_128_GCM_TYPE:
  773. case AES_192_GCM_TYPE:
  774. case AES_256_GCM_TYPE:
  775. #ifndef WOLFSSL_AESGCM_STREAM
  776. if ((ctx->gcmBuffer && ctx->gcmBufferLen > 0)
  777. || (ctx->gcmBufferLen == 0)) {
  778. if (ctx->enc)
  779. ret = wc_AesGcmEncrypt(&ctx->cipher.aes, out,
  780. ctx->gcmBuffer, ctx->gcmBufferLen,
  781. ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
  782. ctx->gcmAuthIn, ctx->gcmAuthInSz);
  783. else
  784. ret = wc_AesGcmDecrypt(&ctx->cipher.aes, out,
  785. ctx->gcmBuffer, ctx->gcmBufferLen,
  786. ctx->iv, ctx->ivSz, ctx->authTag, ctx->authTagSz,
  787. ctx->gcmAuthIn, ctx->gcmAuthInSz);
  788. if (ret == 0) {
  789. ret = WOLFSSL_SUCCESS;
  790. *outl = ctx->gcmBufferLen;
  791. }
  792. else {
  793. ret = WOLFSSL_FAILURE;
  794. *outl = 0;
  795. }
  796. XFREE(ctx->gcmBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  797. ctx->gcmBuffer = NULL;
  798. ctx->gcmBufferLen = 0;
  799. if (ctx->gcmIncIv) {
  800. IncCtr((byte*)ctx->cipher.aes.reg,
  801. ctx->cipher.aes.nonceSz);
  802. ctx->gcmIncIv = 0;
  803. }
  804. }
  805. else {
  806. *outl = 0;
  807. }
  808. #else
  809. /* No data to return - all handled in Update. */
  810. *outl = 0;
  811. if (ctx->enc) {
  812. ret = wc_AesGcmEncryptFinal(&ctx->cipher.aes, ctx->authTag,
  813. ctx->authTagSz);
  814. }
  815. else {
  816. ret = wc_AesGcmDecryptFinal(&ctx->cipher.aes, ctx->authTag,
  817. ctx->authTagSz);
  818. if (ctx->gcmIncIv) {
  819. IncCtr((byte*)ctx->cipher.aes.reg, ctx->cipher.aes.nonceSz);
  820. }
  821. }
  822. if (ret == 0) {
  823. /* Reinitialize for subsequent wolfSSL_EVP_Cipher calls. */
  824. if (wc_AesGcmInit(&ctx->cipher.aes, NULL, 0,
  825. (byte*)ctx->cipher.aes.reg,
  826. ctx->ivSz) != 0) {
  827. WOLFSSL_MSG("wc_AesGcmInit failed");
  828. ret = WOLFSSL_FAILURE;
  829. }
  830. else {
  831. ret = WOLFSSL_SUCCESS;
  832. }
  833. }
  834. else {
  835. ret = WOLFSSL_FAILURE;
  836. }
  837. #endif /* WOLFSSL_AESGCM_STREAM */
  838. if (ret == WOLFSSL_SUCCESS) {
  839. if (ctx->gcmIncIv) {
  840. ctx->gcmIncIv = 0;
  841. }
  842. else {
  843. /* Clear IV, since IV reuse is not recommended for AES GCM. */
  844. XMEMSET(ctx->iv, 0, AES_BLOCK_SIZE);
  845. }
  846. if (wolfSSL_StoreExternalIV(ctx) != WOLFSSL_SUCCESS) {
  847. ret = WOLFSSL_FAILURE;
  848. }
  849. }
  850. break;
  851. #endif /* HAVE_AESGCM && ((!HAVE_FIPS && !HAVE_SELFTEST) ||
  852. * HAVE_FIPS_VERSION >= 2 */
  853. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  854. case CHACHA20_POLY1305_TYPE:
  855. if (wc_ChaCha20Poly1305_Final(&ctx->cipher.chachaPoly,
  856. ctx->authTag) != 0) {
  857. WOLFSSL_MSG("wc_ChaCha20Poly1305_Final failed");
  858. return WOLFSSL_FAILURE;
  859. }
  860. else {
  861. *outl = 0;
  862. return WOLFSSL_SUCCESS;
  863. }
  864. #endif
  865. default:
  866. if (!out)
  867. return WOLFSSL_FAILURE;
  868. if (ctx->flags & WOLFSSL_EVP_CIPH_NO_PADDING) {
  869. if (ctx->bufUsed != 0) return WOLFSSL_FAILURE;
  870. *outl = 0;
  871. }
  872. else if (ctx->enc) {
  873. if (ctx->block_size == 1) {
  874. *outl = 0;
  875. }
  876. else if ((ctx->bufUsed >= 0) && (ctx->block_size != 1)) {
  877. padBlock(ctx);
  878. PRINT_BUF(ctx->buf, ctx->block_size);
  879. if (evpCipherBlock(ctx, out, ctx->buf, ctx->block_size) == 0) {
  880. WOLFSSL_MSG("Final Cipher Block failed");
  881. ret = WOLFSSL_FAILURE;
  882. }
  883. else {
  884. PRINT_BUF(out, ctx->block_size);
  885. *outl = ctx->block_size;
  886. }
  887. }
  888. }
  889. else {
  890. if (ctx->block_size == 1) {
  891. *outl = 0;
  892. }
  893. else if ((ctx->bufUsed % ctx->block_size) != 0) {
  894. *outl = 0;
  895. /* not enough padding for decrypt */
  896. WOLFSSL_MSG("Final Cipher Block not enough padding");
  897. ret = WOLFSSL_FAILURE;
  898. }
  899. else if (ctx->lastUsed) {
  900. PRINT_BUF(ctx->lastBlock, ctx->block_size);
  901. if ((fl = checkPad(ctx, ctx->lastBlock)) >= 0) {
  902. XMEMCPY(out, ctx->lastBlock, fl);
  903. *outl = fl;
  904. if (ctx->lastUsed == 0 && ctx->bufUsed == 0) {
  905. /* return error in cases where the block length is
  906. * incorrect */
  907. WOLFSSL_MSG("Final Cipher Block bad length");
  908. ret = WOLFSSL_FAILURE;
  909. }
  910. }
  911. else {
  912. ret = WOLFSSL_FAILURE;
  913. }
  914. }
  915. else if (ctx->lastUsed == 0 && ctx->bufUsed == 0) {
  916. /* return error in cases where the block length is
  917. * incorrect */
  918. ret = WOLFSSL_FAILURE;
  919. }
  920. }
  921. break;
  922. }
  923. if (ret == WOLFSSL_SUCCESS) {
  924. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) \
  925. || FIPS_VERSION_GE(2,0))
  926. /*
  927. * This flag needs to retain its value between wolfSSL_EVP_CipherFinal
  928. * calls. wolfSSL_EVP_CipherInit will clear it, so we save and restore
  929. * it here.
  930. */
  931. if (ctx->cipherType == AES_128_GCM_TYPE ||
  932. ctx->cipherType == AES_192_GCM_TYPE ||
  933. ctx->cipherType == AES_256_GCM_TYPE) {
  934. tmp = ctx->gcmIvGenEnable;
  935. }
  936. #endif
  937. /* reset cipher state after final */
  938. ret = wolfSSL_EVP_CipherInit(ctx, NULL, NULL, NULL, -1);
  939. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) \
  940. || FIPS_VERSION_GE(2,0))
  941. if (ctx->cipherType == AES_128_GCM_TYPE ||
  942. ctx->cipherType == AES_192_GCM_TYPE ||
  943. ctx->cipherType == AES_256_GCM_TYPE) {
  944. ctx->gcmIvGenEnable = tmp;
  945. }
  946. #endif
  947. }
  948. return ret;
  949. }
  950. #ifdef WOLFSSL_EVP_DECRYPT_LEGACY
  951. /* This is a version of DecryptFinal to work with data encrypted with
  952. * wolfSSL_EVP_EncryptFinal() with the broken padding. (pre-v3.12.0)
  953. * Only call this after wolfSSL_EVP_CipherFinal() fails on a decrypt.
  954. * Note, you don't know if the padding is good or bad with the old
  955. * encrypt, but it is likely to be or bad. It will update the output
  956. * length with the block_size so the last block is still captured. */
  957. int wolfSSL_EVP_DecryptFinal_legacy(WOLFSSL_EVP_CIPHER_CTX *ctx,
  958. unsigned char *out, int *outl)
  959. {
  960. int fl;
  961. if (ctx == NULL || out == NULL || outl == NULL)
  962. return BAD_FUNC_ARG;
  963. WOLFSSL_ENTER("wolfSSL_EVP_DecryptFinal_legacy");
  964. if (ctx->block_size == 1) {
  965. *outl = 0;
  966. return WOLFSSL_SUCCESS;
  967. }
  968. if ((ctx->bufUsed % ctx->block_size) != 0) {
  969. *outl = 0;
  970. /* not enough padding for decrypt */
  971. return WOLFSSL_FAILURE;
  972. }
  973. /* The original behavior of CipherFinal() was like it is now,
  974. * but checkPad would return 0 in case of a bad pad. It would
  975. * treat the pad as 0, and leave the data in the output buffer,
  976. * and not try to copy anything. This converts checkPad's -1 error
  977. * code to block_size.
  978. */
  979. if (ctx->lastUsed) {
  980. PRINT_BUF(ctx->lastBlock, ctx->block_size);
  981. if ((fl = checkPad(ctx, ctx->lastBlock)) < 0) {
  982. fl = ctx->block_size;
  983. }
  984. else {
  985. XMEMCPY(out, ctx->lastBlock, fl);
  986. }
  987. *outl = fl;
  988. }
  989. /* return error in cases where the block length is incorrect */
  990. if (ctx->lastUsed == 0 && ctx->bufUsed == 0) {
  991. return WOLFSSL_FAILURE;
  992. }
  993. return WOLFSSL_SUCCESS;
  994. }
  995. #endif
  996. int wolfSSL_EVP_CIPHER_CTX_block_size(const WOLFSSL_EVP_CIPHER_CTX *ctx)
  997. {
  998. if (ctx == NULL) return BAD_FUNC_ARG;
  999. switch (ctx->cipherType) {
  1000. #if !defined(NO_AES) || !defined(NO_DES3)
  1001. #if !defined(NO_AES)
  1002. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  1003. case AES_128_CBC_TYPE:
  1004. case AES_192_CBC_TYPE:
  1005. case AES_256_CBC_TYPE:
  1006. #endif
  1007. #if defined(HAVE_AESGCM)
  1008. case AES_128_GCM_TYPE:
  1009. case AES_192_GCM_TYPE:
  1010. case AES_256_GCM_TYPE:
  1011. #endif
  1012. #if defined(WOLFSSL_AES_COUNTER)
  1013. case AES_128_CTR_TYPE:
  1014. case AES_192_CTR_TYPE:
  1015. case AES_256_CTR_TYPE:
  1016. #endif
  1017. #if defined(WOLFSSL_AES_CFB)
  1018. case AES_128_CFB1_TYPE:
  1019. case AES_192_CFB1_TYPE:
  1020. case AES_256_CFB1_TYPE:
  1021. case AES_128_CFB8_TYPE:
  1022. case AES_192_CFB8_TYPE:
  1023. case AES_256_CFB8_TYPE:
  1024. case AES_128_CFB128_TYPE:
  1025. case AES_192_CFB128_TYPE:
  1026. case AES_256_CFB128_TYPE:
  1027. #endif
  1028. #if defined(WOLFSSL_AES_OFB)
  1029. case AES_128_OFB_TYPE:
  1030. case AES_192_OFB_TYPE:
  1031. case AES_256_OFB_TYPE:
  1032. #endif
  1033. #if defined(WOLFSSL_AES_XTS)
  1034. case AES_128_XTS_TYPE:
  1035. case AES_256_XTS_TYPE:
  1036. #endif
  1037. case AES_128_ECB_TYPE:
  1038. case AES_192_ECB_TYPE:
  1039. case AES_256_ECB_TYPE:
  1040. #endif /* !NO_AES */
  1041. #ifndef NO_DES3
  1042. case DES_CBC_TYPE:
  1043. case DES_ECB_TYPE:
  1044. case DES_EDE3_CBC_TYPE:
  1045. case DES_EDE3_ECB_TYPE:
  1046. #endif
  1047. return ctx->block_size;
  1048. #endif /* !NO_AES || !NO_DES3 */
  1049. default:
  1050. return 0;
  1051. }
  1052. }
  1053. static unsigned int cipherType(const WOLFSSL_EVP_CIPHER *cipher)
  1054. {
  1055. if (cipher == NULL) return 0; /* dummy for #ifdef */
  1056. #ifndef NO_DES3
  1057. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_DES_CBC))
  1058. return DES_CBC_TYPE;
  1059. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_DES_EDE3_CBC))
  1060. return DES_EDE3_CBC_TYPE;
  1061. #if !defined(NO_DES3)
  1062. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_DES_ECB))
  1063. return DES_ECB_TYPE;
  1064. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_DES_EDE3_ECB))
  1065. return DES_EDE3_ECB_TYPE;
  1066. #endif /* NO_DES3 && HAVE_AES_ECB */
  1067. #endif
  1068. #if !defined(NO_AES)
  1069. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  1070. #ifdef WOLFSSL_AES_128
  1071. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_CBC))
  1072. return AES_128_CBC_TYPE;
  1073. #endif
  1074. #ifdef WOLFSSL_AES_192
  1075. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_192_CBC))
  1076. return AES_192_CBC_TYPE;
  1077. #endif
  1078. #ifdef WOLFSSL_AES_256
  1079. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_CBC))
  1080. return AES_256_CBC_TYPE;
  1081. #endif
  1082. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  1083. #if defined(HAVE_AESGCM)
  1084. #ifdef WOLFSSL_AES_128
  1085. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_GCM))
  1086. return AES_128_GCM_TYPE;
  1087. #endif
  1088. #ifdef WOLFSSL_AES_192
  1089. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_192_GCM))
  1090. return AES_192_GCM_TYPE;
  1091. #endif
  1092. #ifdef WOLFSSL_AES_256
  1093. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_GCM))
  1094. return AES_256_GCM_TYPE;
  1095. #endif
  1096. #endif /* HAVE_AESGCM */
  1097. #if defined(WOLFSSL_AES_COUNTER)
  1098. #ifdef WOLFSSL_AES_128
  1099. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_CTR))
  1100. return AES_128_CTR_TYPE;
  1101. #endif
  1102. #ifdef WOLFSSL_AES_192
  1103. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_192_CTR))
  1104. return AES_192_CTR_TYPE;
  1105. #endif
  1106. #ifdef WOLFSSL_AES_256
  1107. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_CTR))
  1108. return AES_256_CTR_TYPE;
  1109. #endif
  1110. #endif /* HAVE_AES_CBC */
  1111. #if defined(HAVE_AES_ECB)
  1112. #ifdef WOLFSSL_AES_128
  1113. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_ECB))
  1114. return AES_128_ECB_TYPE;
  1115. #endif
  1116. #ifdef WOLFSSL_AES_192
  1117. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_192_ECB))
  1118. return AES_192_ECB_TYPE;
  1119. #endif
  1120. #ifdef WOLFSSL_AES_256
  1121. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_ECB))
  1122. return AES_256_ECB_TYPE;
  1123. #endif
  1124. #endif /*HAVE_AES_CBC */
  1125. #if defined(WOLFSSL_AES_XTS)
  1126. #ifdef WOLFSSL_AES_128
  1127. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_XTS))
  1128. return AES_128_XTS_TYPE;
  1129. #endif
  1130. #ifdef WOLFSSL_AES_256
  1131. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_XTS))
  1132. return AES_256_XTS_TYPE;
  1133. #endif
  1134. #endif /* WOLFSSL_AES_XTS */
  1135. #if defined(WOLFSSL_AES_CFB)
  1136. #ifdef WOLFSSL_AES_128
  1137. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_CFB1))
  1138. return AES_128_CFB1_TYPE;
  1139. #endif
  1140. #ifdef WOLFSSL_AES_192
  1141. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_192_CFB1))
  1142. return AES_192_CFB1_TYPE;
  1143. #endif
  1144. #ifdef WOLFSSL_AES_256
  1145. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_CFB1))
  1146. return AES_256_CFB1_TYPE;
  1147. #endif
  1148. #ifdef WOLFSSL_AES_128
  1149. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_CFB8))
  1150. return AES_128_CFB8_TYPE;
  1151. #endif
  1152. #ifdef WOLFSSL_AES_192
  1153. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_192_CFB8))
  1154. return AES_192_CFB8_TYPE;
  1155. #endif
  1156. #ifdef WOLFSSL_AES_256
  1157. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_CFB8))
  1158. return AES_256_CFB8_TYPE;
  1159. #endif
  1160. #ifdef WOLFSSL_AES_128
  1161. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_CFB128))
  1162. return AES_128_CFB128_TYPE;
  1163. #endif
  1164. #ifdef WOLFSSL_AES_192
  1165. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_192_CFB128))
  1166. return AES_192_CFB128_TYPE;
  1167. #endif
  1168. #ifdef WOLFSSL_AES_256
  1169. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_CFB128))
  1170. return AES_256_CFB128_TYPE;
  1171. #endif
  1172. #endif /*HAVE_AES_CBC */
  1173. #if defined(WOLFSSL_AES_OFB)
  1174. #ifdef WOLFSSL_AES_128
  1175. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_128_OFB))
  1176. return AES_128_OFB_TYPE;
  1177. #endif
  1178. #ifdef WOLFSSL_AES_192
  1179. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_192_OFB))
  1180. return AES_192_OFB_TYPE;
  1181. #endif
  1182. #ifdef WOLFSSL_AES_256
  1183. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_AES_256_OFB))
  1184. return AES_256_OFB_TYPE;
  1185. #endif
  1186. #endif
  1187. #endif /* !NO_AES */
  1188. #ifndef NO_RC4
  1189. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_ARC4))
  1190. return ARC4_TYPE;
  1191. #endif
  1192. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1193. else if (EVP_CIPHER_TYPE_MATCHES(cipher, EVP_CHACHA20_POLY1305))
  1194. return CHACHA20_POLY1305_TYPE;
  1195. #endif
  1196. else return 0;
  1197. }
  1198. int wolfSSL_EVP_CIPHER_block_size(const WOLFSSL_EVP_CIPHER *cipher)
  1199. {
  1200. if (cipher == NULL) return BAD_FUNC_ARG;
  1201. switch (cipherType(cipher)) {
  1202. #if !defined(NO_AES)
  1203. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  1204. case AES_128_CBC_TYPE:
  1205. case AES_192_CBC_TYPE:
  1206. case AES_256_CBC_TYPE:
  1207. return AES_BLOCK_SIZE;
  1208. #endif
  1209. #if defined(HAVE_AESGCM)
  1210. case AES_128_GCM_TYPE:
  1211. case AES_192_GCM_TYPE:
  1212. case AES_256_GCM_TYPE:
  1213. return 1;
  1214. #endif
  1215. #if defined(WOLFSSL_AES_COUNTER)
  1216. case AES_128_CTR_TYPE:
  1217. case AES_192_CTR_TYPE:
  1218. case AES_256_CTR_TYPE:
  1219. return 1;
  1220. #endif
  1221. #if defined(HAVE_AES_ECB)
  1222. case AES_128_ECB_TYPE:
  1223. case AES_192_ECB_TYPE:
  1224. case AES_256_ECB_TYPE:
  1225. return AES_BLOCK_SIZE;
  1226. #endif
  1227. #if defined(WOLFSSL_AES_CFB)
  1228. case AES_128_CFB1_TYPE:
  1229. case AES_192_CFB1_TYPE:
  1230. case AES_256_CFB1_TYPE:
  1231. case AES_128_CFB8_TYPE:
  1232. case AES_192_CFB8_TYPE:
  1233. case AES_256_CFB8_TYPE:
  1234. case AES_128_CFB128_TYPE:
  1235. case AES_192_CFB128_TYPE:
  1236. case AES_256_CFB128_TYPE:
  1237. return 1;
  1238. #endif
  1239. #if defined(WOLFSSL_AES_OFB)
  1240. case AES_128_OFB_TYPE:
  1241. case AES_192_OFB_TYPE:
  1242. case AES_256_OFB_TYPE:
  1243. return 1;
  1244. #endif
  1245. #if defined(WOLFSSL_AES_XTS)
  1246. case AES_128_XTS_TYPE:
  1247. case AES_256_XTS_TYPE:
  1248. return 1;
  1249. #endif
  1250. #endif /* NO_AES */
  1251. #ifndef NO_RC4
  1252. case ARC4_TYPE:
  1253. return 1;
  1254. #endif
  1255. #ifndef NO_DES3
  1256. case DES_CBC_TYPE: return 8;
  1257. case DES_EDE3_CBC_TYPE: return 8;
  1258. case DES_ECB_TYPE: return 8;
  1259. case DES_EDE3_ECB_TYPE: return 8;
  1260. #endif
  1261. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1262. case CHACHA20_POLY1305_TYPE:
  1263. return 1;
  1264. #endif
  1265. default:
  1266. return 0;
  1267. }
  1268. }
  1269. unsigned long WOLFSSL_CIPHER_mode(const WOLFSSL_EVP_CIPHER *cipher)
  1270. {
  1271. switch (cipherType(cipher)) {
  1272. #if !defined(NO_AES)
  1273. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  1274. case AES_128_CBC_TYPE:
  1275. case AES_192_CBC_TYPE:
  1276. case AES_256_CBC_TYPE:
  1277. return WOLFSSL_EVP_CIPH_CBC_MODE;
  1278. #endif
  1279. #if defined(HAVE_AESGCM)
  1280. case AES_128_GCM_TYPE:
  1281. case AES_192_GCM_TYPE:
  1282. case AES_256_GCM_TYPE:
  1283. return WOLFSSL_EVP_CIPH_GCM_MODE |
  1284. WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER;
  1285. #endif
  1286. #if defined(WOLFSSL_AES_COUNTER)
  1287. case AES_128_CTR_TYPE:
  1288. case AES_192_CTR_TYPE:
  1289. case AES_256_CTR_TYPE:
  1290. return WOLFSSL_EVP_CIPH_CTR_MODE;
  1291. #endif
  1292. #if defined(WOLFSSL_AES_CFB)
  1293. case AES_128_CFB1_TYPE:
  1294. case AES_192_CFB1_TYPE:
  1295. case AES_256_CFB1_TYPE:
  1296. case AES_128_CFB8_TYPE:
  1297. case AES_192_CFB8_TYPE:
  1298. case AES_256_CFB8_TYPE:
  1299. case AES_128_CFB128_TYPE:
  1300. case AES_192_CFB128_TYPE:
  1301. case AES_256_CFB128_TYPE:
  1302. return WOLFSSL_EVP_CIPH_CFB_MODE;
  1303. #endif
  1304. #if defined(WOLFSSL_AES_OFB)
  1305. case AES_128_OFB_TYPE:
  1306. case AES_192_OFB_TYPE:
  1307. case AES_256_OFB_TYPE:
  1308. return WOLFSSL_EVP_CIPH_OFB_MODE;
  1309. #endif
  1310. #if defined(WOLFSSL_AES_XTS)
  1311. case AES_128_XTS_TYPE:
  1312. case AES_256_XTS_TYPE:
  1313. return WOLFSSL_EVP_CIPH_XTS_MODE;
  1314. #endif
  1315. case AES_128_ECB_TYPE:
  1316. case AES_192_ECB_TYPE:
  1317. case AES_256_ECB_TYPE:
  1318. return WOLFSSL_EVP_CIPH_ECB_MODE;
  1319. #endif /* !NO_AES */
  1320. #ifndef NO_DES3
  1321. case DES_CBC_TYPE:
  1322. case DES_EDE3_CBC_TYPE:
  1323. return WOLFSSL_EVP_CIPH_CBC_MODE;
  1324. case DES_ECB_TYPE:
  1325. case DES_EDE3_ECB_TYPE:
  1326. return WOLFSSL_EVP_CIPH_ECB_MODE;
  1327. #endif
  1328. #ifndef NO_RC4
  1329. case ARC4_TYPE:
  1330. return EVP_CIPH_STREAM_CIPHER;
  1331. #endif
  1332. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1333. case CHACHA20_POLY1305_TYPE:
  1334. return WOLFSSL_EVP_CIPH_STREAM_CIPHER |
  1335. WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER;
  1336. #endif
  1337. default:
  1338. return 0;
  1339. }
  1340. }
  1341. unsigned long WOLFSSL_EVP_CIPHER_mode(const WOLFSSL_EVP_CIPHER *cipher)
  1342. {
  1343. if (cipher == NULL) return 0;
  1344. return WOLFSSL_CIPHER_mode(cipher) & WOLFSSL_EVP_CIPH_MODE;
  1345. }
  1346. void wolfSSL_EVP_CIPHER_CTX_set_flags(WOLFSSL_EVP_CIPHER_CTX *ctx, int flags)
  1347. {
  1348. if (ctx != NULL) {
  1349. ctx->flags |= flags;
  1350. }
  1351. }
  1352. void wolfSSL_EVP_CIPHER_CTX_clear_flags(WOLFSSL_EVP_CIPHER_CTX *ctx, int flags)
  1353. {
  1354. if (ctx != NULL) {
  1355. ctx->flags &= ~flags;
  1356. }
  1357. }
  1358. unsigned long wolfSSL_EVP_CIPHER_flags(const WOLFSSL_EVP_CIPHER *cipher)
  1359. {
  1360. if (cipher == NULL) return 0;
  1361. return WOLFSSL_CIPHER_mode(cipher);
  1362. }
  1363. int wolfSSL_EVP_CIPHER_CTX_set_padding(WOLFSSL_EVP_CIPHER_CTX *ctx, int padding)
  1364. {
  1365. if (ctx == NULL) return BAD_FUNC_ARG;
  1366. if (padding) {
  1367. ctx->flags &= ~WOLFSSL_EVP_CIPH_NO_PADDING;
  1368. }
  1369. else {
  1370. ctx->flags |= WOLFSSL_EVP_CIPH_NO_PADDING;
  1371. }
  1372. return 1;
  1373. }
  1374. int wolfSSL_EVP_add_digest(const WOLFSSL_EVP_MD *digest)
  1375. {
  1376. (void)digest;
  1377. /* nothing to do */
  1378. return 0;
  1379. }
  1380. /* Frees the WOLFSSL_EVP_PKEY_CTX passed in.
  1381. *
  1382. * return WOLFSSL_SUCCESS on success
  1383. */
  1384. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  1385. void wolfSSL_EVP_PKEY_CTX_free(WOLFSSL_EVP_PKEY_CTX *ctx)
  1386. #else
  1387. int wolfSSL_EVP_PKEY_CTX_free(WOLFSSL_EVP_PKEY_CTX *ctx)
  1388. #endif
  1389. {
  1390. if (ctx == NULL)
  1391. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  1392. return;
  1393. #else
  1394. return 0;
  1395. #endif
  1396. WOLFSSL_ENTER("EVP_PKEY_CTX_free");
  1397. if (ctx->pkey != NULL)
  1398. wolfSSL_EVP_PKEY_free(ctx->pkey);
  1399. if (ctx->peerKey != NULL)
  1400. wolfSSL_EVP_PKEY_free(ctx->peerKey);
  1401. XFREE(ctx, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  1402. #if !defined(OPENSSL_VERSION_NUMBER) || OPENSSL_VERSION_NUMBER < 0x10100000L
  1403. return WOLFSSL_SUCCESS;
  1404. #endif
  1405. }
  1406. /* Creates a new WOLFSSL_EVP_PKEY_CTX structure.
  1407. *
  1408. * pkey key structure to use with new WOLFSSL_EVP_PKEY_CTX
  1409. * e engine to use. It should be NULL at this time.
  1410. *
  1411. * return the new structure on success and NULL if failed.
  1412. */
  1413. WOLFSSL_EVP_PKEY_CTX *wolfSSL_EVP_PKEY_CTX_new(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_ENGINE *e)
  1414. {
  1415. WOLFSSL_EVP_PKEY_CTX* ctx;
  1416. if (pkey == NULL) return 0;
  1417. if (e != NULL) return 0;
  1418. WOLFSSL_ENTER("EVP_PKEY_CTX_new");
  1419. ctx = (WOLFSSL_EVP_PKEY_CTX*)XMALLOC(sizeof(WOLFSSL_EVP_PKEY_CTX), NULL,
  1420. DYNAMIC_TYPE_PUBLIC_KEY);
  1421. if (ctx == NULL) return NULL;
  1422. XMEMSET(ctx, 0, sizeof(WOLFSSL_EVP_PKEY_CTX));
  1423. ctx->pkey = pkey;
  1424. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  1425. ctx->padding = RSA_PKCS1_PADDING;
  1426. ctx->md = NULL;
  1427. #endif
  1428. #ifdef HAVE_ECC
  1429. if (pkey->ecc && pkey->ecc->group) {
  1430. /* set curve NID from pkey if available */
  1431. ctx->curveNID = pkey->ecc->group->curve_nid;
  1432. }
  1433. else {
  1434. ctx->curveNID = ECC_CURVE_DEF;
  1435. }
  1436. #endif
  1437. if (wolfSSL_EVP_PKEY_up_ref(pkey) != WOLFSSL_SUCCESS) {
  1438. WOLFSSL_MSG("Couldn't increase key reference count");
  1439. }
  1440. return ctx;
  1441. }
  1442. /* Sets the type of RSA padding to use.
  1443. *
  1444. * ctx structure to set padding in.
  1445. * padding RSA padding type
  1446. *
  1447. * returns WOLFSSL_SUCCESS on success.
  1448. */
  1449. int wolfSSL_EVP_PKEY_CTX_set_rsa_padding(WOLFSSL_EVP_PKEY_CTX *ctx, int padding)
  1450. {
  1451. if (ctx == NULL) return 0;
  1452. WOLFSSL_ENTER("EVP_PKEY_CTX_set_rsa_padding");
  1453. ctx->padding = padding;
  1454. return WOLFSSL_SUCCESS;
  1455. }
  1456. /* Sets the message digest type for RSA padding to use.
  1457. *
  1458. * ctx structure to set padding in.
  1459. * md Message digest
  1460. *
  1461. * returns WOLFSSL_SUCCESS on success.
  1462. */
  1463. int wolfSSL_EVP_PKEY_CTX_set_signature_md(WOLFSSL_EVP_PKEY_CTX *ctx,
  1464. const EVP_MD* md)
  1465. {
  1466. if (ctx == NULL) return 0;
  1467. WOLFSSL_ENTER("EVP_PKEY_CTX_set_signature_md");
  1468. #ifndef NO_RSA
  1469. ctx->md = md;
  1470. #else
  1471. (void)md;
  1472. #endif
  1473. return WOLFSSL_SUCCESS;
  1474. }
  1475. /* create a PKEY context and return it */
  1476. WOLFSSL_EVP_PKEY_CTX *wolfSSL_EVP_PKEY_CTX_new_id(int id, WOLFSSL_ENGINE *e)
  1477. {
  1478. WOLFSSL_EVP_PKEY* pkey;
  1479. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  1480. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_CTX_new_id");
  1481. pkey = wolfSSL_EVP_PKEY_new_ex(NULL);
  1482. if (pkey) {
  1483. pkey->type = id;
  1484. ctx = wolfSSL_EVP_PKEY_CTX_new(pkey, e);
  1485. /* wolfSSL_EVP_PKEY_CTX_new calls wolfSSL_EVP_PKEY_up_ref so we need
  1486. * to always call wolfSSL_EVP_PKEY_free (either to free it if an
  1487. * error occurred in the previous function or to decrease the reference
  1488. * count so that pkey is actually free'd when wolfSSL_EVP_PKEY_CTX_free
  1489. * is called) */
  1490. wolfSSL_EVP_PKEY_free(pkey);
  1491. }
  1492. return ctx;
  1493. }
  1494. /* Returns WOLFSSL_SUCCESS or error */
  1495. int wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(WOLFSSL_EVP_PKEY_CTX *ctx, int bits)
  1496. {
  1497. if (ctx) {
  1498. ctx->nbits = bits;
  1499. }
  1500. return WOLFSSL_SUCCESS;
  1501. }
  1502. int wolfSSL_EVP_PKEY_derive_init(WOLFSSL_EVP_PKEY_CTX *ctx)
  1503. {
  1504. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_derive_init");
  1505. if (!ctx) {
  1506. return WOLFSSL_FAILURE;
  1507. }
  1508. wolfSSL_EVP_PKEY_free(ctx->peerKey);
  1509. ctx->op = EVP_PKEY_OP_DERIVE;
  1510. ctx->padding = 0;
  1511. ctx->nbits = 0;
  1512. return WOLFSSL_SUCCESS;
  1513. }
  1514. int wolfSSL_EVP_PKEY_derive_set_peer(WOLFSSL_EVP_PKEY_CTX *ctx, WOLFSSL_EVP_PKEY *peer)
  1515. {
  1516. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_derive_set_peer");
  1517. if (!ctx || ctx->op != EVP_PKEY_OP_DERIVE) {
  1518. return WOLFSSL_FAILURE;
  1519. }
  1520. wolfSSL_EVP_PKEY_free(ctx->peerKey);
  1521. ctx->peerKey = peer;
  1522. if (!wolfSSL_EVP_PKEY_up_ref(peer)) {
  1523. ctx->peerKey = NULL;
  1524. return WOLFSSL_FAILURE;
  1525. }
  1526. return WOLFSSL_SUCCESS;
  1527. }
  1528. #ifndef NO_WOLFSSL_STUB
  1529. int wolfSSL_EVP_PKEY_CTX_ctrl_str(WOLFSSL_EVP_PKEY_CTX *ctx,
  1530. const char *name, const char *value)
  1531. {
  1532. WOLFSSL_STUB("wolfSSL_EVP_PKEY_CTX_ctrl_str");
  1533. (void)ctx;
  1534. (void)name;
  1535. (void)value;
  1536. return WOLFSSL_FAILURE;
  1537. }
  1538. #endif /* NO_WOLFSSL_STUB */
  1539. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC) || \
  1540. defined(HAVE_HKDF)
  1541. int wolfSSL_EVP_PKEY_derive(WOLFSSL_EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
  1542. {
  1543. int len;
  1544. #ifdef HAVE_HKDF
  1545. enum wc_HashType hkdfHashType;
  1546. int hkdfHashSz;
  1547. #endif
  1548. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_derive");
  1549. if (!ctx || ctx->op != EVP_PKEY_OP_DERIVE || !ctx->pkey || (!ctx->peerKey
  1550. && ctx->pkey->type != EVP_PKEY_HKDF) || !keylen || (ctx->pkey->type
  1551. != EVP_PKEY_HKDF && ctx->pkey->type != ctx->peerKey->type)) {
  1552. return WOLFSSL_FAILURE;
  1553. }
  1554. switch (ctx->pkey->type) {
  1555. #ifndef NO_DH
  1556. case EVP_PKEY_DH:
  1557. /* Use DH */
  1558. if (!ctx->pkey->dh || !ctx->peerKey->dh) {
  1559. return WOLFSSL_FAILURE;
  1560. }
  1561. /* set internal peer key if not done */
  1562. if (!ctx->peerKey->dh->inSet) {
  1563. if (SetDhInternal(ctx->peerKey->dh) != WOLFSSL_SUCCESS) {
  1564. WOLFSSL_MSG("SetDhInternal failed");
  1565. return WOLFSSL_FAILURE;
  1566. }
  1567. }
  1568. if (!ctx->peerKey->dh->pub_key) {
  1569. WOLFSSL_MSG("SetDhInternal failed, pub_key is NULL");
  1570. return WOLFSSL_FAILURE;
  1571. }
  1572. if ((len = wolfSSL_DH_size(ctx->pkey->dh)) <= 0) {
  1573. return WOLFSSL_FAILURE;
  1574. }
  1575. if (key) {
  1576. if (*keylen < (size_t)len) {
  1577. return WOLFSSL_FAILURE;
  1578. }
  1579. /* computed DH agreement can be less than DH size if leading zeros */
  1580. if (wolfSSL_DH_compute_key(key, ctx->peerKey->dh->pub_key,
  1581. ctx->pkey->dh) <= 0) {
  1582. return WOLFSSL_FAILURE;
  1583. }
  1584. }
  1585. *keylen = (size_t)len;
  1586. break;
  1587. #endif
  1588. #if defined(HAVE_ECC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1589. case EVP_PKEY_EC:
  1590. /* Use ECDH */
  1591. if (!ctx->pkey->ecc || !ctx->peerKey->ecc) {
  1592. return WOLFSSL_FAILURE;
  1593. }
  1594. /* set internal key if not done */
  1595. if (!ctx->pkey->ecc->inSet) {
  1596. if (SetECKeyInternal(ctx->pkey->ecc) != WOLFSSL_SUCCESS) {
  1597. WOLFSSL_MSG("SetECKeyInternal failed");
  1598. return WOLFSSL_FAILURE;
  1599. }
  1600. }
  1601. if (!ctx->peerKey->ecc->exSet || !ctx->peerKey->ecc->pub_key->internal) {
  1602. if (SetECKeyExternal(ctx->peerKey->ecc) != WOLFSSL_SUCCESS) {
  1603. WOLFSSL_MSG("SetECKeyExternal failed");
  1604. return WOLFSSL_FAILURE;
  1605. }
  1606. }
  1607. if (!(len = wc_ecc_size((ecc_key*)ctx->pkey->ecc->internal))) {
  1608. return WOLFSSL_FAILURE;
  1609. }
  1610. if (key) {
  1611. word32 len32 = (word32)len;
  1612. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
  1613. && (!defined(HAVE_FIPS) || \
  1614. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  1615. WC_RNG rng;
  1616. if (wc_InitRng(&rng) != MP_OKAY) {
  1617. WOLFSSL_MSG("Init RNG failed");
  1618. return WOLFSSL_FAILURE;
  1619. }
  1620. ((ecc_key*)ctx->pkey->ecc->internal)->rng = &rng;
  1621. #endif
  1622. if (*keylen < len32) {
  1623. WOLFSSL_MSG("buffer too short");
  1624. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
  1625. && (!defined(HAVE_FIPS) || \
  1626. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  1627. ((ecc_key*)ctx->pkey->ecc->internal)->rng = NULL;
  1628. wc_FreeRng(&rng);
  1629. #endif
  1630. return WOLFSSL_FAILURE;
  1631. }
  1632. if (wc_ecc_shared_secret_ssh((ecc_key*)ctx->pkey->ecc->internal,
  1633. (ecc_point*)ctx->peerKey->ecc->pub_key->internal,
  1634. key, &len32) != MP_OKAY) {
  1635. WOLFSSL_MSG("wc_ecc_shared_secret failed");
  1636. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
  1637. && (!defined(HAVE_FIPS) || \
  1638. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  1639. ((ecc_key*)ctx->pkey->ecc->internal)->rng = NULL;
  1640. wc_FreeRng(&rng);
  1641. #endif
  1642. return WOLFSSL_FAILURE;
  1643. }
  1644. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) \
  1645. && (!defined(HAVE_FIPS) || \
  1646. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  1647. ((ecc_key*)ctx->pkey->ecc->internal)->rng = NULL;
  1648. wc_FreeRng(&rng);
  1649. #endif
  1650. len = (int)len32;
  1651. }
  1652. *keylen = (size_t)len;
  1653. break;
  1654. #endif
  1655. #ifdef HAVE_HKDF
  1656. case EVP_PKEY_HKDF:
  1657. (void)len;
  1658. hkdfHashType = EvpMd2MacType(ctx->pkey->hkdfMd);
  1659. if (hkdfHashType == WC_HASH_TYPE_NONE) {
  1660. WOLFSSL_MSG("Invalid hash type for HKDF.");
  1661. return WOLFSSL_FAILURE;
  1662. }
  1663. if (ctx->pkey->hkdfMode == EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND) {
  1664. if (wc_HKDF(hkdfHashType, ctx->pkey->hkdfKey, ctx->pkey->hkdfKeySz,
  1665. ctx->pkey->hkdfSalt, ctx->pkey->hkdfSaltSz,
  1666. ctx->pkey->hkdfInfo, ctx->pkey->hkdfInfoSz, key,
  1667. (word32)*keylen) != 0) {
  1668. WOLFSSL_MSG("wc_HKDF failed.");
  1669. return WOLFSSL_FAILURE;
  1670. }
  1671. }
  1672. else if (ctx->pkey->hkdfMode == EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) {
  1673. if (wc_HKDF_Extract(hkdfHashType, ctx->pkey->hkdfSalt,
  1674. ctx->pkey->hkdfSaltSz, ctx->pkey->hkdfKey,
  1675. ctx->pkey->hkdfKeySz, key) != 0) {
  1676. WOLFSSL_MSG("wc_HKDF_Extract failed.");
  1677. return WOLFSSL_FAILURE;
  1678. }
  1679. else {
  1680. hkdfHashSz = wolfSSL_EVP_MD_size(ctx->pkey->hkdfMd);
  1681. if (hkdfHashSz <= 0) {
  1682. WOLFSSL_MSG("Failed to get block size for HKDF hash.");
  1683. return WOLFSSL_FAILURE;
  1684. }
  1685. /* Length of extract only is always the length of the hash. */
  1686. *keylen = hkdfHashSz;
  1687. }
  1688. }
  1689. else if (ctx->pkey->hkdfMode == EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) {
  1690. if (wc_HKDF_Expand(hkdfHashType, ctx->pkey->hkdfKey,
  1691. ctx->pkey->hkdfKeySz, ctx->pkey->hkdfInfo,
  1692. ctx->pkey->hkdfInfoSz, key,
  1693. (word32)*keylen) != 0) {
  1694. WOLFSSL_MSG("wc_HKDF_Expand failed.");
  1695. return WOLFSSL_FAILURE;
  1696. }
  1697. }
  1698. else {
  1699. WOLFSSL_MSG("Invalid HKDF mode.");
  1700. return WOLFSSL_FAILURE;
  1701. }
  1702. break;
  1703. #endif /* HAVE_HKDF */
  1704. default:
  1705. WOLFSSL_MSG("Unknown key type");
  1706. return WOLFSSL_FAILURE;
  1707. }
  1708. return WOLFSSL_SUCCESS;
  1709. }
  1710. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC || HAVE_HKDF */
  1711. #ifdef HAVE_HKDF
  1712. int wolfSSL_EVP_PKEY_CTX_set_hkdf_md(WOLFSSL_EVP_PKEY_CTX* ctx,
  1713. const WOLFSSL_EVP_MD* md)
  1714. {
  1715. int ret = WOLFSSL_SUCCESS;
  1716. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_CTX_set_hkdf_md");
  1717. if (ctx == NULL || ctx->pkey == NULL || md == NULL) {
  1718. WOLFSSL_MSG("Bad argument.");
  1719. ret = WOLFSSL_FAILURE;
  1720. }
  1721. if (ret == WOLFSSL_SUCCESS) {
  1722. ctx->pkey->hkdfMd = md;
  1723. }
  1724. WOLFSSL_LEAVE("wolfSSL_EVP_PKEY_CTX_set_hkdf_md", ret);
  1725. return ret;
  1726. }
  1727. int wolfSSL_EVP_PKEY_CTX_set1_hkdf_salt(WOLFSSL_EVP_PKEY_CTX* ctx, byte* salt,
  1728. int saltSz)
  1729. {
  1730. int ret = WOLFSSL_SUCCESS;
  1731. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_CTX_set1_hkdf_salt");
  1732. if (ctx == NULL || ctx->pkey == NULL || saltSz < 0) {
  1733. WOLFSSL_MSG("Bad argument.");
  1734. ret = WOLFSSL_FAILURE;
  1735. }
  1736. if (ret == WOLFSSL_SUCCESS && ctx->pkey->type != EVP_PKEY_HKDF) {
  1737. WOLFSSL_MSG("WOLFSSL_EVP_PKEY type is not HKDF.");
  1738. ret = WOLFSSL_FAILURE;
  1739. }
  1740. if (ret == WOLFSSL_SUCCESS && salt != NULL && saltSz > 0) {
  1741. if (ctx->pkey->hkdfSalt != NULL) {
  1742. XFREE(ctx->pkey->hkdfSalt, NULL, DYNAMIC_TYPE_SALT);
  1743. }
  1744. ctx->pkey->hkdfSalt = (byte*)XMALLOC(saltSz, NULL, DYNAMIC_TYPE_SALT);
  1745. if (ctx->pkey->hkdfSalt == NULL) {
  1746. WOLFSSL_MSG("Failed to allocate HKDF salt buffer.");
  1747. ret = WOLFSSL_FAILURE;
  1748. }
  1749. else {
  1750. XMEMCPY(ctx->pkey->hkdfSalt, salt, saltSz);
  1751. ctx->pkey->hkdfSaltSz = saltSz;
  1752. }
  1753. }
  1754. WOLFSSL_LEAVE("wolfSSL_EVP_PKEY_CTX_set1_hkdf_salt", ret);
  1755. return ret;
  1756. }
  1757. int wolfSSL_EVP_PKEY_CTX_set1_hkdf_key(WOLFSSL_EVP_PKEY_CTX* ctx, byte* key,
  1758. int keySz)
  1759. {
  1760. int ret = WOLFSSL_SUCCESS;
  1761. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_CTX_set1_hkdf_key");
  1762. if (ctx == NULL || ctx->pkey == NULL || key == NULL || keySz <= 0) {
  1763. WOLFSSL_MSG("Bad argument.");
  1764. ret = WOLFSSL_FAILURE;
  1765. }
  1766. if (ret == WOLFSSL_SUCCESS && ctx->pkey->type != EVP_PKEY_HKDF) {
  1767. WOLFSSL_MSG("WOLFSSL_EVP_PKEY type is not HKDF.");
  1768. ret = WOLFSSL_FAILURE;
  1769. }
  1770. if (ret == WOLFSSL_SUCCESS) {
  1771. if (ctx->pkey->hkdfKey != NULL) {
  1772. XFREE(ctx->pkey->hkdfKey, NULL, DYNAMIC_TYPE_KEY);
  1773. }
  1774. ctx->pkey->hkdfKey = (byte*)XMALLOC(keySz, NULL, DYNAMIC_TYPE_KEY);
  1775. if (ctx->pkey->hkdfKey == NULL) {
  1776. WOLFSSL_MSG("Failed to allocate HKDF key buffer.");
  1777. ret = WOLFSSL_FAILURE;
  1778. }
  1779. else {
  1780. XMEMCPY(ctx->pkey->hkdfKey, key, keySz);
  1781. ctx->pkey->hkdfKeySz = keySz;
  1782. }
  1783. }
  1784. WOLFSSL_LEAVE("wolfSSL_EVP_PKEY_CTX_set1_hkdf_key", ret);
  1785. return ret;
  1786. }
  1787. int wolfSSL_EVP_PKEY_CTX_add1_hkdf_info(WOLFSSL_EVP_PKEY_CTX* ctx, byte* info,
  1788. int infoSz)
  1789. {
  1790. int ret = WOLFSSL_SUCCESS;
  1791. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_CTX_add1_hkdf_info");
  1792. if (ctx == NULL || ctx->pkey == NULL || infoSz < 0) {
  1793. WOLFSSL_MSG("Bad argument.");
  1794. ret = WOLFSSL_FAILURE;
  1795. }
  1796. if (ret == WOLFSSL_SUCCESS && ctx->pkey->type != EVP_PKEY_HKDF) {
  1797. WOLFSSL_MSG("WOLFSSL_EVP_PKEY type is not HKDF.");
  1798. ret = WOLFSSL_FAILURE;
  1799. }
  1800. if (ret == WOLFSSL_SUCCESS && info != NULL && infoSz > 0) {
  1801. /* If there's already info in the buffer, append. */
  1802. ctx->pkey->hkdfInfo = (byte*)XREALLOC(ctx->pkey->hkdfInfo,
  1803. ctx->pkey->hkdfInfoSz + infoSz, NULL, DYNAMIC_TYPE_INFO);
  1804. if (ctx->pkey->hkdfInfo == NULL) {
  1805. WOLFSSL_MSG("Failed to reallocate larger HKDF info buffer.");
  1806. ret = WOLFSSL_FAILURE;
  1807. }
  1808. else {
  1809. XMEMCPY(ctx->pkey->hkdfInfo + ctx->pkey->hkdfInfoSz, info,
  1810. infoSz);
  1811. ctx->pkey->hkdfInfoSz += infoSz;
  1812. }
  1813. }
  1814. WOLFSSL_LEAVE("wolfSSL_EVP_PKEY_CTX_add1_hkdf_info", ret);
  1815. return ret;
  1816. }
  1817. int wolfSSL_EVP_PKEY_CTX_hkdf_mode(WOLFSSL_EVP_PKEY_CTX* ctx, int mode)
  1818. {
  1819. int ret = WOLFSSL_SUCCESS;
  1820. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_CTX_hkdf_mode");
  1821. if (ctx == NULL || ctx->pkey == NULL) {
  1822. WOLFSSL_MSG("Bad argument.");
  1823. ret = WOLFSSL_FAILURE;
  1824. }
  1825. if (ret == WOLFSSL_SUCCESS &&
  1826. mode != EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND &&
  1827. mode != EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY &&
  1828. mode != EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) {
  1829. WOLFSSL_MSG("Invalid HKDF mode.");
  1830. ret = WOLFSSL_FAILURE;
  1831. }
  1832. if (ret == WOLFSSL_SUCCESS) {
  1833. ctx->pkey->hkdfMode = mode;
  1834. }
  1835. WOLFSSL_LEAVE("wolfSSL_EVP_PKEY_CTX_hkdf_mode", ret);
  1836. return ret;
  1837. }
  1838. #endif /* HAVE_HKDF */
  1839. /* Uses the WOLFSSL_EVP_PKEY_CTX to decrypt a buffer.
  1840. *
  1841. * ctx EVP_PKEY context of operation.
  1842. * out Decrypted output buffer. If NULL, puts the maximum output buffer size
  1843. in outLen and returns success.
  1844. * outLen If out is NULL, see above. If out is non-NULL, on input outLen holds
  1845. * the size of out. On output holds the length of actual decryption.
  1846. * in Encrypted input buffer.
  1847. * inLen Length of encrypted data.
  1848. *
  1849. * Returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure.
  1850. */
  1851. int wolfSSL_EVP_PKEY_decrypt(WOLFSSL_EVP_PKEY_CTX *ctx,
  1852. unsigned char *out, size_t *outLen,
  1853. const unsigned char *in, size_t inLen)
  1854. {
  1855. int len = 0;
  1856. WOLFSSL_ENTER("EVP_PKEY_decrypt");
  1857. if (ctx == NULL || ctx->pkey == NULL) {
  1858. WOLFSSL_MSG("Bad parameter.");
  1859. return 0;
  1860. }
  1861. (void)out;
  1862. (void)outLen;
  1863. (void)in;
  1864. (void)inLen;
  1865. (void)len;
  1866. switch (ctx->pkey->type) {
  1867. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  1868. case EVP_PKEY_RSA:
  1869. if (out == NULL) {
  1870. if (ctx->pkey->rsa == NULL) {
  1871. WOLFSSL_MSG("Internal wolfCrypt RSA object is NULL.");
  1872. return WOLFSSL_FAILURE;
  1873. }
  1874. len = wolfSSL_RSA_size(ctx->pkey->rsa);
  1875. if (len <= 0) {
  1876. WOLFSSL_MSG("Error getting RSA size.");
  1877. return WOLFSSL_FAILURE;
  1878. }
  1879. if (outLen == NULL) {
  1880. WOLFSSL_MSG("outLen is NULL.");
  1881. return WOLFSSL_FAILURE;
  1882. }
  1883. *outLen = len;
  1884. return WOLFSSL_SUCCESS;
  1885. }
  1886. len = wolfSSL_RSA_private_decrypt((int)inLen, (unsigned char*)in, out,
  1887. ctx->pkey->rsa, ctx->padding);
  1888. if (len < 0) break;
  1889. else {
  1890. *outLen = len;
  1891. return WOLFSSL_SUCCESS;
  1892. }
  1893. #endif /* NO_RSA */
  1894. case EVP_PKEY_EC:
  1895. WOLFSSL_MSG("EVP_PKEY_EC not implemented.");
  1896. FALL_THROUGH;
  1897. default:
  1898. break;
  1899. }
  1900. return WOLFSSL_FAILURE;
  1901. }
  1902. /* Initialize a WOLFSSL_EVP_PKEY_CTX structure for decryption
  1903. *
  1904. * ctx WOLFSSL_EVP_PKEY_CTX structure to use with decryption
  1905. *
  1906. * Returns WOLFSSL_FAILURE on failure and WOLFSSL_SUCCESS on success
  1907. */
  1908. int wolfSSL_EVP_PKEY_decrypt_init(WOLFSSL_EVP_PKEY_CTX *ctx)
  1909. {
  1910. if (ctx == NULL) return WOLFSSL_FAILURE;
  1911. WOLFSSL_ENTER("EVP_PKEY_decrypt_init");
  1912. switch (ctx->pkey->type) {
  1913. case EVP_PKEY_RSA:
  1914. ctx->op = EVP_PKEY_OP_DECRYPT;
  1915. return WOLFSSL_SUCCESS;
  1916. case EVP_PKEY_EC:
  1917. WOLFSSL_MSG("not implemented");
  1918. FALL_THROUGH;
  1919. default:
  1920. break;
  1921. }
  1922. return WOLFSSL_FAILURE;
  1923. }
  1924. /* Uses the WOLFSSL_EVP_PKEY_CTX to encrypt a buffer.
  1925. *
  1926. * ctx EVP_PKEY context of operation.
  1927. * out Encrypted output buffer. If NULL, puts the maximum output buffer size
  1928. * in outlen and returns success.
  1929. * outLen If out is NULL, see above. If out is non-NULL, on input outLen holds
  1930. * the size of out. On output holds the length of actual encryption.
  1931. * in Plaintext input buffer.
  1932. * inLen Length of plaintext.
  1933. *
  1934. * Returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure.
  1935. */
  1936. int wolfSSL_EVP_PKEY_encrypt(WOLFSSL_EVP_PKEY_CTX *ctx,
  1937. unsigned char *out, size_t *outLen,
  1938. const unsigned char *in, size_t inLen)
  1939. {
  1940. int len = 0;
  1941. WOLFSSL_ENTER("EVP_PKEY_encrypt");
  1942. if (ctx == NULL || ctx->pkey == NULL) {
  1943. WOLFSSL_MSG("Bad parameter.");
  1944. return 0;
  1945. }
  1946. if (ctx->op != EVP_PKEY_OP_ENCRYPT) {
  1947. WOLFSSL_MSG("ctx->op must be set to EVP_PKEY_OP_ENCRYPT. Use "
  1948. "wolfSSL_EVP_PKEY_encrypt_init.");
  1949. return WOLFSSL_FAILURE;
  1950. }
  1951. (void)out;
  1952. (void)outLen;
  1953. (void)in;
  1954. (void)inLen;
  1955. (void)len;
  1956. switch (ctx->pkey->type) {
  1957. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  1958. case EVP_PKEY_RSA:
  1959. if (out == NULL) {
  1960. if (ctx->pkey->rsa == NULL) {
  1961. WOLFSSL_MSG("Internal wolfCrypt RSA object is NULL.");
  1962. return WOLFSSL_FAILURE;
  1963. }
  1964. len = wolfSSL_RSA_size(ctx->pkey->rsa);
  1965. if (len <= 0) {
  1966. WOLFSSL_MSG("Error getting RSA size.");
  1967. return WOLFSSL_FAILURE;
  1968. }
  1969. if (outLen == NULL) {
  1970. WOLFSSL_MSG("outLen is NULL.");
  1971. return WOLFSSL_FAILURE;
  1972. }
  1973. *outLen = len;
  1974. return WOLFSSL_SUCCESS;
  1975. }
  1976. len = wolfSSL_RSA_public_encrypt((int)inLen, (unsigned char *)in, out,
  1977. ctx->pkey->rsa, ctx->padding);
  1978. if (len < 0)
  1979. break;
  1980. else {
  1981. *outLen = len;
  1982. return WOLFSSL_SUCCESS;
  1983. }
  1984. #endif /* NO_RSA */
  1985. case EVP_PKEY_EC:
  1986. WOLFSSL_MSG("EVP_PKEY_EC not implemented");
  1987. FALL_THROUGH;
  1988. default:
  1989. break;
  1990. }
  1991. return WOLFSSL_FAILURE;
  1992. }
  1993. /* Initialize a WOLFSSL_EVP_PKEY_CTX structure to encrypt data
  1994. *
  1995. * ctx WOLFSSL_EVP_PKEY_CTX structure to use with encryption
  1996. *
  1997. * Returns WOLFSSL_FAILURE on failure and WOLFSSL_SUCCESS on success
  1998. */
  1999. int wolfSSL_EVP_PKEY_encrypt_init(WOLFSSL_EVP_PKEY_CTX *ctx)
  2000. {
  2001. if (ctx == NULL) return WOLFSSL_FAILURE;
  2002. WOLFSSL_ENTER("EVP_PKEY_encrypt_init");
  2003. switch (ctx->pkey->type) {
  2004. case EVP_PKEY_RSA:
  2005. ctx->op = EVP_PKEY_OP_ENCRYPT;
  2006. return WOLFSSL_SUCCESS;
  2007. case EVP_PKEY_EC:
  2008. WOLFSSL_MSG("not implemented");
  2009. FALL_THROUGH;
  2010. default:
  2011. break;
  2012. }
  2013. return WOLFSSL_FAILURE;
  2014. }
  2015. /******************************************************************************
  2016. * wolfSSL_EVP_PKEY_sign_init - initializes a public key algorithm context for
  2017. * a signing operation.
  2018. *
  2019. * RETURNS:
  2020. * returns WOLFSSL_SUCCESS on success, otherwise returns -2
  2021. */
  2022. int wolfSSL_EVP_PKEY_sign_init(WOLFSSL_EVP_PKEY_CTX *ctx)
  2023. {
  2024. int ret = -2;
  2025. WOLFSSL_MSG("wolfSSL_EVP_PKEY_sign_init");
  2026. if (!ctx || !ctx->pkey)
  2027. return ret;
  2028. switch (ctx->pkey->type) {
  2029. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  2030. case EVP_PKEY_RSA:
  2031. ctx->op = EVP_PKEY_OP_SIGN;
  2032. ret = WOLFSSL_SUCCESS;
  2033. break;
  2034. #endif /* NO_RSA */
  2035. #ifndef NO_DSA
  2036. case EVP_PKEY_DSA:
  2037. ctx->op = EVP_PKEY_OP_SIGN;
  2038. ret = WOLFSSL_SUCCESS;
  2039. break;
  2040. #endif /* NO_DSA */
  2041. #ifdef HAVE_ECC
  2042. case EVP_PKEY_EC:
  2043. ctx->op = EVP_PKEY_OP_SIGN;
  2044. ret = WOLFSSL_SUCCESS;
  2045. break;
  2046. #endif /* HAVE_ECC */
  2047. default:
  2048. ret = -2;
  2049. }
  2050. return ret;
  2051. }
  2052. /******************************************************************************
  2053. * wolfSSL_EVP_PKEY_sign - performs a public key signing operation using ctx
  2054. * The data to be signed should be hashed since the function does not hash the data.
  2055. *
  2056. * RETURNS:
  2057. * returns WOLFSSL_SUCCESS on success, otherwise returns WOLFSSL_FAILURE
  2058. */
  2059. int wolfSSL_EVP_PKEY_sign(WOLFSSL_EVP_PKEY_CTX *ctx, unsigned char *sig,
  2060. size_t *siglen, const unsigned char *tbs, size_t tbslen)
  2061. {
  2062. WOLFSSL_MSG("wolfSSL_EVP_PKEY_sign");
  2063. if (!ctx || ctx->op != EVP_PKEY_OP_SIGN || !ctx->pkey || !siglen)
  2064. return WOLFSSL_FAILURE;
  2065. (void)sig;
  2066. (void)siglen;
  2067. (void)tbs;
  2068. (void)tbslen;
  2069. switch (ctx->pkey->type) {
  2070. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  2071. case EVP_PKEY_RSA: {
  2072. int len;
  2073. unsigned int usiglen = (unsigned int)*siglen;
  2074. if (!sig) {
  2075. if (!ctx->pkey->rsa)
  2076. return WOLFSSL_FAILURE;
  2077. len = wc_RsaEncryptSize((RsaKey*)ctx->pkey->rsa->internal);
  2078. if (len < 0)
  2079. return WOLFSSL_FAILURE;
  2080. *siglen = (size_t)len;
  2081. return WOLFSSL_SUCCESS;
  2082. }
  2083. /* wolfSSL_RSA_sign_generic_padding performs a check that the output
  2084. * sig buffer is large enough */
  2085. if (wolfSSL_RSA_sign_generic_padding(wolfSSL_EVP_MD_type(ctx->md), tbs,
  2086. (unsigned int)tbslen, sig, &usiglen, ctx->pkey->rsa, 1,
  2087. ctx->padding) != WOLFSSL_SUCCESS) {
  2088. return WOLFSSL_FAILURE;
  2089. }
  2090. *siglen = (size_t)usiglen;
  2091. return WOLFSSL_SUCCESS;
  2092. }
  2093. #endif /* NO_RSA */
  2094. #ifndef NO_DSA
  2095. case EVP_PKEY_DSA: {
  2096. int bytes;
  2097. int ret;
  2098. if (!ctx->pkey->dsa)
  2099. return WOLFSSL_FAILURE;
  2100. bytes = wolfSSL_BN_num_bytes(ctx->pkey->dsa->q);
  2101. if (bytes == WOLFSSL_FAILURE)
  2102. return WOLFSSL_FAILURE;
  2103. bytes *= 2;
  2104. if (!sig) {
  2105. *siglen = bytes;
  2106. return WOLFSSL_SUCCESS;
  2107. }
  2108. if ((int)*siglen < bytes)
  2109. return WOLFSSL_FAILURE;
  2110. ret = wolfSSL_DSA_do_sign(tbs, sig, ctx->pkey->dsa);
  2111. /* wolfSSL_DSA_do_sign() can return WOLFSSL_FATAL_ERROR */
  2112. if (ret != WOLFSSL_SUCCESS)
  2113. return ret;
  2114. if (bytes == WOLFSSL_FAILURE)
  2115. return WOLFSSL_FAILURE;
  2116. *siglen = bytes;
  2117. return WOLFSSL_SUCCESS;
  2118. }
  2119. #endif /* NO_DSA */
  2120. #ifdef HAVE_ECC
  2121. case EVP_PKEY_EC: {
  2122. int ret;
  2123. WOLFSSL_ECDSA_SIG *ecdsaSig;
  2124. if (!sig) {
  2125. WOLFSSL_EC_KEY *key = ctx->pkey->ecc;
  2126. ecc_key* eckey;
  2127. if (!key)
  2128. return WOLFSSL_FAILURE;
  2129. /* set internal key if not done */
  2130. if (key->inSet == 0 && SetECKeyInternal(key) != WOLFSSL_SUCCESS)
  2131. return WOLFSSL_FAILURE;
  2132. eckey = (ecc_key*)ctx->pkey->ecc->internal;
  2133. if (!eckey)
  2134. return WOLFSSL_FAILURE;
  2135. ret = wc_ecc_sig_size(eckey);
  2136. if (ret == 0)
  2137. return WOLFSSL_FAILURE;
  2138. *siglen = ret;
  2139. return WOLFSSL_SUCCESS;
  2140. }
  2141. ecdsaSig = wolfSSL_ECDSA_do_sign(tbs, (int)tbslen, ctx->pkey->ecc);
  2142. if (ecdsaSig == NULL)
  2143. return WOLFSSL_FAILURE;
  2144. ret = wolfSSL_i2d_ECDSA_SIG(ecdsaSig, NULL);
  2145. if (ret == 0 || ret > (int)*siglen) {
  2146. wolfSSL_ECDSA_SIG_free(ecdsaSig);
  2147. return WOLFSSL_FAILURE;
  2148. }
  2149. ret = wolfSSL_i2d_ECDSA_SIG(ecdsaSig, &sig);
  2150. wolfSSL_ECDSA_SIG_free(ecdsaSig);
  2151. if (ret == 0)
  2152. return WOLFSSL_FAILURE;
  2153. *siglen = ret;
  2154. return WOLFSSL_SUCCESS;
  2155. }
  2156. #endif /* HAVE_ECC */
  2157. default:
  2158. break;
  2159. }
  2160. return WOLFSSL_FAILURE;
  2161. }
  2162. /******************************************************************************
  2163. * wolfSSL_EVP_PKEY_verify_init - initializes a public key algorithm context for
  2164. * a verification operation.
  2165. *
  2166. * RETURNS:
  2167. * returns WOLFSSL_SUCCESS on success, WOLFSSL_FAILURE on failure. In particular
  2168. * a return value of -2 indicates the operation is not supported by the public
  2169. * key algorithm.
  2170. */
  2171. int wolfSSL_EVP_PKEY_verify_init(WOLFSSL_EVP_PKEY_CTX *ctx)
  2172. {
  2173. WOLFSSL_MSG("wolfSSL_EVP_PKEY_verify_init");
  2174. if (!ctx || !ctx->pkey)
  2175. return WOLFSSL_FAILURE;
  2176. switch (ctx->pkey->type) {
  2177. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  2178. case EVP_PKEY_RSA:
  2179. ctx->op = EVP_PKEY_OP_VERIFY;
  2180. return WOLFSSL_SUCCESS;
  2181. #endif /* NO_RSA */
  2182. #ifndef NO_DSA
  2183. case EVP_PKEY_DSA:
  2184. ctx->op = EVP_PKEY_OP_VERIFY;
  2185. return WOLFSSL_SUCCESS;
  2186. #endif /* NO_DSA */
  2187. #ifdef HAVE_ECC
  2188. case EVP_PKEY_EC:
  2189. ctx->op = EVP_PKEY_OP_VERIFY;
  2190. return WOLFSSL_SUCCESS;
  2191. #endif /* HAVE_ECC */
  2192. default:
  2193. return -2;
  2194. }
  2195. }
  2196. /******************************************************************************
  2197. * wolfSSL_EVP_PKEY_verify - verifies a signature using ctx
  2198. *
  2199. * RETURNS:
  2200. * returns WOLFSSL_SUCCESS on success, WOLFSSL_FAILURE on failure. In particular
  2201. * a return value of -2 indicates the operation is not supported by the public
  2202. * key algorithm.
  2203. */
  2204. int wolfSSL_EVP_PKEY_verify(WOLFSSL_EVP_PKEY_CTX *ctx, const unsigned char *sig,
  2205. size_t siglen, const unsigned char *tbs,
  2206. size_t tbslen)
  2207. {
  2208. WOLFSSL_MSG("wolfSSL_EVP_PKEY_verify");
  2209. if (!ctx || ctx->op != EVP_PKEY_OP_VERIFY || !ctx->pkey)
  2210. return WOLFSSL_FAILURE;
  2211. switch (ctx->pkey->type) {
  2212. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  2213. case EVP_PKEY_RSA:
  2214. return wolfSSL_RSA_verify_ex(WC_HASH_TYPE_NONE, tbs,
  2215. (unsigned int)tbslen, sig, (unsigned int)siglen, ctx->pkey->rsa,
  2216. ctx->padding);
  2217. #endif /* NO_RSA */
  2218. #ifndef NO_DSA
  2219. case EVP_PKEY_DSA: {
  2220. int dsacheck = 0;
  2221. if (wolfSSL_DSA_do_verify(tbs, (unsigned char *)sig, ctx->pkey->dsa,
  2222. &dsacheck) != WOLFSSL_SUCCESS || dsacheck != 1)
  2223. return WOLFSSL_FAILURE;
  2224. return WOLFSSL_SUCCESS;
  2225. }
  2226. #endif /* NO_DSA */
  2227. #ifdef HAVE_ECC
  2228. case EVP_PKEY_EC: {
  2229. int ret;
  2230. WOLFSSL_ECDSA_SIG *ecdsaSig = wolfSSL_d2i_ECDSA_SIG(
  2231. NULL, (const unsigned char **)&sig, (long)siglen);
  2232. if (ecdsaSig == NULL)
  2233. return WOLFSSL_FAILURE;
  2234. ret = wolfSSL_ECDSA_do_verify(tbs, (int)tbslen, ecdsaSig,
  2235. ctx->pkey->ecc);
  2236. wolfSSL_ECDSA_SIG_free(ecdsaSig);
  2237. return ret;
  2238. }
  2239. #endif /* HAVE_ECC */
  2240. default:
  2241. return -2;
  2242. }
  2243. }
  2244. /* Get the size in bits for WOLFSSL_EVP_PKEY key
  2245. *
  2246. * pkey WOLFSSL_EVP_PKEY structure to get key size of
  2247. *
  2248. * returns the size in bits of key on success
  2249. */
  2250. int wolfSSL_EVP_PKEY_bits(const WOLFSSL_EVP_PKEY *pkey)
  2251. {
  2252. int bytes;
  2253. if (pkey == NULL) return 0;
  2254. WOLFSSL_ENTER("EVP_PKEY_bits");
  2255. if ((bytes = wolfSSL_EVP_PKEY_size((WOLFSSL_EVP_PKEY*)pkey)) ==0) return 0;
  2256. return bytes*8;
  2257. }
  2258. int wolfSSL_EVP_PKEY_paramgen_init(WOLFSSL_EVP_PKEY_CTX *ctx)
  2259. {
  2260. (void)ctx;
  2261. return WOLFSSL_SUCCESS;
  2262. }
  2263. int wolfSSL_EVP_PKEY_CTX_set_ec_paramgen_curve_nid(WOLFSSL_EVP_PKEY_CTX *ctx,
  2264. int nid)
  2265. {
  2266. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_CTX_set_ec_paramgen_curve_nid");
  2267. #ifdef HAVE_ECC
  2268. if (ctx != NULL && ctx->pkey != NULL && ctx->pkey->type == EVP_PKEY_EC) {
  2269. ctx->curveNID = nid;
  2270. return WOLFSSL_SUCCESS;
  2271. }
  2272. else
  2273. #endif
  2274. {
  2275. #ifndef HAVE_ECC
  2276. (void)ctx;
  2277. (void)nid;
  2278. WOLFSSL_MSG("Support not compiled in");
  2279. #else
  2280. WOLFSSL_MSG("Bad parameter");
  2281. #endif
  2282. return WOLFSSL_FAILURE;
  2283. }
  2284. }
  2285. int wolfSSL_EVP_PKEY_paramgen(WOLFSSL_EVP_PKEY_CTX* ctx,
  2286. WOLFSSL_EVP_PKEY** pkey)
  2287. {
  2288. int ret = WOLFSSL_SUCCESS;
  2289. int ownPkey = 0;
  2290. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_paramgen");
  2291. if (ctx == NULL || pkey == NULL) {
  2292. WOLFSSL_MSG("Bad parameter");
  2293. ret = WOLFSSL_FAILURE;
  2294. }
  2295. if (ret == WOLFSSL_SUCCESS && *pkey == NULL) {
  2296. /* Only ECC is supported currently. */
  2297. if (ctx->pkey == NULL || ctx->pkey->type != EVP_PKEY_EC) {
  2298. WOLFSSL_MSG("Key not set or key type not supported.");
  2299. ret = WOLFSSL_FAILURE;
  2300. }
  2301. else {
  2302. *pkey = wolfSSL_EVP_PKEY_new();
  2303. if (*pkey == NULL) {
  2304. WOLFSSL_MSG("Failed to create WOLFSSL_EVP_PKEY.");
  2305. ret = WOLFSSL_FAILURE;
  2306. }
  2307. else {
  2308. (*pkey)->type = ctx->pkey->type;
  2309. ownPkey = 1;
  2310. }
  2311. }
  2312. }
  2313. if (ret == WOLFSSL_SUCCESS) {
  2314. switch ((*pkey)->type) {
  2315. #ifdef HAVE_ECC
  2316. /* For ECC parameter generation we just need to set the group, which
  2317. * wolfSSL_EC_KEY_new_by_curve_name will do. */
  2318. case EVP_PKEY_EC:
  2319. (*pkey)->ecc = wolfSSL_EC_KEY_new_by_curve_name(ctx->curveNID);
  2320. if ((*pkey)->ecc == NULL) {
  2321. WOLFSSL_MSG("Failed to create WOLFSSL_EC_KEY.");
  2322. ret = WOLFSSL_FAILURE;
  2323. }
  2324. else {
  2325. (*pkey)->ownEcc = 1;
  2326. }
  2327. break;
  2328. #endif
  2329. default:
  2330. ret = WOLFSSL_FAILURE;
  2331. break;
  2332. }
  2333. }
  2334. if (ret != WOLFSSL_SUCCESS && ownPkey) {
  2335. wolfSSL_EVP_PKEY_free(*pkey);
  2336. *pkey = NULL;
  2337. }
  2338. WOLFSSL_LEAVE("wolfSSL_EVP_PKEY_paramgen", ret);
  2339. return ret;
  2340. }
  2341. /* wolfSSL only supports writing out named curves so no need to store the flag.
  2342. * In short, it is preferred to write out the name of the curve chosen instead
  2343. * of the explicit parameters.
  2344. * The difference is nicely explained and illustrated in section
  2345. * "ECDH and Named Curves" of
  2346. * https://wiki.openssl.org/index.php/Elliptic_Curve_Diffie_Hellman */
  2347. int wolfSSL_EVP_PKEY_CTX_set_ec_param_enc(WOLFSSL_EVP_PKEY_CTX *ctx,
  2348. int flag)
  2349. {
  2350. (void)ctx;
  2351. (void)flag;
  2352. return WOLFSSL_SUCCESS;
  2353. }
  2354. int wolfSSL_EVP_PKEY_keygen_init(WOLFSSL_EVP_PKEY_CTX *ctx)
  2355. {
  2356. (void)ctx;
  2357. return WOLFSSL_SUCCESS;
  2358. }
  2359. int wolfSSL_EVP_PKEY_keygen(WOLFSSL_EVP_PKEY_CTX *ctx,
  2360. WOLFSSL_EVP_PKEY **ppkey)
  2361. {
  2362. int ret = WOLFSSL_FAILURE;
  2363. int ownPkey = 0;
  2364. WOLFSSL_EVP_PKEY* pkey;
  2365. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_keygen");
  2366. if (ctx == NULL || ppkey == NULL) {
  2367. return BAD_FUNC_ARG;
  2368. }
  2369. pkey = *ppkey;
  2370. if (pkey == NULL) {
  2371. if (ctx->pkey == NULL ||
  2372. (ctx->pkey->type != EVP_PKEY_EC &&
  2373. ctx->pkey->type != EVP_PKEY_RSA &&
  2374. ctx->pkey->type != EVP_PKEY_DH)) {
  2375. WOLFSSL_MSG("Key not set or key type not supported");
  2376. return BAD_FUNC_ARG;
  2377. }
  2378. pkey = wolfSSL_EVP_PKEY_new();
  2379. if (pkey == NULL) {
  2380. return MEMORY_E;
  2381. }
  2382. ownPkey = 1;
  2383. pkey->type = ctx->pkey->type;
  2384. }
  2385. switch (pkey->type) {
  2386. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  2387. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  2388. case EVP_PKEY_RSA:
  2389. pkey->rsa = wolfSSL_RSA_generate_key(ctx->nbits, WC_RSA_EXPONENT,
  2390. NULL, NULL);
  2391. if (pkey->rsa) {
  2392. pkey->ownRsa = 1;
  2393. pkey->pkey_sz = wolfSSL_i2d_RSAPrivateKey(pkey->rsa,
  2394. (unsigned char**)&pkey->pkey.ptr);
  2395. ret = WOLFSSL_SUCCESS;
  2396. }
  2397. break;
  2398. #endif
  2399. #ifdef HAVE_ECC
  2400. case EVP_PKEY_EC:
  2401. /* pkey->ecc may not be NULL, if, for example, it was populated by a
  2402. * prior call to wolfSSL_EVP_PKEY_paramgen. */
  2403. if (pkey->ecc == NULL) {
  2404. pkey->ecc = wolfSSL_EC_KEY_new_by_curve_name(ctx->curveNID);
  2405. }
  2406. if (pkey->ecc) {
  2407. ret = wolfSSL_EC_KEY_generate_key(pkey->ecc);
  2408. if (ret == WOLFSSL_SUCCESS) {
  2409. pkey->ownEcc = 1;
  2410. }
  2411. }
  2412. break;
  2413. #endif
  2414. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  2415. case EVP_PKEY_DH:
  2416. pkey->dh = wolfSSL_DH_new();
  2417. if (pkey->dh) {
  2418. pkey->ownDh = 1;
  2419. /* load DH params from CTX */
  2420. ret = wolfSSL_DH_LoadDer(pkey->dh,
  2421. (const unsigned char*)ctx->pkey->pkey.ptr,
  2422. ctx->pkey->pkey_sz);
  2423. if (ret == WOLFSSL_SUCCESS) {
  2424. ret = wolfSSL_DH_generate_key(pkey->dh);
  2425. }
  2426. if (ret == WOLFSSL_SUCCESS) {
  2427. /* copy private/public key from external to internal */
  2428. ret = SetDhInternal(pkey->dh);
  2429. }
  2430. }
  2431. break;
  2432. #endif
  2433. default:
  2434. break;
  2435. }
  2436. if (ret != WOLFSSL_SUCCESS && ownPkey) {
  2437. wolfSSL_EVP_PKEY_free(pkey);
  2438. pkey = NULL;
  2439. }
  2440. *ppkey = pkey;
  2441. return ret;
  2442. }
  2443. /* Get the size in bytes for WOLFSSL_EVP_PKEY key
  2444. *
  2445. * pkey WOLFSSL_EVP_PKEY structure to get key size of
  2446. *
  2447. * returns the size of a key on success which is the maximum size of a
  2448. * signature
  2449. */
  2450. int wolfSSL_EVP_PKEY_size(WOLFSSL_EVP_PKEY *pkey)
  2451. {
  2452. if (pkey == NULL) return 0;
  2453. WOLFSSL_ENTER("EVP_PKEY_size");
  2454. switch (pkey->type) {
  2455. #ifndef NO_RSA
  2456. case EVP_PKEY_RSA:
  2457. return (int)wolfSSL_RSA_size((const WOLFSSL_RSA*)(pkey->rsa));
  2458. #endif /* !NO_RSA */
  2459. #ifndef NO_DSA
  2460. case EVP_PKEY_DSA:
  2461. if (pkey->dsa == NULL ||
  2462. (!pkey->dsa->exSet &&
  2463. SetDsaExternal(pkey->dsa) != WOLFSSL_SUCCESS))
  2464. return WOLFSSL_FAILURE;
  2465. return wolfSSL_BN_num_bytes(pkey->dsa->p);
  2466. #endif
  2467. #ifdef HAVE_ECC
  2468. case EVP_PKEY_EC:
  2469. if (pkey->ecc == NULL || pkey->ecc->internal == NULL) {
  2470. WOLFSSL_MSG("No ECC key has been set");
  2471. break;
  2472. }
  2473. return wc_ecc_size((ecc_key*)(pkey->ecc->internal));
  2474. #endif /* HAVE_ECC */
  2475. default:
  2476. break;
  2477. }
  2478. return 0;
  2479. }
  2480. int wolfSSL_EVP_PKEY_copy_parameters(WOLFSSL_EVP_PKEY *to,
  2481. const WOLFSSL_EVP_PKEY *from)
  2482. {
  2483. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_copy_parameters");
  2484. if (!to || !from) {
  2485. WOLFSSL_MSG("Bad parameter");
  2486. return WOLFSSL_FAILURE;
  2487. }
  2488. if (to->type == EVP_PKEY_NONE) {
  2489. to->type = from->type;
  2490. }
  2491. else if (to->type != from->type) {
  2492. WOLFSSL_MSG("Different key types");
  2493. return WOLFSSL_FAILURE;
  2494. }
  2495. switch(from->type) {
  2496. #ifdef HAVE_ECC
  2497. case EVP_PKEY_EC:
  2498. if (from->ecc) {
  2499. if (!to->ecc) {
  2500. if ((to->ecc = wolfSSL_EC_KEY_new()) == NULL) {
  2501. WOLFSSL_MSG("wolfSSL_EC_KEY_new error");
  2502. return WOLFSSL_FAILURE;
  2503. }
  2504. to->ownEcc = 1;
  2505. }
  2506. to->ecc->group->curve_idx = from->ecc->group->curve_idx;
  2507. to->ecc->group->curve_nid = from->ecc->group->curve_nid;
  2508. to->ecc->group->curve_oid = from->ecc->group->curve_oid;
  2509. }
  2510. else {
  2511. WOLFSSL_MSG("Missing ECC struct");
  2512. return WOLFSSL_FAILURE;
  2513. }
  2514. break;
  2515. #endif
  2516. #ifndef NO_DSA
  2517. case EVP_PKEY_DSA:
  2518. if (from->dsa) {
  2519. WOLFSSL_BIGNUM* cpy;
  2520. if (!to->dsa) {
  2521. if ((to->dsa = wolfSSL_DSA_new()) == NULL) {
  2522. WOLFSSL_MSG("wolfSSL_DSA_new error");
  2523. return WOLFSSL_FAILURE;
  2524. }
  2525. to->ownDsa = 1;
  2526. }
  2527. /* free existing BIGNUMs if needed before copying over new */
  2528. wolfSSL_BN_free(to->dsa->p);
  2529. wolfSSL_BN_free(to->dsa->g);
  2530. wolfSSL_BN_free(to->dsa->q);
  2531. to->dsa->p = NULL;
  2532. to->dsa->g = NULL;
  2533. to->dsa->q = NULL;
  2534. if (!(cpy = wolfSSL_BN_dup(from->dsa->p))) {
  2535. WOLFSSL_MSG("wolfSSL_BN_dup error");
  2536. return WOLFSSL_FAILURE;
  2537. }
  2538. to->dsa->p = cpy;
  2539. if (!(cpy = wolfSSL_BN_dup(from->dsa->q))) {
  2540. WOLFSSL_MSG("wolfSSL_BN_dup error");
  2541. return WOLFSSL_FAILURE;
  2542. }
  2543. to->dsa->q = cpy;
  2544. if (!(cpy = wolfSSL_BN_dup(from->dsa->g))) {
  2545. WOLFSSL_MSG("wolfSSL_BN_dup error");
  2546. return WOLFSSL_FAILURE;
  2547. }
  2548. to->dsa->g = cpy;
  2549. }
  2550. else {
  2551. WOLFSSL_MSG("Missing DSA struct");
  2552. return WOLFSSL_FAILURE;
  2553. }
  2554. break;
  2555. #endif
  2556. #ifndef NO_DH
  2557. case EVP_PKEY_DH:
  2558. if (from->dh) {
  2559. WOLFSSL_BIGNUM* cpy;
  2560. if (!to->dh) {
  2561. if ((to->dh = wolfSSL_DH_new()) == NULL) {
  2562. WOLFSSL_MSG("wolfSSL_DH_new error");
  2563. return WOLFSSL_FAILURE;
  2564. }
  2565. to->ownDh = 1;
  2566. }
  2567. /* free existing BIGNUMs if needed before copying over new */
  2568. wolfSSL_BN_free(to->dh->p);
  2569. wolfSSL_BN_free(to->dh->g);
  2570. wolfSSL_BN_free(to->dh->q);
  2571. to->dh->p = NULL;
  2572. to->dh->g = NULL;
  2573. to->dh->q = NULL;
  2574. if (!(cpy = wolfSSL_BN_dup(from->dh->p))) {
  2575. WOLFSSL_MSG("wolfSSL_BN_dup error, DH p");
  2576. return WOLFSSL_FAILURE;
  2577. }
  2578. to->dh->p = cpy;
  2579. if (!(cpy = wolfSSL_BN_dup(from->dh->g))) {
  2580. WOLFSSL_MSG("wolfSSL_BN_dup error, DH g");
  2581. return WOLFSSL_FAILURE;
  2582. }
  2583. to->dh->g = cpy;
  2584. if (!(cpy = wolfSSL_BN_dup(from->dh->q))) {
  2585. WOLFSSL_MSG("wolfSSL_BN_dup error, DH q");
  2586. return WOLFSSL_FAILURE;
  2587. }
  2588. to->dh->q = cpy;
  2589. }
  2590. else {
  2591. WOLFSSL_MSG("Missing DH struct");
  2592. return WOLFSSL_FAILURE;
  2593. }
  2594. break;
  2595. #endif
  2596. #ifndef NO_RSA
  2597. case EVP_PKEY_RSA:
  2598. #endif
  2599. default:
  2600. WOLFSSL_MSG("Copy parameters not available for this key type");
  2601. return WOLFSSL_FAILURE;
  2602. }
  2603. return WOLFSSL_SUCCESS;
  2604. }
  2605. #ifndef NO_WOLFSSL_STUB
  2606. int wolfSSL_EVP_PKEY_missing_parameters(WOLFSSL_EVP_PKEY *pkey)
  2607. {
  2608. (void)pkey;
  2609. /* not using missing params callback and returning zero to indicate success */
  2610. return 0;
  2611. }
  2612. #endif
  2613. /* wolfSSL_EVP_PKEY_cmp
  2614. * returns 0 on success, -1 on failure.
  2615. *
  2616. * This behavior is different from openssl.
  2617. * EVP_PKEY_cmp returns:
  2618. * 1 : two keys match
  2619. * 0 : do not match
  2620. * -1: key types are different
  2621. * -2: the operation is not supported
  2622. * If you want this function behave the same as openSSL,
  2623. * define WOLFSSL_ERROR_CODE_OPENSSL so that WS_RETURN_CODE translates return
  2624. * codes to match OpenSSL equivalent behavior.
  2625. */
  2626. int wolfSSL_EVP_PKEY_cmp(const WOLFSSL_EVP_PKEY *a, const WOLFSSL_EVP_PKEY *b)
  2627. {
  2628. int ret = -1; /* failure */
  2629. int a_sz = 0, b_sz = 0;
  2630. if (a == NULL || b == NULL)
  2631. return WS_RETURN_CODE(ret, WOLFSSL_FAILURE);
  2632. /* check its the same type of key */
  2633. if (a->type != b->type)
  2634. return WS_RETURN_CODE(ret, -1);
  2635. /* get size based on key type */
  2636. switch (a->type) {
  2637. #ifndef NO_RSA
  2638. case EVP_PKEY_RSA:
  2639. a_sz = (int)wolfSSL_RSA_size((const WOLFSSL_RSA*)(a->rsa));
  2640. b_sz = (int)wolfSSL_RSA_size((const WOLFSSL_RSA*)(b->rsa));
  2641. break;
  2642. #endif /* !NO_RSA */
  2643. #ifdef HAVE_ECC
  2644. case EVP_PKEY_EC:
  2645. if (a->ecc == NULL || a->ecc->internal == NULL ||
  2646. b->ecc == NULL || b->ecc->internal == NULL) {
  2647. return ret;
  2648. }
  2649. a_sz = wc_ecc_size((ecc_key*)(a->ecc->internal));
  2650. b_sz = wc_ecc_size((ecc_key*)(b->ecc->internal));
  2651. break;
  2652. #endif /* HAVE_ECC */
  2653. default:
  2654. return WS_RETURN_CODE(ret, -2);
  2655. } /* switch (a->type) */
  2656. /* check size */
  2657. if (a_sz <= 0 || b_sz <= 0 || a_sz != b_sz) {
  2658. return WS_RETURN_CODE(ret, WOLFSSL_FAILURE);
  2659. }
  2660. /* check public key size */
  2661. if (a->pkey_sz > 0 && b->pkey_sz > 0 && a->pkey_sz != b->pkey_sz) {
  2662. return WS_RETURN_CODE(ret, WOLFSSL_FAILURE);
  2663. }
  2664. /* check public key */
  2665. if (a->pkey.ptr && b->pkey.ptr) {
  2666. if (XMEMCMP(a->pkey.ptr, b->pkey.ptr, a->pkey_sz) != 0) {
  2667. return WS_RETURN_CODE(ret, WOLFSSL_FAILURE);
  2668. }
  2669. }
  2670. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  2671. ret = 1; /* the keys match */
  2672. #else
  2673. ret = 0; /* success */
  2674. #endif
  2675. return ret;
  2676. }
  2677. /**
  2678. * validate DH algorithm parameters
  2679. * @param dh_key a pointer to WOLFSSL_EVP_PKEY_CTX structure
  2680. * @return WOLFSSL_SUCCESS on success, otherwise failure
  2681. */
  2682. static int DH_param_check(WOLFSSL_DH* dh_key)
  2683. {
  2684. int ret = WOLFSSL_SUCCESS;
  2685. WOLFSSL_BN_CTX* ctx = NULL;
  2686. WOLFSSL_BIGNUM *num1 = NULL;
  2687. WOLFSSL_BIGNUM *num2 = NULL;
  2688. WOLFSSL_ENTER("DH_param_check");
  2689. ctx = wolfSSL_BN_CTX_new();
  2690. if (ctx == NULL) {
  2691. WOLFSSL_MSG("failed to allocate memory");
  2692. return WOLFSSL_FAILURE;
  2693. }
  2694. num1 = wolfSSL_BN_new();
  2695. num2 = wolfSSL_BN_new();
  2696. if (num1 == NULL || num2 == NULL) {
  2697. WOLFSSL_MSG("failed to assign big number");
  2698. ret = WOLFSSL_FAILURE;
  2699. }
  2700. /* prime check */
  2701. if (ret == WOLFSSL_SUCCESS &&
  2702. wolfSSL_BN_is_odd(dh_key->p) == 0){
  2703. WOLFSSL_MSG("dh_key->p is not prime");
  2704. ret = WOLFSSL_FAILURE;
  2705. } /* TODO safe prime check. need BN_rshift1 */
  2706. /* generator check */
  2707. if (ret == WOLFSSL_SUCCESS &&
  2708. (wolfSSL_BN_is_one(dh_key->g) ||
  2709. wolfSSL_BN_is_negative(dh_key->g) ||
  2710. wolfSSL_BN_is_zero(dh_key->g))) {
  2711. WOLFSSL_MSG("dh_key->g is not suitable generator");
  2712. ret = WOLFSSL_FAILURE;
  2713. }
  2714. if (ret == WOLFSSL_SUCCESS &&
  2715. wolfSSL_BN_cmp(dh_key->p, dh_key->g) <= 0) {
  2716. WOLFSSL_MSG("dh_key->g is not suitable generator");
  2717. ret = WOLFSSL_FAILURE;
  2718. }
  2719. if (ret == WOLFSSL_SUCCESS &&
  2720. dh_key->q != NULL)
  2721. {
  2722. if (ret == WOLFSSL_SUCCESS &&
  2723. wolfSSL_BN_mod_exp(num1, dh_key->g, dh_key->q, dh_key->p, ctx) ==
  2724. WOLFSSL_FAILURE) {
  2725. WOLFSSL_MSG("BN_mod_exp failed");
  2726. ret = WOLFSSL_FAILURE;
  2727. }
  2728. else
  2729. if (ret == WOLFSSL_SUCCESS &&
  2730. wolfSSL_BN_is_one(num1) == WOLFSSL_FAILURE) {
  2731. WOLFSSL_MSG("dh_key->g is not suitable generator");
  2732. ret = WOLFSSL_FAILURE;
  2733. }
  2734. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  2735. /* test if the number q is prime. */
  2736. if (ret == WOLFSSL_SUCCESS &&
  2737. (wolfSSL_BN_is_prime_ex(dh_key->q, 64, ctx, NULL) <= 0)) {
  2738. WOLFSSL_MSG("dh_key->q is not prime or error during check.");
  2739. ret = WOLFSSL_FAILURE;
  2740. } /* else TODO check q div q - 1. need BN_div */
  2741. #endif
  2742. }
  2743. /* clean up */
  2744. wolfSSL_BN_CTX_free(ctx);
  2745. wolfSSL_BN_free(num1);
  2746. wolfSSL_BN_free(num2);
  2747. WOLFSSL_LEAVE("DH_param_check", WOLFSSL_SUCCESS);
  2748. return ret;
  2749. }
  2750. /**
  2751. * validate the algorithm parameters
  2752. * @param ctx a pointer to WOLFSSL_EVP_PKEY_CTX structure
  2753. * @return WOLFSSL_SUCCESS on success, otherwise failure
  2754. */
  2755. int wolfSSL_EVP_PKEY_param_check(WOLFSSL_EVP_PKEY_CTX* ctx)
  2756. {
  2757. int type;
  2758. int ret;
  2759. WOLFSSL_DH* dh_key = NULL;
  2760. /* sanity check */
  2761. if (ctx == NULL) {
  2762. return WOLFSSL_FAILURE;
  2763. }
  2764. type = wolfSSL_EVP_PKEY_type(wolfSSL_EVP_PKEY_base_id(ctx->pkey));
  2765. switch (type) {
  2766. #if !defined(NO_RSA)
  2767. case EVP_PKEY_RSA:
  2768. WOLFSSL_MSG("EVP_PKEY_RSA not yet implemented");
  2769. return WOLFSSL_FAILURE;
  2770. #endif
  2771. #if defined(HAVE_ECC)
  2772. case EVP_PKEY_EC:
  2773. WOLFSSL_MSG("EVP_PKEY_EC not yet implemented");
  2774. return WOLFSSL_FAILURE;
  2775. #endif
  2776. #if !defined(NO_DSA)
  2777. case EVP_PKEY_DSA:
  2778. WOLFSSL_MSG("EVP_PKEY_DSA not yet implemented");
  2779. return WOLFSSL_FAILURE;
  2780. #endif
  2781. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  2782. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  2783. case EVP_PKEY_DH:
  2784. dh_key = wolfSSL_EVP_PKEY_get1_DH(ctx->pkey);
  2785. if (dh_key != NULL) {
  2786. ret = DH_param_check(dh_key);
  2787. wolfSSL_DH_free(dh_key);
  2788. }
  2789. else
  2790. ret = WOLFSSL_FAILURE;
  2791. return ret;
  2792. #endif
  2793. #endif
  2794. default:
  2795. WOLFSSL_MSG("Unknown PKEY type");
  2796. break;
  2797. }
  2798. (void)ret;
  2799. (void)DH_param_check;
  2800. (void)dh_key;
  2801. return WOLFSSL_FAILURE;
  2802. }
  2803. /* Initialize structure for signing
  2804. *
  2805. * ctx WOLFSSL_EVP_MD_CTX structure to initialize
  2806. * type is the type of message digest to use
  2807. *
  2808. * returns WOLFSSL_SUCCESS on success
  2809. */
  2810. int wolfSSL_EVP_SignInit(WOLFSSL_EVP_MD_CTX *ctx, const WOLFSSL_EVP_MD *type)
  2811. {
  2812. if (ctx == NULL) return WOLFSSL_FAILURE;
  2813. WOLFSSL_ENTER("EVP_SignInit");
  2814. return wolfSSL_EVP_DigestInit(ctx,type);
  2815. }
  2816. int wolfSSL_EVP_SignInit_ex(WOLFSSL_EVP_MD_CTX* ctx,
  2817. const WOLFSSL_EVP_MD* type,
  2818. WOLFSSL_ENGINE *impl)
  2819. {
  2820. if (ctx == NULL) return WOLFSSL_FAILURE;
  2821. WOLFSSL_ENTER("EVP_SignInit");
  2822. return wolfSSL_EVP_DigestInit_ex(ctx,type,impl);
  2823. }
  2824. /* Update structure with data for signing
  2825. *
  2826. * ctx WOLFSSL_EVP_MD_CTX structure to update
  2827. * data buffer holding data to update with for sign
  2828. * len length of data buffer
  2829. *
  2830. * returns WOLFSSL_SUCCESS on success
  2831. */
  2832. int wolfSSL_EVP_SignUpdate(WOLFSSL_EVP_MD_CTX *ctx, const void *data, size_t len)
  2833. {
  2834. if (ctx == NULL) return 0;
  2835. WOLFSSL_ENTER("EVP_SignUpdate(");
  2836. return wolfSSL_EVP_DigestUpdate(ctx, data, len);
  2837. }
  2838. static const struct s_ent {
  2839. const enum wc_HashType macType;
  2840. const int nid;
  2841. const char *name;
  2842. } md_tbl[] = {
  2843. #ifndef NO_MD4
  2844. {WC_HASH_TYPE_MD4, NID_md4, "MD4"},
  2845. #endif /* NO_MD4 */
  2846. #ifndef NO_MD5
  2847. {WC_HASH_TYPE_MD5, NID_md5, "MD5"},
  2848. #endif /* NO_MD5 */
  2849. #ifndef NO_SHA
  2850. {WC_HASH_TYPE_SHA, NID_sha1, "SHA1"},
  2851. {WC_HASH_TYPE_SHA, NID_sha1, "SHA"}, /* Leave for backwards compatibility */
  2852. #endif /* NO_SHA */
  2853. #ifdef WOLFSSL_SHA224
  2854. {WC_HASH_TYPE_SHA224, NID_sha224, "SHA224"},
  2855. #endif /* WOLFSSL_SHA224 */
  2856. #ifndef NO_SHA256
  2857. {WC_HASH_TYPE_SHA256, NID_sha256, "SHA256"},
  2858. #endif
  2859. #ifdef WOLFSSL_SHA384
  2860. {WC_HASH_TYPE_SHA384, NID_sha384, "SHA384"},
  2861. #endif /* WOLFSSL_SHA384 */
  2862. #ifdef WOLFSSL_SHA512
  2863. {WC_HASH_TYPE_SHA512, NID_sha512, "SHA512"},
  2864. #endif /* WOLFSSL_SHA512 */
  2865. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  2866. {WC_HASH_TYPE_SHA512_224, NID_sha512_224, "SHA512_224"},
  2867. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  2868. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  2869. {WC_HASH_TYPE_SHA512_256, NID_sha512_256, "SHA512_256"},
  2870. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  2871. #ifndef WOLFSSL_NOSHA3_224
  2872. {WC_HASH_TYPE_SHA3_224, NID_sha3_224, "SHA3_224"},
  2873. #endif
  2874. #ifndef WOLFSSL_NOSHA3_256
  2875. {WC_HASH_TYPE_SHA3_256, NID_sha3_256, "SHA3_256"},
  2876. #endif
  2877. #ifndef WOLFSSL_NOSHA3_384
  2878. {WC_HASH_TYPE_SHA3_384, NID_sha3_384, "SHA3_384"},
  2879. #endif
  2880. #ifndef WOLFSSL_NOSHA3_512
  2881. {WC_HASH_TYPE_SHA3_512, NID_sha3_512, "SHA3_512"},
  2882. #endif
  2883. #ifdef HAVE_BLAKE2
  2884. {WC_HASH_TYPE_BLAKE2B, NID_blake2b512, "BLAKE2B512"},
  2885. #endif
  2886. #ifdef HAVE_BLAKE2S
  2887. {WC_HASH_TYPE_BLAKE2S, NID_blake2s256, "BLAKE2S256"},
  2888. #endif
  2889. #ifdef WOLFSSL_SHAKE128
  2890. {WC_HASH_TYPE_SHAKE128, NID_shake128, "SHAKE128"},
  2891. #endif
  2892. #ifdef WOLFSSL_SHAKE256
  2893. {WC_HASH_TYPE_SHAKE256, NID_shake256, "SHAKE256"},
  2894. #endif
  2895. {WC_HASH_TYPE_NONE, 0, NULL}
  2896. };
  2897. static enum wc_HashType EvpMd2MacType(const WOLFSSL_EVP_MD *md)
  2898. {
  2899. const struct s_ent *ent ;
  2900. if (md != NULL) {
  2901. for (ent = md_tbl; ent->name != NULL; ent++) {
  2902. if (XSTRCMP((const char *)md, ent->name) == 0) {
  2903. return ent->macType;
  2904. }
  2905. }
  2906. }
  2907. return WC_HASH_TYPE_NONE;
  2908. }
  2909. static const WOLFSSL_EVP_MD* wolfSSL_macType2EVP_md(enum wc_HashType type)
  2910. {
  2911. const struct s_ent *ent ;
  2912. for (ent = md_tbl; ent->name != NULL; ent++) {
  2913. if (ent->macType == type) {
  2914. return ent->name;
  2915. }
  2916. }
  2917. return NULL;
  2918. }
  2919. /* Finalize structure for signing
  2920. *
  2921. * ctx WOLFSSL_EVP_MD_CTX structure to finalize
  2922. * sigret buffer to hold resulting signature
  2923. * siglen length of sigret buffer
  2924. * pkey key to sign with
  2925. *
  2926. * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
  2927. */
  2928. int wolfSSL_EVP_SignFinal(WOLFSSL_EVP_MD_CTX *ctx, unsigned char *sigret,
  2929. unsigned int *siglen, WOLFSSL_EVP_PKEY *pkey)
  2930. {
  2931. unsigned int mdsize;
  2932. unsigned char md[WC_MAX_DIGEST_SIZE];
  2933. int ret;
  2934. (void)sigret;
  2935. (void)siglen;
  2936. WOLFSSL_ENTER("EVP_SignFinal");
  2937. if (ctx == NULL)
  2938. return WOLFSSL_FAILURE;
  2939. ret = wolfSSL_EVP_DigestFinal(ctx, md, &mdsize);
  2940. if (ret <= 0)
  2941. return ret;
  2942. switch (pkey->type) {
  2943. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  2944. case EVP_PKEY_RSA: {
  2945. int nid;
  2946. const WOLFSSL_EVP_MD *ctxmd;
  2947. ctxmd = wolfSSL_EVP_MD_CTX_md(ctx);
  2948. if (ctxmd == NULL)
  2949. return WOLFSSL_FAILURE;
  2950. nid = wolfSSL_EVP_MD_type(ctxmd);
  2951. if (nid < 0)
  2952. return WOLFSSL_FAILURE;
  2953. return wolfSSL_RSA_sign(nid, md, mdsize, sigret,
  2954. siglen, pkey->rsa);
  2955. }
  2956. #endif /* NO_RSA */
  2957. #ifndef NO_DSA
  2958. case EVP_PKEY_DSA: {
  2959. int bytes;
  2960. ret = wolfSSL_DSA_do_sign(md, sigret, pkey->dsa);
  2961. /* wolfSSL_DSA_do_sign() can return WOLFSSL_FATAL_ERROR */
  2962. if (ret != WOLFSSL_SUCCESS)
  2963. return ret;
  2964. bytes = wolfSSL_BN_num_bytes(pkey->dsa->q);
  2965. if (bytes == WOLFSSL_FAILURE || (int)*siglen < bytes * 2)
  2966. return WOLFSSL_FAILURE;
  2967. *siglen = bytes * 2;
  2968. return WOLFSSL_SUCCESS;
  2969. }
  2970. #endif
  2971. case EVP_PKEY_EC:
  2972. WOLFSSL_MSG("not implemented");
  2973. FALL_THROUGH;
  2974. default:
  2975. break;
  2976. }
  2977. return WOLFSSL_FAILURE;
  2978. }
  2979. /* Initialize structure for verifying signature
  2980. *
  2981. * ctx WOLFSSL_EVP_MD_CTX structure to initialize
  2982. * type is the type of message digest to use
  2983. *
  2984. * returns WOLFSSL_SUCCESS on success
  2985. */
  2986. int wolfSSL_EVP_VerifyInit(WOLFSSL_EVP_MD_CTX *ctx, const WOLFSSL_EVP_MD *type)
  2987. {
  2988. if (ctx == NULL) return WOLFSSL_FAILURE;
  2989. WOLFSSL_ENTER("EVP_VerifyInit");
  2990. return wolfSSL_EVP_DigestInit(ctx,type);
  2991. }
  2992. /* Update structure for verifying signature
  2993. *
  2994. * ctx WOLFSSL_EVP_MD_CTX structure to update
  2995. * data buffer holding data to update with for verify
  2996. * len length of data buffer
  2997. *
  2998. * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
  2999. */
  3000. int wolfSSL_EVP_VerifyUpdate(WOLFSSL_EVP_MD_CTX *ctx, const void *data, size_t len)
  3001. {
  3002. if (ctx == NULL) return WOLFSSL_FAILURE;
  3003. WOLFSSL_ENTER("EVP_VerifyUpdate");
  3004. return wolfSSL_EVP_DigestUpdate(ctx, data, len);
  3005. }
  3006. /* Finalize structure for verifying signature
  3007. *
  3008. * ctx WOLFSSL_EVP_MD_CTX structure to finalize
  3009. * sig buffer holding signature
  3010. * siglen length of sig buffer
  3011. * pkey key to verify with
  3012. *
  3013. * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
  3014. */
  3015. int wolfSSL_EVP_VerifyFinal(WOLFSSL_EVP_MD_CTX *ctx,
  3016. const unsigned char*sig, unsigned int siglen, WOLFSSL_EVP_PKEY *pkey)
  3017. {
  3018. int ret;
  3019. unsigned char md[WC_MAX_DIGEST_SIZE];
  3020. unsigned int mdsize;
  3021. if (ctx == NULL) return WOLFSSL_FAILURE;
  3022. WOLFSSL_ENTER("EVP_VerifyFinal");
  3023. ret = wolfSSL_EVP_DigestFinal(ctx, md, &mdsize);
  3024. if (ret <= 0) return ret;
  3025. (void)sig;
  3026. (void)siglen;
  3027. switch (pkey->type) {
  3028. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  3029. case EVP_PKEY_RSA: {
  3030. int nid;
  3031. const WOLFSSL_EVP_MD *ctxmd = wolfSSL_EVP_MD_CTX_md(ctx);
  3032. if (ctxmd == NULL) break;
  3033. nid = wolfSSL_EVP_MD_type(ctxmd);
  3034. if (nid < 0) break;
  3035. return wolfSSL_RSA_verify(nid, md, mdsize, sig,
  3036. (unsigned int)siglen, pkey->rsa);
  3037. }
  3038. #endif /* NO_RSA */
  3039. case EVP_PKEY_DSA:
  3040. case EVP_PKEY_EC:
  3041. WOLFSSL_MSG("not implemented");
  3042. FALL_THROUGH;
  3043. default:
  3044. break;
  3045. }
  3046. return WOLFSSL_FAILURE;
  3047. }
  3048. int wolfSSL_EVP_add_cipher(const WOLFSSL_EVP_CIPHER *cipher)
  3049. {
  3050. (void)cipher;
  3051. /* nothing to do */
  3052. return 0;
  3053. }
  3054. WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_mac_key(int type, WOLFSSL_ENGINE* e,
  3055. const unsigned char* key, int keylen)
  3056. {
  3057. WOLFSSL_EVP_PKEY* pkey;
  3058. (void)e;
  3059. if (type != EVP_PKEY_HMAC || (key == NULL && keylen != 0))
  3060. return NULL;
  3061. pkey = wolfSSL_EVP_PKEY_new();
  3062. if (pkey != NULL) {
  3063. pkey->pkey.ptr = (char*)XMALLOC(keylen, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  3064. if (pkey->pkey.ptr == NULL && keylen > 0) {
  3065. wolfSSL_EVP_PKEY_free(pkey);
  3066. pkey = NULL;
  3067. }
  3068. else {
  3069. if (keylen) {
  3070. XMEMCPY(pkey->pkey.ptr, key, keylen);
  3071. }
  3072. pkey->pkey_sz = keylen;
  3073. pkey->type = pkey->save_type = type;
  3074. }
  3075. }
  3076. return pkey;
  3077. }
  3078. const unsigned char* wolfSSL_EVP_PKEY_get0_hmac(const WOLFSSL_EVP_PKEY* pkey,
  3079. size_t* len)
  3080. {
  3081. if (pkey == NULL || len == NULL)
  3082. return NULL;
  3083. *len = (size_t)pkey->pkey_sz;
  3084. return (const unsigned char*)pkey->pkey.ptr;
  3085. }
  3086. /* Initialize an EVP_DigestSign/Verify operation.
  3087. * Initialize a digest for RSA and ECC keys, or HMAC for HMAC key.
  3088. */
  3089. static int wolfSSL_evp_digest_pk_init(WOLFSSL_EVP_MD_CTX *ctx,
  3090. WOLFSSL_EVP_PKEY_CTX **pctx,
  3091. const WOLFSSL_EVP_MD *type,
  3092. WOLFSSL_ENGINE *e,
  3093. WOLFSSL_EVP_PKEY *pkey)
  3094. {
  3095. if (!type) {
  3096. int default_digest;
  3097. if (wolfSSL_EVP_PKEY_get_default_digest_nid(pkey, &default_digest)
  3098. != WOLFSSL_SUCCESS) {
  3099. WOLFSSL_MSG("Could not get default digest");
  3100. return WOLFSSL_FAILURE;
  3101. }
  3102. type = wolfSSL_EVP_get_digestbynid(default_digest);
  3103. if (!type) {
  3104. return BAD_FUNC_ARG;
  3105. }
  3106. }
  3107. if (pkey->type == EVP_PKEY_HMAC) {
  3108. int hashType;
  3109. const unsigned char* key;
  3110. #ifndef NO_SHA256
  3111. if (XSTRCMP(type, "SHA256") == 0) {
  3112. hashType = WC_SHA256;
  3113. } else
  3114. #endif
  3115. #ifndef NO_SHA
  3116. if ((XSTRCMP(type, "SHA") == 0) || (XSTRCMP(type, "SHA1") == 0)) {
  3117. hashType = WC_SHA;
  3118. } else
  3119. #endif /* NO_SHA */
  3120. #ifdef WOLFSSL_SHA224
  3121. if (XSTRCMP(type, "SHA224") == 0) {
  3122. hashType = WC_SHA224;
  3123. } else
  3124. #endif
  3125. #ifdef WOLFSSL_SHA384
  3126. if (XSTRCMP(type, "SHA384") == 0) {
  3127. hashType = WC_SHA384;
  3128. } else
  3129. #endif
  3130. #ifdef WOLFSSL_SHA512
  3131. if (XSTRCMP(type, "SHA512") == 0) {
  3132. hashType = WC_SHA512;
  3133. } else
  3134. #endif
  3135. #ifdef WOLFSSL_SHA3
  3136. #ifndef WOLFSSL_NOSHA3_224
  3137. if (XSTRCMP(type, "SHA3_224") == 0) {
  3138. hashType = WC_SHA3_224;
  3139. } else
  3140. #endif
  3141. #ifndef WOLFSSL_NOSHA3_256
  3142. if (XSTRCMP(type, "SHA3_256") == 0) {
  3143. hashType = WC_SHA3_256;
  3144. } else
  3145. #endif
  3146. #ifndef WOLFSSL_NOSHA3_384
  3147. if (XSTRCMP(type, "SHA3_384") == 0) {
  3148. hashType = WC_SHA3_384;
  3149. } else
  3150. #endif
  3151. #ifndef WOLFSSL_NOSHA3_512
  3152. if (XSTRCMP(type, "SHA3_512") == 0) {
  3153. hashType = WC_SHA3_512;
  3154. } else
  3155. #endif
  3156. #endif
  3157. #ifndef NO_MD5
  3158. if (XSTRCMP(type, "MD5") == 0) {
  3159. hashType = WC_MD5;
  3160. } else
  3161. #endif
  3162. return BAD_FUNC_ARG;
  3163. {
  3164. size_t keySz = 0;
  3165. key = wolfSSL_EVP_PKEY_get0_hmac(pkey, &keySz);
  3166. if (wc_HmacInit(&ctx->hash.hmac, NULL, INVALID_DEVID) != 0)
  3167. return WOLFSSL_FAILURE;
  3168. if (wc_HmacSetKey(&ctx->hash.hmac, hashType, key, (word32)keySz) != 0)
  3169. return WOLFSSL_FAILURE;
  3170. }
  3171. ctx->isHMAC = 1;
  3172. }
  3173. else if (wolfSSL_EVP_DigestInit(ctx, type) != 1)
  3174. return WOLFSSL_FAILURE;
  3175. if (ctx->pctx == NULL) {
  3176. ctx->pctx = wolfSSL_EVP_PKEY_CTX_new(pkey, e);
  3177. if (ctx->pctx == NULL)
  3178. return WOLFSSL_FAILURE;
  3179. }
  3180. if (pctx != NULL)
  3181. *pctx = ctx->pctx;
  3182. return WOLFSSL_SUCCESS;
  3183. }
  3184. /* Update an EVP_DigestSign/Verify operation.
  3185. * Update a digest for RSA and ECC keys, or HMAC for HMAC key.
  3186. */
  3187. static int wolfssl_evp_digest_pk_update(WOLFSSL_EVP_MD_CTX *ctx,
  3188. const void *d, unsigned int cnt)
  3189. {
  3190. if (ctx->isHMAC) {
  3191. if (wc_HmacUpdate(&ctx->hash.hmac, (const byte *)d, cnt) != 0)
  3192. return WOLFSSL_FAILURE;
  3193. return WOLFSSL_SUCCESS;
  3194. }
  3195. else
  3196. return wolfSSL_EVP_DigestUpdate(ctx, d, cnt);
  3197. }
  3198. /* Finalize an EVP_DigestSign/Verify operation - common part only.
  3199. * Finalize a digest for RSA and ECC keys, or HMAC for HMAC key.
  3200. * Copies the digest so that you can keep updating.
  3201. */
  3202. static int wolfssl_evp_digest_pk_final(WOLFSSL_EVP_MD_CTX *ctx,
  3203. unsigned char *md, unsigned int* mdlen)
  3204. {
  3205. int ret;
  3206. if (ctx->isHMAC) {
  3207. Hmac hmacCopy;
  3208. if (wolfSSL_HmacCopy(&hmacCopy, &ctx->hash.hmac) != WOLFSSL_SUCCESS)
  3209. return WOLFSSL_FAILURE;
  3210. ret = wc_HmacFinal(&hmacCopy, md) == 0;
  3211. wc_HmacFree(&hmacCopy);
  3212. return ret;
  3213. }
  3214. else {
  3215. WOLFSSL_EVP_MD_CTX ctxCopy;
  3216. if (wolfSSL_EVP_MD_CTX_copy_ex(&ctxCopy, ctx) != WOLFSSL_SUCCESS)
  3217. return WOLFSSL_FAILURE;
  3218. ret = wolfSSL_EVP_DigestFinal(&ctxCopy, md, mdlen);
  3219. wolfSSL_EVP_MD_CTX_cleanup(&ctxCopy);
  3220. return ret;
  3221. }
  3222. }
  3223. /* Get the length of the mac based on the digest algorithm. */
  3224. static int wolfssl_mac_len(unsigned char macType)
  3225. {
  3226. int hashLen;
  3227. switch (macType) {
  3228. #ifndef NO_MD5
  3229. case WC_MD5:
  3230. hashLen = WC_MD5_DIGEST_SIZE;
  3231. break;
  3232. #endif /* !NO_MD5 */
  3233. #ifndef NO_SHA
  3234. case WC_SHA:
  3235. hashLen = WC_SHA_DIGEST_SIZE;
  3236. break;
  3237. #endif /* !NO_SHA */
  3238. #ifdef WOLFSSL_SHA224
  3239. case WC_SHA224:
  3240. hashLen = WC_SHA224_DIGEST_SIZE;
  3241. break;
  3242. #endif /* WOLFSSL_SHA224 */
  3243. #ifndef NO_SHA256
  3244. case WC_SHA256:
  3245. hashLen = WC_SHA256_DIGEST_SIZE;
  3246. break;
  3247. #endif /* !NO_SHA256 */
  3248. #ifdef WOLFSSL_SHA384
  3249. case WC_SHA384:
  3250. hashLen = WC_SHA384_DIGEST_SIZE;
  3251. break;
  3252. #endif /* WOLFSSL_SHA384 */
  3253. #ifdef WOLFSSL_SHA512
  3254. case WC_SHA512:
  3255. hashLen = WC_SHA512_DIGEST_SIZE;
  3256. break;
  3257. #endif /* WOLFSSL_SHA512 */
  3258. #ifdef HAVE_BLAKE2
  3259. case BLAKE2B_ID:
  3260. hashLen = BLAKE2B_OUTBYTES;
  3261. break;
  3262. #endif /* HAVE_BLAKE2 */
  3263. #ifdef WOLFSSL_SHA3
  3264. #ifndef WOLFSSL_NOSHA3_224
  3265. case WC_SHA3_224:
  3266. hashLen = WC_SHA3_224_DIGEST_SIZE;
  3267. break;
  3268. #endif
  3269. #ifndef WOLFSSL_NOSHA3_256
  3270. case WC_SHA3_256:
  3271. hashLen = WC_SHA3_256_DIGEST_SIZE;
  3272. break;
  3273. #endif
  3274. #ifndef WOLFSSL_NOSHA3_384
  3275. case WC_SHA3_384:
  3276. hashLen = WC_SHA3_384_DIGEST_SIZE;
  3277. break;
  3278. #endif
  3279. #ifndef WOLFSSL_NOSHA3_512
  3280. case WC_SHA3_512:
  3281. hashLen = WC_SHA3_512_DIGEST_SIZE;
  3282. break;
  3283. #endif
  3284. #endif
  3285. default:
  3286. hashLen = 0;
  3287. }
  3288. return hashLen;
  3289. }
  3290. int wolfSSL_EVP_DigestSignInit(WOLFSSL_EVP_MD_CTX *ctx,
  3291. WOLFSSL_EVP_PKEY_CTX **pctx,
  3292. const WOLFSSL_EVP_MD *type,
  3293. WOLFSSL_ENGINE *e,
  3294. WOLFSSL_EVP_PKEY *pkey)
  3295. {
  3296. WOLFSSL_ENTER("EVP_DigestSignInit");
  3297. if (ctx == NULL || pkey == NULL)
  3298. return BAD_FUNC_ARG;
  3299. return wolfSSL_evp_digest_pk_init(ctx, pctx, type, e, pkey);
  3300. }
  3301. int wolfSSL_EVP_DigestSignUpdate(WOLFSSL_EVP_MD_CTX *ctx, const void *d,
  3302. unsigned int cnt)
  3303. {
  3304. WOLFSSL_ENTER("EVP_DigestSignUpdate");
  3305. if (ctx == NULL || d == NULL)
  3306. return BAD_FUNC_ARG;
  3307. return wolfssl_evp_digest_pk_update(ctx, d, cnt);
  3308. }
  3309. int wolfSSL_EVP_DigestSignFinal(WOLFSSL_EVP_MD_CTX *ctx, unsigned char *sig,
  3310. size_t *siglen)
  3311. {
  3312. unsigned char digest[WC_MAX_DIGEST_SIZE];
  3313. unsigned int hashLen;
  3314. int ret = WOLFSSL_FAILURE;
  3315. WOLFSSL_ENTER("EVP_DigestSignFinal");
  3316. if (ctx == NULL || siglen == NULL)
  3317. return WOLFSSL_FAILURE;
  3318. /* Return the maximum size of the signature when sig is NULL. */
  3319. if (ctx->isHMAC) {
  3320. hashLen = wolfssl_mac_len(ctx->hash.hmac.macType);
  3321. if (sig == NULL) {
  3322. *siglen = hashLen;
  3323. return WOLFSSL_SUCCESS;
  3324. }
  3325. }
  3326. #ifndef NO_RSA
  3327. else if (ctx->pctx->pkey->type == EVP_PKEY_RSA) {
  3328. if (sig == NULL) {
  3329. *siglen = wolfSSL_RSA_size(ctx->pctx->pkey->rsa);
  3330. return WOLFSSL_SUCCESS;
  3331. }
  3332. }
  3333. #endif /* !NO_RSA */
  3334. #ifdef HAVE_ECC
  3335. else if (ctx->pctx->pkey->type == EVP_PKEY_EC) {
  3336. if (sig == NULL) {
  3337. /* SEQ + INT + INT */
  3338. *siglen = ecc_sets[ctx->pctx->pkey->ecc->group->curve_idx].size * 2
  3339. + 8;
  3340. return WOLFSSL_SUCCESS;
  3341. }
  3342. }
  3343. #endif
  3344. if (wolfssl_evp_digest_pk_final(ctx, digest, &hashLen) <= 0)
  3345. return WOLFSSL_FAILURE;
  3346. if (ctx->isHMAC) {
  3347. /* Copy the HMAC result as signature. */
  3348. if ((unsigned int)(*siglen) > hashLen)
  3349. *siglen = hashLen;
  3350. /* May be a truncated signature. */
  3351. XMEMCPY(sig, digest, *siglen);
  3352. ret = WOLFSSL_SUCCESS;
  3353. }
  3354. else {
  3355. /* Sign the digest. */
  3356. switch (ctx->pctx->pkey->type) {
  3357. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  3358. case EVP_PKEY_RSA: {
  3359. unsigned int sigSz = (unsigned int)*siglen;
  3360. int nid;
  3361. const WOLFSSL_EVP_MD *md = wolfSSL_EVP_MD_CTX_md(ctx);
  3362. if (md == NULL)
  3363. break;
  3364. nid = wolfSSL_EVP_MD_type(md);
  3365. if (nid < 0)
  3366. break;
  3367. ret = wolfSSL_RSA_sign_generic_padding(nid, digest, hashLen,
  3368. sig, &sigSz, ctx->pctx->pkey->rsa, 1, ctx->pctx->padding);
  3369. if (ret >= 0)
  3370. *siglen = sigSz;
  3371. break;
  3372. }
  3373. #endif /* NO_RSA */
  3374. #ifdef HAVE_ECC
  3375. case EVP_PKEY_EC: {
  3376. int len;
  3377. WOLFSSL_ECDSA_SIG *ecdsaSig;
  3378. ecdsaSig = wolfSSL_ECDSA_do_sign(digest, hashLen,
  3379. ctx->pctx->pkey->ecc);
  3380. if (ecdsaSig == NULL)
  3381. break;
  3382. len = wolfSSL_i2d_ECDSA_SIG(ecdsaSig, &sig);
  3383. wolfSSL_ECDSA_SIG_free(ecdsaSig);
  3384. if (len == 0)
  3385. break;
  3386. *siglen = len;
  3387. ret = WOLFSSL_SUCCESS;
  3388. break;
  3389. }
  3390. #endif
  3391. default:
  3392. break;
  3393. }
  3394. }
  3395. ForceZero(digest, sizeof(digest));
  3396. return ret;
  3397. }
  3398. int wolfSSL_EVP_DigestVerifyInit(WOLFSSL_EVP_MD_CTX *ctx,
  3399. WOLFSSL_EVP_PKEY_CTX **pctx,
  3400. const WOLFSSL_EVP_MD *type,
  3401. WOLFSSL_ENGINE *e,
  3402. WOLFSSL_EVP_PKEY *pkey)
  3403. {
  3404. WOLFSSL_ENTER("EVP_DigestVerifyInit");
  3405. if (ctx == NULL || type == NULL || pkey == NULL)
  3406. return BAD_FUNC_ARG;
  3407. return wolfSSL_evp_digest_pk_init(ctx, pctx, type, e, pkey);
  3408. }
  3409. int wolfSSL_EVP_DigestVerifyUpdate(WOLFSSL_EVP_MD_CTX *ctx, const void *d,
  3410. size_t cnt)
  3411. {
  3412. WOLFSSL_ENTER("EVP_DigestVerifyUpdate");
  3413. if (ctx == NULL || d == NULL)
  3414. return BAD_FUNC_ARG;
  3415. return wolfssl_evp_digest_pk_update(ctx, d, (unsigned int)cnt);
  3416. }
  3417. int wolfSSL_EVP_DigestVerifyFinal(WOLFSSL_EVP_MD_CTX *ctx,
  3418. const unsigned char *sig, size_t siglen)
  3419. {
  3420. unsigned char digest[WC_MAX_DIGEST_SIZE];
  3421. unsigned int hashLen;
  3422. WOLFSSL_ENTER("EVP_DigestVerifyFinal");
  3423. if (ctx == NULL || sig == NULL)
  3424. return WOLFSSL_FAILURE;
  3425. if (ctx->isHMAC) {
  3426. hashLen = wolfssl_mac_len(ctx->hash.hmac.macType);
  3427. if (siglen > hashLen)
  3428. return WOLFSSL_FAILURE;
  3429. /* May be a truncated signature. */
  3430. }
  3431. if (wolfssl_evp_digest_pk_final(ctx, digest, &hashLen) <= 0)
  3432. return WOLFSSL_FAILURE;
  3433. if (ctx->isHMAC) {
  3434. /* Check HMAC result matches the signature. */
  3435. if (XMEMCMP(sig, digest, siglen) == 0)
  3436. return WOLFSSL_SUCCESS;
  3437. return WOLFSSL_FAILURE;
  3438. }
  3439. else {
  3440. /* Verify the signature with the digest. */
  3441. switch (ctx->pctx->pkey->type) {
  3442. #if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  3443. case EVP_PKEY_RSA: {
  3444. int nid;
  3445. const WOLFSSL_EVP_MD *md = wolfSSL_EVP_MD_CTX_md(ctx);
  3446. if (md == NULL)
  3447. return WOLFSSL_FAILURE;
  3448. nid = wolfSSL_EVP_MD_type(md);
  3449. if (nid < 0)
  3450. return WOLFSSL_FAILURE;
  3451. return wolfSSL_RSA_verify_ex(nid, digest, hashLen, sig,
  3452. (unsigned int)siglen,
  3453. ctx->pctx->pkey->rsa, ctx->pctx->padding);
  3454. }
  3455. #endif /* NO_RSA */
  3456. #ifdef HAVE_ECC
  3457. case EVP_PKEY_EC: {
  3458. int ret;
  3459. WOLFSSL_ECDSA_SIG *ecdsaSig;
  3460. ecdsaSig = wolfSSL_d2i_ECDSA_SIG(NULL, &sig, (long)siglen);
  3461. if (ecdsaSig == NULL)
  3462. return WOLFSSL_FAILURE;
  3463. ret = wolfSSL_ECDSA_do_verify(digest, hashLen, ecdsaSig,
  3464. ctx->pctx->pkey->ecc);
  3465. wolfSSL_ECDSA_SIG_free(ecdsaSig);
  3466. return ret;
  3467. }
  3468. #endif
  3469. default:
  3470. break;
  3471. }
  3472. }
  3473. return WOLFSSL_FAILURE;
  3474. }
  3475. #ifdef WOLFSSL_APACHE_HTTPD
  3476. #if !defined(USE_WINDOWS_API) && !defined(MICROCHIP_PIC32)
  3477. #include <termios.h>
  3478. #endif
  3479. #ifndef XGETPASSWD
  3480. static int XGETPASSWD(char* buf, int bufSz) {
  3481. int ret = WOLFSSL_SUCCESS;
  3482. /* turn off echo for passwords */
  3483. #ifdef USE_WINDOWS_API
  3484. DWORD originalTerm;
  3485. DWORD newTerm;
  3486. CONSOLE_SCREEN_BUFFER_INFO screenOrig;
  3487. HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
  3488. if (GetConsoleMode(stdinHandle, &originalTerm) == 0) {
  3489. WOLFSSL_MSG("Couldn't get the original terminal settings");
  3490. return WOLFSSL_FAILURE;
  3491. }
  3492. newTerm = originalTerm;
  3493. newTerm &= ~ENABLE_ECHO_INPUT;
  3494. if (SetConsoleMode(stdinHandle, newTerm) == 0) {
  3495. WOLFSSL_MSG("Couldn't turn off echo");
  3496. return WOLFSSL_FAILURE;
  3497. }
  3498. #else
  3499. struct termios originalTerm;
  3500. struct termios newTerm;
  3501. if (tcgetattr(STDIN_FILENO, &originalTerm) != 0) {
  3502. WOLFSSL_MSG("Couldn't get the original terminal settings");
  3503. return WOLFSSL_FAILURE;
  3504. }
  3505. XMEMCPY(&newTerm, &originalTerm, sizeof(struct termios));
  3506. newTerm.c_lflag &= ~ECHO;
  3507. newTerm.c_lflag |= (ICANON | ECHONL);
  3508. if (tcsetattr(STDIN_FILENO, TCSANOW, &newTerm) != 0) {
  3509. WOLFSSL_MSG("Couldn't turn off echo");
  3510. return WOLFSSL_FAILURE;
  3511. }
  3512. #endif
  3513. if (XFGETS(buf, bufSz, stdin) == NULL) {
  3514. ret = WOLFSSL_FAILURE;
  3515. }
  3516. /* restore default echo */
  3517. #ifdef USE_WINDOWS_API
  3518. if (SetConsoleMode(stdinHandle, originalTerm) == 0) {
  3519. WOLFSSL_MSG("Couldn't restore the terminal settings");
  3520. return WOLFSSL_FAILURE;
  3521. }
  3522. #else
  3523. if (tcsetattr(STDIN_FILENO, TCSANOW, &originalTerm) != 0) {
  3524. WOLFSSL_MSG("Couldn't restore the terminal settings");
  3525. return WOLFSSL_FAILURE;
  3526. }
  3527. #endif
  3528. return ret;
  3529. }
  3530. #endif
  3531. /* returns 0 on success and -2 or -1 on failure */
  3532. int wolfSSL_EVP_read_pw_string(char* buf, int bufSz, const char* banner, int v)
  3533. {
  3534. printf("%s", banner);
  3535. if (XGETPASSWD(buf, bufSz) == WOLFSSL_FAILURE) {
  3536. return -1;
  3537. }
  3538. (void)v; /* fgets always sanity checks size of input vs buffer */
  3539. return 0;
  3540. }
  3541. #endif /* WOLFSSL_APACHE_HTTPD */
  3542. #if !defined(NO_PWDBASED) && !defined(NO_SHA)
  3543. int wolfSSL_PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen,
  3544. const unsigned char *salt,
  3545. int saltlen, int iter,
  3546. int keylen, unsigned char *out)
  3547. {
  3548. const char *nostring = "";
  3549. int ret = 0;
  3550. if (pass == NULL) {
  3551. passlen = 0;
  3552. pass = nostring;
  3553. }
  3554. else if (passlen == -1) {
  3555. passlen = (int)XSTRLEN(pass);
  3556. }
  3557. ret = wc_PBKDF2((byte*)out, (byte*)pass, passlen, (byte*)salt, saltlen,
  3558. iter, keylen, WC_SHA);
  3559. if (ret == 0)
  3560. return WOLFSSL_SUCCESS;
  3561. else
  3562. return WOLFSSL_FAILURE;
  3563. }
  3564. #endif /* !NO_PWDBASED !NO_SHA*/
  3565. #if !defined(NO_PWDBASED)
  3566. int wolfSSL_PKCS5_PBKDF2_HMAC(const char *pass, int passlen,
  3567. const unsigned char *salt,
  3568. int saltlen, int iter,
  3569. const WOLFSSL_EVP_MD *digest,
  3570. int keylen, unsigned char *out)
  3571. {
  3572. const char *nostring = "";
  3573. int ret = 0;
  3574. if (pass == NULL) {
  3575. passlen = 0;
  3576. pass = nostring;
  3577. } else if (passlen == -1) {
  3578. passlen = (int)XSTRLEN(pass);
  3579. }
  3580. ret = wc_PBKDF2((byte*)out, (byte*)pass, passlen, (byte*)salt, saltlen,
  3581. iter, keylen, EvpMd2MacType(digest));
  3582. if (ret == 0)
  3583. return WOLFSSL_SUCCESS;
  3584. else
  3585. return WOLFSSL_FAILURE;
  3586. }
  3587. #endif /* !NO_PWDBASED */
  3588. #if defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && !defined(NO_PWDBASED) && \
  3589. !defined(NO_SHA256)
  3590. /**
  3591. * Derives a key from the specified password and the salt using SCRYPT
  3592. * algorithm.
  3593. *
  3594. * Parameters:
  3595. * - pass :password data. no need to be null-terminated. NULL is accepted.
  3596. * - passlen :length of the password. Must be 0 when pass is NULL.
  3597. * - salt :salt. NULL is accepted.
  3598. * - saltlen :length of the salt. Must be 0 when salt is NULL.
  3599. * - N :cost parameter. Must be grater or equal to 2 and be a power of 2.
  3600. * - r :block size. Must 1 or greater.
  3601. * - p :parallelism
  3602. * - maxmem :maximum size of buffer used for calculation in definition,
  3603. * Not referred in this implementation.
  3604. * - key :derived key.
  3605. * - keylen :length of the derived key
  3606. *
  3607. * Returns:
  3608. * 1 on success, otherwise 0.
  3609. */
  3610. int wolfSSL_EVP_PBE_scrypt(const char *pass, size_t passlen,
  3611. const unsigned char *salt, size_t saltlen,
  3612. word64 N, word64 r, word64 p,
  3613. word64 maxmem, unsigned char *key, size_t keylen)
  3614. {
  3615. int ret;
  3616. int exp = 0;
  3617. (void)maxmem;
  3618. WOLFSSL_ENTER("wolfSSL_EVP_PBE_scrypt");
  3619. if (r > INT32_MAX || p > INT32_MAX) {
  3620. WOLFSSL_MSG("Doesn't support greater than 32 bit values of r and p");
  3621. return WOLFSSL_FAILURE;
  3622. }
  3623. /* N must be a power of 2 and > 2.
  3624. if (N & (N-1)) is zero, it means N is a power of 2.
  3625. */
  3626. if (N < 2 || (N & (N-1)) || r <= 0 || p <= 0)
  3627. return WOLFSSL_FAILURE;
  3628. if (key == NULL)
  3629. return WOLFSSL_SUCCESS;
  3630. /* get exponent of power of 2. Confirmed N is power of 2. */
  3631. while (N != 1) {
  3632. N >>= 1;
  3633. exp++;
  3634. }
  3635. ret = wc_scrypt(key, (const byte*)pass, (int)passlen, salt, (int)saltlen,
  3636. exp, (int)r, (int)p, (int)keylen);
  3637. WOLFSSL_LEAVE("wolfSSL_EVP_PBE_scrypt", ret);
  3638. if (ret == 0)
  3639. return WOLFSSL_SUCCESS;
  3640. else
  3641. return WOLFSSL_FAILURE;
  3642. }
  3643. #endif /* HAVE_SCRYPT && HAVE_PBKDF2 && !NO_PWDBASED && !NO_SHA */
  3644. static const struct cipher{
  3645. unsigned char type;
  3646. const char *name;
  3647. int nid;
  3648. } cipher_tbl[] = {
  3649. #ifndef NO_AES
  3650. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  3651. #ifdef WOLFSSL_AES_128
  3652. {AES_128_CBC_TYPE, EVP_AES_128_CBC, NID_aes_128_cbc},
  3653. #endif
  3654. #ifdef WOLFSSL_AES_192
  3655. {AES_192_CBC_TYPE, EVP_AES_192_CBC, NID_aes_192_cbc},
  3656. #endif
  3657. #ifdef WOLFSSL_AES_256
  3658. {AES_256_CBC_TYPE, EVP_AES_256_CBC, NID_aes_256_cbc},
  3659. #endif
  3660. #endif
  3661. #ifdef WOLFSSL_AES_CFB
  3662. #ifdef WOLFSSL_AES_128
  3663. {AES_128_CFB1_TYPE, EVP_AES_128_CFB1, NID_aes_128_cfb1},
  3664. #endif
  3665. #ifdef WOLFSSL_AES_192
  3666. {AES_192_CFB1_TYPE, EVP_AES_192_CFB1, NID_aes_192_cfb1},
  3667. #endif
  3668. #ifdef WOLFSSL_AES_256
  3669. {AES_256_CFB1_TYPE, EVP_AES_256_CFB1, NID_aes_256_cfb1},
  3670. #endif
  3671. #ifdef WOLFSSL_AES_128
  3672. {AES_128_CFB8_TYPE, EVP_AES_128_CFB8, NID_aes_128_cfb8},
  3673. #endif
  3674. #ifdef WOLFSSL_AES_192
  3675. {AES_192_CFB8_TYPE, EVP_AES_192_CFB8, NID_aes_192_cfb8},
  3676. #endif
  3677. #ifdef WOLFSSL_AES_256
  3678. {AES_256_CFB8_TYPE, EVP_AES_256_CFB8, NID_aes_256_cfb8},
  3679. #endif
  3680. #ifdef WOLFSSL_AES_128
  3681. {AES_128_CFB128_TYPE, EVP_AES_128_CFB128, NID_aes_128_cfb128},
  3682. #endif
  3683. #ifdef WOLFSSL_AES_192
  3684. {AES_192_CFB128_TYPE, EVP_AES_192_CFB128, NID_aes_192_cfb128},
  3685. #endif
  3686. #ifdef WOLFSSL_AES_256
  3687. {AES_256_CFB128_TYPE, EVP_AES_256_CFB128, NID_aes_256_cfb128},
  3688. #endif
  3689. #endif
  3690. #ifdef HAVE_AES_OFB
  3691. #ifdef WOLFSSL_AES_128
  3692. {AES_128_OFB_TYPE, EVP_AES_128_OFB, NID_aes_128_ofb},
  3693. #endif
  3694. #ifdef WOLFSSL_AES_192
  3695. {AES_192_OFB_TYPE, EVP_AES_192_OFB, NID_aes_192_ofb},
  3696. #endif
  3697. #ifdef WOLFSSL_AES_256
  3698. {AES_256_OFB_TYPE, EVP_AES_256_OFB, NID_aes_256_ofb},
  3699. #endif
  3700. #endif
  3701. #ifdef HAVE_AES_XTS
  3702. #ifdef WOLFSSL_AES_128
  3703. {AES_128_XTS_TYPE, EVP_AES_128_XTS, NID_aes_128_xts},
  3704. #endif
  3705. #ifdef WOLFSSL_AES_256
  3706. {AES_256_XTS_TYPE, EVP_AES_256_XTS, NID_aes_256_xts},
  3707. #endif
  3708. #endif
  3709. #ifdef HAVE_AESGCM
  3710. #ifdef WOLFSSL_AES_128
  3711. {AES_128_GCM_TYPE, EVP_AES_128_GCM, NID_aes_128_gcm},
  3712. #endif
  3713. #ifdef WOLFSSL_AES_192
  3714. {AES_192_GCM_TYPE, EVP_AES_192_GCM, NID_aes_192_gcm},
  3715. #endif
  3716. #ifdef WOLFSSL_AES_256
  3717. {AES_256_GCM_TYPE, EVP_AES_256_GCM, NID_aes_256_gcm},
  3718. #endif
  3719. #endif
  3720. #ifdef WOLFSSL_AES_COUNTER
  3721. #ifdef WOLFSSL_AES_128
  3722. {AES_128_CTR_TYPE, EVP_AES_128_CTR, NID_aes_128_ctr},
  3723. #endif
  3724. #ifdef WOLFSSL_AES_192
  3725. {AES_192_CTR_TYPE, EVP_AES_192_CTR, NID_aes_192_ctr},
  3726. #endif
  3727. #ifdef WOLFSSL_AES_256
  3728. {AES_256_CTR_TYPE, EVP_AES_256_CTR, NID_aes_256_ctr},
  3729. #endif
  3730. #endif
  3731. #ifdef HAVE_AES_ECB
  3732. #ifdef WOLFSSL_AES_128
  3733. {AES_128_ECB_TYPE, EVP_AES_128_ECB, NID_aes_128_ecb},
  3734. #endif
  3735. #ifdef WOLFSSL_AES_192
  3736. {AES_192_ECB_TYPE, EVP_AES_192_ECB, NID_aes_192_ecb},
  3737. #endif
  3738. #ifdef WOLFSSL_AES_256
  3739. {AES_256_ECB_TYPE, EVP_AES_256_ECB, NID_aes_256_ecb},
  3740. #endif
  3741. #endif
  3742. #endif
  3743. #ifndef NO_DES3
  3744. {DES_CBC_TYPE, EVP_DES_CBC, NID_des_cbc},
  3745. {DES_ECB_TYPE, EVP_DES_ECB, NID_des_ecb},
  3746. {DES_EDE3_CBC_TYPE, EVP_DES_EDE3_CBC, NID_des_ede3_cbc},
  3747. {DES_EDE3_ECB_TYPE, EVP_DES_EDE3_ECB, NID_des_ede3_ecb},
  3748. #endif
  3749. #ifndef NO_RC4
  3750. {ARC4_TYPE, EVP_ARC4, NID_undef},
  3751. #endif
  3752. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  3753. {CHACHA20_POLY1305_TYPE, EVP_CHACHA20_POLY1305, NID_chacha20_poly1305},
  3754. #endif
  3755. { 0, NULL, 0}
  3756. };
  3757. /* returns cipher using provided ctx type */
  3758. const WOLFSSL_EVP_CIPHER *wolfSSL_EVP_CIPHER_CTX_cipher(
  3759. const WOLFSSL_EVP_CIPHER_CTX *ctx)
  3760. {
  3761. const struct cipher* c;
  3762. if (!ctx || !ctx->cipherType) {
  3763. return NULL;
  3764. }
  3765. for (c = cipher_tbl; c->type != 0; c++) {
  3766. if (ctx->cipherType == c->type) {
  3767. return wolfSSL_EVP_get_cipherbyname(c->name);
  3768. }
  3769. }
  3770. return NULL;
  3771. }
  3772. int wolfSSL_EVP_CIPHER_nid(const WOLFSSL_EVP_CIPHER *cipher)
  3773. {
  3774. const struct cipher* c;
  3775. if (!cipher) {
  3776. return 0;
  3777. }
  3778. for (c = cipher_tbl; c->type != 0; c++) {
  3779. if (XSTRCMP(cipher, c->name) == 0) {
  3780. return c->nid;
  3781. }
  3782. }
  3783. return 0;
  3784. }
  3785. const WOLFSSL_EVP_CIPHER *wolfSSL_EVP_get_cipherbyname(const char *name)
  3786. {
  3787. const struct alias {
  3788. const char *name;
  3789. const char *alias;
  3790. } cipher_alias_tbl[] = {
  3791. #ifndef NO_DES3
  3792. {EVP_DES_CBC, "des"},
  3793. {EVP_DES_ECB, "des-ecb"},
  3794. {EVP_DES_EDE3_CBC, "des3"},
  3795. {EVP_DES_EDE3_CBC, "3des"},
  3796. {EVP_DES_EDE3_ECB, "des-ede3"},
  3797. {EVP_DES_EDE3_ECB, "des-ede3-ecb"},
  3798. #endif
  3799. #ifndef NO_AES
  3800. #ifdef HAVE_AES_CBC
  3801. #ifdef WOLFSSL_AES_128
  3802. {EVP_AES_128_CBC, "aes128-cbc"},
  3803. {EVP_AES_128_CBC, "aes128"},
  3804. #endif
  3805. #ifdef WOLFSSL_AES_192
  3806. {EVP_AES_192_CBC, "aes192-cbc"},
  3807. {EVP_AES_192_CBC, "aes192"},
  3808. #endif
  3809. #ifdef WOLFSSL_AES_256
  3810. {EVP_AES_256_CBC, "aes256-cbc"},
  3811. {EVP_AES_256_CBC, "aes256"},
  3812. #endif
  3813. #endif
  3814. #ifdef HAVE_AES_ECB
  3815. #ifdef WOLFSSL_AES_128
  3816. {EVP_AES_128_ECB, "aes128-ecb"},
  3817. #endif
  3818. #ifdef WOLFSSL_AES_192
  3819. {EVP_AES_192_ECB, "aes192-ecb"},
  3820. #endif
  3821. #ifdef WOLFSSL_AES_256
  3822. {EVP_AES_256_ECB, "aes256-ecb"},
  3823. #endif
  3824. #endif
  3825. #ifdef HAVE_AESGCM
  3826. #ifdef WOLFSSL_AES_128
  3827. {EVP_AES_128_GCM, "aes-128-gcm"},
  3828. {EVP_AES_128_GCM, "id-aes128-GCM"},
  3829. #endif
  3830. #ifdef WOLFSSL_AES_192
  3831. {EVP_AES_192_GCM, "aes-192-gcm"},
  3832. {EVP_AES_192_GCM, "id-aes192-GCM"},
  3833. #endif
  3834. #ifdef WOLFSSL_AES_256
  3835. {EVP_AES_256_GCM, "aes-256-gcm"},
  3836. {EVP_AES_256_GCM, "id-aes256-GCM"},
  3837. #endif
  3838. #endif
  3839. #endif
  3840. #ifndef NO_RC4
  3841. {EVP_ARC4, "RC4"},
  3842. #endif
  3843. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  3844. {EVP_CHACHA20_POLY1305, "chacha20-poly1305"},
  3845. #endif
  3846. { NULL, NULL}
  3847. };
  3848. const struct cipher *ent;
  3849. const struct alias *al;
  3850. WOLFSSL_ENTER("EVP_get_cipherbyname");
  3851. for (al = cipher_alias_tbl; al->name != NULL; al++) {
  3852. /* Accept any case alternative version of an alias. */
  3853. if (XSTRCASECMP(name, al->alias) == 0) {
  3854. name = al->name;
  3855. break;
  3856. }
  3857. }
  3858. for (ent = cipher_tbl; ent->name != NULL; ent++) {
  3859. /* Accept any case alternative version of name. */
  3860. if (XSTRCASECMP(name, ent->name) == 0) {
  3861. return (WOLFSSL_EVP_CIPHER *)ent->name;
  3862. }
  3863. }
  3864. return NULL;
  3865. }
  3866. /*
  3867. * return an EVP_CIPHER structure when cipher NID is passed.
  3868. *
  3869. * id cipher NID
  3870. *
  3871. * return WOLFSSL_EVP_CIPHER
  3872. */
  3873. const WOLFSSL_EVP_CIPHER *wolfSSL_EVP_get_cipherbynid(int id)
  3874. {
  3875. WOLFSSL_ENTER("EVP_get_cipherbynid");
  3876. switch(id) {
  3877. #ifndef NO_AES
  3878. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  3879. #ifdef WOLFSSL_AES_128
  3880. case NID_aes_128_cbc:
  3881. return wolfSSL_EVP_aes_128_cbc();
  3882. #endif
  3883. #ifdef WOLFSSL_AES_192
  3884. case NID_aes_192_cbc:
  3885. return wolfSSL_EVP_aes_192_cbc();
  3886. #endif
  3887. #ifdef WOLFSSL_AES_256
  3888. case NID_aes_256_cbc:
  3889. return wolfSSL_EVP_aes_256_cbc();
  3890. #endif
  3891. #endif
  3892. #ifdef WOLFSSL_AES_COUNTER
  3893. #ifdef WOLFSSL_AES_128
  3894. case NID_aes_128_ctr:
  3895. return wolfSSL_EVP_aes_128_ctr();
  3896. #endif
  3897. #ifdef WOLFSSL_AES_192
  3898. case NID_aes_192_ctr:
  3899. return wolfSSL_EVP_aes_192_ctr();
  3900. #endif
  3901. #ifdef WOLFSSL_AES_256
  3902. case NID_aes_256_ctr:
  3903. return wolfSSL_EVP_aes_256_ctr();
  3904. #endif
  3905. #endif /* WOLFSSL_AES_COUNTER */
  3906. #ifdef HAVE_AES_ECB
  3907. #ifdef WOLFSSL_AES_128
  3908. case NID_aes_128_ecb:
  3909. return wolfSSL_EVP_aes_128_ecb();
  3910. #endif
  3911. #ifdef WOLFSSL_AES_192
  3912. case NID_aes_192_ecb:
  3913. return wolfSSL_EVP_aes_192_ecb();
  3914. #endif
  3915. #ifdef WOLFSSL_AES_256
  3916. case NID_aes_256_ecb:
  3917. return wolfSSL_EVP_aes_256_ecb();
  3918. #endif
  3919. #endif /* HAVE_AES_ECB */
  3920. #ifdef HAVE_AESGCM
  3921. #ifdef WOLFSSL_AES_128
  3922. case NID_aes_128_gcm:
  3923. return wolfSSL_EVP_aes_128_gcm();
  3924. #endif
  3925. #ifdef WOLFSSL_AES_192
  3926. case NID_aes_192_gcm:
  3927. return wolfSSL_EVP_aes_192_gcm();
  3928. #endif
  3929. #ifdef WOLFSSL_AES_256
  3930. case NID_aes_256_gcm:
  3931. return wolfSSL_EVP_aes_256_gcm();
  3932. #endif
  3933. #endif
  3934. #endif
  3935. #ifndef NO_DES3
  3936. case NID_des_cbc:
  3937. return wolfSSL_EVP_des_cbc();
  3938. #ifdef WOLFSSL_DES_ECB
  3939. case NID_des_ecb:
  3940. return wolfSSL_EVP_des_ecb();
  3941. #endif
  3942. case NID_des_ede3_cbc:
  3943. return wolfSSL_EVP_des_ede3_cbc();
  3944. #ifdef WOLFSSL_DES_ECB
  3945. case NID_des_ede3_ecb:
  3946. return wolfSSL_EVP_des_ede3_ecb();
  3947. #endif
  3948. #endif /*NO_DES3*/
  3949. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  3950. case NID_chacha20_poly1305:
  3951. return wolfSSL_EVP_chacha20_poly1305();
  3952. #endif
  3953. default:
  3954. WOLFSSL_MSG("Bad cipher id value");
  3955. }
  3956. return NULL;
  3957. }
  3958. void wolfSSL_EVP_init(void)
  3959. {
  3960. /* Does nothing. */
  3961. }
  3962. /* this function makes the assumption that out buffer is big enough for digest*/
  3963. int wolfSSL_EVP_Digest(const unsigned char* in, int inSz, unsigned char* out,
  3964. unsigned int* outSz, const WOLFSSL_EVP_MD* evp,
  3965. WOLFSSL_ENGINE* eng)
  3966. {
  3967. int err;
  3968. int hashType = WC_HASH_TYPE_NONE;
  3969. int hashSz;
  3970. WOLFSSL_ENTER("wolfSSL_EVP_Digest");
  3971. if (in == NULL || out == NULL || evp == NULL) {
  3972. WOLFSSL_MSG("Null argument passed in");
  3973. return WOLFSSL_FAILURE;
  3974. }
  3975. err = wolfSSL_EVP_get_hashinfo(evp, &hashType, &hashSz);
  3976. if (err != WOLFSSL_SUCCESS)
  3977. return err;
  3978. if (wc_Hash((enum wc_HashType)hashType, in, inSz, out, hashSz) != 0) {
  3979. return WOLFSSL_FAILURE;
  3980. }
  3981. if (outSz != NULL)
  3982. *outSz = hashSz;
  3983. (void)eng;
  3984. return WOLFSSL_SUCCESS;
  3985. }
  3986. static const struct alias {
  3987. const char *name;
  3988. const char *alias;
  3989. } digest_alias_tbl[] =
  3990. {
  3991. {"MD4", "ssl3-md4"},
  3992. {"MD5", "ssl3-md5"},
  3993. {"SHA1", "ssl3-sha1"},
  3994. {"SHA1", "SHA"},
  3995. { NULL, NULL}
  3996. };
  3997. const WOLFSSL_EVP_MD *wolfSSL_EVP_get_digestbyname(const char *name)
  3998. {
  3999. char nameUpper[15]; /* 15 bytes should be enough for any name */
  4000. size_t i;
  4001. const struct alias *al;
  4002. const struct s_ent *ent;
  4003. for (i = 0; i < sizeof(nameUpper) && name[i] != '\0'; i++) {
  4004. nameUpper[i] = (char)XTOUPPER((unsigned char) name[i]);
  4005. }
  4006. if (i < sizeof(nameUpper))
  4007. nameUpper[i] = '\0';
  4008. else
  4009. return NULL;
  4010. name = nameUpper;
  4011. for (al = digest_alias_tbl; al->name != NULL; al++)
  4012. if(XSTRCMP(name, al->alias) == 0) {
  4013. name = al->name;
  4014. break;
  4015. }
  4016. for (ent = md_tbl; ent->name != NULL; ent++)
  4017. if(XSTRCMP(name, ent->name) == 0) {
  4018. return (EVP_MD *)ent->name;
  4019. }
  4020. return NULL;
  4021. }
  4022. /* Returns the NID of the WOLFSSL_EVP_MD passed in.
  4023. *
  4024. * type - pointer to WOLFSSL_EVP_MD for which to return NID value
  4025. *
  4026. * Returns NID on success, or NID_undef if none exists.
  4027. */
  4028. int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD* type)
  4029. {
  4030. const struct s_ent *ent ;
  4031. WOLFSSL_ENTER("EVP_MD_type");
  4032. if (type == NULL) {
  4033. WOLFSSL_MSG("MD type arg is NULL");
  4034. return NID_undef;
  4035. }
  4036. for( ent = md_tbl; ent->name != NULL; ent++){
  4037. if(XSTRCMP((const char *)type, ent->name) == 0) {
  4038. return ent->nid;
  4039. }
  4040. }
  4041. return NID_undef;
  4042. }
  4043. #ifndef NO_MD4
  4044. /* return a pointer to MD4 EVP type */
  4045. const WOLFSSL_EVP_MD* wolfSSL_EVP_md4(void)
  4046. {
  4047. WOLFSSL_ENTER("wolfSSL_EVP_md4");
  4048. return EVP_get_digestbyname("MD4");
  4049. }
  4050. #endif /* !NO_MD4 */
  4051. #ifndef NO_MD5
  4052. const WOLFSSL_EVP_MD* wolfSSL_EVP_md5(void)
  4053. {
  4054. WOLFSSL_ENTER("EVP_md5");
  4055. return EVP_get_digestbyname("MD5");
  4056. }
  4057. #endif /* !NO_MD5 */
  4058. #ifdef HAVE_BLAKE2
  4059. /* return EVP_MD
  4060. * @param none
  4061. * @return "blake2b512"
  4062. */
  4063. const WOLFSSL_EVP_MD* wolfSSL_EVP_blake2b512(void)
  4064. {
  4065. WOLFSSL_ENTER("EVP_blake2b512");
  4066. return EVP_get_digestbyname("BLAKE2b512");
  4067. }
  4068. #endif
  4069. #ifdef HAVE_BLAKE2S
  4070. /* return EVP_MD
  4071. * @param none
  4072. * @return "blake2s256"
  4073. */
  4074. const WOLFSSL_EVP_MD* wolfSSL_EVP_blake2s256(void)
  4075. {
  4076. WOLFSSL_ENTER("EVP_blake2s256");
  4077. return EVP_get_digestbyname("BLAKE2s256");
  4078. }
  4079. #endif
  4080. #ifndef NO_WOLFSSL_STUB
  4081. void wolfSSL_EVP_set_pw_prompt(const char *prompt)
  4082. {
  4083. (void)prompt;
  4084. WOLFSSL_STUB("EVP_set_pw_prompt");
  4085. }
  4086. #endif
  4087. #ifndef NO_WOLFSSL_STUB
  4088. const WOLFSSL_EVP_MD* wolfSSL_EVP_mdc2(void)
  4089. {
  4090. WOLFSSL_STUB("EVP_mdc2");
  4091. return NULL;
  4092. }
  4093. #endif
  4094. #ifndef NO_SHA
  4095. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha1(void)
  4096. {
  4097. WOLFSSL_ENTER("EVP_sha1");
  4098. return EVP_get_digestbyname("SHA1");
  4099. }
  4100. #endif /* NO_SHA */
  4101. #ifdef WOLFSSL_SHA224
  4102. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha224(void)
  4103. {
  4104. WOLFSSL_ENTER("EVP_sha224");
  4105. return EVP_get_digestbyname("SHA224");
  4106. }
  4107. #endif /* WOLFSSL_SHA224 */
  4108. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha256(void)
  4109. {
  4110. WOLFSSL_ENTER("EVP_sha256");
  4111. return EVP_get_digestbyname("SHA256");
  4112. }
  4113. #ifdef WOLFSSL_SHA384
  4114. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha384(void)
  4115. {
  4116. WOLFSSL_ENTER("EVP_sha384");
  4117. return EVP_get_digestbyname("SHA384");
  4118. }
  4119. #endif /* WOLFSSL_SHA384 */
  4120. #ifdef WOLFSSL_SHA512
  4121. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha512(void)
  4122. {
  4123. WOLFSSL_ENTER("EVP_sha512");
  4124. return EVP_get_digestbyname("SHA512");
  4125. }
  4126. #ifndef WOLFSSL_NOSHA512_224
  4127. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha512_224(void)
  4128. {
  4129. WOLFSSL_ENTER("EVP_sha512_224");
  4130. return EVP_get_digestbyname("SHA512_224");
  4131. }
  4132. #endif /* !WOLFSSL_NOSHA512_224 */
  4133. #ifndef WOLFSSL_NOSHA512_224
  4134. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha512_256(void)
  4135. {
  4136. WOLFSSL_ENTER("EVP_sha512_256");
  4137. return EVP_get_digestbyname("SHA512_256");
  4138. }
  4139. #endif /* !WOLFSSL_NOSHA512_224 */
  4140. #endif /* WOLFSSL_SHA512 */
  4141. #ifdef WOLFSSL_SHA3
  4142. #ifndef WOLFSSL_NOSHA3_224
  4143. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha3_224(void)
  4144. {
  4145. WOLFSSL_ENTER("EVP_sha3_224");
  4146. return EVP_get_digestbyname("SHA3_224");
  4147. }
  4148. #endif /* WOLFSSL_NOSHA3_224 */
  4149. #ifndef WOLFSSL_NOSHA3_256
  4150. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha3_256(void)
  4151. {
  4152. WOLFSSL_ENTER("EVP_sha3_256");
  4153. return EVP_get_digestbyname("SHA3_256");
  4154. }
  4155. #endif /* WOLFSSL_NOSHA3_256 */
  4156. #ifndef WOLFSSL_NOSHA3_384
  4157. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha3_384(void)
  4158. {
  4159. WOLFSSL_ENTER("EVP_sha3_384");
  4160. return EVP_get_digestbyname("SHA3_384");
  4161. }
  4162. #endif /* WOLFSSL_NOSHA3_384 */
  4163. #ifndef WOLFSSL_NOSHA3_512
  4164. const WOLFSSL_EVP_MD* wolfSSL_EVP_sha3_512(void)
  4165. {
  4166. WOLFSSL_ENTER("EVP_sha3_512");
  4167. return EVP_get_digestbyname("SHA3_512");
  4168. }
  4169. #endif /* WOLFSSL_NOSHA3_512 */
  4170. #ifdef WOLFSSL_SHAKE128
  4171. const WOLFSSL_EVP_MD* wolfSSL_EVP_shake128(void)
  4172. {
  4173. WOLFSSL_ENTER("EVP_shake128");
  4174. return EVP_get_digestbyname("SHAKE128");
  4175. }
  4176. #endif /* WOLFSSL_SHAKE128 */
  4177. #ifdef WOLFSSL_SHAKE256
  4178. const WOLFSSL_EVP_MD* wolfSSL_EVP_shake256(void)
  4179. {
  4180. WOLFSSL_ENTER("EVP_shake256");
  4181. return EVP_get_digestbyname("SHAKE256");
  4182. }
  4183. #endif /* WOLFSSL_SHAKE256 */
  4184. #endif /* WOLFSSL_SHA3 */
  4185. WOLFSSL_EVP_MD_CTX *wolfSSL_EVP_MD_CTX_new(void)
  4186. {
  4187. WOLFSSL_EVP_MD_CTX* ctx;
  4188. WOLFSSL_ENTER("EVP_MD_CTX_new");
  4189. ctx = (WOLFSSL_EVP_MD_CTX*)XMALLOC(sizeof *ctx, NULL,
  4190. DYNAMIC_TYPE_OPENSSL);
  4191. if (ctx){
  4192. wolfSSL_EVP_MD_CTX_init(ctx);
  4193. }
  4194. return ctx;
  4195. }
  4196. void wolfSSL_EVP_MD_CTX_free(WOLFSSL_EVP_MD_CTX *ctx)
  4197. {
  4198. if (ctx) {
  4199. WOLFSSL_ENTER("EVP_MD_CTX_free");
  4200. wolfSSL_EVP_MD_CTX_cleanup(ctx);
  4201. XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
  4202. }
  4203. }
  4204. /* returns the NID of message digest used by the ctx */
  4205. int wolfSSL_EVP_MD_CTX_type(const WOLFSSL_EVP_MD_CTX *ctx)
  4206. {
  4207. const struct s_ent *ent;
  4208. WOLFSSL_ENTER("EVP_MD_CTX_type");
  4209. if (ctx) {
  4210. if (ctx->isHMAC) {
  4211. return NID_hmac;
  4212. }
  4213. for(ent = md_tbl; ent->name != NULL; ent++) {
  4214. if (ctx->macType == ent->macType) {
  4215. return ent->nid;
  4216. }
  4217. }
  4218. /* Return whatever we got */
  4219. return ctx->macType;
  4220. }
  4221. return 0;
  4222. }
  4223. /* returns WOLFSSL_SUCCESS on success */
  4224. int wolfSSL_EVP_MD_CTX_copy(WOLFSSL_EVP_MD_CTX *out, const WOLFSSL_EVP_MD_CTX *in)
  4225. {
  4226. return wolfSSL_EVP_MD_CTX_copy_ex(out, in);
  4227. }
  4228. /* returns digest size */
  4229. int wolfSSL_EVP_MD_CTX_size(const WOLFSSL_EVP_MD_CTX *ctx) {
  4230. return(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(ctx)));
  4231. }
  4232. /* returns block size */
  4233. int wolfSSL_EVP_MD_CTX_block_size(const WOLFSSL_EVP_MD_CTX *ctx) {
  4234. return(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(ctx)));
  4235. }
  4236. /* Deep copy of EVP_MD hasher
  4237. * return WOLFSSL_SUCCESS on success */
  4238. static int wolfSSL_EVP_MD_Copy_Hasher(WOLFSSL_EVP_MD_CTX* des,
  4239. const WOLFSSL_EVP_MD_CTX* src)
  4240. {
  4241. int ret;
  4242. if (src->isHMAC) {
  4243. return wolfSSL_HmacCopy(&des->hash.hmac, (Hmac*)&src->hash.hmac);
  4244. }
  4245. else {
  4246. switch (src->macType) {
  4247. case WC_HASH_TYPE_MD5:
  4248. #ifndef NO_MD5
  4249. ret = wc_Md5Copy((wc_Md5*)&src->hash.digest,
  4250. (wc_Md5*)&des->hash.digest);
  4251. #else
  4252. ret = NOT_COMPILED_IN;
  4253. #endif /* !NO_MD5 */
  4254. break;
  4255. case WC_HASH_TYPE_SHA:
  4256. #ifndef NO_SHA
  4257. ret = wc_ShaCopy((wc_Sha*)&src->hash.digest,
  4258. (wc_Sha*)&des->hash.digest);
  4259. #else
  4260. ret = NOT_COMPILED_IN;
  4261. #endif /* !NO_SHA */
  4262. break;
  4263. case WC_HASH_TYPE_SHA224:
  4264. #ifdef WOLFSSL_SHA224
  4265. ret = wc_Sha224Copy((wc_Sha224*)&src->hash.digest,
  4266. (wc_Sha224*)&des->hash.digest);
  4267. #else
  4268. ret = NOT_COMPILED_IN;
  4269. #endif /* WOLFSSL_SHA224 */
  4270. break;
  4271. case WC_HASH_TYPE_SHA256:
  4272. #ifndef NO_SHA256
  4273. ret = wc_Sha256Copy((wc_Sha256*)&src->hash.digest,
  4274. (wc_Sha256*)&des->hash.digest);
  4275. #else
  4276. ret = NOT_COMPILED_IN;
  4277. #endif /* !NO_SHA256 */
  4278. break;
  4279. case WC_HASH_TYPE_SHA384:
  4280. #ifdef WOLFSSL_SHA384
  4281. ret = wc_Sha384Copy((wc_Sha384*)&src->hash.digest,
  4282. (wc_Sha384*)&des->hash.digest);
  4283. #else
  4284. ret = NOT_COMPILED_IN;
  4285. #endif /* WOLFSSL_SHA384 */
  4286. break;
  4287. case WC_HASH_TYPE_SHA512:
  4288. #ifdef WOLFSSL_SHA512
  4289. ret = wc_Sha512Copy((wc_Sha512*)&src->hash.digest,
  4290. (wc_Sha512*)&des->hash.digest);
  4291. #else
  4292. ret = NOT_COMPILED_IN;
  4293. #endif /* WOLFSSL_SHA512 */
  4294. break;
  4295. #ifndef WOLFSSL_NOSHA512_224
  4296. case WC_HASH_TYPE_SHA512_224:
  4297. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  4298. defined(WOLFSSL_SHA512)
  4299. ret = wc_Sha512_224Copy((wc_Sha512*)&src->hash.digest,
  4300. (wc_Sha512*)&des->hash.digest);
  4301. #else
  4302. ret = NOT_COMPILED_IN;
  4303. #endif
  4304. break;
  4305. #endif /* !WOLFSSL_NOSHA512_224 */
  4306. #ifndef WOLFSSL_NOSHA512_256
  4307. case WC_HASH_TYPE_SHA512_256:
  4308. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  4309. defined(WOLFSSL_SHA512)
  4310. ret = wc_Sha512_256Copy((wc_Sha512*)&src->hash.digest,
  4311. (wc_Sha512*)&des->hash.digest);
  4312. #else
  4313. ret = NOT_COMPILED_IN;
  4314. #endif
  4315. break;
  4316. #endif /* !WOLFSSL_NOSHA512_256 */
  4317. case WC_HASH_TYPE_SHA3_224:
  4318. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  4319. ret = wc_Sha3_224_Copy((wc_Sha3*)&src->hash.digest,
  4320. (wc_Sha3*)&des->hash.digest);
  4321. #else
  4322. ret = NOT_COMPILED_IN;
  4323. #endif
  4324. break;
  4325. case WC_HASH_TYPE_SHA3_256:
  4326. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  4327. ret = wc_Sha3_256_Copy((wc_Sha3*)&src->hash.digest,
  4328. (wc_Sha3*)&des->hash.digest);
  4329. #else
  4330. ret = NOT_COMPILED_IN;
  4331. #endif
  4332. break;
  4333. case WC_HASH_TYPE_SHA3_384:
  4334. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  4335. ret = wc_Sha3_384_Copy((wc_Sha3*)&src->hash.digest,
  4336. (wc_Sha3*)&des->hash.digest);
  4337. #else
  4338. ret = NOT_COMPILED_IN;
  4339. #endif
  4340. break;
  4341. case WC_HASH_TYPE_SHA3_512:
  4342. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  4343. ret = wc_Sha3_512_Copy((wc_Sha3*)&src->hash.digest,
  4344. (wc_Sha3*)&des->hash.digest);
  4345. #else
  4346. ret = NOT_COMPILED_IN;
  4347. #endif
  4348. break;
  4349. case WC_HASH_TYPE_NONE:
  4350. case WC_HASH_TYPE_MD2:
  4351. case WC_HASH_TYPE_MD4:
  4352. case WC_HASH_TYPE_MD5_SHA:
  4353. case WC_HASH_TYPE_BLAKE2B:
  4354. case WC_HASH_TYPE_BLAKE2S:
  4355. #ifndef WOLFSSL_NO_SHAKE256
  4356. case WC_HASH_TYPE_SHAKE128:
  4357. case WC_HASH_TYPE_SHAKE256:
  4358. #endif
  4359. default:
  4360. ret = BAD_FUNC_ARG;
  4361. break;
  4362. }
  4363. return ret == 0 ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
  4364. }
  4365. }
  4366. /* copies structure in to the structure out
  4367. *
  4368. * returns WOLFSSL_SUCCESS on success */
  4369. int wolfSSL_EVP_MD_CTX_copy_ex(WOLFSSL_EVP_MD_CTX *out, const WOLFSSL_EVP_MD_CTX *in)
  4370. {
  4371. if ((out == NULL) || (in == NULL)) return WOLFSSL_FAILURE;
  4372. WOLFSSL_ENTER("EVP_CIPHER_MD_CTX_copy_ex");
  4373. XMEMCPY(out, in, sizeof(WOLFSSL_EVP_MD_CTX));
  4374. if (in->pctx != NULL) {
  4375. out->pctx = wolfSSL_EVP_PKEY_CTX_new(in->pctx->pkey, NULL);
  4376. if (out->pctx == NULL)
  4377. return WOLFSSL_FAILURE;
  4378. }
  4379. return wolfSSL_EVP_MD_Copy_Hasher(out, (WOLFSSL_EVP_MD_CTX*)in);
  4380. }
  4381. void wolfSSL_EVP_MD_CTX_init(WOLFSSL_EVP_MD_CTX* ctx)
  4382. {
  4383. WOLFSSL_ENTER("EVP_CIPHER_MD_CTX_init");
  4384. XMEMSET(ctx, 0, sizeof(WOLFSSL_EVP_MD_CTX));
  4385. }
  4386. const WOLFSSL_EVP_MD *wolfSSL_EVP_MD_CTX_md(const WOLFSSL_EVP_MD_CTX *ctx)
  4387. {
  4388. const struct s_ent *ent;
  4389. if (ctx == NULL)
  4390. return NULL;
  4391. WOLFSSL_ENTER("EVP_MD_CTX_md");
  4392. if (ctx->isHMAC) {
  4393. return "HMAC";
  4394. }
  4395. for(ent = md_tbl; ent->name != NULL; ent++) {
  4396. if(ctx->macType == ent->macType) {
  4397. return (const WOLFSSL_EVP_MD *)ent->name;
  4398. }
  4399. }
  4400. return (WOLFSSL_EVP_MD *)NULL;
  4401. }
  4402. /* return alias name if has
  4403. * @param n message digest type name
  4404. * @return alias name, otherwise NULL
  4405. */
  4406. static const char* hasAliasName(const char* n)
  4407. {
  4408. const char* aliasnm = NULL;
  4409. const struct alias *al;
  4410. for (al = digest_alias_tbl; al->name != NULL; al++)
  4411. if(XSTRCMP(n, al->name) == 0) {
  4412. aliasnm = al->alias;
  4413. break;
  4414. }
  4415. return aliasnm;
  4416. }
  4417. struct do_all_md {
  4418. void *arg;
  4419. void (*fn) (const WOLFSSL_EVP_MD *m,
  4420. const char* from, const char* to, void *arg);
  4421. };
  4422. /* do all md algorithm
  4423. * @param nm a pointer to WOLFSSL_OBJ_NAME
  4424. * @param arg arguments to pass to the callback
  4425. * @return none
  4426. */
  4427. static void md_do_all_func(const WOLFSSL_OBJ_NAME* nm, void* arg)
  4428. {
  4429. struct do_all_md *md = (struct do_all_md*)arg;
  4430. const struct s_ent *ent;
  4431. /* sanity check */
  4432. if (md == NULL || nm == NULL || md->fn == NULL ||
  4433. nm->type != WOLFSSL_OBJ_NAME_TYPE_MD_METH)
  4434. return;
  4435. /* loop all md */
  4436. for (ent = md_tbl; ent->name != NULL; ent++){
  4437. /* check if the md has alias */
  4438. if(hasAliasName(ent->name) != NULL) {
  4439. md->fn(NULL, ent->name, ent->name, md->arg);
  4440. }
  4441. else {
  4442. md->fn(ent->name, ent->name, NULL, md->arg);
  4443. }
  4444. }
  4445. }
  4446. /* call md_do_all function to do all md algorithm via a callback function
  4447. * @param fn a callback function to be called with all 'md'
  4448. * @param args arguments to pass to the callback
  4449. * @return none
  4450. */
  4451. void wolfSSL_EVP_MD_do_all(void (*fn) (const WOLFSSL_EVP_MD *m,
  4452. const char* from, const char* to, void* xx), void* args)
  4453. {
  4454. struct do_all_md md;
  4455. md.fn = fn;
  4456. md.arg = args;
  4457. wolfSSL_OBJ_NAME_do_all(WOLFSSL_OBJ_NAME_TYPE_MD_METH,
  4458. md_do_all_func, &md);
  4459. }
  4460. /* call "fn" based on OBJ_NAME type
  4461. * @param type OBJ_NAME type
  4462. * @param fn a callback function
  4463. * @param args arguments to pass to the callback
  4464. * @return none
  4465. */
  4466. void wolfSSL_OBJ_NAME_do_all(int type,
  4467. void (*fn)(const WOLFSSL_OBJ_NAME*, void* arg), void* arg)
  4468. {
  4469. WOLFSSL_OBJ_NAME objnm;
  4470. /* sanity check */
  4471. if (!fn)
  4472. return;
  4473. objnm.type = type;
  4474. switch(type) {
  4475. case WOLFSSL_OBJ_NAME_TYPE_MD_METH:
  4476. fn(&objnm, arg);
  4477. break;
  4478. case WOLFSSL_OBJ_NAME_TYPE_CIPHER_METH:
  4479. case WOLFSSL_OBJ_NAME_TYPE_PKEY_METH:
  4480. case WOLFSSL_OBJ_NAME_TYPE_COMP_METH:
  4481. case WOLFSSL_OBJ_NAME_TYPE_NUM:
  4482. WOLFSSL_MSG("not implemented");
  4483. FALL_THROUGH;
  4484. case WOLFSSL_OBJ_NAME_TYPE_UNDEF:
  4485. default:
  4486. break;
  4487. }
  4488. }
  4489. #ifndef NO_AES
  4490. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  4491. #ifdef WOLFSSL_AES_128
  4492. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cbc(void)
  4493. {
  4494. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cbc");
  4495. return EVP_AES_128_CBC;
  4496. }
  4497. #endif /* WOLFSSL_AES_128 */
  4498. #ifdef WOLFSSL_AES_192
  4499. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cbc(void)
  4500. {
  4501. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cbc");
  4502. return EVP_AES_192_CBC;
  4503. }
  4504. #endif /* WOLFSSL_AES_192 */
  4505. #ifdef WOLFSSL_AES_256
  4506. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cbc(void)
  4507. {
  4508. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cbc");
  4509. return EVP_AES_256_CBC;
  4510. }
  4511. #endif /* WOLFSSL_AES_256 */
  4512. #endif /* HAVE_AES_CBC */
  4513. #ifdef WOLFSSL_AES_CFB
  4514. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  4515. #ifdef WOLFSSL_AES_128
  4516. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cfb1(void)
  4517. {
  4518. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cfb1");
  4519. return EVP_AES_128_CFB1;
  4520. }
  4521. #endif /* WOLFSSL_AES_128 */
  4522. #ifdef WOLFSSL_AES_192
  4523. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cfb1(void)
  4524. {
  4525. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cfb1");
  4526. return EVP_AES_192_CFB1;
  4527. }
  4528. #endif /* WOLFSSL_AES_192 */
  4529. #ifdef WOLFSSL_AES_256
  4530. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cfb1(void)
  4531. {
  4532. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cfb1");
  4533. return EVP_AES_256_CFB1;
  4534. }
  4535. #endif /* WOLFSSL_AES_256 */
  4536. #ifdef WOLFSSL_AES_128
  4537. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cfb8(void)
  4538. {
  4539. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cfb8");
  4540. return EVP_AES_128_CFB8;
  4541. }
  4542. #endif /* WOLFSSL_AES_128 */
  4543. #ifdef WOLFSSL_AES_192
  4544. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cfb8(void)
  4545. {
  4546. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cfb8");
  4547. return EVP_AES_192_CFB8;
  4548. }
  4549. #endif /* WOLFSSL_AES_192 */
  4550. #ifdef WOLFSSL_AES_256
  4551. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cfb8(void)
  4552. {
  4553. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cfb8");
  4554. return EVP_AES_256_CFB8;
  4555. }
  4556. #endif /* WOLFSSL_AES_256 */
  4557. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  4558. #ifdef WOLFSSL_AES_128
  4559. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cfb128(void)
  4560. {
  4561. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cfb128");
  4562. return EVP_AES_128_CFB128;
  4563. }
  4564. #endif /* WOLFSSL_AES_128 */
  4565. #ifdef WOLFSSL_AES_192
  4566. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cfb128(void)
  4567. {
  4568. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cfb128");
  4569. return EVP_AES_192_CFB128;
  4570. }
  4571. #endif /* WOLFSSL_AES_192 */
  4572. #ifdef WOLFSSL_AES_256
  4573. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cfb128(void)
  4574. {
  4575. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cfb128");
  4576. return EVP_AES_256_CFB128;
  4577. }
  4578. #endif /* WOLFSSL_AES_256 */
  4579. #endif /* WOLFSSL_AES_CFB */
  4580. #ifdef WOLFSSL_AES_OFB
  4581. #ifdef WOLFSSL_AES_128
  4582. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ofb(void)
  4583. {
  4584. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ofb");
  4585. return EVP_AES_128_OFB;
  4586. }
  4587. #endif /* WOLFSSL_AES_128 */
  4588. #ifdef WOLFSSL_AES_192
  4589. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ofb(void)
  4590. {
  4591. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ofb");
  4592. return EVP_AES_192_OFB;
  4593. }
  4594. #endif /* WOLFSSL_AES_192 */
  4595. #ifdef WOLFSSL_AES_256
  4596. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ofb(void)
  4597. {
  4598. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ofb");
  4599. return EVP_AES_256_OFB;
  4600. }
  4601. #endif /* WOLFSSL_AES_256 */
  4602. #endif /* WOLFSSL_AES_OFB */
  4603. #ifdef WOLFSSL_AES_XTS
  4604. #ifdef WOLFSSL_AES_128
  4605. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_xts(void)
  4606. {
  4607. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_xts");
  4608. return EVP_AES_128_XTS;
  4609. }
  4610. #endif /* WOLFSSL_AES_128 */
  4611. #ifdef WOLFSSL_AES_256
  4612. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_xts(void)
  4613. {
  4614. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_xts");
  4615. return EVP_AES_256_XTS;
  4616. }
  4617. #endif /* WOLFSSL_AES_256 */
  4618. #endif /* WOLFSSL_AES_XTS */
  4619. #ifdef HAVE_AESGCM
  4620. #ifdef WOLFSSL_AES_128
  4621. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_gcm(void)
  4622. {
  4623. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_gcm");
  4624. return EVP_AES_128_GCM;
  4625. }
  4626. #endif /* WOLFSSL_GCM_128 */
  4627. #ifdef WOLFSSL_AES_192
  4628. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_gcm(void)
  4629. {
  4630. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_gcm");
  4631. return EVP_AES_192_GCM;
  4632. }
  4633. #endif /* WOLFSSL_AES_192 */
  4634. #ifdef WOLFSSL_AES_256
  4635. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_gcm(void)
  4636. {
  4637. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_gcm");
  4638. return EVP_AES_256_GCM;
  4639. }
  4640. #endif /* WOLFSSL_AES_256 */
  4641. #endif /* HAVE_AESGCM */
  4642. #ifdef WOLFSSL_AES_COUNTER
  4643. #ifdef WOLFSSL_AES_128
  4644. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ctr(void)
  4645. {
  4646. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ctr");
  4647. return EVP_AES_128_CTR;
  4648. }
  4649. #endif /* WOLFSSL_AES_2128 */
  4650. #ifdef WOLFSSL_AES_192
  4651. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ctr(void)
  4652. {
  4653. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ctr");
  4654. return EVP_AES_192_CTR;
  4655. }
  4656. #endif /* WOLFSSL_AES_192 */
  4657. #ifdef WOLFSSL_AES_256
  4658. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ctr(void)
  4659. {
  4660. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ctr");
  4661. return EVP_AES_256_CTR;
  4662. }
  4663. #endif /* WOLFSSL_AES_256 */
  4664. #endif /* WOLFSSL_AES_COUNTER */
  4665. #ifdef HAVE_AES_ECB
  4666. #ifdef WOLFSSL_AES_128
  4667. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ecb(void)
  4668. {
  4669. WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ecb");
  4670. return EVP_AES_128_ECB;
  4671. }
  4672. #endif /* WOLFSSL_AES_128 */
  4673. #ifdef WOLFSSL_AES_192
  4674. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ecb(void)
  4675. {
  4676. WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ecb");
  4677. return EVP_AES_192_ECB;
  4678. }
  4679. #endif /* WOLFSSL_AES_192*/
  4680. #ifdef WOLFSSL_AES_256
  4681. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ecb(void)
  4682. {
  4683. WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ecb");
  4684. return EVP_AES_256_ECB;
  4685. }
  4686. #endif /* WOLFSSL_AES_256 */
  4687. #endif /* HAVE_AES_ECB */
  4688. #endif /* NO_AES */
  4689. #ifndef NO_DES3
  4690. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_cbc(void)
  4691. {
  4692. WOLFSSL_ENTER("wolfSSL_EVP_des_cbc");
  4693. return EVP_DES_CBC;
  4694. }
  4695. #ifdef WOLFSSL_DES_ECB
  4696. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ecb(void)
  4697. {
  4698. WOLFSSL_ENTER("wolfSSL_EVP_des_ecb");
  4699. return EVP_DES_ECB;
  4700. }
  4701. #endif
  4702. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_cbc(void)
  4703. {
  4704. WOLFSSL_ENTER("wolfSSL_EVP_des_ede3_cbc");
  4705. return EVP_DES_EDE3_CBC;
  4706. }
  4707. #ifdef WOLFSSL_DES_ECB
  4708. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_ecb(void)
  4709. {
  4710. WOLFSSL_ENTER("wolfSSL_EVP_des_ede3_ecb");
  4711. return EVP_DES_EDE3_ECB;
  4712. }
  4713. #endif
  4714. #endif /* NO_DES3 */
  4715. #ifndef NO_RC4
  4716. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_rc4(void)
  4717. {
  4718. WOLFSSL_ENTER("wolfSSL_EVP_rc4");
  4719. return EVP_ARC4;
  4720. }
  4721. #endif
  4722. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4723. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_chacha20_poly1305(void)
  4724. {
  4725. WOLFSSL_ENTER("wolfSSL_EVP_chacha20_poly1305");
  4726. return EVP_CHACHA20_POLY1305;
  4727. }
  4728. #endif
  4729. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_enc_null(void)
  4730. {
  4731. WOLFSSL_ENTER("wolfSSL_EVP_enc_null");
  4732. return EVP_NULL;
  4733. }
  4734. int wolfSSL_EVP_MD_CTX_cleanup(WOLFSSL_EVP_MD_CTX* ctx)
  4735. {
  4736. int ret = WOLFSSL_SUCCESS;
  4737. WOLFSSL_ENTER("EVP_MD_CTX_cleanup");
  4738. if (ctx->pctx != NULL)
  4739. wolfSSL_EVP_PKEY_CTX_free(ctx->pctx);
  4740. if (ctx->isHMAC) {
  4741. wc_HmacFree(&ctx->hash.hmac);
  4742. }
  4743. else {
  4744. switch (ctx->macType) {
  4745. case WC_HASH_TYPE_MD5:
  4746. #ifndef NO_MD5
  4747. wc_Md5Free((wc_Md5*)&ctx->hash.digest);
  4748. #endif /* !NO_MD5 */
  4749. break;
  4750. case WC_HASH_TYPE_SHA:
  4751. #ifndef NO_SHA
  4752. wc_ShaFree((wc_Sha*)&ctx->hash.digest);
  4753. #endif /* !NO_SHA */
  4754. break;
  4755. case WC_HASH_TYPE_SHA224:
  4756. #ifdef WOLFSSL_SHA224
  4757. wc_Sha224Free((wc_Sha224*)&ctx->hash.digest);
  4758. #endif /* WOLFSSL_SHA224 */
  4759. break;
  4760. case WC_HASH_TYPE_SHA256:
  4761. #ifndef NO_SHA256
  4762. wc_Sha256Free((wc_Sha256*)&ctx->hash.digest);
  4763. #endif /* !NO_SHA256 */
  4764. break;
  4765. case WC_HASH_TYPE_SHA384:
  4766. #ifdef WOLFSSL_SHA384
  4767. wc_Sha384Free((wc_Sha384*)&ctx->hash.digest);
  4768. #endif /* WOLFSSL_SHA384 */
  4769. break;
  4770. case WC_HASH_TYPE_SHA512:
  4771. #ifdef WOLFSSL_SHA512
  4772. wc_Sha512Free((wc_Sha512*)&ctx->hash.digest);
  4773. #endif /* WOLFSSL_SHA512 */
  4774. break;
  4775. #ifndef WOLFSSL_NOSHA512_224
  4776. case WC_HASH_TYPE_SHA512_224:
  4777. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  4778. defined(WOLFSSL_SHA512)
  4779. wc_Sha512_224Free((wc_Sha512*)&ctx->hash.digest);
  4780. #endif
  4781. break;
  4782. #endif /* !WOLFSSL_NOSHA512_224 */
  4783. #ifndef WOLFSSL_NOSHA512_256
  4784. case WC_HASH_TYPE_SHA512_256:
  4785. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  4786. defined(WOLFSSL_SHA512)
  4787. wc_Sha512_256Free((wc_Sha512*)&ctx->hash.digest);
  4788. #endif
  4789. break;
  4790. #endif /* !WOLFSSL_NOSHA512_256 */
  4791. case WC_HASH_TYPE_SHA3_224:
  4792. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  4793. wc_Sha3_224_Free((wc_Sha3*)&ctx->hash.digest);
  4794. #endif
  4795. break;
  4796. case WC_HASH_TYPE_SHA3_256:
  4797. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  4798. wc_Sha3_256_Free((wc_Sha3*)&ctx->hash.digest);
  4799. #endif
  4800. break;
  4801. case WC_HASH_TYPE_SHA3_384:
  4802. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  4803. wc_Sha3_384_Free((wc_Sha3*)&ctx->hash.digest);
  4804. #endif
  4805. break;
  4806. case WC_HASH_TYPE_SHA3_512:
  4807. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  4808. wc_Sha3_512_Free((wc_Sha3*)&ctx->hash.digest);
  4809. #endif
  4810. break;
  4811. case WC_HASH_TYPE_NONE:
  4812. /* Not an error since an unused struct could be free'd or
  4813. * reset. */
  4814. break;
  4815. case WC_HASH_TYPE_MD2:
  4816. case WC_HASH_TYPE_MD4:
  4817. case WC_HASH_TYPE_MD5_SHA:
  4818. case WC_HASH_TYPE_BLAKE2B:
  4819. case WC_HASH_TYPE_BLAKE2S:
  4820. #ifndef WOLFSSL_NO_SHAKE256
  4821. case WC_HASH_TYPE_SHAKE128:
  4822. case WC_HASH_TYPE_SHAKE256:
  4823. #endif
  4824. default:
  4825. ret = WOLFSSL_FAILURE;
  4826. break;
  4827. }
  4828. }
  4829. ForceZero(ctx, sizeof(*ctx));
  4830. ctx->macType = WC_HASH_TYPE_NONE;
  4831. return ret;
  4832. }
  4833. void wolfSSL_EVP_CIPHER_CTX_init(WOLFSSL_EVP_CIPHER_CTX* ctx)
  4834. {
  4835. WOLFSSL_ENTER("EVP_CIPHER_CTX_init");
  4836. if (ctx) {
  4837. XMEMSET(ctx, 0, sizeof(WOLFSSL_EVP_CIPHER_CTX));
  4838. ctx->cipherType = WOLFSSL_EVP_CIPH_TYPE_INIT; /* not yet initialized */
  4839. ctx->keyLen = 0;
  4840. ctx->enc = 1; /* start in encrypt mode */
  4841. }
  4842. }
  4843. /* This function allows cipher specific parameters to be
  4844. determined and set. */
  4845. int wolfSSL_EVP_CIPHER_CTX_ctrl(WOLFSSL_EVP_CIPHER_CTX *ctx, int type, \
  4846. int arg, void *ptr)
  4847. {
  4848. int ret = WOLFSSL_FAILURE;
  4849. #ifndef WC_NO_RNG
  4850. WC_RNG rng;
  4851. #endif
  4852. if (ctx == NULL)
  4853. return WOLFSSL_FAILURE;
  4854. (void)arg;
  4855. (void)ptr;
  4856. WOLFSSL_ENTER("EVP_CIPHER_CTX_ctrl");
  4857. switch(type) {
  4858. case EVP_CTRL_INIT:
  4859. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4860. if(ctx)
  4861. ret = WOLFSSL_SUCCESS;
  4862. break;
  4863. case EVP_CTRL_SET_KEY_LENGTH:
  4864. ret = wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, arg);
  4865. break;
  4866. #if defined(HAVE_AESGCM) || (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  4867. case EVP_CTRL_AEAD_SET_IVLEN:
  4868. if ((ctx->flags & WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
  4869. break;
  4870. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4871. if (ctx->cipherType == CHACHA20_POLY1305_TYPE) {
  4872. if (arg != CHACHA20_POLY1305_AEAD_IV_SIZE) {
  4873. break;
  4874. }
  4875. }
  4876. else
  4877. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  4878. {
  4879. if (arg <= 0 || arg > AES_BLOCK_SIZE)
  4880. break;
  4881. }
  4882. ret = wolfSSL_EVP_CIPHER_CTX_set_iv_length(ctx, arg);
  4883. break;
  4884. case EVP_CTRL_AEAD_SET_IV_FIXED:
  4885. if ((ctx->flags & WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
  4886. break;
  4887. if (arg == -1) {
  4888. /* arg == -1 copies ctx->ivSz from ptr */
  4889. ret = wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, (byte*)ptr, ctx->ivSz);
  4890. }
  4891. #ifndef WC_NO_RNG
  4892. else {
  4893. /*
  4894. * Fixed field must be at least 4 bytes and invocation
  4895. * field at least 8.
  4896. */
  4897. if ((arg < 4) || (ctx->ivSz - arg) < 8) {
  4898. WOLFSSL_MSG("Fixed field or invocation field too short");
  4899. break;
  4900. }
  4901. /* arg is 4...(ctx->ivSz - 8) */
  4902. XMEMCPY(ctx->iv, ptr, arg);
  4903. if (wc_InitRng(&rng) != 0) {
  4904. WOLFSSL_MSG("wc_InitRng failed");
  4905. break;
  4906. }
  4907. if (wc_RNG_GenerateBlock(&rng, ctx->iv + arg,
  4908. ctx->ivSz - arg) == 0) {
  4909. ret = WOLFSSL_SUCCESS;
  4910. } else {
  4911. /* rng is freed immediately after if block so no need
  4912. * to do it here
  4913. */
  4914. WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
  4915. }
  4916. if (wc_FreeRng(&rng) != 0) {
  4917. WOLFSSL_MSG("wc_FreeRng failed");
  4918. ret = WOLFSSL_FAILURE;
  4919. break;
  4920. }
  4921. }
  4922. #ifdef HAVE_AESGCM
  4923. if (ret == WOLFSSL_SUCCESS) {
  4924. /*
  4925. * OpenSSL requires that a EVP_CTRL_AEAD_SET_IV_FIXED
  4926. * command be issued before a EVP_CTRL_GCM_IV_GEN command.
  4927. * This flag is used to enforce that.
  4928. */
  4929. ctx->gcmIvGenEnable = 1;
  4930. }
  4931. #endif
  4932. #endif /* !WC_NO_RNG */
  4933. break;
  4934. #if defined(HAVE_AESGCM) && !defined(_WIN32) && !defined(HAVE_SELFTEST) && \
  4935. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(2,0))
  4936. /*
  4937. * Using EVP_CTRL_GCM_IV_GEN is a way to do AES-GCM encrypt/decrypt
  4938. * multiple times with EVP_Cipher without having to call
  4939. * EVP_CipherInit between each iteration. The IV is incremented for
  4940. * each subsequent EVP_Cipher call to prevent IV reuse.
  4941. */
  4942. case EVP_CTRL_GCM_IV_GEN:
  4943. if ((ctx->flags & WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
  4944. break;
  4945. if (!ctx->gcmIvGenEnable) {
  4946. WOLFSSL_MSG("Must use EVP_CTRL_AEAD_SET_IV_FIXED before "
  4947. "EVP_CTRL_GCM_IV_GEN");
  4948. break;
  4949. }
  4950. if (ctx->cipher.aes.keylen == 0 || ctx->ivSz == 0) {
  4951. WOLFSSL_MSG("Key or IV not set");
  4952. break;
  4953. }
  4954. if (ptr == NULL) {
  4955. WOLFSSL_MSG("Destination buffer for IV bytes NULL.");
  4956. break;
  4957. }
  4958. if (arg <= 0 || arg > ctx->ivSz) {
  4959. XMEMCPY(ptr, ctx->iv, ctx->ivSz);
  4960. }
  4961. else {
  4962. /*
  4963. * Copy the last "arg" bytes of ctx->iv into the buffer at
  4964. * "ptr." Not sure why OpenSSL does this, but it does.
  4965. */
  4966. XMEMCPY(ptr, ctx->iv + ctx->ivSz - arg, arg);
  4967. }
  4968. /*
  4969. * The gcmIncIV flag indicates that the IV should be incremented
  4970. * after the next cipher operation.
  4971. */
  4972. ctx->gcmIncIv = 1;
  4973. ret = WOLFSSL_SUCCESS;
  4974. break;
  4975. #endif /* HAVE_AESGCM && !_WIN32 && !HAVE_SELFTEST && (!HAVE_FIPS ||
  4976. * FIPS_VERSION >= 2)*/
  4977. case EVP_CTRL_AEAD_SET_TAG:
  4978. if ((ctx->flags & WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
  4979. break;
  4980. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4981. if (ctx->cipherType == CHACHA20_POLY1305_TYPE) {
  4982. if (arg != CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
  4983. break;
  4984. }
  4985. ctx->authTagSz = arg;
  4986. ret = WOLFSSL_SUCCESS;
  4987. if (ptr != NULL) {
  4988. XMEMCPY(ctx->authTag, ptr, arg);
  4989. }
  4990. break;
  4991. }
  4992. else
  4993. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  4994. {
  4995. if(arg <= 0 || arg > 16 || (ptr == NULL))
  4996. break;
  4997. XMEMCPY(ctx->authTag, ptr, arg);
  4998. ctx->authTagSz = arg;
  4999. ret = WOLFSSL_SUCCESS;
  5000. break;
  5001. }
  5002. case EVP_CTRL_AEAD_GET_TAG:
  5003. if ((ctx->flags & WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
  5004. break;
  5005. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5006. if (ctx->cipherType == CHACHA20_POLY1305_TYPE) {
  5007. if (arg != CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) {
  5008. break;
  5009. }
  5010. }
  5011. else
  5012. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5013. {
  5014. if (arg <= 0 || arg > AES_BLOCK_SIZE)
  5015. break;
  5016. }
  5017. if (ptr != NULL) {
  5018. XMEMCPY(ptr, ctx->authTag, arg);
  5019. ret = WOLFSSL_SUCCESS;
  5020. }
  5021. break;
  5022. #endif /* HAVE_AESGCM || (HAVE_CHACHA && HAVE_POLY1305) */
  5023. default:
  5024. WOLFSSL_MSG("EVP_CIPHER_CTX_ctrl operation not yet handled");
  5025. break;
  5026. }
  5027. return ret;
  5028. }
  5029. /* WOLFSSL_SUCCESS on ok */
  5030. int wolfSSL_EVP_CIPHER_CTX_cleanup(WOLFSSL_EVP_CIPHER_CTX* ctx)
  5031. {
  5032. WOLFSSL_ENTER("EVP_CIPHER_CTX_cleanup");
  5033. if (ctx) {
  5034. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  5035. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  5036. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  5037. if ((ctx->cipherType == AES_128_GCM_TYPE) ||
  5038. (ctx->cipherType == AES_192_GCM_TYPE) ||
  5039. (ctx->cipherType == AES_256_GCM_TYPE)) {
  5040. wc_AesFree(&ctx->cipher.aes);
  5041. }
  5042. #endif /* HAVE_AESGCM && WOLFSSL_AESGCM_STREAM */
  5043. #endif /* not FIPS or FIPS v2+ */
  5044. ctx->cipherType = WOLFSSL_EVP_CIPH_TYPE_INIT; /* not yet initialized */
  5045. ctx->keyLen = 0;
  5046. #ifdef HAVE_AESGCM
  5047. if (ctx->gcmBuffer) {
  5048. XFREE(ctx->gcmBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  5049. ctx->gcmBuffer = NULL;
  5050. }
  5051. ctx->gcmBufferLen = 0;
  5052. if (ctx->gcmAuthIn) {
  5053. XFREE(ctx->gcmAuthIn, NULL, DYNAMIC_TYPE_OPENSSL);
  5054. ctx->gcmAuthIn = NULL;
  5055. }
  5056. ctx->gcmAuthInSz = 0;
  5057. ctx->gcmIvGenEnable = 0;
  5058. ctx->gcmIncIv = 0;
  5059. #endif
  5060. }
  5061. return WOLFSSL_SUCCESS;
  5062. }
  5063. /* Permanent stub for Qt compilation. */
  5064. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  5065. const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_rc2_cbc(void)
  5066. {
  5067. WOLFSSL_ENTER("wolfSSL_EVP_rc2_cbc");
  5068. WOLFSSL_STUB("EVP_rc2_cbc");
  5069. return NULL;
  5070. }
  5071. #endif
  5072. #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
  5073. int wolfSSL_EVP_BytesToKey(const WOLFSSL_EVP_CIPHER* type,
  5074. const WOLFSSL_EVP_MD* md, const byte* salt,
  5075. const byte* data, int sz, int count, byte* key, byte* iv)
  5076. {
  5077. int ret;
  5078. int hashType = WC_HASH_TYPE_NONE;
  5079. #ifdef WOLFSSL_SMALL_STACK
  5080. EncryptedInfo* info;
  5081. #else
  5082. EncryptedInfo info[1];
  5083. #endif
  5084. #ifdef WOLFSSL_SMALL_STACK
  5085. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  5086. DYNAMIC_TYPE_ENCRYPTEDINFO);
  5087. if (info == NULL) {
  5088. WOLFSSL_MSG("malloc failed");
  5089. return WOLFSSL_FAILURE;
  5090. }
  5091. #endif
  5092. XMEMSET(info, 0, sizeof(EncryptedInfo));
  5093. ret = wc_EncryptedInfoGet(info, type);
  5094. if (ret < 0)
  5095. goto end;
  5096. if (data == NULL) {
  5097. ret = info->keySz;
  5098. goto end;
  5099. }
  5100. ret = wolfSSL_EVP_get_hashinfo(md, &hashType, NULL);
  5101. if (ret == WOLFSSL_FAILURE)
  5102. goto end;
  5103. ret = wc_PBKDF1_ex(key, info->keySz, iv, info->ivSz, data, sz, salt,
  5104. EVP_SALT_SIZE, count, hashType, NULL);
  5105. if (ret == 0)
  5106. ret = info->keySz;
  5107. end:
  5108. #ifdef WOLFSSL_SMALL_STACK
  5109. XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO);
  5110. #endif
  5111. if (ret < 0)
  5112. return 0; /* failure - for compatibility */
  5113. return ret;
  5114. }
  5115. #endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */
  5116. #ifndef NO_AES
  5117. #if defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_192) || \
  5118. defined(WOLFSSL_AES_256)
  5119. #define AES_SIZE_ANY
  5120. #endif
  5121. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER) || \
  5122. defined(HAVE_AES_ECB) || defined(WOLFSSL_AES_CFB) || \
  5123. defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_DIRECT)
  5124. #define AES_SET_KEY
  5125. #endif
  5126. #if defined(AES_SIZE_ANY) && defined(AES_SET_KEY)
  5127. static int AesSetKey_ex(Aes* aes, const byte* key, word32 len,
  5128. const byte* iv, int dir, int direct)
  5129. {
  5130. int ret;
  5131. /* wc_AesSetKey clear aes.reg if iv == NULL.
  5132. Keep IV for openSSL compatibility */
  5133. if (iv == NULL)
  5134. XMEMCPY((byte *)aes->tmp, (byte *)aes->reg, AES_BLOCK_SIZE);
  5135. if (direct) {
  5136. #if defined(WOLFSSL_AES_DIRECT)
  5137. ret = wc_AesSetKeyDirect(aes, key, len, iv, dir);
  5138. #else
  5139. ret = NOT_COMPILED_IN;
  5140. #endif
  5141. }
  5142. else {
  5143. ret = wc_AesSetKey(aes, key, len, iv, dir);
  5144. }
  5145. if (iv == NULL)
  5146. XMEMCPY((byte *)aes->reg, (byte *)aes->tmp, AES_BLOCK_SIZE);
  5147. return ret;
  5148. }
  5149. #endif /* AES_ANY_SIZE && AES_SET_KEY */
  5150. #endif /* NO_AES */
  5151. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) \
  5152. || FIPS_VERSION_GE(2,0))
  5153. static int EvpCipherInitAesGCM(WOLFSSL_EVP_CIPHER_CTX* ctx,
  5154. const WOLFSSL_EVP_CIPHER* type,
  5155. const byte* key, const byte* iv, int enc)
  5156. {
  5157. int ret = WOLFSSL_SUCCESS;
  5158. if (ctx->gcmAuthIn) {
  5159. XFREE(ctx->gcmAuthIn, NULL, DYNAMIC_TYPE_OPENSSL);
  5160. ctx->gcmAuthIn = NULL;
  5161. }
  5162. ctx->gcmAuthInSz = 0;
  5163. ctx->block_size = AES_BLOCK_SIZE;
  5164. ctx->authTagSz = AES_BLOCK_SIZE;
  5165. if (ctx->ivSz == 0) {
  5166. ctx->ivSz = GCM_NONCE_MID_SZ;
  5167. }
  5168. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5169. ctx->flags |= WOLFSSL_EVP_CIPH_GCM_MODE |
  5170. WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER;
  5171. if (enc == 0 || enc == 1) {
  5172. ctx->enc = enc ? 1 : 0;
  5173. }
  5174. #ifdef WOLFSSL_AES_128
  5175. if (ctx->cipherType == AES_128_GCM_TYPE ||
  5176. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_GCM))) {
  5177. WOLFSSL_MSG("EVP_AES_128_GCM");
  5178. ctx->cipherType = AES_128_GCM_TYPE;
  5179. ctx->keyLen = AES_128_KEY_SIZE;
  5180. }
  5181. #endif
  5182. #ifdef WOLFSSL_AES_192
  5183. if (ctx->cipherType == AES_192_GCM_TYPE ||
  5184. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_GCM))) {
  5185. WOLFSSL_MSG("EVP_AES_192_GCM");
  5186. ctx->cipherType = AES_192_GCM_TYPE;
  5187. ctx->keyLen = AES_192_KEY_SIZE;
  5188. }
  5189. #endif
  5190. #ifdef WOLFSSL_AES_256
  5191. if (ctx->cipherType == AES_256_GCM_TYPE ||
  5192. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_GCM))) {
  5193. WOLFSSL_MSG("EVP_AES_256_GCM");
  5194. ctx->cipherType = AES_256_GCM_TYPE;
  5195. ctx->keyLen = AES_256_KEY_SIZE;
  5196. }
  5197. #endif
  5198. #ifndef WOLFSSL_AESGCM_STREAM
  5199. if (ret == WOLFSSL_SUCCESS && key &&
  5200. wc_AesGcmSetKey(&ctx->cipher.aes, key, ctx->keyLen)) {
  5201. WOLFSSL_MSG("wc_AesGcmSetKey() failed");
  5202. ret = WOLFSSL_FAILURE;
  5203. }
  5204. #endif /* !WOLFSSL_AESGCM_STREAM */
  5205. if (ret == WOLFSSL_SUCCESS && iv &&
  5206. wc_AesGcmSetExtIV(&ctx->cipher.aes, iv, ctx->ivSz)) {
  5207. WOLFSSL_MSG("wc_AesGcmSetExtIV() failed");
  5208. ret = WOLFSSL_FAILURE;
  5209. }
  5210. #ifdef WOLFSSL_AESGCM_STREAM
  5211. /*
  5212. * Initialize with key and IV if available. wc_AesGcmInit will fail
  5213. * if called with IV only and no key has been set.
  5214. */
  5215. if (ret == WOLFSSL_SUCCESS &&
  5216. (key || (iv && ctx->cipher.aes.gcmKeySet)) &&
  5217. wc_AesGcmInit(&ctx->cipher.aes, key,
  5218. (key == NULL) ? 0 : ctx->keyLen, iv,
  5219. (iv == NULL) ? 0 : ctx->ivSz) != 0) {
  5220. WOLFSSL_MSG("wc_AesGcmInit() failed");
  5221. ret = WOLFSSL_FAILURE;
  5222. }
  5223. #endif /* WOLFSSL_AESGCM_STREAM */
  5224. /*
  5225. * OpenSSL clears this flag, which permits subsequent use of
  5226. * EVP_CTRL_GCM_IV_GEN, when EVP_CipherInit is called with no key.
  5227. * If a key is provided, the flag retains its value.
  5228. */
  5229. if (ret == WOLFSSL_SUCCESS && key == NULL) {
  5230. ctx->gcmIvGenEnable = 0;
  5231. }
  5232. return ret;
  5233. }
  5234. static int EvpCipherAesGCM(WOLFSSL_EVP_CIPHER_CTX* ctx, byte* dst,
  5235. byte* src, word32 len)
  5236. {
  5237. int ret = WOLFSSL_FAILURE;
  5238. #ifndef WOLFSSL_AESGCM_STREAM
  5239. /* No destination means only AAD. */
  5240. if (src != NULL && dst == NULL) {
  5241. ret = wolfSSL_EVP_CipherUpdate_GCM_AAD(ctx, src, len);
  5242. }
  5243. else if (src != NULL && dst != NULL) {
  5244. if (ctx->enc) {
  5245. ret = wc_AesGcmEncrypt(&ctx->cipher.aes, dst, src,
  5246. len, ctx->iv, ctx->ivSz, ctx->authTag,
  5247. ctx->authTagSz, ctx->gcmAuthIn,
  5248. ctx->gcmAuthInSz);
  5249. }
  5250. else {
  5251. ret = wc_AesGcmDecrypt(&ctx->cipher.aes, dst, src,
  5252. len, ctx->iv, ctx->ivSz, ctx->authTag,
  5253. ctx->authTagSz, ctx->gcmAuthIn,
  5254. ctx->gcmAuthInSz);
  5255. }
  5256. if (ctx->gcmIncIv) {
  5257. IncCtr((byte*)ctx->cipher.aes.reg,
  5258. ctx->cipher.aes.nonceSz);
  5259. ctx->gcmIncIv = 0;
  5260. }
  5261. }
  5262. #else
  5263. /*
  5264. * No need to call wc_AesGcmInit. Should have been called by
  5265. * wolfSSL_EVP_CipherInit.
  5266. */
  5267. /* NULL dst and non-NULL src means only AAD. */
  5268. if (src != NULL && dst == NULL) {
  5269. if (ctx->enc) {
  5270. ret = wc_AesGcmEncryptUpdate(&ctx->cipher.aes, NULL,
  5271. NULL, 0, src, len);
  5272. }
  5273. else {
  5274. ret = wc_AesGcmDecryptUpdate(&ctx->cipher.aes, NULL,
  5275. NULL, 0, src, len);
  5276. }
  5277. }
  5278. /* Only plain/cipher text. */
  5279. else if (src != NULL && dst != NULL) {
  5280. if (ctx->enc) {
  5281. ret = wc_AesGcmEncryptUpdate(&ctx->cipher.aes, dst, src,
  5282. len, NULL, 0);
  5283. }
  5284. else {
  5285. ret = wc_AesGcmDecryptUpdate(&ctx->cipher.aes, dst, src,
  5286. len, NULL, 0);
  5287. }
  5288. }
  5289. /*
  5290. * src == NULL is analogous to other "final"-type functions
  5291. * (e.g. EVP_CipherFinal). Calculates tag on encrypt
  5292. * and checks tag on decrypt.
  5293. */
  5294. else {
  5295. if (ctx->enc) {
  5296. /* Calculate authentication tag. */
  5297. ret = wc_AesGcmEncryptFinal(&ctx->cipher.aes,
  5298. ctx->authTag, ctx->authTagSz);
  5299. /*
  5300. * wc_AesGcmEncryptFinal increments the IV in
  5301. * ctx->cipher.aes.reg, so we don't call IncCtr here.
  5302. */
  5303. }
  5304. else {
  5305. /* Calculate authentication tag and compare. */
  5306. ret = wc_AesGcmDecryptFinal(&ctx->cipher.aes,
  5307. ctx->authTag, ctx->authTagSz);
  5308. if (ctx->gcmIncIv) {
  5309. IncCtr((byte*)ctx->cipher.aes.reg,
  5310. ctx->cipher.aes.nonceSz);
  5311. }
  5312. }
  5313. /* Reinitialize for subsequent wolfSSL_EVP_Cipher calls. */
  5314. if (wc_AesGcmInit(&ctx->cipher.aes, NULL, 0,
  5315. (byte*)ctx->cipher.aes.reg,
  5316. ctx->ivSz) != 0) {
  5317. WOLFSSL_MSG("wc_AesGcmInit failed");
  5318. return WOLFSSL_FATAL_ERROR;
  5319. }
  5320. ctx->gcmIncIv = 0;
  5321. }
  5322. #endif /* WOLFSSL_AESGCM_STREAM */
  5323. if (src == NULL) {
  5324. /*
  5325. * Clear any leftover AAD on final (final is when src is
  5326. * NULL).
  5327. */
  5328. if (ctx->gcmAuthIn != NULL) {
  5329. XMEMSET(ctx->gcmAuthIn, 0, ctx->gcmAuthInSz);
  5330. }
  5331. ctx->gcmAuthInSz = 0;
  5332. }
  5333. if (ret == 0) {
  5334. ret = len;
  5335. }
  5336. return ret;
  5337. }
  5338. #endif /* HAVE_AESGCM && ((!HAVE_FIPS && !HAVE_SELFTEST) ||
  5339. * HAVE_FIPS_VERSION >= 2 */
  5340. /* return WOLFSSL_SUCCESS on ok, 0 on failure to match API compatibility */
  5341. int wolfSSL_EVP_CipherInit(WOLFSSL_EVP_CIPHER_CTX* ctx,
  5342. const WOLFSSL_EVP_CIPHER* type, const byte* key,
  5343. const byte* iv, int enc)
  5344. {
  5345. int ret = 0;
  5346. (void)key;
  5347. (void)iv;
  5348. (void)enc;
  5349. WOLFSSL_ENTER("wolfSSL_EVP_CipherInit");
  5350. if (ctx == NULL) {
  5351. WOLFSSL_MSG("no ctx");
  5352. return WOLFSSL_FAILURE;
  5353. }
  5354. if (type == NULL && ctx->cipherType == WOLFSSL_EVP_CIPH_TYPE_INIT) {
  5355. WOLFSSL_MSG("no type set");
  5356. return WOLFSSL_FAILURE;
  5357. }
  5358. if (ctx->cipherType == WOLFSSL_EVP_CIPH_TYPE_INIT){
  5359. /* only first EVP_CipherInit invoke. ctx->cipherType is set below */
  5360. XMEMSET(&ctx->cipher, 0, sizeof(ctx->cipher));
  5361. ctx->flags = 0;
  5362. }
  5363. /* always clear buffer state */
  5364. ctx->bufUsed = 0;
  5365. ctx->lastUsed = 0;
  5366. #ifdef HAVE_WOLFSSL_EVP_CIPHER_CTX_IV
  5367. if (!iv && ctx->ivSz) {
  5368. iv = ctx->iv;
  5369. }
  5370. #endif
  5371. #ifndef NO_AES
  5372. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  5373. #ifdef WOLFSSL_AES_128
  5374. if (ctx->cipherType == AES_128_CBC_TYPE ||
  5375. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_CBC))) {
  5376. WOLFSSL_MSG("EVP_AES_128_CBC");
  5377. ctx->cipherType = AES_128_CBC_TYPE;
  5378. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5379. ctx->flags |= WOLFSSL_EVP_CIPH_CBC_MODE;
  5380. ctx->keyLen = 16;
  5381. ctx->block_size = AES_BLOCK_SIZE;
  5382. ctx->ivSz = AES_BLOCK_SIZE;
  5383. if (enc == 0 || enc == 1)
  5384. ctx->enc = enc ? 1 : 0;
  5385. if (key) {
  5386. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5387. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, 0);
  5388. if (ret != 0)
  5389. return WOLFSSL_FAILURE;
  5390. }
  5391. if (iv && key == NULL) {
  5392. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5393. if (ret != 0)
  5394. return WOLFSSL_FAILURE;
  5395. }
  5396. }
  5397. #endif /* WOLFSSL_AES_128 */
  5398. #ifdef WOLFSSL_AES_192
  5399. if (ctx->cipherType == AES_192_CBC_TYPE ||
  5400. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_CBC))) {
  5401. WOLFSSL_MSG("EVP_AES_192_CBC");
  5402. ctx->cipherType = AES_192_CBC_TYPE;
  5403. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5404. ctx->flags |= WOLFSSL_EVP_CIPH_CBC_MODE;
  5405. ctx->keyLen = 24;
  5406. ctx->block_size = AES_BLOCK_SIZE;
  5407. ctx->ivSz = AES_BLOCK_SIZE;
  5408. if (enc == 0 || enc == 1)
  5409. ctx->enc = enc ? 1 : 0;
  5410. if (key) {
  5411. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5412. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, 0);
  5413. if (ret != 0)
  5414. return WOLFSSL_FAILURE;
  5415. }
  5416. if (iv && key == NULL) {
  5417. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5418. if (ret != 0)
  5419. return WOLFSSL_FAILURE;
  5420. }
  5421. }
  5422. #endif /* WOLFSSL_AES_192 */
  5423. #ifdef WOLFSSL_AES_256
  5424. if (ctx->cipherType == AES_256_CBC_TYPE ||
  5425. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_CBC))) {
  5426. WOLFSSL_MSG("EVP_AES_256_CBC");
  5427. ctx->cipherType = AES_256_CBC_TYPE;
  5428. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5429. ctx->flags |= WOLFSSL_EVP_CIPH_CBC_MODE;
  5430. ctx->keyLen = 32;
  5431. ctx->block_size = AES_BLOCK_SIZE;
  5432. ctx->ivSz = AES_BLOCK_SIZE;
  5433. if (enc == 0 || enc == 1)
  5434. ctx->enc = enc ? 1 : 0;
  5435. if (key) {
  5436. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5437. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, 0);
  5438. if (ret != 0){
  5439. WOLFSSL_MSG("AesSetKey() failed");
  5440. return WOLFSSL_FAILURE;
  5441. }
  5442. }
  5443. if (iv && key == NULL) {
  5444. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5445. if (ret != 0){
  5446. WOLFSSL_MSG("wc_AesSetIV() failed");
  5447. return WOLFSSL_FAILURE;
  5448. }
  5449. }
  5450. }
  5451. #endif /* WOLFSSL_AES_256 */
  5452. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  5453. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) \
  5454. || FIPS_VERSION_GE(2,0))
  5455. if (
  5456. #ifdef WOLFSSL_AES_128
  5457. ctx->cipherType == AES_128_GCM_TYPE ||
  5458. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_GCM))
  5459. #endif
  5460. #ifdef WOLFSSL_AES_192
  5461. || ctx->cipherType == AES_192_GCM_TYPE ||
  5462. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_GCM))
  5463. #endif
  5464. #ifdef WOLFSSL_AES_256
  5465. || ctx->cipherType == AES_256_GCM_TYPE ||
  5466. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_GCM))
  5467. #endif
  5468. ) {
  5469. if (EvpCipherInitAesGCM(ctx, type, key, iv, enc)
  5470. != WOLFSSL_SUCCESS) {
  5471. return WOLFSSL_FAILURE;
  5472. }
  5473. }
  5474. #endif /* HAVE_AESGCM && ((!HAVE_FIPS && !HAVE_SELFTEST) ||
  5475. * HAVE_FIPS_VERSION >= 2 */
  5476. #ifdef WOLFSSL_AES_COUNTER
  5477. #ifdef WOLFSSL_AES_128
  5478. if (ctx->cipherType == AES_128_CTR_TYPE ||
  5479. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_CTR))) {
  5480. WOLFSSL_MSG("EVP_AES_128_CTR");
  5481. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5482. ctx->cipherType = AES_128_CTR_TYPE;
  5483. ctx->flags |= WOLFSSL_EVP_CIPH_CTR_MODE;
  5484. ctx->keyLen = 16;
  5485. ctx->block_size = NO_PADDING_BLOCK_SIZE;
  5486. ctx->ivSz = AES_BLOCK_SIZE;
  5487. #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB)
  5488. ctx->cipher.aes.left = 0;
  5489. #endif
  5490. if (enc == 0 || enc == 1)
  5491. ctx->enc = enc ? 1 : 0;
  5492. if (key) {
  5493. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5494. AES_ENCRYPTION, 1);
  5495. if (ret != 0)
  5496. return WOLFSSL_FAILURE;
  5497. }
  5498. if (iv && key == NULL) {
  5499. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5500. if (ret != 0)
  5501. return WOLFSSL_FAILURE;
  5502. }
  5503. }
  5504. #endif /* WOLFSSL_AES_128 */
  5505. #ifdef WOLFSSL_AES_192
  5506. if (ctx->cipherType == AES_192_CTR_TYPE ||
  5507. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_CTR))) {
  5508. WOLFSSL_MSG("EVP_AES_192_CTR");
  5509. ctx->cipherType = AES_192_CTR_TYPE;
  5510. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5511. ctx->flags |= WOLFSSL_EVP_CIPH_CTR_MODE;
  5512. ctx->keyLen = 24;
  5513. ctx->block_size = NO_PADDING_BLOCK_SIZE;
  5514. ctx->ivSz = AES_BLOCK_SIZE;
  5515. #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB)
  5516. ctx->cipher.aes.left = 0;
  5517. #endif
  5518. if (enc == 0 || enc == 1)
  5519. ctx->enc = enc ? 1 : 0;
  5520. if (key) {
  5521. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5522. AES_ENCRYPTION, 1);
  5523. if (ret != 0)
  5524. return WOLFSSL_FAILURE;
  5525. }
  5526. if (iv && key == NULL) {
  5527. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5528. if (ret != 0)
  5529. return WOLFSSL_FAILURE;
  5530. }
  5531. }
  5532. #endif /* WOLFSSL_AES_192 */
  5533. #ifdef WOLFSSL_AES_256
  5534. if (ctx->cipherType == AES_256_CTR_TYPE ||
  5535. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_CTR))) {
  5536. WOLFSSL_MSG("EVP_AES_256_CTR");
  5537. ctx->cipherType = AES_256_CTR_TYPE;
  5538. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5539. ctx->flags |= WOLFSSL_EVP_CIPH_CTR_MODE;
  5540. ctx->keyLen = 32;
  5541. ctx->block_size = NO_PADDING_BLOCK_SIZE;
  5542. ctx->ivSz = AES_BLOCK_SIZE;
  5543. #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB)
  5544. ctx->cipher.aes.left = 0;
  5545. #endif
  5546. if (enc == 0 || enc == 1)
  5547. ctx->enc = enc ? 1 : 0;
  5548. if (key) {
  5549. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5550. AES_ENCRYPTION, 1);
  5551. if (ret != 0)
  5552. return WOLFSSL_FAILURE;
  5553. }
  5554. if (iv && key == NULL) {
  5555. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5556. if (ret != 0)
  5557. return WOLFSSL_FAILURE;
  5558. }
  5559. }
  5560. #endif /* WOLFSSL_AES_256 */
  5561. #endif /* WOLFSSL_AES_COUNTER */
  5562. #ifdef HAVE_AES_ECB
  5563. #ifdef WOLFSSL_AES_128
  5564. if (ctx->cipherType == AES_128_ECB_TYPE ||
  5565. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_ECB))) {
  5566. WOLFSSL_MSG("EVP_AES_128_ECB");
  5567. ctx->cipherType = AES_128_ECB_TYPE;
  5568. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5569. ctx->flags |= WOLFSSL_EVP_CIPH_ECB_MODE;
  5570. ctx->keyLen = 16;
  5571. ctx->block_size = AES_BLOCK_SIZE;
  5572. if (enc == 0 || enc == 1)
  5573. ctx->enc = enc ? 1 : 0;
  5574. if (key) {
  5575. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, NULL,
  5576. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, 1);
  5577. }
  5578. if (ret != 0)
  5579. return WOLFSSL_FAILURE;
  5580. }
  5581. #endif /* WOLFSSL_AES_128 */
  5582. #ifdef WOLFSSL_AES_192
  5583. if (ctx->cipherType == AES_192_ECB_TYPE ||
  5584. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_ECB))) {
  5585. WOLFSSL_MSG("EVP_AES_192_ECB");
  5586. ctx->cipherType = AES_192_ECB_TYPE;
  5587. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5588. ctx->flags |= WOLFSSL_EVP_CIPH_ECB_MODE;
  5589. ctx->keyLen = 24;
  5590. ctx->block_size = AES_BLOCK_SIZE;
  5591. if (enc == 0 || enc == 1)
  5592. ctx->enc = enc ? 1 : 0;
  5593. if (key) {
  5594. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, NULL,
  5595. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, 1);
  5596. }
  5597. if (ret != 0)
  5598. return WOLFSSL_FAILURE;
  5599. }
  5600. #endif /* WOLFSSL_AES_192 */
  5601. #ifdef WOLFSSL_AES_256
  5602. if (ctx->cipherType == AES_256_ECB_TYPE ||
  5603. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_ECB))) {
  5604. WOLFSSL_MSG("EVP_AES_256_ECB");
  5605. ctx->cipherType = AES_256_ECB_TYPE;
  5606. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5607. ctx->flags |= WOLFSSL_EVP_CIPH_ECB_MODE;
  5608. ctx->keyLen = 32;
  5609. ctx->block_size = AES_BLOCK_SIZE;
  5610. if (enc == 0 || enc == 1)
  5611. ctx->enc = enc ? 1 : 0;
  5612. if (key) {
  5613. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, NULL,
  5614. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, 1);
  5615. }
  5616. if (ret != 0)
  5617. return WOLFSSL_FAILURE;
  5618. }
  5619. #endif /* WOLFSSL_AES_256 */
  5620. #endif /* HAVE_AES_ECB */
  5621. #ifdef WOLFSSL_AES_CFB
  5622. #ifdef WOLFSSL_AES_128
  5623. if (ctx->cipherType == AES_128_CFB1_TYPE ||
  5624. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_CFB1))) {
  5625. WOLFSSL_MSG("EVP_AES_128_CFB1");
  5626. ctx->cipherType = AES_128_CFB1_TYPE;
  5627. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5628. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5629. ctx->keyLen = 16;
  5630. ctx->block_size = 1;
  5631. if (enc == 0 || enc == 1)
  5632. ctx->enc = enc ? 1 : 0;
  5633. if (key) {
  5634. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5635. AES_ENCRYPTION, 0);
  5636. if (ret != 0)
  5637. return WOLFSSL_FAILURE;
  5638. }
  5639. if (iv && key == NULL) {
  5640. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5641. if (ret != 0)
  5642. return WOLFSSL_FAILURE;
  5643. }
  5644. }
  5645. #endif /* WOLFSSL_AES_128 */
  5646. #ifdef WOLFSSL_AES_192
  5647. if (ctx->cipherType == AES_192_CFB1_TYPE ||
  5648. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_CFB1))) {
  5649. WOLFSSL_MSG("EVP_AES_192_CFB1");
  5650. ctx->cipherType = AES_192_CFB1_TYPE;
  5651. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5652. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5653. ctx->keyLen = 24;
  5654. ctx->block_size = 1;
  5655. if (enc == 0 || enc == 1)
  5656. ctx->enc = enc ? 1 : 0;
  5657. if (key) {
  5658. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5659. AES_ENCRYPTION, 0);
  5660. if (ret != 0)
  5661. return WOLFSSL_FAILURE;
  5662. }
  5663. if (iv && key == NULL) {
  5664. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5665. if (ret != 0)
  5666. return WOLFSSL_FAILURE;
  5667. }
  5668. }
  5669. #endif /* WOLFSSL_AES_192 */
  5670. #ifdef WOLFSSL_AES_256
  5671. if (ctx->cipherType == AES_256_CFB1_TYPE ||
  5672. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_CFB1))) {
  5673. WOLFSSL_MSG("EVP_AES_256_CFB1");
  5674. ctx->cipherType = AES_256_CFB1_TYPE;
  5675. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5676. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5677. ctx->keyLen = 32;
  5678. ctx->block_size = 1;
  5679. if (enc == 0 || enc == 1)
  5680. ctx->enc = enc ? 1 : 0;
  5681. if (key) {
  5682. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5683. AES_ENCRYPTION, 0);
  5684. if (ret != 0){
  5685. WOLFSSL_MSG("AesSetKey() failed");
  5686. return WOLFSSL_FAILURE;
  5687. }
  5688. }
  5689. if (iv && key == NULL) {
  5690. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5691. if (ret != 0){
  5692. WOLFSSL_MSG("wc_AesSetIV() failed");
  5693. return WOLFSSL_FAILURE;
  5694. }
  5695. }
  5696. }
  5697. #endif /* WOLFSSL_AES_256 */
  5698. #ifdef WOLFSSL_AES_128
  5699. if (ctx->cipherType == AES_128_CFB8_TYPE ||
  5700. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_CFB8))) {
  5701. WOLFSSL_MSG("EVP_AES_128_CFB8");
  5702. ctx->cipherType = AES_128_CFB8_TYPE;
  5703. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5704. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5705. ctx->keyLen = 16;
  5706. ctx->block_size = 1;
  5707. if (enc == 0 || enc == 1)
  5708. ctx->enc = enc ? 1 : 0;
  5709. if (key) {
  5710. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5711. AES_ENCRYPTION, 0);
  5712. if (ret != 0)
  5713. return WOLFSSL_FAILURE;
  5714. }
  5715. if (iv && key == NULL) {
  5716. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5717. if (ret != 0)
  5718. return WOLFSSL_FAILURE;
  5719. }
  5720. }
  5721. #endif /* WOLFSSL_AES_128 */
  5722. #ifdef WOLFSSL_AES_192
  5723. if (ctx->cipherType == AES_192_CFB8_TYPE ||
  5724. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_CFB8))) {
  5725. WOLFSSL_MSG("EVP_AES_192_CFB8");
  5726. ctx->cipherType = AES_192_CFB8_TYPE;
  5727. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5728. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5729. ctx->keyLen = 24;
  5730. ctx->block_size = 1;
  5731. if (enc == 0 || enc == 1)
  5732. ctx->enc = enc ? 1 : 0;
  5733. if (key) {
  5734. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5735. AES_ENCRYPTION, 0);
  5736. if (ret != 0)
  5737. return WOLFSSL_FAILURE;
  5738. }
  5739. if (iv && key == NULL) {
  5740. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5741. if (ret != 0)
  5742. return WOLFSSL_FAILURE;
  5743. }
  5744. }
  5745. #endif /* WOLFSSL_AES_192 */
  5746. #ifdef WOLFSSL_AES_256
  5747. if (ctx->cipherType == AES_256_CFB8_TYPE ||
  5748. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_CFB8))) {
  5749. WOLFSSL_MSG("EVP_AES_256_CFB8");
  5750. ctx->cipherType = AES_256_CFB8_TYPE;
  5751. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5752. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5753. ctx->keyLen = 32;
  5754. ctx->block_size = 1;
  5755. if (enc == 0 || enc == 1)
  5756. ctx->enc = enc ? 1 : 0;
  5757. if (key) {
  5758. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5759. AES_ENCRYPTION, 0);
  5760. if (ret != 0){
  5761. WOLFSSL_MSG("AesSetKey() failed");
  5762. return WOLFSSL_FAILURE;
  5763. }
  5764. }
  5765. if (iv && key == NULL) {
  5766. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5767. if (ret != 0){
  5768. WOLFSSL_MSG("wc_AesSetIV() failed");
  5769. return WOLFSSL_FAILURE;
  5770. }
  5771. }
  5772. }
  5773. #endif /* WOLFSSL_AES_256 */
  5774. #ifdef WOLFSSL_AES_128
  5775. if (ctx->cipherType == AES_128_CFB128_TYPE ||
  5776. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_CFB128))) {
  5777. WOLFSSL_MSG("EVP_AES_128_CFB128");
  5778. ctx->cipherType = AES_128_CFB128_TYPE;
  5779. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5780. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5781. ctx->keyLen = 16;
  5782. ctx->block_size = 1;
  5783. if (enc == 0 || enc == 1)
  5784. ctx->enc = enc ? 1 : 0;
  5785. if (key) {
  5786. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5787. AES_ENCRYPTION, 0);
  5788. if (ret != 0)
  5789. return WOLFSSL_FAILURE;
  5790. }
  5791. if (iv && key == NULL) {
  5792. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5793. if (ret != 0)
  5794. return WOLFSSL_FAILURE;
  5795. }
  5796. }
  5797. #endif /* WOLFSSL_AES_128 */
  5798. #ifdef WOLFSSL_AES_192
  5799. if (ctx->cipherType == AES_192_CFB128_TYPE ||
  5800. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_CFB128))) {
  5801. WOLFSSL_MSG("EVP_AES_192_CFB128");
  5802. ctx->cipherType = AES_192_CFB128_TYPE;
  5803. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5804. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5805. ctx->keyLen = 24;
  5806. ctx->block_size = 1;
  5807. if (enc == 0 || enc == 1)
  5808. ctx->enc = enc ? 1 : 0;
  5809. if (key) {
  5810. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5811. AES_ENCRYPTION, 0);
  5812. if (ret != 0)
  5813. return WOLFSSL_FAILURE;
  5814. }
  5815. if (iv && key == NULL) {
  5816. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5817. if (ret != 0)
  5818. return WOLFSSL_FAILURE;
  5819. }
  5820. }
  5821. #endif /* WOLFSSL_AES_192 */
  5822. #ifdef WOLFSSL_AES_256
  5823. if (ctx->cipherType == AES_256_CFB128_TYPE ||
  5824. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_CFB128))) {
  5825. WOLFSSL_MSG("EVP_AES_256_CFB128");
  5826. ctx->cipherType = AES_256_CFB128_TYPE;
  5827. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5828. ctx->flags |= WOLFSSL_EVP_CIPH_CFB_MODE;
  5829. ctx->keyLen = 32;
  5830. ctx->block_size = 1;
  5831. if (enc == 0 || enc == 1)
  5832. ctx->enc = enc ? 1 : 0;
  5833. if (key) {
  5834. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5835. AES_ENCRYPTION, 0);
  5836. if (ret != 0){
  5837. WOLFSSL_MSG("AesSetKey() failed");
  5838. return WOLFSSL_FAILURE;
  5839. }
  5840. }
  5841. if (iv && key == NULL) {
  5842. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5843. if (ret != 0){
  5844. WOLFSSL_MSG("wc_AesSetIV() failed");
  5845. return WOLFSSL_FAILURE;
  5846. }
  5847. }
  5848. }
  5849. #endif /* WOLFSSL_AES_256 */
  5850. #endif /* WOLFSSL_AES_CFB */
  5851. #ifdef WOLFSSL_AES_OFB
  5852. #ifdef WOLFSSL_AES_128
  5853. if (ctx->cipherType == AES_128_OFB_TYPE ||
  5854. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_OFB))) {
  5855. WOLFSSL_MSG("EVP_AES_128_OFB");
  5856. ctx->cipherType = AES_128_OFB_TYPE;
  5857. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5858. ctx->flags |= WOLFSSL_EVP_CIPH_OFB_MODE;
  5859. ctx->keyLen = 16;
  5860. ctx->block_size = 1;
  5861. if (enc == 0 || enc == 1)
  5862. ctx->enc = enc ? 1 : 0;
  5863. if (key) {
  5864. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5865. AES_ENCRYPTION, 0);
  5866. if (ret != 0)
  5867. return WOLFSSL_FAILURE;
  5868. }
  5869. if (iv && key == NULL) {
  5870. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5871. if (ret != 0)
  5872. return WOLFSSL_FAILURE;
  5873. }
  5874. }
  5875. #endif /* WOLFSSL_AES_128 */
  5876. #ifdef WOLFSSL_AES_192
  5877. if (ctx->cipherType == AES_192_OFB_TYPE ||
  5878. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_192_OFB))) {
  5879. WOLFSSL_MSG("EVP_AES_192_OFB");
  5880. ctx->cipherType = AES_192_OFB_TYPE;
  5881. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5882. ctx->flags |= WOLFSSL_EVP_CIPH_OFB_MODE;
  5883. ctx->keyLen = 24;
  5884. ctx->block_size = 1;
  5885. if (enc == 0 || enc == 1)
  5886. ctx->enc = enc ? 1 : 0;
  5887. if (key) {
  5888. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5889. AES_ENCRYPTION, 0);
  5890. if (ret != 0)
  5891. return WOLFSSL_FAILURE;
  5892. }
  5893. if (iv && key == NULL) {
  5894. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5895. if (ret != 0)
  5896. return WOLFSSL_FAILURE;
  5897. }
  5898. }
  5899. #endif /* WOLFSSL_AES_192 */
  5900. #ifdef WOLFSSL_AES_256
  5901. if (ctx->cipherType == AES_256_OFB_TYPE ||
  5902. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_OFB))) {
  5903. WOLFSSL_MSG("EVP_AES_256_OFB");
  5904. ctx->cipherType = AES_256_OFB_TYPE;
  5905. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5906. ctx->flags |= WOLFSSL_EVP_CIPH_OFB_MODE;
  5907. ctx->keyLen = 32;
  5908. ctx->block_size = 1;
  5909. if (enc == 0 || enc == 1)
  5910. ctx->enc = enc ? 1 : 0;
  5911. if (key) {
  5912. ret = AesSetKey_ex(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5913. AES_ENCRYPTION, 0);
  5914. if (ret != 0){
  5915. WOLFSSL_MSG("AesSetKey() failed");
  5916. return WOLFSSL_FAILURE;
  5917. }
  5918. }
  5919. if (iv && key == NULL) {
  5920. ret = wc_AesSetIV(&ctx->cipher.aes, iv);
  5921. if (ret != 0){
  5922. WOLFSSL_MSG("wc_AesSetIV() failed");
  5923. return WOLFSSL_FAILURE;
  5924. }
  5925. }
  5926. }
  5927. #endif /* WOLFSSL_AES_256 */
  5928. #endif /* HAVE_AES_OFB */
  5929. #ifdef WOLFSSL_AES_XTS
  5930. #ifdef WOLFSSL_AES_128
  5931. if (ctx->cipherType == AES_128_XTS_TYPE ||
  5932. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_128_XTS))) {
  5933. WOLFSSL_MSG("EVP_AES_128_XTS");
  5934. ctx->cipherType = AES_128_XTS_TYPE;
  5935. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5936. ctx->flags |= WOLFSSL_EVP_CIPH_XTS_MODE;
  5937. ctx->keyLen = 32;
  5938. ctx->block_size = 1;
  5939. ctx->ivSz = AES_BLOCK_SIZE;
  5940. if (iv != NULL) {
  5941. if (iv != ctx->iv) /* Valgrind error when src == dst */
  5942. XMEMCPY(ctx->iv, iv, ctx->ivSz);
  5943. }
  5944. else
  5945. XMEMSET(ctx->iv, 0, AES_BLOCK_SIZE);
  5946. if (enc == 0 || enc == 1)
  5947. ctx->enc = enc ? 1 : 0;
  5948. if (key) {
  5949. ret = wc_AesXtsSetKey(&ctx->cipher.xts, key, ctx->keyLen,
  5950. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, NULL, 0);
  5951. if (ret != 0) {
  5952. WOLFSSL_MSG("wc_AesXtsSetKey() failed");
  5953. return WOLFSSL_FAILURE;
  5954. }
  5955. }
  5956. }
  5957. #endif /* WOLFSSL_AES_128 */
  5958. #ifdef WOLFSSL_AES_256
  5959. if (ctx->cipherType == AES_256_XTS_TYPE ||
  5960. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_AES_256_XTS))) {
  5961. WOLFSSL_MSG("EVP_AES_256_XTS");
  5962. ctx->cipherType = AES_256_XTS_TYPE;
  5963. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5964. ctx->flags |= WOLFSSL_EVP_CIPH_XTS_MODE;
  5965. ctx->keyLen = 64;
  5966. ctx->block_size = 1;
  5967. ctx->ivSz = AES_BLOCK_SIZE;
  5968. if (iv != NULL) {
  5969. if (iv != ctx->iv) /* Valgrind error when src == dst */
  5970. XMEMCPY(ctx->iv, iv, ctx->ivSz);
  5971. }
  5972. else
  5973. XMEMSET(ctx->iv, 0, AES_BLOCK_SIZE);
  5974. if (enc == 0 || enc == 1)
  5975. ctx->enc = enc ? 1 : 0;
  5976. if (key) {
  5977. ret = wc_AesXtsSetKey(&ctx->cipher.xts, key, ctx->keyLen,
  5978. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, NULL, 0);
  5979. if (ret != 0) {
  5980. WOLFSSL_MSG("wc_AesXtsSetKey() failed");
  5981. return WOLFSSL_FAILURE;
  5982. }
  5983. }
  5984. }
  5985. #endif /* WOLFSSL_AES_256 */
  5986. #endif /* HAVE_AES_XTS */
  5987. #endif /* NO_AES */
  5988. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5989. if (ctx->cipherType == CHACHA20_POLY1305_TYPE ||
  5990. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_CHACHA20_POLY1305))) {
  5991. WOLFSSL_MSG("EVP_CHACHA20_POLY1305");
  5992. ctx->cipherType = CHACHA20_POLY1305_TYPE;
  5993. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  5994. ctx->flags |= WOLFSSL_EVP_CIPH_FLAG_AEAD_CIPHER;
  5995. ctx->keyLen = CHACHA20_POLY1305_AEAD_KEYSIZE;
  5996. ctx->block_size = CHACHA_CHUNK_BYTES;
  5997. ctx->authTagSz = CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE;
  5998. ctx->ivSz = CHACHA20_POLY1305_AEAD_IV_SIZE;
  5999. if (enc == 0 || enc == 1) {
  6000. ctx->enc = (byte) enc;
  6001. }
  6002. if (key != NULL && iv != NULL && wc_ChaCha20Poly1305_Init(
  6003. &ctx->cipher.chachaPoly, key, iv, enc) != 0) {
  6004. WOLFSSL_MSG("wc_ChaCha20Poly1305_Init() failed");
  6005. return WOLFSSL_FAILURE;
  6006. }
  6007. }
  6008. #endif
  6009. #ifndef NO_DES3
  6010. if (ctx->cipherType == DES_CBC_TYPE ||
  6011. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_DES_CBC))) {
  6012. WOLFSSL_MSG("EVP_DES_CBC");
  6013. ctx->cipherType = DES_CBC_TYPE;
  6014. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  6015. ctx->flags |= WOLFSSL_EVP_CIPH_CBC_MODE;
  6016. ctx->keyLen = 8;
  6017. ctx->block_size = DES_BLOCK_SIZE;
  6018. ctx->ivSz = DES_BLOCK_SIZE;
  6019. if (enc == 0 || enc == 1)
  6020. ctx->enc = enc ? 1 : 0;
  6021. if (key) {
  6022. ret = wc_Des_SetKey(&ctx->cipher.des, key, iv,
  6023. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  6024. if (ret != 0)
  6025. return WOLFSSL_FAILURE;
  6026. }
  6027. if (iv && key == NULL)
  6028. wc_Des_SetIV(&ctx->cipher.des, iv);
  6029. }
  6030. #ifdef WOLFSSL_DES_ECB
  6031. else if (ctx->cipherType == DES_ECB_TYPE ||
  6032. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_DES_ECB))) {
  6033. WOLFSSL_MSG("EVP_DES_ECB");
  6034. ctx->cipherType = DES_ECB_TYPE;
  6035. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  6036. ctx->flags |= WOLFSSL_EVP_CIPH_ECB_MODE;
  6037. ctx->keyLen = 8;
  6038. ctx->block_size = DES_BLOCK_SIZE;
  6039. if (enc == 0 || enc == 1)
  6040. ctx->enc = enc ? 1 : 0;
  6041. if (key) {
  6042. WOLFSSL_MSG("Des_SetKey");
  6043. ret = wc_Des_SetKey(&ctx->cipher.des, key, NULL,
  6044. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  6045. if (ret != 0)
  6046. return WOLFSSL_FAILURE;
  6047. }
  6048. }
  6049. #endif
  6050. else if (ctx->cipherType == DES_EDE3_CBC_TYPE ||
  6051. (type &&
  6052. EVP_CIPHER_TYPE_MATCHES(type, EVP_DES_EDE3_CBC))) {
  6053. WOLFSSL_MSG("EVP_DES_EDE3_CBC");
  6054. ctx->cipherType = DES_EDE3_CBC_TYPE;
  6055. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  6056. ctx->flags |= WOLFSSL_EVP_CIPH_CBC_MODE;
  6057. ctx->keyLen = 24;
  6058. ctx->block_size = DES_BLOCK_SIZE;
  6059. ctx->ivSz = DES_BLOCK_SIZE;
  6060. if (enc == 0 || enc == 1)
  6061. ctx->enc = enc ? 1 : 0;
  6062. if (key) {
  6063. ret = wc_Des3_SetKey(&ctx->cipher.des3, key, iv,
  6064. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  6065. if (ret != 0)
  6066. return WOLFSSL_FAILURE;
  6067. }
  6068. if (iv && key == NULL) {
  6069. ret = wc_Des3_SetIV(&ctx->cipher.des3, iv);
  6070. if (ret != 0)
  6071. return WOLFSSL_FAILURE;
  6072. }
  6073. }
  6074. else if (ctx->cipherType == DES_EDE3_ECB_TYPE ||
  6075. (type &&
  6076. EVP_CIPHER_TYPE_MATCHES(type, EVP_DES_EDE3_ECB))) {
  6077. WOLFSSL_MSG("EVP_DES_EDE3_ECB");
  6078. ctx->cipherType = DES_EDE3_ECB_TYPE;
  6079. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  6080. ctx->flags |= WOLFSSL_EVP_CIPH_ECB_MODE;
  6081. ctx->keyLen = 24;
  6082. ctx->block_size = DES_BLOCK_SIZE;
  6083. if (enc == 0 || enc == 1)
  6084. ctx->enc = enc ? 1 : 0;
  6085. if (key) {
  6086. ret = wc_Des3_SetKey(&ctx->cipher.des3, key, NULL,
  6087. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  6088. if (ret != 0)
  6089. return WOLFSSL_FAILURE;
  6090. }
  6091. }
  6092. #endif /* NO_DES3 */
  6093. #ifndef NO_RC4
  6094. if (ctx->cipherType == ARC4_TYPE ||
  6095. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_ARC4))) {
  6096. WOLFSSL_MSG("ARC4");
  6097. ctx->cipherType = ARC4_TYPE;
  6098. ctx->flags &= ~WOLFSSL_EVP_CIPH_MODE;
  6099. ctx->flags |= WOLFSSL_EVP_CIPH_STREAM_CIPHER;
  6100. ctx->block_size = 1;
  6101. if (ctx->keyLen == 0) /* user may have already set */
  6102. ctx->keyLen = 16; /* default to 128 */
  6103. if (key)
  6104. wc_Arc4SetKey(&ctx->cipher.arc4, key, ctx->keyLen);
  6105. }
  6106. #endif /* NO_RC4 */
  6107. if (ctx->cipherType == NULL_CIPHER_TYPE ||
  6108. (type && EVP_CIPHER_TYPE_MATCHES(type, EVP_NULL))) {
  6109. WOLFSSL_MSG("NULL cipher");
  6110. ctx->cipherType = NULL_CIPHER_TYPE;
  6111. ctx->keyLen = 0;
  6112. ctx->block_size = 16;
  6113. }
  6114. #ifdef HAVE_WOLFSSL_EVP_CIPHER_CTX_IV
  6115. if (iv && iv != ctx->iv) {
  6116. if (wolfSSL_StoreExternalIV(ctx) != WOLFSSL_SUCCESS) {
  6117. return WOLFSSL_FAILURE;
  6118. }
  6119. }
  6120. #endif
  6121. (void)ret; /* remove warning. If execution reaches this point, ret=0 */
  6122. return WOLFSSL_SUCCESS;
  6123. }
  6124. /* WOLFSSL_SUCCESS on ok */
  6125. int wolfSSL_EVP_CIPHER_CTX_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx)
  6126. {
  6127. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_key_length");
  6128. if (ctx)
  6129. return ctx->keyLen;
  6130. else
  6131. return WOLFSSL_FAILURE;
  6132. }
  6133. /* WOLFSSL_SUCCESS on ok */
  6134. int wolfSSL_EVP_CIPHER_CTX_set_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx,
  6135. int keylen)
  6136. {
  6137. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_set_key_length");
  6138. if (ctx)
  6139. ctx->keyLen = keylen;
  6140. else
  6141. return WOLFSSL_FAILURE;
  6142. return WOLFSSL_SUCCESS;
  6143. }
  6144. #ifdef HAVE_WOLFSSL_EVP_CIPHER_CTX_IV
  6145. /* returns WOLFSSL_SUCCESS on success, otherwise returns WOLFSSL_FAILURE */
  6146. int wolfSSL_EVP_CIPHER_CTX_set_iv_length(WOLFSSL_EVP_CIPHER_CTX* ctx,
  6147. int ivLen)
  6148. {
  6149. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_set_iv_length");
  6150. if (ctx)
  6151. ctx->ivSz= ivLen;
  6152. else
  6153. return WOLFSSL_FAILURE;
  6154. return WOLFSSL_SUCCESS;
  6155. }
  6156. #endif
  6157. #if defined(HAVE_AESGCM) || (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  6158. /* returns WOLFSSL_SUCCESS on success, otherwise returns WOLFSSL_FAILURE */
  6159. int wolfSSL_EVP_CIPHER_CTX_set_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, byte* iv,
  6160. int ivLen)
  6161. {
  6162. int expectedIvLen;
  6163. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_set_iv");
  6164. if (!ctx || !iv || !ivLen) {
  6165. return WOLFSSL_FAILURE;
  6166. }
  6167. expectedIvLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  6168. if (expectedIvLen == 0 || expectedIvLen != ivLen) {
  6169. WOLFSSL_MSG("Wrong ivLen value");
  6170. return WOLFSSL_FAILURE;
  6171. }
  6172. return wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, -1);
  6173. }
  6174. #endif
  6175. #if !defined(NO_AES) || !defined(NO_DES3)
  6176. /* returns WOLFSSL_SUCCESS on success, otherwise returns WOLFSSL_FAILURE */
  6177. int wolfSSL_EVP_CIPHER_CTX_get_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, byte* iv,
  6178. int ivLen)
  6179. {
  6180. int expectedIvLen;
  6181. WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_get_iv");
  6182. if (ctx == NULL || iv == NULL || ivLen == 0) {
  6183. WOLFSSL_MSG("Bad parameter");
  6184. return WOLFSSL_FAILURE;
  6185. }
  6186. expectedIvLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  6187. if (expectedIvLen == 0 || expectedIvLen != ivLen) {
  6188. WOLFSSL_MSG("Wrong ivLen value");
  6189. return WOLFSSL_FAILURE;
  6190. }
  6191. XMEMCPY(iv, ctx->iv, ivLen);
  6192. return WOLFSSL_SUCCESS;
  6193. }
  6194. #endif /* !NO_AES || !NO_DES3 */
  6195. /* Return length on ok */
  6196. int wolfSSL_EVP_Cipher(WOLFSSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src,
  6197. word32 len)
  6198. {
  6199. int ret = WOLFSSL_FAILURE;
  6200. WOLFSSL_ENTER("wolfSSL_EVP_Cipher");
  6201. if (ctx == NULL || ((src == NULL || dst == NULL) &&
  6202. (ctx->cipherType != AES_128_GCM_TYPE &&
  6203. ctx->cipherType != AES_192_GCM_TYPE &&
  6204. ctx->cipherType != AES_256_GCM_TYPE))) {
  6205. WOLFSSL_MSG("Bad argument.");
  6206. return WOLFSSL_FATAL_ERROR;
  6207. }
  6208. if (ctx->cipherType == WOLFSSL_EVP_CIPH_TYPE_INIT) {
  6209. WOLFSSL_MSG("Cipher operation not initialized. Call "
  6210. "wolfSSL_EVP_CipherInit.");
  6211. return WOLFSSL_FATAL_ERROR;
  6212. }
  6213. switch (ctx->cipherType) {
  6214. #ifndef NO_AES
  6215. #ifdef HAVE_AES_CBC
  6216. case AES_128_CBC_TYPE :
  6217. case AES_192_CBC_TYPE :
  6218. case AES_256_CBC_TYPE :
  6219. WOLFSSL_MSG("AES CBC");
  6220. if (ctx->enc)
  6221. ret = wc_AesCbcEncrypt(&ctx->cipher.aes, dst, src, len);
  6222. else
  6223. ret = wc_AesCbcDecrypt(&ctx->cipher.aes, dst, src, len);
  6224. if (ret == 0)
  6225. ret = (len / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  6226. break;
  6227. #endif /* HAVE_AES_CBC */
  6228. #ifdef WOLFSSL_AES_CFB
  6229. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  6230. case AES_128_CFB1_TYPE:
  6231. case AES_192_CFB1_TYPE:
  6232. case AES_256_CFB1_TYPE:
  6233. WOLFSSL_MSG("AES CFB1");
  6234. if (ctx->enc)
  6235. ret = wc_AesCfb1Encrypt(&ctx->cipher.aes, dst, src, len);
  6236. else
  6237. ret = wc_AesCfb1Decrypt(&ctx->cipher.aes, dst, src, len);
  6238. if (ret == 0)
  6239. ret = len;
  6240. break;
  6241. case AES_128_CFB8_TYPE:
  6242. case AES_192_CFB8_TYPE:
  6243. case AES_256_CFB8_TYPE:
  6244. WOLFSSL_MSG("AES CFB8");
  6245. if (ctx->enc)
  6246. ret = wc_AesCfb8Encrypt(&ctx->cipher.aes, dst, src, len);
  6247. else
  6248. ret = wc_AesCfb8Decrypt(&ctx->cipher.aes, dst, src, len);
  6249. if (ret == 0)
  6250. ret = len;
  6251. break;
  6252. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  6253. case AES_128_CFB128_TYPE:
  6254. case AES_192_CFB128_TYPE:
  6255. case AES_256_CFB128_TYPE:
  6256. WOLFSSL_MSG("AES CFB128");
  6257. if (ctx->enc)
  6258. ret = wc_AesCfbEncrypt(&ctx->cipher.aes, dst, src, len);
  6259. else
  6260. ret = wc_AesCfbDecrypt(&ctx->cipher.aes, dst, src, len);
  6261. if (ret == 0)
  6262. ret = len;
  6263. break;
  6264. #endif /* WOLFSSL_AES_CFB */
  6265. #if defined(WOLFSSL_AES_OFB)
  6266. case AES_128_OFB_TYPE:
  6267. case AES_192_OFB_TYPE:
  6268. case AES_256_OFB_TYPE:
  6269. WOLFSSL_MSG("AES OFB");
  6270. if (ctx->enc)
  6271. ret = wc_AesOfbEncrypt(&ctx->cipher.aes, dst, src, len);
  6272. else
  6273. ret = wc_AesOfbDecrypt(&ctx->cipher.aes, dst, src, len);
  6274. if (ret == 0)
  6275. ret = len;
  6276. break;
  6277. #endif /* WOLFSSL_AES_OFB */
  6278. #if defined(WOLFSSL_AES_XTS)
  6279. case AES_128_XTS_TYPE:
  6280. case AES_256_XTS_TYPE:
  6281. WOLFSSL_MSG("AES XTS");
  6282. if (ctx->enc)
  6283. ret = wc_AesXtsEncrypt(&ctx->cipher.xts, dst, src, len,
  6284. ctx->iv, ctx->ivSz);
  6285. else
  6286. ret = wc_AesXtsDecrypt(&ctx->cipher.xts, dst, src, len,
  6287. ctx->iv, ctx->ivSz);
  6288. if (ret == 0)
  6289. ret = len;
  6290. break;
  6291. #endif /* WOLFSSL_AES_XTS */
  6292. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) \
  6293. || FIPS_VERSION_GE(2,0))
  6294. case AES_128_GCM_TYPE :
  6295. case AES_192_GCM_TYPE :
  6296. case AES_256_GCM_TYPE :
  6297. WOLFSSL_MSG("AES GCM");
  6298. ret = EvpCipherAesGCM(ctx, dst, src, len);
  6299. break;
  6300. #endif /* HAVE_AESGCM && ((!HAVE_FIPS && !HAVE_SELFTEST) ||
  6301. * HAVE_FIPS_VERSION >= 2 */
  6302. #ifdef HAVE_AES_ECB
  6303. case AES_128_ECB_TYPE :
  6304. case AES_192_ECB_TYPE :
  6305. case AES_256_ECB_TYPE :
  6306. WOLFSSL_MSG("AES ECB");
  6307. if (ctx->enc)
  6308. ret = wc_AesEcbEncrypt(&ctx->cipher.aes, dst, src, len);
  6309. else
  6310. ret = wc_AesEcbDecrypt(&ctx->cipher.aes, dst, src, len);
  6311. if (ret == 0)
  6312. ret = (len / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
  6313. break;
  6314. #endif
  6315. #ifdef WOLFSSL_AES_COUNTER
  6316. case AES_128_CTR_TYPE :
  6317. case AES_192_CTR_TYPE :
  6318. case AES_256_CTR_TYPE :
  6319. WOLFSSL_MSG("AES CTR");
  6320. ret = wc_AesCtrEncrypt(&ctx->cipher.aes, dst, src, len);
  6321. if (ret == 0)
  6322. ret = len;
  6323. break;
  6324. #endif /* WOLFSSL_AES_COUNTER */
  6325. #endif /* NO_AES */
  6326. #ifndef NO_DES3
  6327. case DES_CBC_TYPE :
  6328. WOLFSSL_MSG("DES CBC");
  6329. if (ctx->enc)
  6330. wc_Des_CbcEncrypt(&ctx->cipher.des, dst, src, len);
  6331. else
  6332. wc_Des_CbcDecrypt(&ctx->cipher.des, dst, src, len);
  6333. if (ret == 0)
  6334. ret = (len / DES_BLOCK_SIZE) * DES_BLOCK_SIZE;
  6335. break;
  6336. case DES_EDE3_CBC_TYPE :
  6337. WOLFSSL_MSG("DES3 CBC");
  6338. if (ctx->enc)
  6339. ret = wc_Des3_CbcEncrypt(&ctx->cipher.des3, dst, src, len);
  6340. else
  6341. ret = wc_Des3_CbcDecrypt(&ctx->cipher.des3, dst, src, len);
  6342. if (ret == 0)
  6343. ret = (len / DES_BLOCK_SIZE) * DES_BLOCK_SIZE;
  6344. break;
  6345. #ifdef WOLFSSL_DES_ECB
  6346. case DES_ECB_TYPE :
  6347. WOLFSSL_MSG("DES ECB");
  6348. ret = wc_Des_EcbEncrypt(&ctx->cipher.des, dst, src, len);
  6349. if (ret == 0)
  6350. ret = (len / DES_BLOCK_SIZE) * DES_BLOCK_SIZE;
  6351. break;
  6352. case DES_EDE3_ECB_TYPE :
  6353. WOLFSSL_MSG("DES3 ECB");
  6354. ret = wc_Des3_EcbEncrypt(&ctx->cipher.des3, dst, src, len);
  6355. if (ret == 0)
  6356. ret = (len / DES_BLOCK_SIZE) * DES_BLOCK_SIZE;
  6357. break;
  6358. #endif
  6359. #endif /* !NO_DES3 */
  6360. #ifndef NO_RC4
  6361. case ARC4_TYPE :
  6362. WOLFSSL_MSG("ARC4");
  6363. wc_Arc4Process(&ctx->cipher.arc4, dst, src, len);
  6364. if (ret == 0)
  6365. ret = len;
  6366. break;
  6367. #endif
  6368. case NULL_CIPHER_TYPE :
  6369. WOLFSSL_MSG("NULL CIPHER");
  6370. XMEMCPY(dst, src, len);
  6371. ret = len;
  6372. break;
  6373. default: {
  6374. WOLFSSL_MSG("bad type");
  6375. return WOLFSSL_FATAL_ERROR;
  6376. }
  6377. }
  6378. if (ret < 0) {
  6379. if (ret == AES_GCM_AUTH_E) {
  6380. WOLFSSL_MSG("wolfSSL_EVP_Cipher failure: bad AES-GCM tag.");
  6381. }
  6382. WOLFSSL_MSG("wolfSSL_EVP_Cipher failure");
  6383. return WOLFSSL_FATAL_ERROR;
  6384. }
  6385. if (wolfSSL_StoreExternalIV(ctx) != WOLFSSL_SUCCESS) {
  6386. return WOLFSSL_FATAL_ERROR;
  6387. }
  6388. WOLFSSL_MSG("wolfSSL_EVP_Cipher success");
  6389. return ret;
  6390. }
  6391. /* WOLFSSL_SUCCESS on ok */
  6392. int wolfSSL_EVP_DigestInit(WOLFSSL_EVP_MD_CTX* ctx,
  6393. const WOLFSSL_EVP_MD* md)
  6394. {
  6395. int ret = WOLFSSL_SUCCESS;
  6396. WOLFSSL_ENTER("EVP_DigestInit");
  6397. if (ctx == NULL) {
  6398. return BAD_FUNC_ARG;
  6399. }
  6400. #ifdef WOLFSSL_ASYNC_CRYPT
  6401. /* compile-time validation of ASYNC_CTX_SIZE */
  6402. typedef char async_test[WC_ASYNC_DEV_SIZE >= sizeof(WC_ASYNC_DEV) ?
  6403. 1 : -1];
  6404. (void)sizeof(async_test);
  6405. #endif
  6406. /* Set to 0 if no match */
  6407. ctx->macType = EvpMd2MacType(md);
  6408. if (md == NULL) {
  6409. XMEMSET(&ctx->hash.digest, 0, sizeof(WOLFSSL_Hasher));
  6410. } else
  6411. #ifndef NO_SHA
  6412. if ((XSTRCMP(md, "SHA") == 0) || (XSTRCMP(md, "SHA1") == 0)) {
  6413. ret = wolfSSL_SHA_Init(&(ctx->hash.digest.sha));
  6414. } else
  6415. #endif
  6416. #ifndef NO_SHA256
  6417. if (XSTRCMP(md, "SHA256") == 0) {
  6418. ret = wolfSSL_SHA256_Init(&(ctx->hash.digest.sha256));
  6419. } else
  6420. #endif
  6421. #ifdef WOLFSSL_SHA224
  6422. if (XSTRCMP(md, "SHA224") == 0) {
  6423. ret = wolfSSL_SHA224_Init(&(ctx->hash.digest.sha224));
  6424. } else
  6425. #endif
  6426. #ifdef WOLFSSL_SHA384
  6427. if (XSTRCMP(md, "SHA384") == 0) {
  6428. ret = wolfSSL_SHA384_Init(&(ctx->hash.digest.sha384));
  6429. } else
  6430. #endif
  6431. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  6432. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  6433. if (XSTRCMP(md, "SHA512_224") == 0) {
  6434. ret = wolfSSL_SHA512_224_Init(&(ctx->hash.digest.sha512));
  6435. } else
  6436. #endif
  6437. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  6438. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  6439. if (XSTRCMP(md, "SHA512_256") == 0) {
  6440. ret = wolfSSL_SHA512_256_Init(&(ctx->hash.digest.sha512));
  6441. } else
  6442. #endif
  6443. #ifdef WOLFSSL_SHA512
  6444. if (XSTRCMP(md, "SHA512") == 0) {
  6445. ret = wolfSSL_SHA512_Init(&(ctx->hash.digest.sha512));
  6446. } else
  6447. #endif
  6448. #ifndef NO_MD4
  6449. if (XSTRCMP(md, "MD4") == 0) {
  6450. wolfSSL_MD4_Init(&(ctx->hash.digest.md4));
  6451. } else
  6452. #endif
  6453. #ifndef NO_MD5
  6454. if (XSTRCMP(md, "MD5") == 0) {
  6455. ret = wolfSSL_MD5_Init(&(ctx->hash.digest.md5));
  6456. } else
  6457. #endif
  6458. #ifdef WOLFSSL_SHA3
  6459. #ifndef WOLFSSL_NOSHA3_224
  6460. if (XSTRCMP(md, "SHA3_224") == 0) {
  6461. ret = wolfSSL_SHA3_224_Init(&(ctx->hash.digest.sha3_224));
  6462. } else
  6463. #endif
  6464. #ifndef WOLFSSL_NOSHA3_256
  6465. if (XSTRCMP(md, "SHA3_256") == 0) {
  6466. ret = wolfSSL_SHA3_256_Init(&(ctx->hash.digest.sha3_256));
  6467. } else
  6468. #endif
  6469. #ifndef WOLFSSL_NOSHA3_384
  6470. if (XSTRCMP(md, "SHA3_384") == 0) {
  6471. ret = wolfSSL_SHA3_384_Init(&(ctx->hash.digest.sha3_384));
  6472. } else
  6473. #endif
  6474. #ifndef WOLFSSL_NOSHA3_512
  6475. if (XSTRCMP(md, "SHA3_512") == 0) {
  6476. ret = wolfSSL_SHA3_512_Init(&(ctx->hash.digest.sha3_512));
  6477. } else
  6478. #endif
  6479. #endif
  6480. {
  6481. ctx->macType = WC_HASH_TYPE_NONE;
  6482. return BAD_FUNC_ARG;
  6483. }
  6484. return ret;
  6485. }
  6486. /* WOLFSSL_SUCCESS on ok, WOLFSSL_FAILURE on failure */
  6487. int wolfSSL_EVP_DigestUpdate(WOLFSSL_EVP_MD_CTX* ctx, const void* data,
  6488. size_t sz)
  6489. {
  6490. int ret = WOLFSSL_FAILURE;
  6491. enum wc_HashType macType;
  6492. WOLFSSL_ENTER("EVP_DigestUpdate");
  6493. macType = EvpMd2MacType(EVP_MD_CTX_md(ctx));
  6494. switch (macType) {
  6495. case WC_HASH_TYPE_MD4:
  6496. #ifndef NO_MD4
  6497. wolfSSL_MD4_Update((MD4_CTX*)&ctx->hash, data,
  6498. (unsigned long)sz);
  6499. ret = WOLFSSL_SUCCESS;
  6500. #endif
  6501. break;
  6502. case WC_HASH_TYPE_MD5:
  6503. #ifndef NO_MD5
  6504. ret = wolfSSL_MD5_Update((MD5_CTX*)&ctx->hash, data,
  6505. (unsigned long)sz);
  6506. #endif
  6507. break;
  6508. case WC_HASH_TYPE_SHA:
  6509. #ifndef NO_SHA
  6510. ret = wolfSSL_SHA_Update((SHA_CTX*)&ctx->hash, data,
  6511. (unsigned long)sz);
  6512. #endif
  6513. break;
  6514. case WC_HASH_TYPE_SHA224:
  6515. #ifdef WOLFSSL_SHA224
  6516. ret = wolfSSL_SHA224_Update((SHA224_CTX*)&ctx->hash, data,
  6517. (unsigned long)sz);
  6518. #endif
  6519. break;
  6520. case WC_HASH_TYPE_SHA256:
  6521. #ifndef NO_SHA256
  6522. ret = wolfSSL_SHA256_Update((SHA256_CTX*)&ctx->hash, data,
  6523. (unsigned long)sz);
  6524. #endif /* !NO_SHA256 */
  6525. break;
  6526. case WC_HASH_TYPE_SHA384:
  6527. #ifdef WOLFSSL_SHA384
  6528. ret = wolfSSL_SHA384_Update((SHA384_CTX*)&ctx->hash, data,
  6529. (unsigned long)sz);
  6530. #endif
  6531. break;
  6532. case WC_HASH_TYPE_SHA512:
  6533. #ifdef WOLFSSL_SHA512
  6534. ret = wolfSSL_SHA512_Update((SHA512_CTX*)&ctx->hash, data,
  6535. (unsigned long)sz);
  6536. #endif /* WOLFSSL_SHA512 */
  6537. break;
  6538. #ifndef WOLFSSL_NOSHA512_224
  6539. case WC_HASH_TYPE_SHA512_224:
  6540. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  6541. defined(WOLFSSL_SHA512)
  6542. ret = wolfSSL_SHA512_224_Update((SHA512_CTX*)&ctx->hash, data,
  6543. (unsigned long)sz);
  6544. #endif
  6545. break;
  6546. #endif /* !WOLFSSL_NOSHA512_224 */
  6547. #ifndef WOLFSSL_NOSHA512_256
  6548. case WC_HASH_TYPE_SHA512_256:
  6549. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  6550. defined(WOLFSSL_SHA512)
  6551. ret = wolfSSL_SHA512_256_Update((SHA512_CTX*)&ctx->hash, data,
  6552. (unsigned long)sz);
  6553. #endif /* WOLFSSL_SHA512 */
  6554. break;
  6555. #endif /* !WOLFSSL_NOSHA512_256 */
  6556. case WC_HASH_TYPE_SHA3_224:
  6557. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  6558. ret = wolfSSL_SHA3_224_Update((SHA3_224_CTX*)&ctx->hash, data,
  6559. (unsigned long)sz);
  6560. #endif
  6561. break;
  6562. case WC_HASH_TYPE_SHA3_256:
  6563. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  6564. ret = wolfSSL_SHA3_256_Update((SHA3_256_CTX*)&ctx->hash, data,
  6565. (unsigned long)sz);
  6566. #endif
  6567. break;
  6568. case WC_HASH_TYPE_SHA3_384:
  6569. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  6570. ret = wolfSSL_SHA3_384_Update((SHA3_384_CTX*)&ctx->hash, data,
  6571. (unsigned long)sz);
  6572. #endif
  6573. break;
  6574. case WC_HASH_TYPE_SHA3_512:
  6575. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  6576. ret = wolfSSL_SHA3_512_Update((SHA3_512_CTX*)&ctx->hash, data,
  6577. (unsigned long)sz);
  6578. #endif
  6579. break;
  6580. case WC_HASH_TYPE_NONE:
  6581. case WC_HASH_TYPE_MD2:
  6582. case WC_HASH_TYPE_MD5_SHA:
  6583. case WC_HASH_TYPE_BLAKE2B:
  6584. case WC_HASH_TYPE_BLAKE2S:
  6585. #ifndef WOLFSSL_NO_SHAKE256
  6586. case WC_HASH_TYPE_SHAKE128:
  6587. case WC_HASH_TYPE_SHAKE256:
  6588. #endif
  6589. default:
  6590. return WOLFSSL_FAILURE;
  6591. }
  6592. return ret;
  6593. }
  6594. /* WOLFSSL_SUCCESS on ok */
  6595. int wolfSSL_EVP_DigestFinal(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md,
  6596. unsigned int* s)
  6597. {
  6598. int ret = WOLFSSL_FAILURE;
  6599. enum wc_HashType macType;
  6600. WOLFSSL_ENTER("EVP_DigestFinal");
  6601. macType = EvpMd2MacType(EVP_MD_CTX_md(ctx));
  6602. switch (macType) {
  6603. case WC_HASH_TYPE_MD4:
  6604. #ifndef NO_MD4
  6605. wolfSSL_MD4_Final(md, (MD4_CTX*)&ctx->hash);
  6606. if (s) *s = MD4_DIGEST_SIZE;
  6607. ret = WOLFSSL_SUCCESS;
  6608. #endif
  6609. break;
  6610. case WC_HASH_TYPE_MD5:
  6611. #ifndef NO_MD5
  6612. ret = wolfSSL_MD5_Final(md, (MD5_CTX*)&ctx->hash);
  6613. if (s) *s = WC_MD5_DIGEST_SIZE;
  6614. #endif
  6615. break;
  6616. case WC_HASH_TYPE_SHA:
  6617. #ifndef NO_SHA
  6618. ret = wolfSSL_SHA_Final(md, (SHA_CTX*)&ctx->hash);
  6619. if (s) *s = WC_SHA_DIGEST_SIZE;
  6620. #endif
  6621. break;
  6622. case WC_HASH_TYPE_SHA224:
  6623. #ifdef WOLFSSL_SHA224
  6624. ret = wolfSSL_SHA224_Final(md, (SHA224_CTX*)&ctx->hash);
  6625. if (s) *s = WC_SHA224_DIGEST_SIZE;
  6626. #endif
  6627. break;
  6628. case WC_HASH_TYPE_SHA256:
  6629. #ifndef NO_SHA256
  6630. ret = wolfSSL_SHA256_Final(md, (SHA256_CTX*)&ctx->hash);
  6631. if (s) *s = WC_SHA256_DIGEST_SIZE;
  6632. #endif /* !NO_SHA256 */
  6633. break;
  6634. case WC_HASH_TYPE_SHA384:
  6635. #ifdef WOLFSSL_SHA384
  6636. ret = wolfSSL_SHA384_Final(md, (SHA384_CTX*)&ctx->hash);
  6637. if (s) *s = WC_SHA384_DIGEST_SIZE;
  6638. #endif
  6639. break;
  6640. case WC_HASH_TYPE_SHA512:
  6641. #ifdef WOLFSSL_SHA512
  6642. ret = wolfSSL_SHA512_Final(md, (SHA512_CTX*)&ctx->hash);
  6643. if (s) *s = WC_SHA512_DIGEST_SIZE;
  6644. #endif /* WOLFSSL_SHA512 */
  6645. break;
  6646. #ifndef WOLFSSL_NOSHA512_224
  6647. case WC_HASH_TYPE_SHA512_224:
  6648. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  6649. defined(WOLFSSL_SHA512)
  6650. ret = wolfSSL_SHA512_224_Final(md, (SHA512_CTX*)&ctx->hash);
  6651. if (s) *s = WC_SHA512_224_DIGEST_SIZE;
  6652. #endif
  6653. break;
  6654. #endif /* !WOLFSSL_NOSHA512_224 */
  6655. #ifndef WOLFSSL_NOSHA512_256
  6656. case WC_HASH_TYPE_SHA512_256:
  6657. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  6658. defined(WOLFSSL_SHA512)
  6659. ret = wolfSSL_SHA512_256_Final(md, (SHA512_CTX*)&ctx->hash);
  6660. if (s) *s = WC_SHA512_256_DIGEST_SIZE;
  6661. #endif
  6662. break;
  6663. #endif /* !WOLFSSL_NOSHA512_256 */
  6664. case WC_HASH_TYPE_SHA3_224:
  6665. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  6666. ret = wolfSSL_SHA3_224_Final(md, (SHA3_224_CTX*)&ctx->hash);
  6667. if (s) *s = WC_SHA3_224_DIGEST_SIZE;
  6668. #endif
  6669. break;
  6670. case WC_HASH_TYPE_SHA3_256:
  6671. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  6672. ret = wolfSSL_SHA3_256_Final(md, (SHA3_256_CTX*)&ctx->hash);
  6673. if (s) *s = WC_SHA3_256_DIGEST_SIZE;
  6674. #endif
  6675. break;
  6676. case WC_HASH_TYPE_SHA3_384:
  6677. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  6678. ret = wolfSSL_SHA3_384_Final(md, (SHA3_384_CTX*)&ctx->hash);
  6679. if (s) *s = WC_SHA3_384_DIGEST_SIZE;
  6680. #endif
  6681. break;
  6682. case WC_HASH_TYPE_SHA3_512:
  6683. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  6684. ret = wolfSSL_SHA3_512_Final(md, (SHA3_512_CTX*)&ctx->hash);
  6685. if (s) *s = WC_SHA3_512_DIGEST_SIZE;
  6686. #endif
  6687. break;
  6688. case WC_HASH_TYPE_NONE:
  6689. case WC_HASH_TYPE_MD2:
  6690. case WC_HASH_TYPE_MD5_SHA:
  6691. case WC_HASH_TYPE_BLAKE2B:
  6692. case WC_HASH_TYPE_BLAKE2S:
  6693. #ifndef WOLFSSL_NO_SHAKE256
  6694. case WC_HASH_TYPE_SHAKE128:
  6695. case WC_HASH_TYPE_SHAKE256:
  6696. #endif
  6697. default:
  6698. return WOLFSSL_FAILURE;
  6699. }
  6700. return ret;
  6701. }
  6702. /* WOLFSSL_SUCCESS on ok */
  6703. int wolfSSL_EVP_DigestFinal_ex(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md,
  6704. unsigned int* s)
  6705. {
  6706. WOLFSSL_ENTER("EVP_DigestFinal_ex");
  6707. return EVP_DigestFinal(ctx, md, s);
  6708. }
  6709. void wolfSSL_EVP_cleanup(void)
  6710. {
  6711. /* nothing to do here */
  6712. }
  6713. const WOLFSSL_EVP_MD* wolfSSL_EVP_get_digestbynid(int id)
  6714. {
  6715. WOLFSSL_MSG("wolfSSL_get_digestbynid");
  6716. switch(id) {
  6717. #ifndef NO_MD5
  6718. case NID_md5:
  6719. return wolfSSL_EVP_md5();
  6720. #endif
  6721. #ifndef NO_SHA
  6722. case NID_sha1:
  6723. return wolfSSL_EVP_sha1();
  6724. #endif
  6725. #ifdef WOLFSSL_SHA224
  6726. case NID_sha224:
  6727. return wolfSSL_EVP_sha224();
  6728. #endif
  6729. #ifndef NO_SHA256
  6730. case NID_sha256:
  6731. return wolfSSL_EVP_sha256();
  6732. #endif
  6733. #ifdef WOLFSSL_SHA384
  6734. case NID_sha384:
  6735. return wolfSSL_EVP_sha384();
  6736. #endif
  6737. #ifdef WOLFSSL_SHA512
  6738. case NID_sha512:
  6739. return wolfSSL_EVP_sha512();
  6740. #endif
  6741. default:
  6742. WOLFSSL_MSG("Bad digest id value");
  6743. }
  6744. return NULL;
  6745. }
  6746. static void clearEVPPkeyKeys(WOLFSSL_EVP_PKEY *pkey)
  6747. {
  6748. if(pkey == NULL)
  6749. return;
  6750. WOLFSSL_ENTER("clearEVPPkeyKeys");
  6751. #ifndef NO_RSA
  6752. if (pkey->rsa != NULL && pkey->ownRsa == 1) {
  6753. wolfSSL_RSA_free(pkey->rsa);
  6754. pkey->rsa = NULL;
  6755. }
  6756. pkey->ownRsa = 0;
  6757. #endif
  6758. #ifndef NO_DSA
  6759. if (pkey->dsa != NULL && pkey->ownDsa == 1) {
  6760. wolfSSL_DSA_free(pkey->dsa);
  6761. pkey->dsa = NULL;
  6762. }
  6763. pkey->ownDsa = 0;
  6764. #endif
  6765. #ifndef NO_DH
  6766. if (pkey->dh != NULL && pkey->ownDh == 1) {
  6767. wolfSSL_DH_free(pkey->dh);
  6768. pkey->dh = NULL;
  6769. }
  6770. pkey->ownDh = 0;
  6771. #endif
  6772. #ifdef HAVE_ECC
  6773. if (pkey->ecc != NULL && pkey->ownEcc == 1) {
  6774. wolfSSL_EC_KEY_free(pkey->ecc);
  6775. pkey->ecc = NULL;
  6776. }
  6777. pkey->ownEcc = 0;
  6778. #endif
  6779. }
  6780. #ifndef NO_RSA
  6781. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  6782. static int PopulateRSAEvpPkeyDer(WOLFSSL_EVP_PKEY *pkey)
  6783. {
  6784. int ret = 0;
  6785. int derSz = 0;
  6786. word32 pkcs8Sz = 0;
  6787. byte* derBuf = NULL;
  6788. RsaKey* rsa = NULL;
  6789. WOLFSSL_RSA *key = NULL;
  6790. if (pkey == NULL || pkey->rsa == NULL || pkey->rsa->internal == NULL) {
  6791. WOLFSSL_MSG("bad parameter");
  6792. return WOLFSSL_FAILURE;
  6793. }
  6794. key = pkey->rsa;
  6795. rsa = (RsaKey*)pkey->rsa->internal;
  6796. /* Get DER size */
  6797. if (rsa->type == RSA_PRIVATE) {
  6798. ret = wc_RsaKeyToDer(rsa, NULL, 0);
  6799. if (ret > 0) {
  6800. derSz = ret;
  6801. #ifdef HAVE_PKCS8
  6802. if (key->pkcs8HeaderSz) {
  6803. ret = wc_CreatePKCS8Key(NULL, &pkcs8Sz, NULL, derSz,
  6804. RSAk, NULL, 0);
  6805. if (ret == LENGTH_ONLY_E)
  6806. ret = 0;
  6807. }
  6808. #endif
  6809. }
  6810. }
  6811. else {
  6812. ret = wc_RsaKeyToPublicDer(rsa, NULL, 0);
  6813. if (ret > 0)
  6814. derSz = ret;
  6815. }
  6816. if (derSz == 0 || ret < 0) {
  6817. WOLFSSL_MSG("Error getting RSA DER size");
  6818. return WOLFSSL_FAILURE;
  6819. }
  6820. derBuf = (byte*)XREALLOC(pkey->pkey.ptr, derSz,
  6821. pkey->heap, DYNAMIC_TYPE_DER);
  6822. if (derBuf == NULL) {
  6823. WOLFSSL_MSG("PopulateRSAEvpPkeyDer malloc failed");
  6824. return WOLFSSL_FAILURE;
  6825. }
  6826. /* Old pointer is invalid from this point on */
  6827. pkey->pkey.ptr = (char*)derBuf;
  6828. if (rsa->type == RSA_PRIVATE) {
  6829. ret = wc_RsaKeyToDer(rsa, derBuf, derSz);
  6830. if (ret > 0) {
  6831. derSz = ret;
  6832. #ifdef HAVE_PKCS8
  6833. if (key->pkcs8HeaderSz) {
  6834. byte* keyBuf = derBuf;
  6835. int keySz = derSz;
  6836. derSz = pkcs8Sz;
  6837. /* Need new buffer for PKCS8 since we can't
  6838. * do this in-place */
  6839. derBuf = (byte*)XMALLOC(pkcs8Sz, pkey->heap,
  6840. DYNAMIC_TYPE_DER);
  6841. if (derBuf != NULL) {
  6842. ret = wc_CreatePKCS8Key(derBuf, (word32*)&derSz, keyBuf,
  6843. keySz, RSAk, NULL, 0);
  6844. XFREE(keyBuf, pkey->heap, DYNAMIC_TYPE_DER);
  6845. pkey->pkey.ptr = (char*)derBuf;
  6846. }
  6847. else
  6848. ret = MEMORY_E;
  6849. }
  6850. #endif
  6851. }
  6852. }
  6853. else {
  6854. /* Public key to DER */
  6855. ret = wc_RsaKeyToPublicDer(rsa, derBuf, derSz);
  6856. if (ret > 0)
  6857. derSz = ret;
  6858. }
  6859. if (ret < 0) {
  6860. WOLFSSL_MSG("PopulateRSAEvpPkeyDer failed");
  6861. return WOLFSSL_FAILURE;
  6862. }
  6863. else {
  6864. pkey->pkey_sz = derSz;
  6865. return WOLFSSL_SUCCESS;
  6866. }
  6867. }
  6868. #endif
  6869. WOLFSSL_RSA* wolfSSL_EVP_PKEY_get0_RSA(WOLFSSL_EVP_PKEY *pkey)
  6870. {
  6871. WOLFSSL_MSG("wolfSSL_EVP_PKEY_get0_RSA");
  6872. if (pkey == NULL)
  6873. return NULL;
  6874. return pkey->rsa;
  6875. }
  6876. WOLFSSL_RSA* wolfSSL_EVP_PKEY_get1_RSA(WOLFSSL_EVP_PKEY* pkey)
  6877. {
  6878. WOLFSSL_MSG("wolfSSL_EVP_PKEY_get1_RSA");
  6879. if (pkey == NULL || pkey->rsa == NULL)
  6880. return NULL;
  6881. if (wolfSSL_RSA_up_ref(pkey->rsa) != WOLFSSL_SUCCESS)
  6882. return NULL;
  6883. return pkey->rsa;
  6884. }
  6885. /* with set1 functions the pkey struct does not own the RSA structure
  6886. *
  6887. * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
  6888. */
  6889. int wolfSSL_EVP_PKEY_set1_RSA(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_RSA *key)
  6890. {
  6891. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_set1_RSA");
  6892. if (pkey == NULL || key == NULL)
  6893. return WOLFSSL_FAILURE;
  6894. if (wolfSSL_RSA_up_ref(key) != WOLFSSL_SUCCESS) {
  6895. WOLFSSL_MSG("wolfSSL_RSA_up_ref failed");
  6896. return WOLFSSL_FAILURE;
  6897. }
  6898. clearEVPPkeyKeys(pkey);
  6899. pkey->rsa = key;
  6900. pkey->ownRsa = 1; /* pkey does not own RSA but needs to call free on it */
  6901. pkey->type = EVP_PKEY_RSA;
  6902. pkey->pkcs8HeaderSz = key->pkcs8HeaderSz;
  6903. if (key->inSet == 0) {
  6904. if (SetRsaInternal(key) != WOLFSSL_SUCCESS) {
  6905. WOLFSSL_MSG("SetRsaInternal failed");
  6906. return WOLFSSL_FAILURE;
  6907. }
  6908. }
  6909. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  6910. if (PopulateRSAEvpPkeyDer(pkey) != WOLFSSL_SUCCESS) {
  6911. WOLFSSL_MSG("PopulateRSAEvpPkeyDer failed");
  6912. return WOLFSSL_FAILURE;
  6913. }
  6914. #endif /* WOLFSSL_KEY_GEN && !HAVE_USER_RSA */
  6915. #ifdef WC_RSA_BLINDING
  6916. if (key->ownRng == 0) {
  6917. if (wc_RsaSetRNG((RsaKey*)pkey->rsa->internal, &pkey->rng) != 0) {
  6918. WOLFSSL_MSG("Error setting RSA rng");
  6919. return WOLFSSL_FAILURE;
  6920. }
  6921. }
  6922. #endif
  6923. return WOLFSSL_SUCCESS;
  6924. }
  6925. #endif /* !NO_RSA */
  6926. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  6927. /* with set1 functions the pkey struct does not own the DSA structure
  6928. *
  6929. * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
  6930. */
  6931. int wolfSSL_EVP_PKEY_set1_DSA(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DSA *key)
  6932. {
  6933. int derMax = 0;
  6934. int derSz = 0;
  6935. DsaKey* dsa = NULL;
  6936. byte* derBuf = NULL;
  6937. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_set1_DSA");
  6938. if((pkey == NULL) || (key == NULL))return WOLFSSL_FAILURE;
  6939. clearEVPPkeyKeys(pkey);
  6940. pkey->dsa = key;
  6941. pkey->ownDsa = 0; /* pkey does not own DSA */
  6942. pkey->type = EVP_PKEY_DSA;
  6943. if (key->inSet == 0) {
  6944. if (SetDsaInternal(key) != WOLFSSL_SUCCESS) {
  6945. WOLFSSL_MSG("SetDsaInternal failed");
  6946. return WOLFSSL_FAILURE;
  6947. }
  6948. }
  6949. dsa = (DsaKey*)key->internal;
  6950. /* 4 > size of pub, priv, p, q, g + ASN.1 additional information */
  6951. derMax = 4 * wolfSSL_BN_num_bytes(key->g) + AES_BLOCK_SIZE;
  6952. derBuf = (byte*)XMALLOC(derMax, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
  6953. if (derBuf == NULL) {
  6954. WOLFSSL_MSG("malloc failed");
  6955. return WOLFSSL_FAILURE;
  6956. }
  6957. if (dsa->type == DSA_PRIVATE) {
  6958. /* Private key to DER */
  6959. derSz = wc_DsaKeyToDer(dsa, derBuf, derMax);
  6960. }
  6961. else {
  6962. /* Public key to DER */
  6963. derSz = wc_DsaKeyToPublicDer(dsa, derBuf, derMax);
  6964. }
  6965. if (derSz < 0) {
  6966. if (dsa->type == DSA_PRIVATE) {
  6967. WOLFSSL_MSG("wc_DsaKeyToDer failed");
  6968. }
  6969. else {
  6970. WOLFSSL_MSG("wc_DsaKeyToPublicDer failed");
  6971. }
  6972. XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
  6973. return WOLFSSL_FAILURE;
  6974. }
  6975. pkey->pkey.ptr = (char*)XMALLOC(derSz, pkey->heap, DYNAMIC_TYPE_DER);
  6976. if (pkey->pkey.ptr == NULL) {
  6977. WOLFSSL_MSG("key malloc failed");
  6978. XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
  6979. return WOLFSSL_FAILURE;
  6980. }
  6981. pkey->pkey_sz = derSz;
  6982. XMEMCPY(pkey->pkey.ptr, derBuf, derSz);
  6983. XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
  6984. return WOLFSSL_SUCCESS;
  6985. }
  6986. WOLFSSL_DSA* wolfSSL_EVP_PKEY_get0_DSA(struct WOLFSSL_EVP_PKEY *pkey)
  6987. {
  6988. if (!pkey) {
  6989. return NULL;
  6990. }
  6991. return pkey->dsa;
  6992. }
  6993. WOLFSSL_DSA* wolfSSL_EVP_PKEY_get1_DSA(WOLFSSL_EVP_PKEY* key)
  6994. {
  6995. WOLFSSL_DSA* local;
  6996. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_get1_DSA");
  6997. if (key == NULL) {
  6998. WOLFSSL_MSG("Bad function argument");
  6999. return NULL;
  7000. }
  7001. local = wolfSSL_DSA_new();
  7002. if (local == NULL) {
  7003. WOLFSSL_MSG("Error creating a new WOLFSSL_DSA structure");
  7004. return NULL;
  7005. }
  7006. if (key->type == EVP_PKEY_DSA) {
  7007. if (wolfSSL_DSA_LoadDer(local, (const unsigned char*)key->pkey.ptr,
  7008. key->pkey_sz) != SSL_SUCCESS) {
  7009. /* now try public key */
  7010. if (wolfSSL_DSA_LoadDer_ex(local,
  7011. (const unsigned char*)key->pkey.ptr, key->pkey_sz,
  7012. WOLFSSL_DSA_LOAD_PUBLIC) != SSL_SUCCESS) {
  7013. wolfSSL_DSA_free(local);
  7014. local = NULL;
  7015. }
  7016. }
  7017. }
  7018. else {
  7019. WOLFSSL_MSG("WOLFSSL_EVP_PKEY does not hold a DSA key");
  7020. wolfSSL_DSA_free(local);
  7021. local = NULL;
  7022. }
  7023. return local;
  7024. }
  7025. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  7026. #ifdef HAVE_ECC
  7027. WOLFSSL_EC_KEY *wolfSSL_EVP_PKEY_get0_EC_KEY(WOLFSSL_EVP_PKEY *pkey)
  7028. {
  7029. WOLFSSL_EC_KEY *eckey = NULL;
  7030. if (pkey && pkey->type == EVP_PKEY_EC) {
  7031. #ifdef HAVE_ECC
  7032. eckey = pkey->ecc;
  7033. #endif
  7034. }
  7035. return eckey;
  7036. }
  7037. WOLFSSL_EC_KEY* wolfSSL_EVP_PKEY_get1_EC_KEY(WOLFSSL_EVP_PKEY* key)
  7038. {
  7039. WOLFSSL_EC_KEY* local = NULL;
  7040. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_get1_EC_KEY");
  7041. if (key == NULL || key->type != EVP_PKEY_EC) {
  7042. return NULL;
  7043. }
  7044. if (key->type == EVP_PKEY_EC) {
  7045. if (key->ecc != NULL) {
  7046. if (wolfSSL_EC_KEY_up_ref(key->ecc) != WOLFSSL_SUCCESS) {
  7047. return NULL;
  7048. }
  7049. local = key->ecc;
  7050. }
  7051. else {
  7052. key->ecc = local = wolfSSL_EC_KEY_new();
  7053. if (local == NULL) {
  7054. WOLFSSL_MSG("Error creating a new WOLFSSL_EC_KEY structure");
  7055. return NULL;
  7056. }
  7057. if (wolfSSL_EC_KEY_LoadDer(local,
  7058. (const unsigned char*)key->pkey.ptr,
  7059. key->pkey_sz) != WOLFSSL_SUCCESS) {
  7060. /* now try public key */
  7061. if (wolfSSL_EC_KEY_LoadDer_ex(local,
  7062. (const unsigned char*)key->pkey.ptr, key->pkey_sz,
  7063. WOLFSSL_EC_KEY_LOAD_PUBLIC) != WOLFSSL_SUCCESS) {
  7064. wolfSSL_EC_KEY_free(local);
  7065. local = NULL;
  7066. }
  7067. }
  7068. }
  7069. }
  7070. else {
  7071. WOLFSSL_MSG("WOLFSSL_EVP_PKEY does not hold an EC key");
  7072. }
  7073. return local;
  7074. }
  7075. #endif /* HAVE_ECC */
  7076. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  7077. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  7078. /* with set1 functions the pkey struct does not own the DH structure
  7079. * Build the following DH Key format from the passed in WOLFSSL_DH
  7080. * then store in WOLFSSL_EVP_PKEY in DER format.
  7081. *
  7082. * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
  7083. */
  7084. int wolfSSL_EVP_PKEY_set1_DH(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_DH *key)
  7085. {
  7086. byte havePublic = 0, havePrivate = 0;
  7087. int ret;
  7088. word32 derSz = 0;
  7089. byte* derBuf = NULL;
  7090. DhKey* dhkey = NULL;
  7091. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_set1_DH");
  7092. if (pkey == NULL || key == NULL)
  7093. return WOLFSSL_FAILURE;
  7094. clearEVPPkeyKeys(pkey);
  7095. if (wolfSSL_DH_up_ref(key) != WOLFSSL_SUCCESS) {
  7096. WOLFSSL_MSG("Failed to increase dh key ref count");
  7097. return WOLFSSL_FAILURE;
  7098. }
  7099. pkey->dh = key;
  7100. pkey->ownDh = 1; /* pkey does not own DH but needs to call free on it */
  7101. pkey->type = EVP_PKEY_DH;
  7102. if (key->inSet == 0) {
  7103. if (SetDhInternal(key) != WOLFSSL_SUCCESS) {
  7104. WOLFSSL_MSG("SetDhInternal failed");
  7105. return WOLFSSL_FAILURE;
  7106. }
  7107. }
  7108. dhkey = (DhKey*)key->internal;
  7109. havePublic = mp_unsigned_bin_size(&dhkey->pub) > 0;
  7110. havePrivate = mp_unsigned_bin_size(&dhkey->priv) > 0;
  7111. /* Get size of DER buffer only */
  7112. if (havePublic && !havePrivate) {
  7113. ret = wc_DhPubKeyToDer(dhkey, NULL, &derSz);
  7114. } else if (havePrivate && !havePublic) {
  7115. ret = wc_DhPrivKeyToDer(dhkey, NULL, &derSz);
  7116. } else {
  7117. ret = wc_DhParamsToDer(dhkey,NULL,&derSz);
  7118. }
  7119. if (derSz == 0 || ret != LENGTH_ONLY_E) {
  7120. WOLFSSL_MSG("Failed to get size of DH Key");
  7121. return WOLFSSL_FAILURE;
  7122. }
  7123. derBuf = (byte*)XMALLOC(derSz, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
  7124. if (derBuf == NULL) {
  7125. WOLFSSL_MSG("malloc failed");
  7126. return WOLFSSL_FAILURE;
  7127. }
  7128. /* Fill DER buffer */
  7129. if (havePublic && !havePrivate) {
  7130. ret = wc_DhPubKeyToDer(dhkey, derBuf, &derSz);
  7131. } else if (havePrivate && !havePublic) {
  7132. ret = wc_DhPrivKeyToDer(dhkey, derBuf, &derSz);
  7133. } else {
  7134. ret = wc_DhParamsToDer(dhkey,derBuf,&derSz);
  7135. }
  7136. if (ret <= 0) {
  7137. WOLFSSL_MSG("Failed to export DH Key");
  7138. XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_TMP_BUFFER);
  7139. return WOLFSSL_FAILURE;
  7140. }
  7141. /* Store DH key into pkey (DER format) */
  7142. pkey->pkey.ptr = (char*)derBuf;
  7143. pkey->pkey_sz = derSz;
  7144. return WOLFSSL_SUCCESS;
  7145. }
  7146. WOLFSSL_DH* wolfSSL_EVP_PKEY_get0_DH(WOLFSSL_EVP_PKEY* key)
  7147. {
  7148. if (!key) {
  7149. return NULL;
  7150. }
  7151. return key->dh;
  7152. }
  7153. WOLFSSL_DH* wolfSSL_EVP_PKEY_get1_DH(WOLFSSL_EVP_PKEY* key)
  7154. {
  7155. WOLFSSL_DH* local = NULL;
  7156. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_get1_DH");
  7157. if (key == NULL || key->dh == NULL) {
  7158. WOLFSSL_MSG("Bad function argument");
  7159. return NULL;
  7160. }
  7161. if (key->type == EVP_PKEY_DH) {
  7162. /* if key->dh already exists copy instead of re-importing from DER */
  7163. if (key->dh != NULL) {
  7164. if (wolfSSL_DH_up_ref(key->dh) != WOLFSSL_SUCCESS) {
  7165. return NULL;
  7166. }
  7167. local = key->dh;
  7168. }
  7169. else {
  7170. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  7171. (HAVE_FIPS_VERSION>2)))
  7172. local = wolfSSL_DH_new();
  7173. if (local == NULL) {
  7174. WOLFSSL_MSG("Error creating a new WOLFSSL_DH structure");
  7175. return NULL;
  7176. }
  7177. if (wolfSSL_DH_LoadDer(local, (const unsigned char*)key->pkey.ptr,
  7178. key->pkey_sz) != SSL_SUCCESS) {
  7179. wolfSSL_DH_free(local);
  7180. WOLFSSL_MSG("Error wolfSSL_DH_LoadDer");
  7181. local = NULL;
  7182. }
  7183. #else
  7184. WOLFSSL_MSG("EVP_PKEY does not hold DH struct");
  7185. return NULL;
  7186. #endif
  7187. }
  7188. }
  7189. else {
  7190. WOLFSSL_MSG("WOLFSSL_EVP_PKEY does not hold a DH key");
  7191. wolfSSL_DH_free(local);
  7192. return NULL;
  7193. }
  7194. return local;
  7195. }
  7196. #endif /* NO_DH && WOLFSSL_DH_EXTRA && NO_FILESYSTEM */
  7197. int wolfSSL_EVP_PKEY_assign(WOLFSSL_EVP_PKEY *pkey, int type, void *key)
  7198. {
  7199. int ret;
  7200. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_assign");
  7201. /* pkey and key checked if NULL in subsequent assign functions */
  7202. switch(type) {
  7203. #ifndef NO_RSA
  7204. case EVP_PKEY_RSA:
  7205. ret = wolfSSL_EVP_PKEY_assign_RSA(pkey, (WOLFSSL_RSA*)key);
  7206. break;
  7207. #endif
  7208. #ifndef NO_DSA
  7209. case EVP_PKEY_DSA:
  7210. ret = wolfSSL_EVP_PKEY_assign_DSA(pkey, (WOLFSSL_DSA*)key);
  7211. break;
  7212. #endif
  7213. #ifdef HAVE_ECC
  7214. case EVP_PKEY_EC:
  7215. ret = wolfSSL_EVP_PKEY_assign_EC_KEY(pkey, (WOLFSSL_EC_KEY*)key);
  7216. break;
  7217. #endif
  7218. #ifndef NO_DH
  7219. case EVP_PKEY_DH:
  7220. ret = wolfSSL_EVP_PKEY_assign_DH(pkey, (WOLFSSL_DH*)key);
  7221. break;
  7222. #endif
  7223. default:
  7224. WOLFSSL_MSG("Unknown EVP_PKEY type in wolfSSL_EVP_PKEY_assign.");
  7225. ret = WOLFSSL_FAILURE;
  7226. }
  7227. return ret;
  7228. }
  7229. #endif /* WOLFSSL_QT || OPENSSL_ALL */
  7230. #if defined(HAVE_ECC)
  7231. /* try and populate public pkey_sz and pkey.ptr */
  7232. static int ECC_populate_EVP_PKEY(EVP_PKEY* pkey, WOLFSSL_EC_KEY *key)
  7233. {
  7234. int derSz = 0;
  7235. byte* derBuf = NULL;
  7236. ecc_key* ecc;
  7237. if (pkey == NULL || key == NULL || key->internal == NULL)
  7238. return WOLFSSL_FAILURE;
  7239. ecc = (ecc_key*)key->internal;
  7240. if (ecc->type == ECC_PRIVATEKEY || ecc->type == ECC_PRIVATEKEY_ONLY) {
  7241. #ifdef HAVE_PKCS8
  7242. if (key->pkcs8HeaderSz) {
  7243. /* when key has pkcs8 header the pkey should too */
  7244. if (wc_EccKeyToPKCS8(ecc, NULL, (word32*)&derSz) == LENGTH_ONLY_E) {
  7245. derBuf = (byte*)XMALLOC(derSz, pkey->heap, DYNAMIC_TYPE_OPENSSL);
  7246. if (derBuf) {
  7247. if (wc_EccKeyToPKCS8(ecc, derBuf, (word32*)&derSz) >= 0) {
  7248. if (pkey->pkey.ptr) {
  7249. XFREE(pkey->pkey.ptr, pkey->heap, DYNAMIC_TYPE_OPENSSL);
  7250. }
  7251. pkey->pkey_sz = (int)derSz;
  7252. pkey->pkey.ptr = (char*)derBuf;
  7253. pkey->pkcs8HeaderSz = key->pkcs8HeaderSz;
  7254. return WOLFSSL_SUCCESS;
  7255. }
  7256. else {
  7257. XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_OPENSSL);
  7258. derBuf = NULL;
  7259. }
  7260. }
  7261. }
  7262. }
  7263. else
  7264. #endif /* HAVE_PKCS8 */
  7265. {
  7266. if (ecc->type == ECC_PRIVATEKEY_ONLY) {
  7267. if (wc_ecc_make_pub(ecc, NULL) != MP_OKAY) {
  7268. return WOLFSSL_FAILURE;
  7269. }
  7270. }
  7271. /* if not, the pkey will be traditional ecc key */
  7272. if ((derSz = wc_EccKeyDerSize(ecc, 1)) > 0) {
  7273. derBuf = (byte*)XMALLOC(derSz, pkey->heap, DYNAMIC_TYPE_OPENSSL);
  7274. if (derBuf) {
  7275. if (wc_EccKeyToDer(ecc, derBuf, derSz) >= 0) {
  7276. if (pkey->pkey.ptr) {
  7277. XFREE(pkey->pkey.ptr, pkey->heap, DYNAMIC_TYPE_OPENSSL);
  7278. }
  7279. pkey->pkey_sz = (int)derSz;
  7280. pkey->pkey.ptr = (char*)derBuf;
  7281. return WOLFSSL_SUCCESS;
  7282. }
  7283. else {
  7284. XFREE(derBuf, pkey->heap, DYNAMIC_TYPE_OPENSSL);
  7285. derBuf = NULL;
  7286. }
  7287. }
  7288. }
  7289. }
  7290. }
  7291. else if (ecc->type == ECC_PUBLICKEY) {
  7292. if ((derSz = (word32)wc_EccPublicKeyDerSize(ecc, 1)) > 0) {
  7293. derBuf = (byte*)XREALLOC(pkey->pkey.ptr, derSz, NULL,
  7294. DYNAMIC_TYPE_OPENSSL);
  7295. if (derBuf != NULL) {
  7296. pkey->pkey.ptr = (char*)derBuf;
  7297. if ((derSz = wc_EccPublicKeyToDer(ecc, derBuf, derSz, 1)) < 0) {
  7298. XFREE(derBuf, NULL, DYNAMIC_TYPE_OPENSSL);
  7299. derBuf = NULL;
  7300. }
  7301. }
  7302. }
  7303. }
  7304. if (derBuf != NULL) {
  7305. pkey->pkey_sz = (int)derSz;
  7306. return WOLFSSL_SUCCESS;
  7307. }
  7308. else {
  7309. return WOLFSSL_FAILURE;
  7310. }
  7311. }
  7312. int wolfSSL_EVP_PKEY_set1_EC_KEY(WOLFSSL_EVP_PKEY *pkey, WOLFSSL_EC_KEY *key)
  7313. {
  7314. #ifdef HAVE_ECC
  7315. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_set1_EC_KEY");
  7316. if (pkey == NULL || key == NULL)
  7317. return WOLFSSL_FAILURE;
  7318. clearEVPPkeyKeys(pkey);
  7319. if (wolfSSL_EC_KEY_up_ref(key) != WOLFSSL_SUCCESS) {
  7320. WOLFSSL_MSG("wolfSSL_EC_KEY_up_ref failed");
  7321. return WOLFSSL_FAILURE;
  7322. }
  7323. pkey->ecc = key;
  7324. pkey->ownEcc = 1; /* pkey needs to call free on key */
  7325. pkey->type = EVP_PKEY_EC;
  7326. return ECC_populate_EVP_PKEY(pkey, key);
  7327. #else
  7328. (void)pkey;
  7329. (void)key;
  7330. return WOLFSSL_FAILURE;
  7331. #endif /* HAVE_ECC */
  7332. }
  7333. void* wolfSSL_EVP_X_STATE(const WOLFSSL_EVP_CIPHER_CTX* ctx)
  7334. {
  7335. WOLFSSL_MSG("wolfSSL_EVP_X_STATE");
  7336. if (ctx) {
  7337. switch (ctx->cipherType) {
  7338. case ARC4_TYPE:
  7339. WOLFSSL_MSG("returning arc4 state");
  7340. return (void*)&ctx->cipher.arc4.x;
  7341. default:
  7342. WOLFSSL_MSG("bad x state type");
  7343. return 0;
  7344. }
  7345. }
  7346. return NULL;
  7347. }
  7348. int wolfSSL_EVP_PKEY_assign_EC_KEY(EVP_PKEY* pkey, WOLFSSL_EC_KEY* key)
  7349. {
  7350. int ret;
  7351. if (pkey == NULL || key == NULL)
  7352. return WOLFSSL_FAILURE;
  7353. /* try and populate public pkey_sz and pkey.ptr */
  7354. ret = ECC_populate_EVP_PKEY(pkey, key);
  7355. if (ret == WOLFSSL_SUCCESS) { /* take ownership of key if can be used */
  7356. clearEVPPkeyKeys(pkey); /* clear out any previous keys */
  7357. pkey->type = EVP_PKEY_EC;
  7358. pkey->ecc = key;
  7359. pkey->ownEcc = 1;
  7360. }
  7361. return ret;
  7362. }
  7363. #endif /* HAVE_ECC */
  7364. #ifndef NO_WOLFSSL_STUB
  7365. const WOLFSSL_EVP_MD* wolfSSL_EVP_ripemd160(void)
  7366. {
  7367. WOLFSSL_MSG("wolfSSL_ripemd160");
  7368. WOLFSSL_STUB("EVP_ripemd160");
  7369. return NULL;
  7370. }
  7371. #endif
  7372. int wolfSSL_EVP_MD_block_size(const WOLFSSL_EVP_MD* type)
  7373. {
  7374. WOLFSSL_MSG("wolfSSL_EVP_MD_block_size");
  7375. if (type == NULL) {
  7376. WOLFSSL_MSG("No md type arg");
  7377. return BAD_FUNC_ARG;
  7378. }
  7379. #ifndef NO_SHA
  7380. if ((XSTRCMP(type, "SHA") == 0) || (XSTRCMP(type, "SHA1") == 0)) {
  7381. return WC_SHA_BLOCK_SIZE;
  7382. } else
  7383. #endif
  7384. #ifndef NO_SHA256
  7385. if (XSTRCMP(type, "SHA256") == 0) {
  7386. return WC_SHA256_BLOCK_SIZE;
  7387. } else
  7388. #endif
  7389. #ifndef NO_MD4
  7390. if (XSTRCMP(type, "MD4") == 0) {
  7391. return MD4_BLOCK_SIZE;
  7392. } else
  7393. #endif
  7394. #ifndef NO_MD5
  7395. if (XSTRCMP(type, "MD5") == 0) {
  7396. return WC_MD5_BLOCK_SIZE;
  7397. } else
  7398. #endif
  7399. #ifdef WOLFSSL_SHA224
  7400. if (XSTRCMP(type, "SHA224") == 0) {
  7401. return WC_SHA224_BLOCK_SIZE;
  7402. } else
  7403. #endif
  7404. #ifdef WOLFSSL_SHA384
  7405. if (XSTRCMP(type, "SHA384") == 0) {
  7406. return WC_SHA384_BLOCK_SIZE;
  7407. } else
  7408. #endif
  7409. #ifdef WOLFSSL_SHA512
  7410. if (XSTRCMP(type, "SHA512") == 0) {
  7411. return WC_SHA512_BLOCK_SIZE;
  7412. } else
  7413. #endif
  7414. #ifdef WOLFSSL_SHA3
  7415. #ifndef WOLFSSL_NOSHA3_224
  7416. if (XSTRCMP(type, "SHA3_224") == 0) {
  7417. return WC_SHA3_224_BLOCK_SIZE;
  7418. } else
  7419. #endif
  7420. #ifndef WOLFSSL_NOSHA3_256
  7421. if (XSTRCMP(type, "SHA3_256") == 0) {
  7422. return WC_SHA3_256_BLOCK_SIZE;
  7423. } else
  7424. #endif
  7425. #ifndef WOLFSSL_NOSHA3_384
  7426. if (XSTRCMP(type, "SHA3_384") == 0) {
  7427. return WC_SHA3_384_BLOCK_SIZE;
  7428. } else
  7429. #endif
  7430. #ifndef WOLFSSL_NOSHA3_512
  7431. if (XSTRCMP(type, "SHA3_512") == 0) {
  7432. return WC_SHA3_512_BLOCK_SIZE;
  7433. }
  7434. #endif
  7435. #endif /* WOLFSSL_SHA3 */
  7436. return BAD_FUNC_ARG;
  7437. }
  7438. int wolfSSL_EVP_MD_size(const WOLFSSL_EVP_MD* type)
  7439. {
  7440. WOLFSSL_MSG("wolfSSL_EVP_MD_size");
  7441. if (type == NULL) {
  7442. WOLFSSL_MSG("No md type arg");
  7443. return BAD_FUNC_ARG;
  7444. }
  7445. #ifndef NO_SHA
  7446. if ((XSTRCMP(type, "SHA") == 0) || (XSTRCMP(type, "SHA1") == 0)) {
  7447. return WC_SHA_DIGEST_SIZE;
  7448. } else
  7449. #endif
  7450. #ifndef NO_SHA256
  7451. if (XSTRCMP(type, "SHA256") == 0) {
  7452. return WC_SHA256_DIGEST_SIZE;
  7453. } else
  7454. #endif
  7455. #ifndef NO_MD4
  7456. if (XSTRCMP(type, "MD4") == 0) {
  7457. return MD4_DIGEST_SIZE;
  7458. } else
  7459. #endif
  7460. #ifndef NO_MD5
  7461. if (XSTRCMP(type, "MD5") == 0) {
  7462. return WC_MD5_DIGEST_SIZE;
  7463. } else
  7464. #endif
  7465. #ifdef WOLFSSL_SHA224
  7466. if (XSTRCMP(type, "SHA224") == 0) {
  7467. return WC_SHA224_DIGEST_SIZE;
  7468. } else
  7469. #endif
  7470. #ifdef WOLFSSL_SHA384
  7471. if (XSTRCMP(type, "SHA384") == 0) {
  7472. return WC_SHA384_DIGEST_SIZE;
  7473. } else
  7474. #endif
  7475. #ifdef WOLFSSL_SHA512
  7476. if (XSTRCMP(type, "SHA512") == 0) {
  7477. return WC_SHA512_DIGEST_SIZE;
  7478. } else
  7479. #ifndef WOLFSSL_NOSHA512_224
  7480. if (XSTRCMP(type, "SHA512_224") == 0) {
  7481. return WC_SHA512_224_DIGEST_SIZE;
  7482. } else
  7483. #endif
  7484. #ifndef WOLFSSL_NOSHA512_256
  7485. if (XSTRCMP(type, "SHA512_256") == 0) {
  7486. return WC_SHA512_256_DIGEST_SIZE;
  7487. } else
  7488. #endif
  7489. #endif
  7490. #ifdef WOLFSSL_SHA3
  7491. #ifndef WOLFSSL_NOSHA3_224
  7492. if (XSTRCMP(type, "SHA3_224") == 0) {
  7493. return WC_SHA3_224_DIGEST_SIZE;
  7494. } else
  7495. #endif
  7496. #ifndef WOLFSSL_NOSHA3_256
  7497. if (XSTRCMP(type, "SHA3_256") == 0) {
  7498. return WC_SHA3_256_DIGEST_SIZE;
  7499. } else
  7500. #endif
  7501. #ifndef WOLFSSL_NOSHA3_384
  7502. if (XSTRCMP(type, "SHA3_384") == 0) {
  7503. return WC_SHA3_384_DIGEST_SIZE;
  7504. } else
  7505. #endif
  7506. #ifndef WOLFSSL_NOSHA3_512
  7507. if (XSTRCMP(type, "SHA3_512") == 0) {
  7508. return WC_SHA3_512_DIGEST_SIZE;
  7509. }
  7510. #endif
  7511. #endif /* WOLFSSL_SHA3 */
  7512. return BAD_FUNC_ARG;
  7513. }
  7514. int wolfSSL_EVP_MD_pkey_type(const WOLFSSL_EVP_MD* type)
  7515. {
  7516. int ret = BAD_FUNC_ARG;
  7517. WOLFSSL_ENTER("wolfSSL_EVP_MD_pkey_type");
  7518. if (type != NULL) {
  7519. if (XSTRCMP(type, "MD5") == 0) {
  7520. ret = NID_md5WithRSAEncryption;
  7521. }
  7522. else if (XSTRCMP(type, "SHA1") == 0) {
  7523. ret = NID_sha1WithRSAEncryption;
  7524. }
  7525. else if (XSTRCMP(type, "SHA224") == 0) {
  7526. ret = NID_sha224WithRSAEncryption;
  7527. }
  7528. else if (XSTRCMP(type, "SHA256") == 0) {
  7529. ret = NID_sha256WithRSAEncryption;
  7530. }
  7531. else if (XSTRCMP(type, "SHA384") == 0) {
  7532. ret = NID_sha384WithRSAEncryption;
  7533. }
  7534. else if (XSTRCMP(type, "SHA512") == 0) {
  7535. ret = NID_sha512WithRSAEncryption;
  7536. }
  7537. }
  7538. WOLFSSL_LEAVE("wolfSSL_EVP_MD_pkey_type", ret);
  7539. return ret;
  7540. }
  7541. int wolfSSL_EVP_CIPHER_CTX_iv_length(const WOLFSSL_EVP_CIPHER_CTX* ctx)
  7542. {
  7543. WOLFSSL_MSG("wolfSSL_EVP_CIPHER_CTX_iv_length");
  7544. switch (ctx->cipherType) {
  7545. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  7546. case AES_128_CBC_TYPE :
  7547. case AES_192_CBC_TYPE :
  7548. case AES_256_CBC_TYPE :
  7549. WOLFSSL_MSG("AES CBC");
  7550. return AES_BLOCK_SIZE;
  7551. #endif
  7552. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  7553. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  7554. #ifdef HAVE_AESGCM
  7555. case AES_128_GCM_TYPE :
  7556. case AES_192_GCM_TYPE :
  7557. case AES_256_GCM_TYPE :
  7558. WOLFSSL_MSG("AES GCM");
  7559. if (ctx->ivSz != 0) {
  7560. return ctx->ivSz;
  7561. }
  7562. return GCM_NONCE_MID_SZ;
  7563. #endif
  7564. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION >= 2 */
  7565. #ifdef WOLFSSL_AES_COUNTER
  7566. case AES_128_CTR_TYPE :
  7567. case AES_192_CTR_TYPE :
  7568. case AES_256_CTR_TYPE :
  7569. WOLFSSL_MSG("AES CTR");
  7570. return AES_BLOCK_SIZE;
  7571. #endif
  7572. #ifndef NO_DES3
  7573. case DES_CBC_TYPE :
  7574. WOLFSSL_MSG("DES CBC");
  7575. return DES_BLOCK_SIZE;
  7576. case DES_EDE3_CBC_TYPE :
  7577. WOLFSSL_MSG("DES EDE3 CBC");
  7578. return DES_BLOCK_SIZE;
  7579. #endif
  7580. #ifndef NO_RC4
  7581. case ARC4_TYPE :
  7582. WOLFSSL_MSG("ARC4");
  7583. return 0;
  7584. #endif
  7585. #ifdef WOLFSSL_AES_CFB
  7586. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  7587. case AES_128_CFB1_TYPE:
  7588. case AES_192_CFB1_TYPE:
  7589. case AES_256_CFB1_TYPE:
  7590. WOLFSSL_MSG("AES CFB1");
  7591. return AES_BLOCK_SIZE;
  7592. case AES_128_CFB8_TYPE:
  7593. case AES_192_CFB8_TYPE:
  7594. case AES_256_CFB8_TYPE:
  7595. WOLFSSL_MSG("AES CFB8");
  7596. return AES_BLOCK_SIZE;
  7597. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  7598. case AES_128_CFB128_TYPE:
  7599. case AES_192_CFB128_TYPE:
  7600. case AES_256_CFB128_TYPE:
  7601. WOLFSSL_MSG("AES CFB128");
  7602. return AES_BLOCK_SIZE;
  7603. #endif /* WOLFSSL_AES_CFB */
  7604. #if defined(WOLFSSL_AES_OFB)
  7605. case AES_128_OFB_TYPE:
  7606. case AES_192_OFB_TYPE:
  7607. case AES_256_OFB_TYPE:
  7608. WOLFSSL_MSG("AES OFB");
  7609. return AES_BLOCK_SIZE;
  7610. #endif /* WOLFSSL_AES_OFB */
  7611. #ifdef WOLFSSL_AES_XTS
  7612. case AES_128_XTS_TYPE:
  7613. case AES_256_XTS_TYPE:
  7614. WOLFSSL_MSG("AES XTS");
  7615. return AES_BLOCK_SIZE;
  7616. #endif /* WOLFSSL_AES_XTS */
  7617. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  7618. case CHACHA20_POLY1305_TYPE:
  7619. WOLFSSL_MSG("CHACHA20 POLY1305");
  7620. return CHACHA20_POLY1305_AEAD_IV_SIZE;
  7621. #endif /* HAVE_CHACHA HAVE_POLY1305 */
  7622. case NULL_CIPHER_TYPE :
  7623. WOLFSSL_MSG("NULL");
  7624. return 0;
  7625. default: {
  7626. WOLFSSL_MSG("bad type");
  7627. }
  7628. }
  7629. return 0;
  7630. }
  7631. int wolfSSL_EVP_CIPHER_iv_length(const WOLFSSL_EVP_CIPHER* cipher)
  7632. {
  7633. const char *name = (const char *)cipher;
  7634. WOLFSSL_MSG("wolfSSL_EVP_CIPHER_iv_length");
  7635. #ifndef NO_AES
  7636. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  7637. #ifdef WOLFSSL_AES_128
  7638. if (XSTRCMP(name, EVP_AES_128_CBC) == 0)
  7639. return AES_BLOCK_SIZE;
  7640. #endif
  7641. #ifdef WOLFSSL_AES_192
  7642. if (XSTRCMP(name, EVP_AES_192_CBC) == 0)
  7643. return AES_BLOCK_SIZE;
  7644. #endif
  7645. #ifdef WOLFSSL_AES_256
  7646. if (XSTRCMP(name, EVP_AES_256_CBC) == 0)
  7647. return AES_BLOCK_SIZE;
  7648. #endif
  7649. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  7650. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  7651. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  7652. #ifdef HAVE_AESGCM
  7653. #ifdef WOLFSSL_AES_128
  7654. if (XSTRCMP(name, EVP_AES_128_GCM) == 0)
  7655. return GCM_NONCE_MID_SZ;
  7656. #endif
  7657. #ifdef WOLFSSL_AES_192
  7658. if (XSTRCMP(name, EVP_AES_192_GCM) == 0)
  7659. return GCM_NONCE_MID_SZ;
  7660. #endif
  7661. #ifdef WOLFSSL_AES_256
  7662. if (XSTRCMP(name, EVP_AES_256_GCM) == 0)
  7663. return GCM_NONCE_MID_SZ;
  7664. #endif
  7665. #endif /* HAVE_AESGCM */
  7666. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION >= 2 */
  7667. #ifdef WOLFSSL_AES_COUNTER
  7668. #ifdef WOLFSSL_AES_128
  7669. if (XSTRCMP(name, EVP_AES_128_CTR) == 0)
  7670. return AES_BLOCK_SIZE;
  7671. #endif
  7672. #ifdef WOLFSSL_AES_192
  7673. if (XSTRCMP(name, EVP_AES_192_CTR) == 0)
  7674. return AES_BLOCK_SIZE;
  7675. #endif
  7676. #ifdef WOLFSSL_AES_256
  7677. if (XSTRCMP(name, EVP_AES_256_CTR) == 0)
  7678. return AES_BLOCK_SIZE;
  7679. #endif
  7680. #endif
  7681. #ifdef WOLFSSL_AES_XTS
  7682. #ifdef WOLFSSL_AES_128
  7683. if (XSTRCMP(name, EVP_AES_128_XTS) == 0)
  7684. return AES_BLOCK_SIZE;
  7685. #endif /* WOLFSSL_AES_128 */
  7686. #ifdef WOLFSSL_AES_256
  7687. if (XSTRCMP(name, EVP_AES_256_XTS) == 0)
  7688. return AES_BLOCK_SIZE;
  7689. #endif /* WOLFSSL_AES_256 */
  7690. #endif /* WOLFSSL_AES_XTS */
  7691. #endif
  7692. #ifndef NO_DES3
  7693. if ((XSTRCMP(name, EVP_DES_CBC) == 0) ||
  7694. (XSTRCMP(name, EVP_DES_EDE3_CBC) == 0)) {
  7695. return DES_BLOCK_SIZE;
  7696. }
  7697. #endif
  7698. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  7699. if (XSTRCMP(name, EVP_CHACHA20_POLY1305) == 0)
  7700. return CHACHA20_POLY1305_AEAD_IV_SIZE;
  7701. #endif
  7702. (void)name;
  7703. return 0;
  7704. }
  7705. int wolfSSL_EVP_X_STATE_LEN(const WOLFSSL_EVP_CIPHER_CTX* ctx)
  7706. {
  7707. WOLFSSL_MSG("wolfSSL_EVP_X_STATE_LEN");
  7708. if (ctx) {
  7709. switch (ctx->cipherType) {
  7710. case ARC4_TYPE:
  7711. WOLFSSL_MSG("returning arc4 state size");
  7712. return sizeof(Arc4);
  7713. default:
  7714. WOLFSSL_MSG("bad x state type");
  7715. return 0;
  7716. }
  7717. }
  7718. return 0;
  7719. }
  7720. /* return of pkey->type which will be EVP_PKEY_RSA for example.
  7721. *
  7722. * type type of EVP_PKEY
  7723. *
  7724. * returns type or if type is not found then NID_undef
  7725. */
  7726. int wolfSSL_EVP_PKEY_type(int type)
  7727. {
  7728. WOLFSSL_MSG("wolfSSL_EVP_PKEY_type");
  7729. switch (type) {
  7730. case EVP_PKEY_RSA:
  7731. return EVP_PKEY_RSA;
  7732. case EVP_PKEY_DSA:
  7733. return EVP_PKEY_DSA;
  7734. case EVP_PKEY_EC:
  7735. return EVP_PKEY_EC;
  7736. case EVP_PKEY_DH:
  7737. return EVP_PKEY_DH;
  7738. default:
  7739. return NID_undef;
  7740. }
  7741. }
  7742. int wolfSSL_EVP_PKEY_id(const WOLFSSL_EVP_PKEY *pkey)
  7743. {
  7744. if (pkey != NULL)
  7745. return pkey->type;
  7746. return 0;
  7747. }
  7748. int wolfSSL_EVP_PKEY_base_id(const WOLFSSL_EVP_PKEY *pkey)
  7749. {
  7750. if (pkey == NULL)
  7751. return NID_undef;
  7752. return wolfSSL_EVP_PKEY_type(pkey->type);
  7753. }
  7754. int wolfSSL_EVP_PKEY_get_default_digest_nid(WOLFSSL_EVP_PKEY *pkey, int *pnid)
  7755. {
  7756. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_get_default_digest_nid");
  7757. if (!pkey || !pnid) {
  7758. WOLFSSL_MSG("Bad parameter");
  7759. return WOLFSSL_FAILURE;
  7760. }
  7761. switch (pkey->type) {
  7762. case EVP_PKEY_HMAC:
  7763. #ifndef NO_DSA
  7764. case EVP_PKEY_DSA:
  7765. #endif
  7766. #ifndef NO_RSA
  7767. case EVP_PKEY_RSA:
  7768. #endif
  7769. #ifdef HAVE_ECC
  7770. case EVP_PKEY_EC:
  7771. #endif
  7772. *pnid = NID_sha256;
  7773. return WOLFSSL_SUCCESS;
  7774. default:
  7775. return WOLFSSL_FAILURE;
  7776. }
  7777. }
  7778. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  7779. WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKCS82PKEY(const WOLFSSL_PKCS8_PRIV_KEY_INFO* p8)
  7780. {
  7781. if (p8 == NULL || p8->pkey.ptr == NULL) {
  7782. return NULL;
  7783. }
  7784. return wolfSSL_d2i_PrivateKey_EVP(NULL, (unsigned char**)&p8->pkey.ptr,
  7785. p8->pkey_sz);
  7786. }
  7787. /* in wolf PKCS8_PRIV_KEY_INFO and WOLFSSL_EVP_PKEY are same type */
  7788. /* this function just casts and returns pointer */
  7789. WOLFSSL_PKCS8_PRIV_KEY_INFO* wolfSSL_EVP_PKEY2PKCS8(const WOLFSSL_EVP_PKEY* pkey)
  7790. {
  7791. return (WOLFSSL_PKCS8_PRIV_KEY_INFO*)pkey;
  7792. }
  7793. #endif
  7794. /* increments ref count of WOLFSSL_EVP_PKEY. Return 1 on success, 0 on error */
  7795. int wolfSSL_EVP_PKEY_up_ref(WOLFSSL_EVP_PKEY* pkey)
  7796. {
  7797. if (pkey) {
  7798. #ifndef SINGLE_THREADED
  7799. if (wc_LockMutex(&pkey->refMutex) != 0) {
  7800. WOLFSSL_MSG("Failed to lock pkey mutex");
  7801. }
  7802. #endif
  7803. pkey->references++;
  7804. #ifndef SINGLE_THREADED
  7805. wc_UnLockMutex(&pkey->refMutex);
  7806. #endif
  7807. return WOLFSSL_SUCCESS;
  7808. }
  7809. return WOLFSSL_FAILURE;
  7810. }
  7811. #ifndef NO_RSA
  7812. int wolfSSL_EVP_PKEY_assign_RSA(EVP_PKEY* pkey, WOLFSSL_RSA* key)
  7813. {
  7814. if (pkey == NULL || key == NULL)
  7815. return WOLFSSL_FAILURE;
  7816. clearEVPPkeyKeys(pkey);
  7817. pkey->type = EVP_PKEY_RSA;
  7818. pkey->rsa = key;
  7819. pkey->ownRsa = 1;
  7820. /* try and populate pkey_sz and pkey.ptr */
  7821. if (key->internal) {
  7822. RsaKey* rsa = (RsaKey*)key->internal;
  7823. int ret = wc_RsaKeyToDer(rsa, NULL, 0);
  7824. if (ret > 0) {
  7825. int derSz = ret;
  7826. byte* derBuf = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7827. if (derBuf) {
  7828. ret = wc_RsaKeyToDer(rsa, derBuf, derSz);
  7829. if (ret >= 0) {
  7830. pkey->pkey_sz = ret;
  7831. pkey->pkey.ptr = (char*)derBuf;
  7832. }
  7833. else { /* failure - okay to ignore */
  7834. XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7835. derBuf = NULL;
  7836. }
  7837. }
  7838. }
  7839. }
  7840. return WOLFSSL_SUCCESS;
  7841. }
  7842. #endif /* !NO_RSA */
  7843. #ifndef NO_DSA
  7844. int wolfSSL_EVP_PKEY_assign_DSA(EVP_PKEY* pkey, WOLFSSL_DSA* key)
  7845. {
  7846. if (pkey == NULL || key == NULL)
  7847. return WOLFSSL_FAILURE;
  7848. clearEVPPkeyKeys(pkey);
  7849. pkey->type = EVP_PKEY_DSA;
  7850. pkey->dsa = key;
  7851. pkey->ownDsa = 1;
  7852. return WOLFSSL_SUCCESS;
  7853. }
  7854. #endif /* !NO_DSA */
  7855. #ifndef NO_DH
  7856. int wolfSSL_EVP_PKEY_assign_DH(EVP_PKEY* pkey, WOLFSSL_DH* key)
  7857. {
  7858. if (pkey == NULL || key == NULL)
  7859. return WOLFSSL_FAILURE;
  7860. clearEVPPkeyKeys(pkey);
  7861. pkey->type = EVP_PKEY_DH;
  7862. pkey->dh = key;
  7863. pkey->ownDh = 1;
  7864. return WOLFSSL_SUCCESS;
  7865. }
  7866. #endif /* !NO_DH */
  7867. #endif /* OPENSSL_EXTRA */
  7868. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  7869. /* Subset of OPENSSL_EXTRA for PKEY operations PKEY free is needed by the
  7870. * subset of X509 API */
  7871. WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new(void){
  7872. return wolfSSL_EVP_PKEY_new_ex(NULL);
  7873. }
  7874. WOLFSSL_EVP_PKEY* wolfSSL_EVP_PKEY_new_ex(void* heap)
  7875. {
  7876. WOLFSSL_EVP_PKEY* pkey;
  7877. int ret;
  7878. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_new_ex");
  7879. pkey = (WOLFSSL_EVP_PKEY*)XMALLOC(sizeof(WOLFSSL_EVP_PKEY), heap,
  7880. DYNAMIC_TYPE_PUBLIC_KEY);
  7881. if (pkey != NULL) {
  7882. XMEMSET(pkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  7883. pkey->heap = heap;
  7884. pkey->type = WOLFSSL_EVP_PKEY_DEFAULT;
  7885. #ifndef SINGLE_THREADED
  7886. /* init of mutex needs to come before wolfSSL_EVP_PKEY_free */
  7887. ret = wc_InitMutex(&pkey->refMutex);
  7888. if (ret != 0){
  7889. XFREE(pkey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7890. WOLFSSL_MSG("Issue initializing mutex");
  7891. return NULL;
  7892. }
  7893. #endif
  7894. #ifndef HAVE_FIPS
  7895. ret = wc_InitRng_ex(&pkey->rng, heap, INVALID_DEVID);
  7896. #else
  7897. ret = wc_InitRng(&pkey->rng);
  7898. #endif
  7899. pkey->references = 1;
  7900. if (ret != 0){
  7901. wolfSSL_EVP_PKEY_free(pkey);
  7902. WOLFSSL_MSG("Issue initializing RNG");
  7903. return NULL;
  7904. }
  7905. }
  7906. else {
  7907. WOLFSSL_MSG("memory failure");
  7908. }
  7909. return pkey;
  7910. }
  7911. void wolfSSL_EVP_PKEY_free(WOLFSSL_EVP_PKEY* key)
  7912. {
  7913. int doFree = 0;
  7914. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_free");
  7915. if (key != NULL) {
  7916. #ifndef SINGLE_THREADED
  7917. if (wc_LockMutex(&key->refMutex) != 0) {
  7918. WOLFSSL_MSG("Couldn't lock pkey mutex");
  7919. }
  7920. #endif
  7921. /* only free if all references to it are done */
  7922. key->references--;
  7923. if (key->references == 0) {
  7924. doFree = 1;
  7925. }
  7926. #ifndef SINGLE_THREADED
  7927. wc_UnLockMutex(&key->refMutex);
  7928. #endif
  7929. if (doFree) {
  7930. wc_FreeRng(&key->rng);
  7931. if (key->pkey.ptr != NULL) {
  7932. XFREE(key->pkey.ptr, key->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7933. key->pkey.ptr = NULL;
  7934. }
  7935. switch(key->type)
  7936. {
  7937. #ifndef NO_RSA
  7938. case EVP_PKEY_RSA:
  7939. if (key->rsa != NULL && key->ownRsa == 1) {
  7940. wolfSSL_RSA_free(key->rsa);
  7941. key->rsa = NULL;
  7942. }
  7943. break;
  7944. #endif /* NO_RSA */
  7945. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA)
  7946. case EVP_PKEY_EC:
  7947. if (key->ecc != NULL && key->ownEcc == 1) {
  7948. wolfSSL_EC_KEY_free(key->ecc);
  7949. key->ecc = NULL;
  7950. }
  7951. break;
  7952. #endif /* HAVE_ECC && OPENSSL_EXTRA */
  7953. #ifndef NO_DSA
  7954. case EVP_PKEY_DSA:
  7955. if (key->dsa != NULL && key->ownDsa == 1) {
  7956. wolfSSL_DSA_free(key->dsa);
  7957. key->dsa = NULL;
  7958. }
  7959. break;
  7960. #endif /* NO_DSA */
  7961. #if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
  7962. case EVP_PKEY_DH:
  7963. if (key->dh != NULL && key->ownDh == 1) {
  7964. wolfSSL_DH_free(key->dh);
  7965. key->dh = NULL;
  7966. }
  7967. break;
  7968. #endif /* ! NO_DH ... */
  7969. #ifdef HAVE_HKDF
  7970. case EVP_PKEY_HKDF:
  7971. if (key->hkdfSalt != NULL) {
  7972. XFREE(key->hkdfSalt, NULL, DYNAMIC_TYPE_SALT);
  7973. key->hkdfSalt = NULL;
  7974. }
  7975. if (key->hkdfKey != NULL) {
  7976. XFREE(key->hkdfKey, NULL, DYNAMIC_TYPE_KEY);
  7977. key->hkdfKey = NULL;
  7978. }
  7979. if (key->hkdfInfo != NULL) {
  7980. XFREE(key->hkdfInfo, NULL, DYNAMIC_TYPE_INFO);
  7981. key->hkdfInfo = NULL;
  7982. }
  7983. key->hkdfSaltSz = 0;
  7984. key->hkdfKeySz = 0;
  7985. key->hkdfInfoSz = 0;
  7986. break;
  7987. #endif /* HAVE_HKDF */
  7988. default:
  7989. break;
  7990. }
  7991. #ifndef SINGLE_THREADED
  7992. if (wc_FreeMutex(&key->refMutex) != 0) {
  7993. WOLFSSL_MSG("Couldn't free pkey mutex");
  7994. }
  7995. #endif
  7996. XFREE(key, key->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7997. }
  7998. }
  7999. }
  8000. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  8001. /* Indent writes white spaces of the number specified by "indents"
  8002. * to the BIO. The number of white spaces is limited from 0 to
  8003. * EVP_PKEY_PRINT_INDENT_MAX.
  8004. * returns the amount written to BIO.
  8005. */
  8006. static int Indent(WOLFSSL_BIO* out, int indents)
  8007. {
  8008. int i;
  8009. char space = ' ';
  8010. if (out == NULL) {
  8011. return 0;
  8012. }
  8013. if (indents > EVP_PKEY_PRINT_INDENT_MAX) {
  8014. indents = EVP_PKEY_PRINT_INDENT_MAX;
  8015. }
  8016. for (i = 0; i < indents; i++) {
  8017. if (wolfSSL_BIO_write(out, &space, 1) < 0) {
  8018. break;
  8019. }
  8020. }
  8021. return indents -i;
  8022. }
  8023. /* PrintHexWithColon dump byte-data specified by "input" to the "out".
  8024. * Each line has leading white spaces( "indent" gives the number ) plus
  8025. * four spaces, then hex coded 15 byte data with separator ":" follow.
  8026. * Each line looks like:
  8027. * " 00:e6:ab: --- 9f:ef:"
  8028. * Parameters:
  8029. * out bio to output dump data
  8030. * input buffer holding data to dump
  8031. * inlen input data size
  8032. * indent the number of spaces for indent
  8033. * Returns 1 on success, 0 on failure.
  8034. */
  8035. static int PrintHexWithColon(WOLFSSL_BIO* out, const byte* input,
  8036. int inlen, int indent)
  8037. {
  8038. #ifdef WOLFSSL_SMALL_STACK
  8039. byte* buff = NULL;
  8040. #else
  8041. byte buff[EVP_PKEY_PRINT_LINE_WIDTH_MAX] = { 0 };
  8042. #endif /* WOLFSSL_SMALL_STACK */
  8043. int ret = WOLFSSL_SUCCESS;
  8044. word32 in = 0;
  8045. word32 i;
  8046. int idx;
  8047. const byte* data;
  8048. word32 outSz;
  8049. byte outHex[3];
  8050. if (!out || !input || inlen <= 0) {
  8051. return WOLFSSL_FAILURE;
  8052. }
  8053. if (indent < 0) {
  8054. indent = 0;
  8055. }
  8056. if (indent > EVP_PKEY_PRINT_INDENT_MAX) {
  8057. indent = EVP_PKEY_PRINT_INDENT_MAX;
  8058. }
  8059. data = input;
  8060. #ifdef WOLFSSL_SMALL_STACK
  8061. buff = (byte*)XMALLOC(EVP_PKEY_PRINT_LINE_WIDTH_MAX, NULL,
  8062. DYNAMIC_TYPE_TMP_BUFFER);
  8063. if (!buff) {
  8064. return WOLFSSL_FAILURE;
  8065. }
  8066. #endif
  8067. /* print pub element */
  8068. idx = 0;
  8069. for (in = 0; in < (word32)inlen && ret == WOLFSSL_SUCCESS; in +=
  8070. EVP_PKEY_PRINT_DIGITS_PER_LINE ) {
  8071. Indent(out, indent);
  8072. for (i = 0; (i < EVP_PKEY_PRINT_DIGITS_PER_LINE) &&
  8073. (in + i < (word32)inlen); i++) {
  8074. if (ret == WOLFSSL_SUCCESS) {
  8075. outSz = sizeof(outHex);
  8076. ret = Base16_Encode((const byte*)&data[in + i], 1,
  8077. outHex, &outSz) == 0;
  8078. }
  8079. if (ret == WOLFSSL_SUCCESS) {
  8080. XMEMCPY(buff + idx, outHex, 2);
  8081. idx += 2;
  8082. if (in + i != (word32)inlen -1) {
  8083. XMEMSET(buff + idx, ':', 1);
  8084. idx += 1;
  8085. }
  8086. }
  8087. }
  8088. if (ret == WOLFSSL_SUCCESS) {
  8089. ret = wolfSSL_BIO_write(out, buff, idx) > 0;
  8090. }
  8091. if (ret == WOLFSSL_SUCCESS) {
  8092. ret = wolfSSL_BIO_write(out, "\n", 1) > 0;
  8093. }
  8094. if (ret == WOLFSSL_SUCCESS) {
  8095. XMEMSET(buff, 0, EVP_PKEY_PRINT_LINE_WIDTH_MAX);
  8096. idx = 0;
  8097. }
  8098. }
  8099. #ifdef WOLFSSL_SMALL_STACK
  8100. XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8101. #endif
  8102. return ret;
  8103. }
  8104. #if !defined(NO_RSA)
  8105. /* PrintPubKeyRSA is a helper function for wolfSSL_EVP_PKEY_print_public
  8106. * to parse a DER format RSA public key specified in the second parameter.
  8107. * Parameters:
  8108. * out bio to output dump data
  8109. * pkey buffer holding public key data
  8110. * pkeySz public key data size
  8111. * indent the number of spaces for indent
  8112. * bitlen bit size of the given key
  8113. * pctx context(not used)
  8114. * Returns 1 on success, 0 on failure.
  8115. */
  8116. static int PrintPubKeyRSA(WOLFSSL_BIO* out, const byte* pkey, int pkeySz,
  8117. int indent, int bitlen, ASN1_PCTX* pctx)
  8118. {
  8119. byte buff[8] = { 0 };
  8120. int res = WOLFSSL_FAILURE;
  8121. word32 inOutIdx = 0;
  8122. word32 nSz; /* size of modulus */
  8123. word32 eSz; /* size of public exponent */
  8124. const byte* n = NULL;
  8125. const byte* e = NULL; /* pointer to modulus/exponent */
  8126. int idx;
  8127. int wsz;
  8128. word32 i;
  8129. unsigned long exponent = 0;
  8130. #ifdef WOLFSSL_SMALL_STACK
  8131. mp_int* a = NULL;
  8132. #else
  8133. mp_int a[1];
  8134. #endif
  8135. char line[32] = { 0 };
  8136. (void)pctx;
  8137. #ifdef WOLFSSL_SMALL_STACK
  8138. a = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  8139. if (a == NULL) {
  8140. return WOLFSSL_FAILURE;
  8141. }
  8142. #endif
  8143. if( mp_init(a) != 0) {
  8144. #ifdef WOLFSSL_SMALL_STACK
  8145. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8146. #endif
  8147. return WOLFSSL_FAILURE;
  8148. }
  8149. if (indent < 0) {
  8150. indent = 0;
  8151. }
  8152. if (indent > EVP_PKEY_PRINT_INDENT_MAX) {
  8153. indent = EVP_PKEY_PRINT_INDENT_MAX;
  8154. }
  8155. do {
  8156. /* parse key to get modulus and exponent */
  8157. if (wc_RsaPublicKeyDecode_ex(pkey, &inOutIdx, pkeySz,
  8158. &n, &nSz, &e, &eSz) != 0) {
  8159. break;
  8160. }
  8161. /* print out public key elements */
  8162. idx = 0;
  8163. XMEMSET(buff, 0, sizeof(buff));
  8164. Indent(out, indent);
  8165. XSTRNCPY(line, "RSA Public-Key: (", sizeof(line));
  8166. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8167. break;
  8168. }
  8169. if (mp_set_int(a, bitlen) != 0) {
  8170. break;
  8171. }
  8172. if (mp_todecimal(a, (char*)buff) != 0) {
  8173. break;
  8174. }
  8175. wsz = (int)XSTRLEN((const char*)buff);
  8176. if (wolfSSL_BIO_write(out, buff + idx, wsz) <= 0) {
  8177. break;
  8178. }
  8179. XSTRNCPY(line, " bit)\n", sizeof(line));
  8180. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8181. break;
  8182. }
  8183. /* print Modulus */
  8184. Indent(out, indent);
  8185. XSTRNCPY(line, "Modulus:\n", sizeof(line));
  8186. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8187. break;
  8188. }
  8189. /* print modulus with leading zero if exists */
  8190. if (*n & 0x80 && *(n-1) == 0) {
  8191. n--;
  8192. nSz++;
  8193. }
  8194. if (PrintHexWithColon(out, n, nSz, indent + 4) != WOLFSSL_SUCCESS) {
  8195. break;
  8196. }
  8197. /* print public Exponent */
  8198. idx = 0;
  8199. Indent(out, indent);
  8200. XSTRNCPY(line, "Exponent: ", sizeof(line));
  8201. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8202. break;
  8203. }
  8204. for (i = 0; i < eSz; i++) {
  8205. exponent <<= 8;
  8206. exponent += e[i];
  8207. }
  8208. XMEMSET(buff, 0, sizeof(buff));
  8209. if (mp_set_int(a, exponent) != 0) {
  8210. break;
  8211. }
  8212. if (mp_todecimal(a, (char*)buff) != 0) {
  8213. break;
  8214. }
  8215. wsz = (int)XSTRLEN((const char*)buff);
  8216. if (wolfSSL_BIO_write(out, buff + idx, wsz) <= 0) {
  8217. break;
  8218. }
  8219. XSTRNCPY(line, " (0x", sizeof(line));
  8220. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8221. break;
  8222. }
  8223. XMEMSET(buff, 0, sizeof(buff));
  8224. if (mp_tohex(a, (char*)buff) != 0) {
  8225. break;
  8226. }
  8227. if (wolfSSL_BIO_write(out, buff, (int)XSTRLEN((char*)buff)) <= 0) {
  8228. break;
  8229. }
  8230. XSTRNCPY(line, ")\n", sizeof(line));
  8231. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8232. break;
  8233. }
  8234. res = WOLFSSL_SUCCESS;
  8235. } while (0);
  8236. mp_free(a);
  8237. #ifdef WOLFSSL_SMALL_STACK
  8238. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8239. #endif
  8240. return res;
  8241. }
  8242. #endif /* !NO_RSA */
  8243. #if defined(HAVE_ECC)
  8244. /* PrintPubKeyEC is a helper function for wolfSSL_EVP_PKEY_print_public
  8245. * to parse a DER format ECC public key specified in the second parameter.
  8246. * Parameters:
  8247. * out bio to output dump data
  8248. * pkey buffer holding public key data
  8249. * pkeySz public key data size
  8250. * indent the number of spaces for indent
  8251. * bitlen bit size of the given key
  8252. * pctx context(not used)
  8253. * Returns 1 on success, 0 on failure.
  8254. */
  8255. static int PrintPubKeyEC(WOLFSSL_BIO* out, const byte* pkey, int pkeySz,
  8256. int indent, int bitlen, ASN1_PCTX* pctx)
  8257. {
  8258. byte* pub = NULL;
  8259. word32 pubSz = 0;
  8260. byte buff[8] = { 0 };
  8261. int res = WOLFSSL_SUCCESS;
  8262. word32 inOutIdx = 0;
  8263. int curveId = 0;
  8264. const byte* curveOID = NULL;
  8265. word32 oidSz = 0;
  8266. const char* OIDName = NULL;
  8267. const char* nistCurveName = NULL;
  8268. int nid;
  8269. int idx = 0;
  8270. int wsz = 0;
  8271. #ifdef WOLFSSL_SMALL_STACK
  8272. mp_int* a = NULL;
  8273. ecc_key* key = NULL;
  8274. #else
  8275. mp_int a[1];
  8276. ecc_key key[1];
  8277. #endif
  8278. char line[32] = { 0 };
  8279. (void)pctx;
  8280. #ifdef WOLFSSL_SMALL_STACK
  8281. a = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  8282. if (a == NULL) {
  8283. WOLFSSL_MSG("Failed to allocate memory for mp_int");
  8284. return WOLFSSL_FAILURE;
  8285. }
  8286. XMEMSET(a, 0, sizeof(mp_int));
  8287. key = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL, DYNAMIC_TYPE_ECC);
  8288. if (key == NULL) {
  8289. WOLFSSL_MSG("Failed to allocate memory for ecc_key");
  8290. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8291. return WOLFSSL_FAILURE;
  8292. }
  8293. #endif
  8294. if (mp_init(a) != 0) {
  8295. #ifdef WOLFSSL_SMALL_STACK
  8296. XFREE(key, NULL, DYNAMIC_TYPE_ECC);
  8297. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8298. #endif
  8299. return WOLFSSL_FAILURE;
  8300. }
  8301. if (wc_ecc_init(key) != 0) {
  8302. /* Return early so we don't have to remember if init succeeded
  8303. * or not. */
  8304. mp_free(a);
  8305. #ifdef WOLFSSL_SMALL_STACK
  8306. XFREE(key, NULL, DYNAMIC_TYPE_ECC);
  8307. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8308. #endif
  8309. return WOLFSSL_FAILURE;
  8310. }
  8311. if (indent < 0) {
  8312. indent = 0;
  8313. }
  8314. else if (indent > EVP_PKEY_PRINT_INDENT_MAX) {
  8315. indent = EVP_PKEY_PRINT_INDENT_MAX;
  8316. }
  8317. if (res == WOLFSSL_SUCCESS) {
  8318. res = wc_EccPublicKeyDecode(pkey, &inOutIdx, key, pkeySz) == 0;
  8319. }
  8320. if (res == WOLFSSL_SUCCESS) {
  8321. curveId = wc_ecc_get_oid(key->dp->oidSum, &curveOID, &oidSz);
  8322. res = curveId > 0 && oidSz > 0;
  8323. }
  8324. /* get NIST curve name */
  8325. if (res == WOLFSSL_SUCCESS) {
  8326. nid = EccEnumToNID(curveId);
  8327. if (nid != -1) {
  8328. /* look up object name and nist curve name*/
  8329. OIDName = wolfSSL_OBJ_nid2sn(nid);
  8330. nistCurveName = wolfSSL_EC_curve_nid2nist(nid);
  8331. res = (nistCurveName != NULL) && (OIDName != NULL);
  8332. }
  8333. else {
  8334. res = WOLFSSL_FAILURE;
  8335. }
  8336. }
  8337. if (res == WOLFSSL_SUCCESS) {
  8338. pub = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  8339. if (pub != NULL) {
  8340. pubSz = ECC_BUFSIZE;
  8341. XMEMSET(pub, 0, ECC_BUFSIZE);
  8342. PRIVATE_KEY_UNLOCK();
  8343. res = wc_ecc_export_x963(key, pub, &pubSz) == 0;
  8344. PRIVATE_KEY_LOCK();
  8345. }
  8346. else {
  8347. res = WOLFSSL_FAILURE;
  8348. }
  8349. }
  8350. if (res == WOLFSSL_SUCCESS) {
  8351. idx = 0;
  8352. res = Indent(out, indent) >= 0;
  8353. }
  8354. if (res == WOLFSSL_SUCCESS) {
  8355. XSTRNCPY(line, "Public-Key: (", sizeof(line));
  8356. res = wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) > 0;
  8357. }
  8358. if (res == WOLFSSL_SUCCESS) {
  8359. res = mp_set_int(a, bitlen) == 0;
  8360. }
  8361. if (res == WOLFSSL_SUCCESS) {
  8362. res = mp_todecimal(a, (char*)buff) == 0;
  8363. }
  8364. if (res == WOLFSSL_SUCCESS) {
  8365. wsz = (int)XSTRLEN((const char*)buff);
  8366. }
  8367. if (res == WOLFSSL_SUCCESS) {
  8368. res = wolfSSL_BIO_write(out, buff + idx, wsz) >= 0;
  8369. }
  8370. if (res == WOLFSSL_SUCCESS) {
  8371. XSTRNCPY(line, " bit)\n", sizeof(line));
  8372. res = wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) > 0;
  8373. }
  8374. if (res == WOLFSSL_SUCCESS) {
  8375. res = Indent(out, indent) >= 0;
  8376. }
  8377. if (res == WOLFSSL_SUCCESS) {
  8378. /* print pub element */
  8379. XSTRNCPY(line, "pub:\n", sizeof(line));
  8380. res = wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) > 0;
  8381. }
  8382. if (res == WOLFSSL_SUCCESS) {
  8383. res = PrintHexWithColon(out, pub, pubSz, indent + 4);
  8384. }
  8385. if (res == WOLFSSL_SUCCESS) {
  8386. res = Indent(out, indent) >= 0;
  8387. }
  8388. if (res == WOLFSSL_SUCCESS) {
  8389. /* print OID in name */
  8390. XSTRNCPY(line, "ASN1 OID: ", sizeof(line));
  8391. res = wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) > 0;
  8392. }
  8393. if (res == WOLFSSL_SUCCESS) {
  8394. res = wolfSSL_BIO_write(out, OIDName, (int)XSTRLEN(OIDName)) > 0;
  8395. }
  8396. if (res == WOLFSSL_SUCCESS) {
  8397. res = wolfSSL_BIO_write(out, "\n", 1) > 0;
  8398. }
  8399. if (res == WOLFSSL_SUCCESS) {
  8400. res = Indent(out, indent) >= 0;
  8401. }
  8402. if (res == WOLFSSL_SUCCESS) {
  8403. /* print NIST curve name */
  8404. XSTRNCPY(line, "NIST CURVE: ", sizeof(line));
  8405. res = wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) > 0;
  8406. }
  8407. if (res == WOLFSSL_SUCCESS) {
  8408. res = wolfSSL_BIO_write(out, nistCurveName,
  8409. (int)XSTRLEN(nistCurveName)) > 0;
  8410. }
  8411. if (res == WOLFSSL_SUCCESS) {
  8412. res = wolfSSL_BIO_write(out, "\n", 1) > 0;
  8413. }
  8414. if (pub != NULL) {
  8415. XFREE(pub, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  8416. pub = NULL;
  8417. }
  8418. wc_ecc_free(key);
  8419. mp_free(a);
  8420. #ifdef WOLFSSL_SMALL_STACK
  8421. XFREE(key, NULL, DYNAMIC_TYPE_ECC);
  8422. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8423. #endif
  8424. return res;
  8425. }
  8426. #endif /* HAVE_ECC */
  8427. #if !defined(NO_DSA)
  8428. /* PrintPubKeyDSA is a helper function for wolfSSL_EVP_PKEY_print_public
  8429. * to parse a DER format DSA public key specified in the second parameter.
  8430. * Parameters:
  8431. * out bio to output dump data
  8432. * pkey buffer holding public key data
  8433. * pkeySz public key data size
  8434. * indent the number of spaces for indent
  8435. * bitlen bit size of the given key
  8436. * pctx context(not used)
  8437. * Returns 1 on success, 0 on failure.
  8438. */
  8439. static int PrintPubKeyDSA(WOLFSSL_BIO* out, const byte* pkey, int pkeySz,
  8440. int indent, int bitlen, ASN1_PCTX* pctx)
  8441. {
  8442. byte buff[8] = { 0 };
  8443. int length;
  8444. int res = WOLFSSL_FAILURE;
  8445. word32 inOutIdx = 0;
  8446. word32 oid;
  8447. byte tagFound;
  8448. byte *p = NULL, * q = NULL, * g = NULL, * y = NULL;
  8449. int pSz, qSz, gSz, ySz;
  8450. int idx;
  8451. int wsz;
  8452. #ifdef WOLFSSL_SMALL_STACK
  8453. mp_int* a = NULL;
  8454. #else
  8455. mp_int a[1];
  8456. #endif
  8457. char line[32] = { 0 };
  8458. #ifdef WOLFSSL_SMALL_STACK
  8459. a = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  8460. if (a == NULL) {
  8461. return WOLFSSL_FAILURE;
  8462. }
  8463. #endif
  8464. if( mp_init(a) != 0) {
  8465. #ifdef WOLFSSL_SMALL_STACK
  8466. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8467. #endif
  8468. return WOLFSSL_FAILURE;
  8469. }
  8470. inOutIdx = 0;
  8471. (void)pctx;
  8472. if (indent < 0) {
  8473. indent = 0;
  8474. }
  8475. if (indent > EVP_PKEY_PRINT_INDENT_MAX) {
  8476. indent = EVP_PKEY_PRINT_INDENT_MAX;
  8477. }
  8478. do {
  8479. if (GetSequence(pkey, &inOutIdx, &length, pkeySz) < 0) {
  8480. break;
  8481. }
  8482. if (GetSequence(pkey, &inOutIdx, &length, pkeySz) < 0) {
  8483. break;
  8484. }
  8485. if (GetObjectId(pkey, &inOutIdx, &oid, oidIgnoreType, pkeySz) != 0) {
  8486. break;
  8487. }
  8488. if (GetSequence(pkey, &inOutIdx, &length, pkeySz) < 0) {
  8489. break;
  8490. }
  8491. /* find P */
  8492. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8493. break;
  8494. }
  8495. if (tagFound != ASN_INTEGER) {
  8496. break;
  8497. }
  8498. if (GetLength(pkey, &inOutIdx, &length, pkeySz) <= 0) {
  8499. break;
  8500. }
  8501. p = (byte*)(pkey + inOutIdx);
  8502. pSz = length;
  8503. if (bitlen == 0) {
  8504. if (*p == 0) {
  8505. bitlen = (pSz - 1) * 8; /* remove leading zero */
  8506. }
  8507. else {
  8508. bitlen = pSz * 8;
  8509. }
  8510. }
  8511. inOutIdx += length;
  8512. /* find Q */
  8513. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8514. break;
  8515. }
  8516. if (tagFound != ASN_INTEGER) {
  8517. break;
  8518. }
  8519. if (GetLength(pkey, &inOutIdx, &length, pkeySz) <= 0) {
  8520. break;
  8521. }
  8522. q = (byte*)(pkey + inOutIdx);
  8523. qSz = length;
  8524. inOutIdx += length;
  8525. /* find G */
  8526. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8527. break;
  8528. }
  8529. if (tagFound != ASN_INTEGER) {
  8530. break;
  8531. }
  8532. if (GetLength(pkey, &inOutIdx, &length, pkeySz) <= 0) {
  8533. break;
  8534. }
  8535. g = (byte*)(pkey + inOutIdx);
  8536. gSz = length;
  8537. inOutIdx += length;
  8538. /* find Y */
  8539. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8540. break;
  8541. }
  8542. if (tagFound != ASN_BIT_STRING) {
  8543. break;
  8544. }
  8545. if (GetLength(pkey, &inOutIdx, &length, pkeySz) <= 0) {
  8546. break;
  8547. }
  8548. inOutIdx++; /* skip the first byte( unused byte number)*/
  8549. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8550. break;
  8551. }
  8552. if (tagFound != ASN_INTEGER) {
  8553. break;
  8554. }
  8555. if (GetLength(pkey, &inOutIdx, &length, pkeySz) <= 0) {
  8556. break;
  8557. }
  8558. y = (byte*)(pkey + inOutIdx);
  8559. ySz = length;
  8560. idx = 0;
  8561. XMEMSET(buff, 0, sizeof(buff));
  8562. Indent(out, indent);
  8563. XSTRNCPY(line, "DSA Public-Key: (", sizeof(line));
  8564. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8565. break;
  8566. }
  8567. if (mp_set_int(a, bitlen) != 0) {
  8568. break;
  8569. }
  8570. if (mp_todecimal(a, (char*)buff) != 0) {
  8571. break;
  8572. }
  8573. wsz = (int)XSTRLEN((const char*)buff);
  8574. if (wolfSSL_BIO_write(out, buff + idx, wsz) <= 0) {
  8575. break;
  8576. }
  8577. XSTRNCPY(line, " bit)\n", sizeof(line));
  8578. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8579. break;
  8580. }
  8581. /* print pub element */
  8582. Indent(out, indent);
  8583. XSTRNCPY(line, "pub:\n", sizeof(line));
  8584. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8585. break;
  8586. }
  8587. if (PrintHexWithColon(out, y, ySz, indent + 4) != WOLFSSL_SUCCESS) {
  8588. break;
  8589. }
  8590. /* print P element */
  8591. Indent(out, indent);
  8592. XSTRNCPY(line, "P:\n", sizeof(line));
  8593. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8594. break;
  8595. }
  8596. if (PrintHexWithColon(out, p, pSz, indent + 4) != WOLFSSL_SUCCESS) {
  8597. break;
  8598. }
  8599. /* print Q element */
  8600. Indent(out, indent);
  8601. XSTRNCPY(line, "Q:\n", sizeof(line));
  8602. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8603. break;
  8604. }
  8605. if (PrintHexWithColon(out, q, qSz, indent + 4) != WOLFSSL_SUCCESS) {
  8606. break;
  8607. }
  8608. /* print G element */
  8609. Indent(out, indent);
  8610. XSTRNCPY(line, "G:\n", sizeof(line));
  8611. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8612. break;
  8613. }
  8614. if (PrintHexWithColon(out, g, gSz, indent + 4) != WOLFSSL_SUCCESS) {
  8615. break;
  8616. }
  8617. res = WOLFSSL_SUCCESS;
  8618. } while (0);
  8619. mp_free(a);
  8620. #ifdef WOLFSSL_SMALL_STACK
  8621. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8622. #endif
  8623. return res;
  8624. }
  8625. #endif /* !NO_DSA */
  8626. #if defined(WOLFSSL_DH_EXTRA)
  8627. /* PrintPubKeyDH is a helper function for wolfSSL_EVP_PKEY_print_public
  8628. * to parse a DER format DH public key specified in the second parameter.
  8629. * Parameters:
  8630. * out bio to output dump data
  8631. * pkey buffer holding public key data
  8632. * pkeySz public key data size
  8633. * indent the number of spaces for indent
  8634. * bitlen bit size of the given key
  8635. * pctx context(not used)
  8636. * Returns 1 on success, 0 on failure.
  8637. */
  8638. static int PrintPubKeyDH(WOLFSSL_BIO* out, const byte* pkey, int pkeySz,
  8639. int indent, int bitlen, ASN1_PCTX* pctx)
  8640. {
  8641. byte buff[8] = { 0 };
  8642. int res = WOLFSSL_FAILURE;
  8643. word32 length;
  8644. word32 inOutIdx;
  8645. word32 oid;
  8646. byte tagFound;
  8647. byte* prime = NULL;
  8648. int primeSz;
  8649. byte generator;
  8650. byte* publicKey = NULL;
  8651. int publicKeySz;
  8652. int idx;
  8653. int wsz;
  8654. word32 outSz;
  8655. byte outHex[3];
  8656. #ifdef WOLFSSL_SMALL_STACK
  8657. mp_int* a = NULL;
  8658. #else
  8659. mp_int a[1];
  8660. #endif
  8661. char line[32] = { 0 };
  8662. #ifdef WOLFSSL_SMALL_STACK
  8663. a = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  8664. if (a == NULL) {
  8665. return WOLFSSL_FAILURE;
  8666. }
  8667. #endif
  8668. if( mp_init(a) != 0) {
  8669. #ifdef WOLFSSL_SMALL_STACK
  8670. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8671. #endif
  8672. return WOLFSSL_FAILURE;
  8673. }
  8674. inOutIdx = 0;
  8675. (void)pctx;
  8676. if (indent < 0) {
  8677. indent = 0;
  8678. }
  8679. if (indent > EVP_PKEY_PRINT_INDENT_MAX) {
  8680. indent = EVP_PKEY_PRINT_INDENT_MAX;
  8681. }
  8682. do {
  8683. if (GetSequence(pkey, &inOutIdx, (int*)&length, pkeySz) < 0) {
  8684. break;
  8685. }
  8686. if (GetSequence(pkey, &inOutIdx, (int*)&length, pkeySz) < 0) {
  8687. break;
  8688. }
  8689. if (GetObjectId(pkey, &inOutIdx, &oid, oidIgnoreType, pkeySz) < 0) {
  8690. break;
  8691. }
  8692. if (GetSequence(pkey, &inOutIdx, (int*)&length, pkeySz) < 0) {
  8693. break;
  8694. }
  8695. /* get prime element */
  8696. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8697. break;
  8698. }
  8699. if (tagFound != ASN_INTEGER) {
  8700. break;
  8701. }
  8702. if (GetLength(pkey, &inOutIdx, (int*)&length, pkeySz) <= 0) {
  8703. break;
  8704. }
  8705. prime = (byte*)(pkey + inOutIdx);
  8706. primeSz = length;
  8707. inOutIdx += length;
  8708. /* get generator element */
  8709. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8710. break;
  8711. }
  8712. if (tagFound != ASN_INTEGER) {
  8713. break;
  8714. }
  8715. if (GetLength(pkey, &inOutIdx, (int*)&length, pkeySz) <= 0) {
  8716. break;
  8717. }
  8718. if (length != 1) {
  8719. break;
  8720. }
  8721. generator = *(pkey + inOutIdx);
  8722. inOutIdx += length;
  8723. /* get public-key element */
  8724. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8725. break;
  8726. }
  8727. if (tagFound != ASN_BIT_STRING) {
  8728. break;
  8729. }
  8730. if (GetLength(pkey, &inOutIdx, (int*)&length, pkeySz) <= 0) {
  8731. break;
  8732. }
  8733. inOutIdx ++;
  8734. if (GetASNTag(pkey, &inOutIdx, &tagFound, pkeySz) != 0) {
  8735. break;
  8736. }
  8737. if (tagFound != ASN_INTEGER) {
  8738. break;
  8739. }
  8740. if (GetLength(pkey, &inOutIdx, (int*)&length, pkeySz) <= 0) {
  8741. break;
  8742. }
  8743. publicKeySz = length;
  8744. publicKey = (byte*)(pkey + inOutIdx);
  8745. if (bitlen == 0) {
  8746. if (*publicKey == 0) {
  8747. bitlen = (publicKeySz - 1) * 8;
  8748. }
  8749. else {
  8750. bitlen = publicKeySz * 8;
  8751. }
  8752. }
  8753. /* print elements */
  8754. idx = 0;
  8755. Indent(out, indent);
  8756. XSTRNCPY(line, "DH Public-Key: (", sizeof(line));
  8757. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8758. break;
  8759. }
  8760. if (mp_set_int(a, bitlen) != 0) {
  8761. break;
  8762. }
  8763. if (mp_todecimal(a, (char*)buff) != 0) {
  8764. break;
  8765. }
  8766. wsz = (int)XSTRLEN((const char*)buff);
  8767. if (wolfSSL_BIO_write(out, buff + idx, wsz) <= 0) {
  8768. break;
  8769. }
  8770. XSTRNCPY(line, " bit)\n", sizeof(line));
  8771. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8772. break;
  8773. }
  8774. Indent(out, indent);
  8775. XSTRNCPY(line, "public-key:\n", sizeof(line));
  8776. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8777. break;
  8778. }
  8779. if (PrintHexWithColon(out, publicKey, publicKeySz, indent + 4)
  8780. != WOLFSSL_SUCCESS) {
  8781. break;
  8782. }
  8783. Indent(out, indent);
  8784. XSTRNCPY(line, "prime:\n", sizeof(line));
  8785. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8786. break;
  8787. }
  8788. if (PrintHexWithColon(out, prime, primeSz, indent + 4)
  8789. != WOLFSSL_SUCCESS) {
  8790. break;
  8791. }
  8792. idx = 0;
  8793. XMEMSET(buff, 0, sizeof(buff));
  8794. Indent(out, indent);
  8795. XSTRNCPY(line, "generator: ", sizeof(line));
  8796. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8797. break;
  8798. }
  8799. if (mp_set_int(a, generator) != 0) {
  8800. break;
  8801. }
  8802. if (mp_todecimal(a, (char*)buff) != 0) {
  8803. break;
  8804. }
  8805. wsz = (int)XSTRLEN((const char*)buff);
  8806. if (wolfSSL_BIO_write(out, buff + idx, wsz) <= 0) {
  8807. break;
  8808. }
  8809. XSTRNCPY(line, " (0x", sizeof(line));
  8810. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8811. break;
  8812. }
  8813. idx = 0;
  8814. XMEMSET(buff, 0, sizeof(buff));
  8815. outSz = sizeof(outHex);
  8816. if (Base16_Encode((const byte*)&generator, 1, outHex, &outSz ) != 0) {
  8817. break;
  8818. }
  8819. if (idx + 2 < (int)sizeof(buff) ) {
  8820. XMEMCPY(buff + idx, outHex, 2);
  8821. idx += 2;
  8822. }
  8823. if (wolfSSL_BIO_write(out, buff, idx) <= 0 ) {
  8824. break;
  8825. }
  8826. XSTRNCPY(line, ")\n", sizeof(line));
  8827. if (wolfSSL_BIO_write(out, line, (int)XSTRLEN(line)) <= 0) {
  8828. break;
  8829. }
  8830. res = WOLFSSL_SUCCESS;
  8831. } while (0);
  8832. mp_free(a);
  8833. #ifdef WOLFSSL_SMALL_STACK
  8834. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  8835. #endif
  8836. return res;
  8837. }
  8838. #endif /* WOLFSSL_DH_EXTRA */
  8839. /* wolfSSL_EVP_PKEY_print_public parses the specified key then
  8840. * outputs public key info in human readable format to the specified BIO.
  8841. * White spaces of the same number which 'indent" gives, will be added to
  8842. * each line to output and ignores pctx parameter.
  8843. * Parameters:
  8844. * out bio to output dump data
  8845. * pkey buffer holding public key data
  8846. * indent the number of spaces for indent
  8847. * pctx context(not used)
  8848. * Returns 1 on success, 0 or negative on error, -2 means specified key
  8849. * algo is not supported.
  8850. * Can handle RSA, ECC, DSA and DH public keys.
  8851. */
  8852. int wolfSSL_EVP_PKEY_print_public(WOLFSSL_BIO* out,
  8853. const WOLFSSL_EVP_PKEY* pkey, int indent, ASN1_PCTX* pctx)
  8854. {
  8855. int res;
  8856. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  8857. defined(WOLFSSL_DH_EXTRA)
  8858. int keybits; /* bit length of the key */
  8859. #endif
  8860. WOLFSSL_ENTER("wolfSSL_EVP_PKEY_print_public");
  8861. if (pkey == NULL || out == NULL) {
  8862. return 0;
  8863. }
  8864. #if !defined(NO_RSA) || defined(HAVE_ECC) || !defined(NO_DSA) || \
  8865. defined(WOLFSSL_DH_EXTRA)
  8866. if (indent < 0) {
  8867. indent = 0;
  8868. }
  8869. if (indent > EVP_PKEY_PRINT_INDENT_MAX) {
  8870. indent = EVP_PKEY_PRINT_INDENT_MAX;
  8871. }
  8872. #endif
  8873. switch (pkey->type) {
  8874. case EVP_PKEY_RSA:
  8875. #if !defined(NO_RSA)
  8876. keybits = wolfSSL_EVP_PKEY_size((WOLFSSL_EVP_PKEY*)pkey) * 8;
  8877. res = PrintPubKeyRSA(
  8878. out,
  8879. (byte*)(pkey->pkey.ptr), /* buffer for pkey raw data */
  8880. pkey->pkey_sz, /* raw pkey size */
  8881. indent, /* indent size */
  8882. keybits, /* bit length of the key */
  8883. pctx); /* not used */
  8884. #else
  8885. res = WOLFSSL_UNKNOWN; /* not supported algo */
  8886. #endif
  8887. break;
  8888. case EVP_PKEY_EC:
  8889. #if defined(HAVE_ECC)
  8890. keybits = wolfSSL_EVP_PKEY_size((WOLFSSL_EVP_PKEY*)pkey) * 8;
  8891. res = PrintPubKeyEC(
  8892. out,
  8893. (byte*)(pkey->pkey.ptr), /* buffer for pkey raw data */
  8894. pkey->pkey_sz, /* raw pkey size */
  8895. indent, /* indent size */
  8896. keybits, /* bit length of the key */
  8897. pctx); /* not used */
  8898. #else
  8899. res = WOLFSSL_UNKNOWN; /* not supported algo */
  8900. #endif
  8901. break;
  8902. case EVP_PKEY_DSA:
  8903. #if !defined(NO_DSA)
  8904. keybits = wolfSSL_EVP_PKEY_size((WOLFSSL_EVP_PKEY*)pkey) * 8;
  8905. res = PrintPubKeyDSA(
  8906. out,
  8907. (byte*)(pkey->pkey.ptr), /* buffer for pkey raw data */
  8908. pkey->pkey_sz, /* raw pkey size */
  8909. indent, /* indent size */
  8910. keybits, /* bit length of the key */
  8911. pctx); /* not used */
  8912. #else
  8913. res = WOLFSSL_UNKNOWN; /* not supported algo */
  8914. #endif
  8915. break;
  8916. case EVP_PKEY_DH:
  8917. #if defined(WOLFSSL_DH_EXTRA)
  8918. keybits = wolfSSL_EVP_PKEY_size((WOLFSSL_EVP_PKEY*)pkey) * 8;
  8919. res = PrintPubKeyDH(
  8920. out,
  8921. (byte*)(pkey->pkey.ptr), /* buffer for pkey raw data */
  8922. pkey->pkey_sz, /* raw pkey size */
  8923. indent, /* indent size */
  8924. keybits, /* bit length of the key */
  8925. pctx); /* not used */
  8926. #else
  8927. res = WOLFSSL_UNKNOWN; /* not supported algo */
  8928. #endif
  8929. break;
  8930. default:
  8931. res = WOLFSSL_UNKNOWN; /* not supported algo */
  8932. break;
  8933. }
  8934. return res;
  8935. }
  8936. #endif /* OPENSSL_EXTRA && !NO_BIO */
  8937. int wolfSSL_EVP_get_hashinfo(const WOLFSSL_EVP_MD* evp,
  8938. int* pHash, int* pHashSz)
  8939. {
  8940. enum wc_HashType hash = WC_HASH_TYPE_NONE;
  8941. int hashSz;
  8942. if (XSTRLEN(evp) < 3) {
  8943. /* do not try comparing strings if size is too small */
  8944. return WOLFSSL_FAILURE;
  8945. }
  8946. #ifndef NO_SHA
  8947. if ((XSTRCMP("SHA", evp) == 0) || (XSTRCMP("SHA1", evp) == 0)) {
  8948. hash = WC_HASH_TYPE_SHA;
  8949. } else
  8950. #endif
  8951. #ifdef WOLFSSL_SHA224
  8952. if (XSTRCMP("SHA224", evp) == 0) {
  8953. hash = WC_HASH_TYPE_SHA224;
  8954. } else
  8955. #endif
  8956. #ifndef NO_SHA256
  8957. if (XSTRCMP("SHA256", evp) == 0) {
  8958. hash = WC_HASH_TYPE_SHA256;
  8959. } else
  8960. #endif
  8961. #ifdef WOLFSSL_SHA384
  8962. if (XSTRCMP("SHA384", evp) == 0) {
  8963. hash = WC_HASH_TYPE_SHA384;
  8964. } else
  8965. #endif
  8966. #ifdef WOLFSSL_SHA512
  8967. if (XSTRCMP("SHA512", evp) == 0) {
  8968. hash = WC_HASH_TYPE_SHA512;
  8969. } else
  8970. #ifndef WOLFSSL_NOSHA512_224
  8971. if (XSTRCMP("SHA512_224", evp) == 0) {
  8972. hash = WC_HASH_TYPE_SHA512_224;
  8973. } else
  8974. #endif
  8975. #ifndef WOLFSSL_NOSHA512_256
  8976. if (XSTRCMP("SHA512_256", evp) == 0) {
  8977. hash = WC_HASH_TYPE_SHA512_256;
  8978. } else
  8979. #endif
  8980. #endif
  8981. #ifdef WOLFSSL_SHA3
  8982. #ifndef WOLFSSL_NOSHA3_224
  8983. if (XSTRCMP("SHA3_224", evp) == 0) {
  8984. hash = WC_HASH_TYPE_SHA3_224;
  8985. } else
  8986. #endif
  8987. #ifndef WOLFSSL_NOSHA3_256
  8988. if (XSTRCMP("SHA3_256", evp) == 0) {
  8989. hash = WC_HASH_TYPE_SHA3_256;
  8990. } else
  8991. #endif
  8992. #ifndef WOLFSSL_NOSHA3_384
  8993. if (XSTRCMP("SHA3_384", evp) == 0) {
  8994. hash = WC_HASH_TYPE_SHA3_384;
  8995. } else
  8996. #endif
  8997. #ifndef WOLFSSL_NOSHA3_512
  8998. if (XSTRCMP("SHA3_512", evp) == 0) {
  8999. hash = WC_HASH_TYPE_SHA3_512;
  9000. } else
  9001. #endif
  9002. #endif /* WOLFSSL_SHA3 */
  9003. #ifdef WOLFSSL_MD2
  9004. if (XSTRCMP("MD2", evp) == 0) {
  9005. hash = WC_HASH_TYPE_MD2;
  9006. } else
  9007. #endif
  9008. #ifndef NO_MD4
  9009. if (XSTRCMP("MD4", evp) == 0) {
  9010. hash = WC_HASH_TYPE_MD4;
  9011. } else
  9012. #endif
  9013. #ifndef NO_MD5
  9014. if (XSTRCMP("MD5", evp) == 0) {
  9015. hash = WC_HASH_TYPE_MD5;
  9016. } else
  9017. #endif
  9018. {
  9019. if (XSTRNCMP("SHA", evp, 3) == 0) {
  9020. WOLFSSL_MSG("Unknown SHA hash");
  9021. }
  9022. return WOLFSSL_FAILURE;
  9023. }
  9024. if (pHash)
  9025. *pHash = hash;
  9026. hashSz = wc_HashGetDigestSize(hash);
  9027. if (pHashSz)
  9028. *pHashSz = hashSz;
  9029. if (hashSz < 0) {
  9030. return WOLFSSL_FAILURE;
  9031. }
  9032. return WOLFSSL_SUCCESS;
  9033. }
  9034. /* Base64 encoding APIs */
  9035. #if defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE)
  9036. /* wolfSSL_EVP_ENCODE_CTX_new allocates WOLFSSL_EVP_ENCODE_CTX
  9037. * Returns WOLFSSL_EVP_ENCODE_CTX structure on success, NULL on failure.
  9038. */
  9039. struct WOLFSSL_EVP_ENCODE_CTX* wolfSSL_EVP_ENCODE_CTX_new(void)
  9040. {
  9041. WOLFSSL_EVP_ENCODE_CTX* ctx = NULL;
  9042. WOLFSSL_ENTER("wolfSSL_EVP_ENCODE_CTX_new");
  9043. ctx = (WOLFSSL_EVP_ENCODE_CTX*)XMALLOC( sizeof(WOLFSSL_EVP_ENCODE_CTX),
  9044. NULL, DYNAMIC_TYPE_OPENSSL );
  9045. if (ctx != NULL) {
  9046. XMEMSET(ctx, 0, sizeof(WOLFSSL_EVP_ENCODE_CTX) );
  9047. ctx->heap = NULL;
  9048. return ctx;
  9049. }
  9050. return NULL;
  9051. }
  9052. /* wolfSSL_EVP_ENCODE_CTX_free frees specified WOLFSSL_EVP_ENCODE_CTX struct.
  9053. */
  9054. void wolfSSL_EVP_ENCODE_CTX_free(WOLFSSL_EVP_ENCODE_CTX* ctx)
  9055. {
  9056. WOLFSSL_ENTER("wolfSSL_EVP_ENCODE_CTX_free");
  9057. if (ctx != NULL) {
  9058. XFREE(ctx, ctx->heap, DYNAMIC_TYPE_OPENSSL);
  9059. }
  9060. }
  9061. #endif /* WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE */
  9062. #if defined(WOLFSSL_BASE64_ENCODE)
  9063. /* Assume that out has enough space */
  9064. int wolfSSL_EVP_EncodeBlock(unsigned char *out, const unsigned char *in,
  9065. int inLen)
  9066. {
  9067. word32 ret = (word32)-1;
  9068. WOLFSSL_ENTER("wolfSSL_EVP_EncodeBlock");
  9069. if (out == NULL || in == NULL)
  9070. return WOLFSSL_FATAL_ERROR;
  9071. if (Base64_Encode(in, inLen, out, &ret) == 0)
  9072. return (int)ret;
  9073. else
  9074. return WOLFSSL_FATAL_ERROR;
  9075. }
  9076. /* Assume that out has enough space */
  9077. int wolfSSL_EVP_DecodeBlock(unsigned char *out, const unsigned char *in,
  9078. int inLen)
  9079. {
  9080. word32 ret = (word32)-1;
  9081. WOLFSSL_ENTER("wolfSSL_EVP_DecodeBlock");
  9082. if (out == NULL || in == NULL)
  9083. return WOLFSSL_FATAL_ERROR;
  9084. if (Base64_Decode(in, inLen, out, &ret) == 0)
  9085. return (int)ret;
  9086. else
  9087. return WOLFSSL_FATAL_ERROR;
  9088. }
  9089. /* wolfSSL_EVP_EncodeInit initializes specified WOLFSSL_EVP_ENCODE_CTX object
  9090. * for the subsequent wolfSSL_EVP_EncodeUpdate.
  9091. */
  9092. void wolfSSL_EVP_EncodeInit(WOLFSSL_EVP_ENCODE_CTX* ctx)
  9093. {
  9094. WOLFSSL_ENTER("wolfSSL_EVP_EncodeInit");
  9095. /* clean up ctx */
  9096. if (ctx != NULL) {
  9097. ctx->remaining = 0;
  9098. XMEMSET(ctx->data, 0, sizeof(ctx->data));
  9099. }
  9100. }
  9101. /* wolfSSL_EVP_EncodeUpdate encodes the input data in 48-byte units
  9102. * and outputs it to out. If less than 48 bytes of data remain, save it in
  9103. * ctx. The data given in the subsequent wolfSSL_EVP_EncodeUpdate
  9104. * is combined with the data stored in CTX and used for encoding.
  9105. * Returns 1 on success, 0 on error.
  9106. */
  9107. int wolfSSL_EVP_EncodeUpdate(WOLFSSL_EVP_ENCODE_CTX* ctx,
  9108. unsigned char* out, int* outl, const unsigned char* in, int inl)
  9109. {
  9110. int cpysz;
  9111. int res;
  9112. word32 outsz = 0;
  9113. WOLFSSL_ENTER("wolfSSL_EVP_EncodeUpdate");
  9114. if (ctx == NULL || out == NULL || in == NULL || outl == NULL)
  9115. return 0;
  9116. *outl = 0;
  9117. /* if the remaining data exists in the ctx, add input data to them
  9118. * to create a block(48bytes) for encoding
  9119. */
  9120. if (ctx->remaining > 0 && inl > 0) {
  9121. cpysz = min((BASE64_ENCODE_BLOCK_SIZE - ctx->remaining), inl);
  9122. XMEMCPY(ctx->data + ctx->remaining, in, cpysz);
  9123. ctx->remaining += cpysz;
  9124. in += cpysz;
  9125. inl -= cpysz;
  9126. /* check if a block for encoding exists in ctx.data, if so encode it */
  9127. if (ctx->remaining >= BASE64_ENCODE_BLOCK_SIZE) {
  9128. /* Base64_Encode asks the out buff size via the 4th param*/
  9129. outsz = BASE64_ENCODE_RESULT_BLOCK_SIZE + 1;
  9130. res = Base64_Encode(ctx->data, BASE64_ENCODE_BLOCK_SIZE, out,
  9131. &outsz);
  9132. if (res == 0) {
  9133. ctx->remaining = 0;
  9134. *outl = outsz;
  9135. }
  9136. else
  9137. return 0; /* return with error */
  9138. }
  9139. else {
  9140. /* could not create a block */
  9141. *outl = 0;
  9142. return 1;
  9143. }
  9144. }
  9145. /* Here, there is no data left in ctx, so try processing the data of
  9146. * the specified input data.
  9147. */
  9148. while (inl >= BASE64_ENCODE_BLOCK_SIZE) {
  9149. outsz = BASE64_ENCODE_RESULT_BLOCK_SIZE + 1;/* 64 byte and one for LF*/
  9150. res = Base64_Encode(in, BASE64_ENCODE_BLOCK_SIZE,out,&outsz);
  9151. if (res == 0) {
  9152. in += BASE64_ENCODE_BLOCK_SIZE;
  9153. inl -= BASE64_ENCODE_BLOCK_SIZE;
  9154. out += outsz;
  9155. *outl += outsz;
  9156. }
  9157. else {
  9158. *outl = 0;
  9159. return 0;
  9160. }
  9161. }
  9162. /* if remaining data exists, copy them into ctx for the next call*/
  9163. if (inl > 0) {
  9164. XMEMSET(ctx->data, 0, sizeof(ctx->data));
  9165. XMEMCPY(ctx->data, in, inl);
  9166. ctx->remaining = inl;
  9167. }
  9168. return 1; /* returns 1 on success, 0 on error */
  9169. }
  9170. /* wolfSSL_EVP_EncodeFinal encodes data in ctx and outputs to out.
  9171. */
  9172. void wolfSSL_EVP_EncodeFinal(WOLFSSL_EVP_ENCODE_CTX* ctx,
  9173. unsigned char* out, int* outl)
  9174. {
  9175. word32 outsz = 0;
  9176. int res;
  9177. WOLFSSL_ENTER("wolfSSL_EVP_EncodeFinal");
  9178. if (outl == NULL)
  9179. return;
  9180. if (ctx == NULL || out == NULL) {
  9181. *outl = 0;
  9182. return;
  9183. }
  9184. if (ctx->remaining >= BASE64_ENCODE_RESULT_BLOCK_SIZE) {
  9185. *outl = 0;
  9186. return;
  9187. }
  9188. /* process remaining data in ctx */
  9189. outsz = BASE64_ENCODE_RESULT_BLOCK_SIZE + 1; /* 64 byte and one for LF*/
  9190. res = Base64_Encode(ctx->data, ctx->remaining ,out, &outsz);
  9191. if (res == 0)
  9192. *outl = outsz;
  9193. else
  9194. *outl = 0;
  9195. ctx->remaining = 0;
  9196. XMEMSET(ctx->data, 0, sizeof(ctx->data));
  9197. return;
  9198. }
  9199. #endif /* WOLFSSL_BASE64_ENCODE */
  9200. #if defined(WOLFSSL_BASE64_DECODE)
  9201. /* wolfSSL_EVP_DecodeInit initializes specified WOLFSSL_EVP_ENCODE_CTX struct
  9202. * for subsequent wolfSSL_EVP_DecodeUpdate.
  9203. */
  9204. void wolfSSL_EVP_DecodeInit(WOLFSSL_EVP_ENCODE_CTX* ctx)
  9205. {
  9206. WOLFSSL_ENTER("wolfSSL_EVP_DecodeInit");
  9207. /* clean up ctx */
  9208. if (ctx != NULL) {
  9209. ctx->remaining = 0;
  9210. XMEMSET(ctx->data, 0, sizeof(ctx->data));
  9211. }
  9212. }
  9213. /* wolfSSL_EVP_DecodeUpdate encodes the input data in 4-byte units
  9214. * and outputs it to out. If less than 4 bytes of data remain, save it in
  9215. * ctx. The data given in the subsequent wolfSSL_EVP_DecodeUpdate
  9216. * is combined with the data stored in CTX and used for decoding.
  9217. * Returns 1 or 0 on success, -1 on error. Return value 0 indicates that
  9218. * clients should call wolfSSL_EVP_DecodeFinal as next call.
  9219. */
  9220. int wolfSSL_EVP_DecodeUpdate(WOLFSSL_EVP_ENCODE_CTX* ctx,
  9221. unsigned char* out, int* outl, const unsigned char* in, int inl)
  9222. {
  9223. word32 outsz = 0;
  9224. word32 j = 0;
  9225. word32 inLen;
  9226. int res;
  9227. int pad = 0;
  9228. int i;
  9229. int cpySz;
  9230. unsigned char c;
  9231. int pad3 = 0;
  9232. int pad4 = 0;
  9233. byte e[4];
  9234. WOLFSSL_ENTER("wolfSSL_EVP_DecodeUpdate");
  9235. if (outl == NULL)
  9236. return -1;
  9237. if (ctx == NULL || out == NULL || in == NULL) {
  9238. *outl = 0;
  9239. return -1;
  9240. }
  9241. if (inl == 0) {
  9242. *outl = 0;
  9243. return 1;
  9244. }
  9245. inLen = inl;
  9246. *outl = 0;
  9247. /* if the remaining data exist in the ctx, add input data to them to create
  9248. a block(4bytes) for decoding*/
  9249. if ( ctx->remaining > 0 && inl > 0) {
  9250. cpySz = min((BASE64_DECODE_BLOCK_SIZE - ctx->remaining), inl);
  9251. for ( i = 0; cpySz > 0 && inLen > 0; i++) {
  9252. if (Base64_SkipNewline(in, &inLen, &j) == ASN_INPUT_E) {
  9253. return -1; /* detected an illegal char in input */
  9254. }
  9255. c = in[j++];
  9256. if (c == '=')
  9257. pad = 1;
  9258. *(ctx->data + ctx->remaining + i) = c;
  9259. inLen--;
  9260. cpySz--;
  9261. }
  9262. outsz = sizeof(ctx->data);
  9263. res = Base64_Decode( ctx->data, BASE64_DECODE_BLOCK_SIZE, out, &outsz);
  9264. if (res == 0) {
  9265. *outl += outsz;
  9266. out += outsz;
  9267. ctx->remaining = 0;
  9268. XMEMSET(ctx->data, 0, sizeof(ctx->data));
  9269. }
  9270. else {
  9271. *outl = 0;
  9272. return -1; /* return with error */
  9273. }
  9274. }
  9275. /* Base64_Decode is not a streaming process, so it processes
  9276. * the input data and exits. If a line break or whitespace
  9277. * character is found in the input data, it will be skipped,
  9278. * but if the end point of the input data is reached as a result,
  9279. * Base64_Decode will stop processing there. The data cleansing is
  9280. * required before Base64_Decode so that the processing does not
  9281. * stop within 4 bytes, which is the unit of Base64 decoding processing.
  9282. * The logic that exists before calling Base64_Decode in a While Loop is
  9283. * a data cleansing process that removes line breaks and whitespace.
  9284. */
  9285. while (inLen > 3) {
  9286. if ((res = Base64_SkipNewline(in, &inLen, &j)) != 0) {
  9287. if (res == BUFFER_E) {
  9288. break;
  9289. }
  9290. else {
  9291. *outl = 0;
  9292. return -1;
  9293. }
  9294. }
  9295. e[0] = in[j++];
  9296. if (e[0] == '\0') {
  9297. break;
  9298. }
  9299. inLen--;
  9300. if ((res = Base64_SkipNewline(in, &inLen, &j)) != 0) {
  9301. if (res == BUFFER_E) {
  9302. break;
  9303. }
  9304. else {
  9305. *outl = 0;
  9306. return -1;
  9307. }
  9308. }
  9309. e[1] = in[j++];
  9310. inLen--;
  9311. if ((res = Base64_SkipNewline(in, &inLen, &j)) != 0) {
  9312. if (res == BUFFER_E) {
  9313. break;
  9314. }
  9315. else {
  9316. *outl = 0;
  9317. return -1;
  9318. }
  9319. }
  9320. e[2] = in[j++];
  9321. inLen--;
  9322. if ((res = Base64_SkipNewline(in, &inLen, &j)) != 0) {
  9323. if (res == BUFFER_E) {
  9324. break;
  9325. }
  9326. else {
  9327. *outl = 0;
  9328. return -1;
  9329. }
  9330. }
  9331. e[3] = in[j++];
  9332. inLen--;
  9333. if (e[0] == '=')
  9334. pad = 1;
  9335. if (e[1] == '=')
  9336. pad = 1;
  9337. if (e[2] == '=') {
  9338. pad = 1;
  9339. pad3 = 1;
  9340. }
  9341. if (e[3] == '=') {
  9342. pad = 1;
  9343. pad4 = 1;
  9344. }
  9345. if (pad3 && !pad4) {
  9346. *outl = 0;
  9347. return -1;
  9348. }
  9349. /* decode four bytes */
  9350. outsz = sizeof(ctx->data);
  9351. res = Base64_Decode( e, BASE64_DECODE_BLOCK_SIZE, out, &outsz);
  9352. if (res < 0) {
  9353. *outl = 0;
  9354. return -1;
  9355. }
  9356. *outl += outsz;
  9357. out += outsz;
  9358. }
  9359. /* copy left data to ctx */
  9360. if (inLen > 0) {
  9361. XMEMSET(ctx->data, 0, sizeof(ctx->data));
  9362. i = 0;
  9363. while (inLen > 0) {
  9364. c = in[j++];
  9365. if (c== '\n' || c == '\r' || c == ' ') {
  9366. inLen--;
  9367. continue;
  9368. }
  9369. if (c == '=') {
  9370. pad = 1;
  9371. }
  9372. ctx->data[i++] = c;
  9373. ctx->remaining++;
  9374. inLen--;
  9375. }
  9376. if (pad)
  9377. return 0; /* indicates that clients should call DecodeFinal */
  9378. else
  9379. return 1;
  9380. }
  9381. /* if the last data is '\n', remove it */
  9382. c = in[j - 1];
  9383. if (c == '\n') {
  9384. c = (in[j - 2]);
  9385. if (c == '=')
  9386. return 0;
  9387. else
  9388. return 1;
  9389. }
  9390. if (c == '=')
  9391. return 0;
  9392. else
  9393. return 1;
  9394. }
  9395. /* wolfSSL_EVP_DecodeFinal decode remaining data in ctx
  9396. * to outputs to out.
  9397. * Returns 1 on success, -1 on failure.
  9398. */
  9399. int wolfSSL_EVP_DecodeFinal(WOLFSSL_EVP_ENCODE_CTX* ctx,
  9400. unsigned char* out, int* outl)
  9401. {
  9402. word32 outsz = 0;
  9403. word32 inLen;
  9404. word32 j = 0;
  9405. int res;
  9406. WOLFSSL_ENTER("wolfSSL_EVP_DecodeFinal");
  9407. if (outl == NULL)
  9408. return -1;
  9409. if (ctx == NULL || out == NULL ) {
  9410. *outl = 0;
  9411. return -1;
  9412. }
  9413. if (ctx->remaining > 0) {
  9414. inLen = ctx->remaining;
  9415. if ((res = Base64_SkipNewline(ctx->data, &inLen, &j)) != 0) {
  9416. *outl = 0;
  9417. if (res == BUFFER_E) /* means no valid data to decode in buffer */
  9418. return 1; /* returns as success with no output */
  9419. else
  9420. return -1;
  9421. }
  9422. outsz = ctx->remaining;
  9423. res = Base64_Decode(ctx->data, ctx->remaining, out, &outsz);
  9424. if (res == 0) {
  9425. *outl = outsz;
  9426. return 1;
  9427. }
  9428. else {
  9429. *outl = 0;
  9430. return -1;
  9431. }
  9432. }
  9433. else {
  9434. *outl = 0;
  9435. return 1;
  9436. }
  9437. }
  9438. #endif /* WOLFSSL_BASE64_DECODE */
  9439. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  9440. #endif /* WOLFSSL_EVP_INCLUDED */