evp.c 321 KB

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