evp.c 394 KB

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