evp.c 379 KB

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