ecc.c 521 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225
  1. /* ecc.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. /* in case user set HAVE_ECC there */
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #ifdef WOLFSSL_ECC_NO_SMALL_STACK
  27. #undef WOLFSSL_SMALL_STACK
  28. #undef WOLFSSL_SMALL_STACK_CACHE
  29. #endif
  30. /*
  31. Possible ECC enable options:
  32. * HAVE_ECC: Overall control of ECC default: on
  33. * HAVE_ECC_ENCRYPT: ECC encrypt/decrypt w/AES and HKDF default: off
  34. * HAVE_ECC_SIGN: ECC sign default: on
  35. * HAVE_ECC_VERIFY: ECC verify default: on
  36. * HAVE_ECC_DHE: ECC build shared secret default: on
  37. * HAVE_ECC_CDH: ECC cofactor DH shared secret default: off
  38. * HAVE_ECC_KEY_IMPORT: ECC Key import default: on
  39. * HAVE_ECC_KEY_EXPORT: ECC Key export default: on
  40. * ECC_SHAMIR: Enables Shamir calc method default: on
  41. * HAVE_COMP_KEY: Enables compressed key default: off
  42. * WOLFSSL_VALIDATE_ECC_IMPORT: Validate ECC key on import default: off
  43. * WOLFSSL_VALIDATE_ECC_KEYGEN: Validate ECC key gen default: off
  44. * WOLFSSL_CUSTOM_CURVES: Allow non-standard curves. default: off
  45. * Includes the curve "a" variable in calculation
  46. * ECC_DUMP_OID: Enables dump of OID encoding and sum default: off
  47. * ECC_CACHE_CURVE: Enables cache of curve info to improve performance
  48. * default: off
  49. * FP_ECC: ECC Fixed Point Cache default: off
  50. * FP cache is not supported for SECP160R1, SECP160R2,
  51. * SECP160K1 and SECP224K1. These do not work with scalars
  52. * that are the length of the order when the order is
  53. * longer than the prime. Use wc_ecc_fp_free to free cache.
  54. * USE_ECC_B_PARAM: Enable ECC curve B param default: off
  55. * (on for HAVE_COMP_KEY)
  56. * WOLFSSL_ECC_CURVE_STATIC: default off (on for windows)
  57. * For the ECC curve parameters `ecc_set_type` use fixed
  58. * array for hex string
  59. * WC_ECC_NONBLOCK: Enable non-blocking support for sign/verify.
  60. * Requires SP with WOLFSSL_SP_NONBLOCK
  61. * WC_ECC_NONBLOCK_ONLY Enable the non-blocking function only, no fall-back to
  62. * normal blocking API's
  63. * WOLFSSL_ECDSA_SET_K: Enables the setting of the 'k' value to use during ECDSA
  64. * signing. If the value is invalid, a new random 'k' is
  65. * generated in the loop. (For testing)
  66. * default: off
  67. * WOLFSSL_ECDSA_SET_K_ONE_LOOP:
  68. * Enables the setting of the 'k' value to use during ECDSA
  69. * signing. If the value is invalid then an error is
  70. * returned rather than generating a new 'k'. (For testing)
  71. * default: off
  72. * WOLFSSL_ECDSA_DETERMINISTIC_K: Enables RFC6979 implementation of
  73. * deterministic ECC signatures. The following function
  74. * can be used to set the deterministic signing flag in the
  75. * ecc key structure.
  76. * int wc_ecc_set_deterministic(ecc_key* key, byte flag)
  77. * default: off
  78. *
  79. * WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT: RFC6979 lists a variant that uses the
  80. * hash directly instead of doing bits2octets(H(m)), when
  81. * the variant macro is used the bits2octets operation on
  82. * the hash is removed.
  83. * default: off
  84. *
  85. * WC_PROTECT_ENCRYPTED_MEM:
  86. * Enables implementations that protect data that is in
  87. * encrypted memory.
  88. * default: off
  89. */
  90. /*
  91. ECC Curve Types:
  92. * NO_ECC_SECP Disables SECP curves default: off (not defined)
  93. * HAVE_ECC_SECPR2 Enables SECP R2 curves default: off
  94. * HAVE_ECC_SECPR3 Enables SECP R3 curves default: off
  95. * HAVE_ECC_BRAINPOOL Enables Brainpool curves default: off
  96. * HAVE_ECC_KOBLITZ Enables Koblitz curves default: off
  97. * WOLFSSL_SM2 Enables SM2 curves default: off
  98. */
  99. /*
  100. ECC Curve Sizes:
  101. * ECC_USER_CURVES: Allows custom combination of key sizes below
  102. * HAVE_ALL_CURVES: Enable all key sizes (on unless ECC_USER_CURVES is defined)
  103. * ECC_MIN_KEY_SZ: Minimum supported ECC key size
  104. * HAVE_ECC112: 112 bit key
  105. * HAVE_ECC128: 128 bit key
  106. * HAVE_ECC160: 160 bit key
  107. * HAVE_ECC192: 192 bit key
  108. * HAVE_ECC224: 224 bit key
  109. * HAVE_ECC239: 239 bit key
  110. * NO_ECC256: Disables 256 bit key (on by default)
  111. * HAVE_ECC320: 320 bit key
  112. * HAVE_ECC384: 384 bit key
  113. * HAVE_ECC512: 512 bit key
  114. * HAVE_ECC521: 521 bit key
  115. */
  116. #ifdef HAVE_ECC
  117. /* Make sure custom curves is enabled for Brainpool or Koblitz curve types */
  118. #if (defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)) &&\
  119. !defined(WOLFSSL_CUSTOM_CURVES)
  120. #error Brainpool and Koblitz curves requires WOLFSSL_CUSTOM_CURVES
  121. #endif
  122. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  123. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  124. #define FIPS_NO_WRAPPERS
  125. #ifdef USE_WINDOWS_API
  126. #pragma code_seg(".fipsA$f")
  127. #pragma const_seg(".fipsB$f")
  128. #endif
  129. #endif
  130. /* public ASN interface */
  131. #include <wolfssl/wolfcrypt/asn_public.h>
  132. #include <wolfssl/wolfcrypt/ecc.h>
  133. #include <wolfssl/wolfcrypt/asn.h>
  134. #include <wolfssl/wolfcrypt/error-crypt.h>
  135. #include <wolfssl/wolfcrypt/logging.h>
  136. #include <wolfssl/wolfcrypt/types.h>
  137. #ifdef WOLFSSL_HAVE_SP_ECC
  138. #include <wolfssl/wolfcrypt/sp.h>
  139. #endif
  140. #ifdef HAVE_ECC_ENCRYPT
  141. #include <wolfssl/wolfcrypt/kdf.h>
  142. #include <wolfssl/wolfcrypt/aes.h>
  143. #endif
  144. #ifdef HAVE_X963_KDF
  145. #include <wolfssl/wolfcrypt/hash.h>
  146. #endif
  147. #ifdef WOLF_CRYPTO_CB
  148. #include <wolfssl/wolfcrypt/cryptocb.h>
  149. #endif
  150. #ifdef NO_INLINE
  151. #include <wolfssl/wolfcrypt/misc.h>
  152. #else
  153. #define WOLFSSL_MISC_INCLUDED
  154. #include <wolfcrypt/src/misc.c>
  155. #endif
  156. #if defined(FREESCALE_LTC_ECC)
  157. #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
  158. #endif
  159. #if defined(WOLFSSL_STM32_PKA)
  160. #include <wolfssl/wolfcrypt/port/st/stm32.h>
  161. #endif
  162. #if defined(WOLFSSL_PSOC6_CRYPTO)
  163. #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h>
  164. #endif
  165. #if defined(WOLFSSL_CAAM)
  166. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  167. #endif
  168. #if defined(WOLFSSL_KCAPI_ECC)
  169. #include <wolfssl/wolfcrypt/port/kcapi/kcapi_ecc.h>
  170. #endif
  171. #ifdef WOLFSSL_SE050
  172. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  173. #endif
  174. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  175. #include <xsecure_ellipticclient.h>
  176. #endif
  177. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  178. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  179. #include <wolfssl/wolfcrypt/hmac.h>
  180. #endif
  181. #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  182. #define GEN_MEM_ERR MP_MEM
  183. #elif defined(USE_FAST_MATH)
  184. #define GEN_MEM_ERR FP_MEM
  185. #else
  186. #define GEN_MEM_ERR MP_MEM
  187. #endif
  188. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  189. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  190. !defined(WOLFSSL_KCAPI_ECC) && !defined(WOLFSSL_SE050) && \
  191. !defined(WOLFSSL_XILINX_CRYPT_VERSAL) && !defined(WOLFSSL_STM32_PKA)
  192. #undef HAVE_ECC_VERIFY_HELPER
  193. #define HAVE_ECC_VERIFY_HELPER
  194. #endif
  195. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  196. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  197. !defined(WOLFSSL_KCAPI_ECC) && !defined(NO_ECC_MAKE_PUB) && \
  198. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  199. #undef HAVE_ECC_MAKE_PUB
  200. #define HAVE_ECC_MAKE_PUB
  201. #endif
  202. #if !defined(WOLFSSL_SP_MATH) && \
  203. !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  204. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  205. !defined(WOLFSSL_SE050) && !defined(WOLFSSL_STM32_PKA) && \
  206. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  207. #undef HAVE_ECC_CHECK_PUBKEY_ORDER
  208. #define HAVE_ECC_CHECK_PUBKEY_ORDER
  209. #endif
  210. #if defined(WOLFSSL_SP_MATH_ALL) && SP_INT_BITS < MAX_ECC_BITS_NEEDED
  211. #define MAX_ECC_BITS_USE SP_INT_BITS
  212. #else
  213. #define MAX_ECC_BITS_USE MAX_ECC_BITS_NEEDED
  214. #endif
  215. #if !defined(WOLFSSL_CUSTOM_CURVES) && (ECC_MIN_KEY_SZ > 160) && \
  216. (!defined(HAVE_ECC_KOBLITZ) || (ECC_MIN_KEY_SZ > 224))
  217. #define ECC_KEY_MAX_BITS(key) \
  218. ((((key) == NULL) || ((key)->dp == NULL)) ? MAX_ECC_BITS_USE : \
  219. ((unsigned)((key)->dp->size * 8)))
  220. #else
  221. /* Add one bit for cases when order is a bit greater than prime. */
  222. #define ECC_KEY_MAX_BITS(key) \
  223. ((((key) == NULL) || ((key)->dp == NULL)) ? MAX_ECC_BITS_USE : \
  224. ((unsigned)((key)->dp->size * 8 + 1)))
  225. #endif
  226. /* forward declarations */
  227. static int wc_ecc_new_point_ex(ecc_point** point, void* heap);
  228. static void wc_ecc_del_point_ex(ecc_point* p, void* heap);
  229. #if defined(HAVE_ECC_SIGN) && (defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  230. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT))
  231. static int deterministic_sign_helper(const byte* in, word32 inlen, ecc_key* key);
  232. #endif
  233. /* internal ECC states */
  234. enum {
  235. ECC_STATE_NONE = 0,
  236. ECC_STATE_SHARED_SEC_GEN,
  237. ECC_STATE_SHARED_SEC_RES,
  238. ECC_STATE_SIGN_DO,
  239. ECC_STATE_SIGN_ENCODE,
  240. ECC_STATE_VERIFY_DECODE,
  241. ECC_STATE_VERIFY_DO,
  242. ECC_STATE_VERIFY_RES
  243. };
  244. /* map
  245. ptmul -> mulmod
  246. */
  247. /* 256-bit curve on by default whether user curves or not */
  248. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  249. #define ECC112
  250. #endif
  251. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  252. #define ECC128
  253. #endif
  254. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  255. #define ECC160
  256. #endif
  257. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  258. #define ECC192
  259. #endif
  260. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  261. #define ECC224
  262. #endif
  263. #if (defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 239
  264. #define ECC239
  265. #endif
  266. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  267. #define ECC256
  268. #endif
  269. #if (defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 320
  270. #define ECC320
  271. #endif
  272. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  273. #define ECC384
  274. #endif
  275. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  276. #define ECC512
  277. #endif
  278. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  279. #define ECC521
  280. #endif
  281. /* The encoded OID's for ECC curves */
  282. #ifdef ECC112
  283. #ifndef NO_ECC_SECP
  284. #ifdef HAVE_OID_ENCODING
  285. #define CODED_SECP112R1 {1,3,132,0,6}
  286. #define CODED_SECP112R1_SZ 5
  287. #else
  288. #define CODED_SECP112R1 {0x2B,0x81,0x04,0x00,0x06}
  289. #define CODED_SECP112R1_SZ 5
  290. #endif
  291. #ifndef WOLFSSL_ECC_CURVE_STATIC
  292. static const ecc_oid_t ecc_oid_secp112r1[] = CODED_SECP112R1;
  293. #else
  294. #define ecc_oid_secp112r1 CODED_SECP112R1
  295. #endif
  296. #define ecc_oid_secp112r1_sz CODED_SECP112R1_SZ
  297. #endif /* !NO_ECC_SECP */
  298. #if defined(HAVE_ECC_SECPR2) && defined(HAVE_ECC_KOBLITZ)
  299. #ifdef HAVE_OID_ENCODING
  300. #define CODED_SECP112R2 {1,3,132,0,7}
  301. #define CODED_SECP112R2_SZ 5
  302. #else
  303. #define CODED_SECP112R2 {0x2B,0x81,0x04,0x00,0x07}
  304. #define CODED_SECP112R2_SZ 5
  305. #endif
  306. #ifndef WOLFSSL_ECC_CURVE_STATIC
  307. static const ecc_oid_t ecc_oid_secp112r2[] = CODED_SECP112R2;
  308. #else
  309. #define ecc_oid_secp112r2 CODED_SECP112R2
  310. #endif
  311. #define ecc_oid_secp112r2_sz CODED_SECP112R2_SZ
  312. #endif /* HAVE_ECC_SECPR2 && HAVE_ECC_KOBLITZ */
  313. #endif /* ECC112 */
  314. #ifdef ECC128
  315. #ifndef NO_ECC_SECP
  316. #ifdef HAVE_OID_ENCODING
  317. #define CODED_SECP128R1 {1,3,132,0,28}
  318. #define CODED_SECP128R1_SZ 5
  319. #else
  320. #define CODED_SECP128R1 {0x2B,0x81,0x04,0x00,0x1C}
  321. #define CODED_SECP128R1_SZ 5
  322. #endif
  323. #ifndef WOLFSSL_ECC_CURVE_STATIC
  324. static const ecc_oid_t ecc_oid_secp128r1[] = CODED_SECP128R1;
  325. #else
  326. #define ecc_oid_secp128r1 CODED_SECP128R1
  327. #endif
  328. #define ecc_oid_secp128r1_sz CODED_SECP128R1_SZ
  329. #endif /* !NO_ECC_SECP */
  330. #if defined(HAVE_ECC_SECPR2) && defined(HAVE_ECC_KOBLITZ)
  331. #ifdef HAVE_OID_ENCODING
  332. #define CODED_SECP128R2 {1,3,132,0,29}
  333. #define CODED_SECP128R2_SZ 5
  334. #else
  335. #define CODED_SECP128R2 {0x2B,0x81,0x04,0x00,0x1D}
  336. #define CODED_SECP128R2_SZ 5
  337. #endif
  338. #ifndef WOLFSSL_ECC_CURVE_STATIC
  339. static const ecc_oid_t ecc_oid_secp128r2[] = CODED_SECP128R2;
  340. #else
  341. #define ecc_oid_secp128r2 CODED_SECP128R2
  342. #endif
  343. #define ecc_oid_secp128r2_sz CODED_SECP128R2_SZ
  344. #endif /* HAVE_ECC_SECPR2 && HAVE_ECC_KOBLITZ */
  345. #endif /* ECC128 */
  346. #ifdef ECC160
  347. #ifndef FP_ECC
  348. #ifndef NO_ECC_SECP
  349. #ifdef HAVE_OID_ENCODING
  350. #define CODED_SECP160R1 {1,3,132,0,8}
  351. #define CODED_SECP160R1_SZ 5
  352. #else
  353. #define CODED_SECP160R1 {0x2B,0x81,0x04,0x00,0x08}
  354. #define CODED_SECP160R1_SZ 5
  355. #endif
  356. #ifndef WOLFSSL_ECC_CURVE_STATIC
  357. static const ecc_oid_t ecc_oid_secp160r1[] = CODED_SECP160R1;
  358. #else
  359. #define ecc_oid_secp160r1 CODED_SECP160R1
  360. #endif
  361. #define ecc_oid_secp160r1_sz CODED_SECP160R1_SZ
  362. #endif /* !NO_ECC_SECP */
  363. #ifdef HAVE_ECC_SECPR2
  364. #ifdef HAVE_OID_ENCODING
  365. #define CODED_SECP160R2 {1,3,132,0,30}
  366. #define CODED_SECP160R2_SZ 5
  367. #else
  368. #define CODED_SECP160R2 {0x2B,0x81,0x04,0x00,0x1E}
  369. #define CODED_SECP160R2_SZ 5
  370. #endif
  371. #ifndef WOLFSSL_ECC_CURVE_STATIC
  372. static const ecc_oid_t ecc_oid_secp160r2[] = CODED_SECP160R2;
  373. #else
  374. #define ecc_oid_secp160r2 CODED_SECP160R2
  375. #endif
  376. #define ecc_oid_secp160r2_sz CODED_SECP160R2_SZ
  377. #endif /* HAVE_ECC_SECPR2 */
  378. #ifdef HAVE_ECC_KOBLITZ
  379. #ifdef HAVE_OID_ENCODING
  380. #define CODED_SECP160K1 {1,3,132,0,9}
  381. #define CODED_SECP160K1_SZ 5
  382. #else
  383. #define CODED_SECP160K1 {0x2B,0x81,0x04,0x00,0x09}
  384. #define CODED_SECP160K1_SZ 5
  385. #endif
  386. #ifndef WOLFSSL_ECC_CURVE_STATIC
  387. static const ecc_oid_t ecc_oid_secp160k1[] = CODED_SECP160K1;
  388. #else
  389. #define ecc_oid_secp160k1 CODED_SECP160K1
  390. #endif
  391. #define ecc_oid_secp160k1_sz CODED_SECP160K1_SZ
  392. #endif /* HAVE_ECC_KOBLITZ */
  393. #endif /* !FP_ECC */
  394. #ifdef HAVE_ECC_BRAINPOOL
  395. #ifdef HAVE_OID_ENCODING
  396. #define CODED_BRAINPOOLP160R1 {1,3,36,3,3,2,8,1,1,1}
  397. #define CODED_BRAINPOOLP160R1_SZ 10
  398. #else
  399. #define CODED_BRAINPOOLP160R1 {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x01}
  400. #define CODED_BRAINPOOLP160R1_SZ 9
  401. #endif
  402. #ifndef WOLFSSL_ECC_CURVE_STATIC
  403. static const ecc_oid_t ecc_oid_brainpoolp160r1[] = CODED_BRAINPOOLP160R1;
  404. #else
  405. #define ecc_oid_brainpoolp160r1 CODED_BRAINPOOLP160R1
  406. #endif
  407. #define ecc_oid_brainpoolp160r1_sz CODED_BRAINPOOLP160R1_SZ
  408. #endif /* HAVE_ECC_BRAINPOOL */
  409. #endif /* ECC160 */
  410. #ifdef ECC192
  411. #ifndef NO_ECC_SECP
  412. #ifdef HAVE_OID_ENCODING
  413. #define CODED_SECP192R1 {1,2,840,10045,3,1,1}
  414. #define CODED_SECP192R1_SZ 7
  415. #else
  416. #define CODED_SECP192R1 {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x01}
  417. #define CODED_SECP192R1_SZ 8
  418. #endif
  419. #ifndef WOLFSSL_ECC_CURVE_STATIC
  420. static const ecc_oid_t ecc_oid_secp192r1[] = CODED_SECP192R1;
  421. #else
  422. #define ecc_oid_secp192r1 CODED_SECP192R1
  423. #endif
  424. #define ecc_oid_secp192r1_sz CODED_SECP192R1_SZ
  425. #endif /* !NO_ECC_SECP */
  426. #ifdef HAVE_ECC_SECPR2
  427. #ifdef HAVE_OID_ENCODING
  428. #define CODED_PRIME192V2 {1,2,840,10045,3,1,2}
  429. #define CODED_PRIME192V2_SZ 7
  430. #else
  431. #define CODED_PRIME192V2 {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x02}
  432. #define CODED_PRIME192V2_SZ 8
  433. #endif
  434. #ifndef WOLFSSL_ECC_CURVE_STATIC
  435. static const ecc_oid_t ecc_oid_prime192v2[] = CODED_PRIME192V2;
  436. #else
  437. #define ecc_oid_prime192v2 CODED_PRIME192V2
  438. #endif
  439. #define ecc_oid_prime192v2_sz CODED_PRIME192V2_SZ
  440. #endif /* HAVE_ECC_SECPR2 */
  441. #ifdef HAVE_ECC_SECPR3
  442. #ifdef HAVE_OID_ENCODING
  443. #define CODED_PRIME192V3 {1,2,840,10045,3,1,3}
  444. #define CODED_PRIME192V3_SZ 7
  445. #else
  446. #define CODED_PRIME192V3 {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x03}
  447. #define CODED_PRIME192V3_SZ 8
  448. #endif
  449. #ifndef WOLFSSL_ECC_CURVE_STATIC
  450. static const ecc_oid_t ecc_oid_prime192v3[] = CODED_PRIME192V3;
  451. #else
  452. #define ecc_oid_prime192v3 CODED_PRIME192V3
  453. #endif
  454. #define ecc_oid_prime192v3_sz CODED_PRIME192V3_SZ
  455. #endif /* HAVE_ECC_SECPR3 */
  456. #ifdef HAVE_ECC_KOBLITZ
  457. #ifdef HAVE_OID_ENCODING
  458. #define CODED_SECP192K1 {1,3,132,0,31}
  459. #define CODED_SECP192K1_SZ 5
  460. #else
  461. #define CODED_SECP192K1 {0x2B,0x81,0x04,0x00,0x1F}
  462. #define CODED_SECP192K1_SZ 5
  463. #endif
  464. #ifndef WOLFSSL_ECC_CURVE_STATIC
  465. static const ecc_oid_t ecc_oid_secp192k1[] = CODED_SECP192K1;
  466. #else
  467. #define ecc_oid_secp192k1 CODED_SECP192K1
  468. #endif
  469. #define ecc_oid_secp192k1_sz CODED_SECP192K1_SZ
  470. #endif /* HAVE_ECC_KOBLITZ */
  471. #ifdef HAVE_ECC_BRAINPOOL
  472. #ifdef HAVE_OID_ENCODING
  473. #define CODED_BRAINPOOLP192R1 {1,3,36,3,3,2,8,1,1,3}
  474. #define CODED_BRAINPOOLP192R1_SZ 10
  475. #else
  476. #define CODED_BRAINPOOLP192R1 {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x03}
  477. #define CODED_BRAINPOOLP192R1_SZ 9
  478. #endif
  479. #ifndef WOLFSSL_ECC_CURVE_STATIC
  480. static const ecc_oid_t ecc_oid_brainpoolp192r1[] = CODED_BRAINPOOLP192R1;
  481. #else
  482. #define ecc_oid_brainpoolp192r1 CODED_BRAINPOOLP192R1
  483. #endif
  484. #define ecc_oid_brainpoolp192r1_sz CODED_BRAINPOOLP192R1_SZ
  485. #endif /* HAVE_ECC_BRAINPOOL */
  486. #endif /* ECC192 */
  487. #ifdef ECC224
  488. #ifndef NO_ECC_SECP
  489. #ifdef HAVE_OID_ENCODING
  490. #define CODED_SECP224R1 {1,3,132,0,33}
  491. #define CODED_SECP224R1_SZ 5
  492. #else
  493. #define CODED_SECP224R1 {0x2B,0x81,0x04,0x00,0x21}
  494. #define CODED_SECP224R1_SZ 5
  495. #endif
  496. #ifndef WOLFSSL_ECC_CURVE_STATIC
  497. static const ecc_oid_t ecc_oid_secp224r1[] = CODED_SECP224R1;
  498. #else
  499. #define ecc_oid_secp224r1 CODED_SECP224R1
  500. #endif
  501. #define ecc_oid_secp224r1_sz CODED_SECP224R1_SZ
  502. #endif /* !NO_ECC_SECP */
  503. #if defined(HAVE_ECC_KOBLITZ) && !defined(FP_ECC)
  504. #ifdef HAVE_OID_ENCODING
  505. #define CODED_SECP224K1 {1,3,132,0,32}
  506. #define CODED_SECP224K1_SZ 5
  507. #else
  508. #define CODED_SECP224K1 {0x2B,0x81,0x04,0x00,0x20}
  509. #define CODED_SECP224K1_SZ 5
  510. #endif
  511. #ifndef WOLFSSL_ECC_CURVE_STATIC
  512. static const ecc_oid_t ecc_oid_secp224k1[] = CODED_SECP224K1;
  513. #else
  514. #define ecc_oid_secp224k1 CODED_SECP224K1
  515. #endif
  516. #define ecc_oid_secp224k1_sz CODED_SECP224K1_SZ
  517. #endif /* HAVE_ECC_KOBLITZ && !FP_ECC */
  518. #ifdef HAVE_ECC_BRAINPOOL
  519. #ifdef HAVE_OID_ENCODING
  520. #define CODED_BRAINPOOLP224R1 {1,3,36,3,3,2,8,1,1,5}
  521. #define CODED_BRAINPOOLP224R1_SZ 10
  522. #else
  523. #define CODED_BRAINPOOLP224R1 {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x05}
  524. #define CODED_BRAINPOOLP224R1_SZ 9
  525. #endif
  526. #ifndef WOLFSSL_ECC_CURVE_STATIC
  527. static const ecc_oid_t ecc_oid_brainpoolp224r1[] = CODED_BRAINPOOLP224R1;
  528. #else
  529. #define ecc_oid_brainpoolp224r1 CODED_BRAINPOOLP224R1
  530. #endif
  531. #define ecc_oid_brainpoolp224r1_sz CODED_BRAINPOOLP224R1_SZ
  532. #endif /* HAVE_ECC_BRAINPOOL */
  533. #endif /* ECC224 */
  534. #ifdef ECC239
  535. #ifndef NO_ECC_SECP
  536. #ifdef HAVE_OID_ENCODING
  537. #define CODED_PRIME239V1 {1,2,840,10045,3,1,4}
  538. #define CODED_PRIME239V1_SZ 7
  539. #else
  540. #define CODED_PRIME239V1 {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x04}
  541. #define CODED_PRIME239V1_SZ 8
  542. #endif
  543. #ifndef WOLFSSL_ECC_CURVE_STATIC
  544. static const ecc_oid_t ecc_oid_prime239v1[] = CODED_PRIME239V1;
  545. #else
  546. #define ecc_oid_prime239v1 CODED_PRIME239V1
  547. #endif
  548. #define ecc_oid_prime239v1_sz CODED_PRIME239V1_SZ
  549. #endif /* !NO_ECC_SECP */
  550. #ifdef HAVE_ECC_SECPR2
  551. #ifdef HAVE_OID_ENCODING
  552. #define CODED_PRIME239V2 {1,2,840,10045,3,1,5}
  553. #define CODED_PRIME239V2_SZ 7
  554. #else
  555. #define CODED_PRIME239V2 {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x05}
  556. #define CODED_PRIME239V2_SZ 8
  557. #endif
  558. #ifndef WOLFSSL_ECC_CURVE_STATIC
  559. static const ecc_oid_t ecc_oid_prime239v2[] = CODED_PRIME239V2;
  560. #else
  561. #define ecc_oid_prime239v2 CODED_PRIME239V2
  562. #endif
  563. #define ecc_oid_prime239v2_sz CODED_PRIME239V2_SZ
  564. #endif /* HAVE_ECC_SECPR2 */
  565. #ifdef HAVE_ECC_SECPR3
  566. #ifdef HAVE_OID_ENCODING
  567. #define CODED_PRIME239V3 {1,2,840,10045,3,1,6}
  568. #define CODED_PRIME239V3_SZ 7
  569. #else
  570. #define CODED_PRIME239V3 {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x06}
  571. #define CODED_PRIME239V3_SZ 8
  572. #endif
  573. #ifndef WOLFSSL_ECC_CURVE_STATIC
  574. static const ecc_oid_t ecc_oid_prime239v3[] = CODED_PRIME239V3;
  575. #else
  576. #define ecc_oid_prime239v3 CODED_PRIME239V3
  577. #endif
  578. #define ecc_oid_prime239v3_sz CODED_PRIME239V3_SZ
  579. #endif /* HAVE_ECC_SECPR3 */
  580. #endif /* ECC239 */
  581. #ifdef ECC256
  582. #ifndef NO_ECC_SECP
  583. #ifdef HAVE_OID_ENCODING
  584. #define CODED_SECP256R1 {1,2,840,10045,3,1,7}
  585. #define CODED_SECP256R1_SZ 7
  586. #else
  587. #define CODED_SECP256R1 {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07}
  588. #define CODED_SECP256R1_SZ 8
  589. #endif
  590. #ifndef WOLFSSL_ECC_CURVE_STATIC
  591. static const ecc_oid_t ecc_oid_secp256r1[] = CODED_SECP256R1;
  592. #else
  593. #define ecc_oid_secp256r1 CODED_SECP256R1
  594. #endif
  595. #define ecc_oid_secp256r1_sz CODED_SECP256R1_SZ
  596. #endif /* !NO_ECC_SECP */
  597. #ifdef HAVE_ECC_KOBLITZ
  598. #ifdef HAVE_OID_ENCODING
  599. #define CODED_SECP256K1 {1,3,132,0,10}
  600. #define CODED_SECP256K1_SZ 5
  601. #else
  602. #define CODED_SECP256K1 {0x2B,0x81,0x04,0x00,0x0A}
  603. #define CODED_SECP256K1_SZ 5
  604. #endif
  605. #ifndef WOLFSSL_ECC_CURVE_STATIC
  606. static const ecc_oid_t ecc_oid_secp256k1[] = CODED_SECP256K1;
  607. #else
  608. #define ecc_oid_secp256k1 CODED_SECP256K1
  609. #endif
  610. #define ecc_oid_secp256k1_sz CODED_SECP256K1_SZ
  611. #endif /* HAVE_ECC_KOBLITZ */
  612. #ifdef HAVE_ECC_BRAINPOOL
  613. #ifdef HAVE_OID_ENCODING
  614. #define CODED_BRAINPOOLP256R1 {1,3,36,3,3,2,8,1,1,7}
  615. #define CODED_BRAINPOOLP256R1_SZ 10
  616. #else
  617. #define CODED_BRAINPOOLP256R1 {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07}
  618. #define CODED_BRAINPOOLP256R1_SZ 9
  619. #endif
  620. #ifndef WOLFSSL_ECC_CURVE_STATIC
  621. static const ecc_oid_t ecc_oid_brainpoolp256r1[] = CODED_BRAINPOOLP256R1;
  622. #else
  623. #define ecc_oid_brainpoolp256r1 CODED_BRAINPOOLP256R1
  624. #endif
  625. #define ecc_oid_brainpoolp256r1_sz CODED_BRAINPOOLP256R1_SZ
  626. #endif /* HAVE_ECC_BRAINPOOL */
  627. #endif /* ECC256 */
  628. #if defined(WOLFSSL_SM2)
  629. #ifdef HAVE_OID_ENCODING
  630. #define CODED_SM2P256V1 {1,2,156,10197,1,301}
  631. #define CODED_SM2P256V1_SZ 6
  632. #else
  633. #define CODED_SM2P256V1 {0x2A,0x81,0x1C,0xCF,0x55,0x01,0x82,0x2d}
  634. #define CODED_SM2P256V1_SZ 8
  635. #endif
  636. #ifndef WOLFSSL_ECC_CURVE_STATIC
  637. static const ecc_oid_t ecc_oid_sm2p256v1[] = CODED_SM2P256V1;
  638. #else
  639. #define ecc_oid_sm2p256v1 CODED_SM2P256V1
  640. #endif
  641. #define ecc_oid_sm2p256v1_sz CODED_SM2P256V1_SZ
  642. #endif /* WOLFSSL_SM2 */
  643. #ifdef ECC320
  644. #ifdef HAVE_ECC_BRAINPOOL
  645. #ifdef HAVE_OID_ENCODING
  646. #define CODED_BRAINPOOLP320R1 {1,3,36,3,3,2,8,1,1,9}
  647. #define CODED_BRAINPOOLP320R1_SZ 10
  648. #else
  649. #define CODED_BRAINPOOLP320R1 {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x09}
  650. #define CODED_BRAINPOOLP320R1_SZ 9
  651. #endif
  652. #ifndef WOLFSSL_ECC_CURVE_STATIC
  653. static const ecc_oid_t ecc_oid_brainpoolp320r1[] = CODED_BRAINPOOLP320R1;
  654. #else
  655. #define ecc_oid_brainpoolp320r1 CODED_BRAINPOOLP320R1
  656. #endif
  657. #define ecc_oid_brainpoolp320r1_sz CODED_BRAINPOOLP320R1_SZ
  658. #endif /* HAVE_ECC_BRAINPOOL */
  659. #endif /* ECC320 */
  660. #ifdef ECC384
  661. #ifndef NO_ECC_SECP
  662. #ifdef HAVE_OID_ENCODING
  663. #define CODED_SECP384R1 {1,3,132,0,34}
  664. #define CODED_SECP384R1_SZ 5
  665. #else
  666. #define CODED_SECP384R1 {0x2B,0x81,0x04,0x00,0x22}
  667. #define CODED_SECP384R1_SZ 5
  668. #endif
  669. #ifndef WOLFSSL_ECC_CURVE_STATIC
  670. static const ecc_oid_t ecc_oid_secp384r1[] = CODED_SECP384R1;
  671. #define CODED_SECP384R1_OID ecc_oid_secp384r1
  672. #else
  673. #define ecc_oid_secp384r1 CODED_SECP384R1
  674. #endif
  675. #define ecc_oid_secp384r1_sz CODED_SECP384R1_SZ
  676. #endif /* !NO_ECC_SECP */
  677. #ifdef HAVE_ECC_BRAINPOOL
  678. #ifdef HAVE_OID_ENCODING
  679. #define CODED_BRAINPOOLP384R1 {1,3,36,3,3,2,8,1,1,11}
  680. #define CODED_BRAINPOOLP384R1_SZ 10
  681. #else
  682. #define CODED_BRAINPOOLP384R1 {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x0B}
  683. #define CODED_BRAINPOOLP384R1_SZ 9
  684. #endif
  685. #ifndef WOLFSSL_ECC_CURVE_STATIC
  686. static const ecc_oid_t ecc_oid_brainpoolp384r1[] = CODED_BRAINPOOLP384R1;
  687. #else
  688. #define ecc_oid_brainpoolp384r1 CODED_BRAINPOOLP384R1
  689. #endif
  690. #define ecc_oid_brainpoolp384r1_sz CODED_BRAINPOOLP384R1_SZ
  691. #endif /* HAVE_ECC_BRAINPOOL */
  692. #endif /* ECC384 */
  693. #ifdef ECC512
  694. #ifdef HAVE_ECC_BRAINPOOL
  695. #ifdef HAVE_OID_ENCODING
  696. #define CODED_BRAINPOOLP512R1 {1,3,36,3,3,2,8,1,1,13}
  697. #define CODED_BRAINPOOLP512R1_SZ 10
  698. #else
  699. #define CODED_BRAINPOOLP512R1 {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x0D}
  700. #define CODED_BRAINPOOLP512R1_SZ 9
  701. #endif
  702. #ifndef WOLFSSL_ECC_CURVE_STATIC
  703. static const ecc_oid_t ecc_oid_brainpoolp512r1[] = CODED_BRAINPOOLP512R1;
  704. #else
  705. #define ecc_oid_brainpoolp512r1 CODED_BRAINPOOLP512R1
  706. #endif
  707. #define ecc_oid_brainpoolp512r1_sz CODED_BRAINPOOLP512R1_SZ
  708. #endif /* HAVE_ECC_BRAINPOOL */
  709. #endif /* ECC512 */
  710. #ifdef ECC521
  711. #ifndef NO_ECC_SECP
  712. #ifdef HAVE_OID_ENCODING
  713. #define CODED_SECP521R1 {1,3,132,0,35}
  714. #define CODED_SECP521R1_SZ 5
  715. #else
  716. #define CODED_SECP521R1 {0x2B,0x81,0x04,0x00,0x23}
  717. #define CODED_SECP521R1_SZ 5
  718. #endif
  719. #ifndef WOLFSSL_ECC_CURVE_STATIC
  720. static const ecc_oid_t ecc_oid_secp521r1[] = CODED_SECP521R1;
  721. #else
  722. #define ecc_oid_secp521r1 CODED_SECP521R1
  723. #endif
  724. #define ecc_oid_secp521r1_sz CODED_SECP521R1_SZ
  725. #endif /* !NO_ECC_SECP */
  726. #endif /* ECC521 */
  727. /* This holds the key settings.
  728. ***MUST*** be organized by size from smallest to largest. */
  729. const ecc_set_type ecc_sets[] = {
  730. #ifdef ECC112
  731. #ifndef NO_ECC_SECP
  732. {
  733. 14, /* size/bytes */
  734. ECC_SECP112R1, /* ID */
  735. "SECP112R1", /* curve name */
  736. "DB7C2ABF62E35E668076BEAD208B", /* prime */
  737. "DB7C2ABF62E35E668076BEAD2088", /* A */
  738. "659EF8BA043916EEDE8911702B22", /* B */
  739. "DB7C2ABF62E35E7628DFAC6561C5", /* order */
  740. "9487239995A5EE76B55F9C2F098", /* Gx */
  741. "A89CE5AF8724C0A23E0E0FF77500", /* Gy */
  742. ecc_oid_secp112r1, /* oid/oidSz */
  743. ecc_oid_secp112r1_sz,
  744. ECC_SECP112R1_OID, /* oid sum */
  745. 1, /* cofactor */
  746. },
  747. #endif /* !NO_ECC_SECP */
  748. #if defined(HAVE_ECC_SECPR2) && defined(HAVE_ECC_KOBLITZ)
  749. {
  750. 14, /* size/bytes */
  751. ECC_SECP112R2, /* ID */
  752. "SECP112R2", /* curve name */
  753. "DB7C2ABF62E35E668076BEAD208B", /* prime */
  754. "6127C24C05F38A0AAAF65C0EF02C", /* A */
  755. "51DEF1815DB5ED74FCC34C85D709", /* B */
  756. "36DF0AAFD8B8D7597CA10520D04B", /* order */
  757. "4BA30AB5E892B4E1649DD0928643", /* Gx */
  758. "ADCD46F5882E3747DEF36E956E97", /* Gy */
  759. ecc_oid_secp112r2, /* oid/oidSz */
  760. ecc_oid_secp112r2_sz,
  761. ECC_SECP112R2_OID, /* oid sum */
  762. 4, /* cofactor */
  763. },
  764. #endif /* HAVE_ECC_SECPR2 && HAVE_ECC_KOBLITZ */
  765. #endif /* ECC112 */
  766. #ifdef ECC128
  767. #ifndef NO_ECC_SECP
  768. {
  769. 16, /* size/bytes */
  770. ECC_SECP128R1, /* ID */
  771. "SECP128R1", /* curve name */
  772. "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF", /* prime */
  773. "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC", /* A */
  774. "E87579C11079F43DD824993C2CEE5ED3", /* B */
  775. "FFFFFFFE0000000075A30D1B9038A115", /* order */
  776. "161FF7528B899B2D0C28607CA52C5B86", /* Gx */
  777. "CF5AC8395BAFEB13C02DA292DDED7A83", /* Gy */
  778. ecc_oid_secp128r1, /* oid/oidSz */
  779. ecc_oid_secp128r1_sz,
  780. ECC_SECP128R1_OID, /* oid sum */
  781. 1, /* cofactor */
  782. },
  783. #endif /* !NO_ECC_SECP */
  784. #if defined(HAVE_ECC_SECPR2) && defined(HAVE_ECC_KOBLITZ)
  785. {
  786. 16, /* size/bytes */
  787. ECC_SECP128R2, /* ID */
  788. "SECP128R2", /* curve name */
  789. "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF", /* prime */
  790. "D6031998D1B3BBFEBF59CC9BBFF9AEE1", /* A */
  791. "5EEEFCA380D02919DC2C6558BB6D8A5D", /* B */
  792. "3FFFFFFF7FFFFFFFBE0024720613B5A3", /* order */
  793. "7B6AA5D85E572983E6FB32A7CDEBC140", /* Gx */
  794. "27B6916A894D3AEE7106FE805FC34B44", /* Gy */
  795. ecc_oid_secp128r2, /* oid/oidSz */
  796. ecc_oid_secp128r2_sz,
  797. ECC_SECP128R2_OID, /* oid sum */
  798. 4, /* cofactor */
  799. },
  800. #endif /* HAVE_ECC_SECPR2 && HAVE_ECC_KOBLITZ */
  801. #endif /* ECC128 */
  802. #ifdef ECC160
  803. #ifndef FP_ECC
  804. #ifndef NO_ECC_SECP
  805. {
  806. 20, /* size/bytes */
  807. ECC_SECP160R1, /* ID */
  808. "SECP160R1", /* curve name */
  809. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF", /* prime */
  810. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC", /* A */
  811. "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45", /* B */
  812. "100000000000000000001F4C8F927AED3CA752257",/* order */
  813. "4A96B5688EF573284664698968C38BB913CBFC82", /* Gx */
  814. "23A628553168947D59DCC912042351377AC5FB32", /* Gy */
  815. ecc_oid_secp160r1, /* oid/oidSz */
  816. ecc_oid_secp160r1_sz,
  817. ECC_SECP160R1_OID, /* oid sum */
  818. 1, /* cofactor */
  819. },
  820. #endif /* !NO_ECC_SECP */
  821. #ifdef HAVE_ECC_SECPR2
  822. {
  823. 20, /* size/bytes */
  824. ECC_SECP160R2, /* ID */
  825. "SECP160R2", /* curve name */
  826. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", /* prime */
  827. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC70", /* A */
  828. "B4E134D3FB59EB8BAB57274904664D5AF50388BA", /* B */
  829. "100000000000000000000351EE786A818F3A1A16B",/* order */
  830. "52DCB034293A117E1F4FF11B30F7199D3144CE6D", /* Gx */
  831. "FEAFFEF2E331F296E071FA0DF9982CFEA7D43F2E", /* Gy */
  832. ecc_oid_secp160r2, /* oid/oidSz */
  833. ecc_oid_secp160r2_sz,
  834. ECC_SECP160R2_OID, /* oid sum */
  835. 1, /* cofactor */
  836. },
  837. #endif /* HAVE_ECC_SECPR2 */
  838. #ifdef HAVE_ECC_KOBLITZ
  839. {
  840. 20, /* size/bytes */
  841. ECC_SECP160K1, /* ID */
  842. "SECP160K1", /* curve name */
  843. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", /* prime */
  844. "0000000000000000000000000000000000000000", /* A */
  845. "0000000000000000000000000000000000000007", /* B */
  846. "100000000000000000001B8FA16DFAB9ACA16B6B3",/* order */
  847. "3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", /* Gx */
  848. "938CF935318FDCED6BC28286531733C3F03C4FEE", /* Gy */
  849. ecc_oid_secp160k1, /* oid/oidSz */
  850. ecc_oid_secp160k1_sz,
  851. ECC_SECP160K1_OID, /* oid sum */
  852. 1, /* cofactor */
  853. },
  854. #endif /* HAVE_ECC_KOBLITZ */
  855. #endif /* !FP_ECC */
  856. #ifdef HAVE_ECC_BRAINPOOL
  857. {
  858. 20, /* size/bytes */
  859. ECC_BRAINPOOLP160R1, /* ID */
  860. "BRAINPOOLP160R1", /* curve name */
  861. "E95E4A5F737059DC60DFC7AD95B3D8139515620F", /* prime */
  862. "340E7BE2A280EB74E2BE61BADA745D97E8F7C300", /* A */
  863. "1E589A8595423412134FAA2DBDEC95C8D8675E58", /* B */
  864. "E95E4A5F737059DC60DF5991D45029409E60FC09", /* order */
  865. "BED5AF16EA3F6A4F62938C4631EB5AF7BDBCDBC3", /* Gx */
  866. "1667CB477A1A8EC338F94741669C976316DA6321", /* Gy */
  867. ecc_oid_brainpoolp160r1, /* oid/oidSz */
  868. ecc_oid_brainpoolp160r1_sz,
  869. ECC_BRAINPOOLP160R1_OID, /* oid sum */
  870. 1, /* cofactor */
  871. },
  872. #endif /* HAVE_ECC_BRAINPOOL */
  873. #endif /* ECC160 */
  874. #ifdef ECC192
  875. #ifndef NO_ECC_SECP
  876. {
  877. 24, /* size/bytes */
  878. ECC_SECP192R1, /* ID */
  879. "SECP192R1", /* curve name */
  880. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", /* prime */
  881. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", /* A */
  882. "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1", /* B */
  883. "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* order */
  884. "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012", /* Gx */
  885. "7192B95FFC8DA78631011ED6B24CDD573F977A11E794811", /* Gy */
  886. ecc_oid_secp192r1, /* oid/oidSz */
  887. ecc_oid_secp192r1_sz,
  888. ECC_SECP192R1_OID, /* oid sum */
  889. 1, /* cofactor */
  890. },
  891. #endif /* !NO_ECC_SECP */
  892. #ifdef HAVE_ECC_SECPR2
  893. {
  894. 24, /* size/bytes */
  895. ECC_PRIME192V2, /* ID */
  896. "PRIME192V2", /* curve name */
  897. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", /* prime */
  898. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", /* A */
  899. "CC22D6DFB95C6B25E49C0D6364A4E5980C393AA21668D953", /* B */
  900. "FFFFFFFFFFFFFFFFFFFFFFFE5FB1A724DC80418648D8DD31", /* order */
  901. "EEA2BAE7E1497842F2DE7769CFE9C989C072AD696F48034A", /* Gx */
  902. "6574D11D69B6EC7A672BB82A083DF2F2B0847DE970B2DE15", /* Gy */
  903. ecc_oid_prime192v2, /* oid/oidSz */
  904. ecc_oid_prime192v2_sz,
  905. ECC_PRIME192V2_OID, /* oid sum */
  906. 1, /* cofactor */
  907. },
  908. #endif /* HAVE_ECC_SECPR2 */
  909. #ifdef HAVE_ECC_SECPR3
  910. {
  911. 24, /* size/bytes */
  912. ECC_PRIME192V3, /* ID */
  913. "PRIME192V3", /* curve name */
  914. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", /* prime */
  915. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", /* A */
  916. "22123DC2395A05CAA7423DAECCC94760A7D462256BD56916", /* B */
  917. "FFFFFFFFFFFFFFFFFFFFFFFF7A62D031C83F4294F640EC13", /* order */
  918. "7D29778100C65A1DA1783716588DCE2B8B4AEE8E228F1896", /* Gx */
  919. "38A90F22637337334B49DCB66A6DC8F9978ACA7648A943B0", /* Gy */
  920. ecc_oid_prime192v3, /* oid/oidSz */
  921. ecc_oid_prime192v3_sz,
  922. ECC_PRIME192V3_OID, /* oid sum */
  923. 1, /* cofactor */
  924. },
  925. #endif /* HAVE_ECC_SECPR3 */
  926. #ifdef HAVE_ECC_KOBLITZ
  927. {
  928. 24, /* size/bytes */
  929. ECC_SECP192K1, /* ID */
  930. "SECP192K1", /* curve name */
  931. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", /* prime */
  932. "000000000000000000000000000000000000000000000000", /* A */
  933. "000000000000000000000000000000000000000000000003", /* B */
  934. "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", /* order */
  935. "DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", /* Gx */
  936. "9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", /* Gy */
  937. ecc_oid_secp192k1, /* oid/oidSz */
  938. ecc_oid_secp192k1_sz,
  939. ECC_SECP192K1_OID, /* oid sum */
  940. 1, /* cofactor */
  941. },
  942. #endif /* HAVE_ECC_KOBLITZ */
  943. #ifdef HAVE_ECC_BRAINPOOL
  944. {
  945. 24, /* size/bytes */
  946. ECC_BRAINPOOLP192R1, /* ID */
  947. "BRAINPOOLP192R1", /* curve name */
  948. "C302F41D932A36CDA7A3463093D18DB78FCE476DE1A86297", /* prime */
  949. "6A91174076B1E0E19C39C031FE8685C1CAE040E5C69A28EF", /* A */
  950. "469A28EF7C28CCA3DC721D044F4496BCCA7EF4146FBF25C9", /* B */
  951. "C302F41D932A36CDA7A3462F9E9E916B5BE8F1029AC4ACC1", /* order */
  952. "C0A0647EAAB6A48753B033C56CB0F0900A2F5C4853375FD6", /* Gx */
  953. "14B690866ABD5BB88B5F4828C1490002E6773FA2FA299B8F", /* Gy */
  954. ecc_oid_brainpoolp192r1, /* oid/oidSz */
  955. ecc_oid_brainpoolp192r1_sz,
  956. ECC_BRAINPOOLP192R1_OID, /* oid sum */
  957. 1, /* cofactor */
  958. },
  959. #endif /* HAVE_ECC_BRAINPOOL */
  960. #endif /* ECC192 */
  961. #ifdef ECC224
  962. #ifndef NO_ECC_SECP
  963. {
  964. 28, /* size/bytes */
  965. ECC_SECP224R1, /* ID */
  966. "SECP224R1", /* curve name */
  967. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", /* prime */
  968. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", /* A */
  969. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", /* B */
  970. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", /* order */
  971. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", /* Gx */
  972. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", /* Gy */
  973. ecc_oid_secp224r1, /* oid/oidSz */
  974. ecc_oid_secp224r1_sz,
  975. ECC_SECP224R1_OID, /* oid sum */
  976. 1, /* cofactor */
  977. },
  978. #endif /* !NO_ECC_SECP */
  979. #if defined(HAVE_ECC_KOBLITZ) && !defined(FP_ECC)
  980. {
  981. 28, /* size/bytes */
  982. ECC_SECP224K1, /* ID */
  983. "SECP224K1", /* curve name */
  984. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D", /* prime */
  985. "00000000000000000000000000000000000000000000000000000000", /* A */
  986. "00000000000000000000000000000000000000000000000000000005", /* B */
  987. "10000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7",/* order */
  988. "A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C", /* Gx */
  989. "7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5", /* Gy */
  990. ecc_oid_secp224k1, /* oid/oidSz */
  991. ecc_oid_secp224k1_sz,
  992. ECC_SECP224K1_OID, /* oid sum */
  993. 1, /* cofactor */
  994. },
  995. #endif /* HAVE_ECC_KOBLITZ && !FP_ECC */
  996. #ifdef HAVE_ECC_BRAINPOOL
  997. {
  998. 28, /* size/bytes */
  999. ECC_BRAINPOOLP224R1, /* ID */
  1000. "BRAINPOOLP224R1", /* curve name */
  1001. "D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF", /* prime */
  1002. "68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43", /* A */
  1003. "2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B", /* B */
  1004. "D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F", /* order */
  1005. "0D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D", /* Gx */
  1006. "58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD", /* Gy */
  1007. ecc_oid_brainpoolp224r1, /* oid/oidSz */
  1008. ecc_oid_brainpoolp224r1_sz,
  1009. ECC_BRAINPOOLP224R1_OID, /* oid sum */
  1010. 1, /* cofactor */
  1011. },
  1012. #endif /* HAVE_ECC_BRAINPOOL */
  1013. #endif /* ECC224 */
  1014. #ifdef ECC239
  1015. #ifndef NO_ECC_SECP
  1016. {
  1017. 30, /* size/bytes */
  1018. ECC_PRIME239V1, /* ID */
  1019. "PRIME239V1", /* curve name */
  1020. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFF", /* prime */
  1021. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFC", /* A */
  1022. "6B016C3BDCF18941D0D654921475CA71A9DB2FB27D1D37796185C2942C0A", /* B */
  1023. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFF9E5E9A9F5D9071FBD1522688909D0B", /* order */
  1024. "0FFA963CDCA8816CCC33B8642BEDF905C3D358573D3F27FBBD3B3CB9AAAF", /* Gx */
  1025. "7DEBE8E4E90A5DAE6E4054CA530BA04654B36818CE226B39FCCB7B02F1AE", /* Gy */
  1026. ecc_oid_prime239v1, /* oid/oidSz */
  1027. ecc_oid_prime239v1_sz,
  1028. ECC_PRIME239V1_OID, /* oid sum */
  1029. 1, /* cofactor */
  1030. },
  1031. #endif /* !NO_ECC_SECP */
  1032. #ifdef HAVE_ECC_SECPR2
  1033. {
  1034. 30, /* size/bytes */
  1035. ECC_PRIME239V2, /* ID */
  1036. "PRIME239V2", /* curve name */
  1037. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFF", /* prime */
  1038. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFC", /* A */
  1039. "617FAB6832576CBBFED50D99F0249C3FEE58B94BA0038C7AE84C8C832F2C", /* B */
  1040. "7FFFFFFFFFFFFFFFFFFFFFFF800000CFA7E8594377D414C03821BC582063", /* order */
  1041. "38AF09D98727705120C921BB5E9E26296A3CDCF2F35757A0EAFD87B830E7", /* Gx */
  1042. "5B0125E4DBEA0EC7206DA0FC01D9B081329FB555DE6EF460237DFF8BE4BA", /* Gy */
  1043. ecc_oid_prime239v2, /* oid/oidSz */
  1044. ecc_oid_prime239v2_sz,
  1045. ECC_PRIME239V2_OID, /* oid sum */
  1046. 1, /* cofactor */
  1047. },
  1048. #endif /* HAVE_ECC_SECPR2 */
  1049. #ifdef HAVE_ECC_SECPR3
  1050. {
  1051. 30, /* size/bytes */
  1052. ECC_PRIME239V3, /* ID */
  1053. "PRIME239V3", /* curve name */
  1054. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFF", /* prime */
  1055. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFC", /* A */
  1056. "255705FA2A306654B1F4CB03D6A750A30C250102D4988717D9BA15AB6D3E", /* B */
  1057. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFF975DEB41B3A6057C3C432146526551", /* order */
  1058. "6768AE8E18BB92CFCF005C949AA2C6D94853D0E660BBF854B1C9505FE95A", /* Gx */
  1059. "1607E6898F390C06BC1D552BAD226F3B6FCFE48B6E818499AF18E3ED6CF3", /* Gy */
  1060. ecc_oid_prime239v3, /* oid/oidSz */
  1061. ecc_oid_prime239v3_sz,
  1062. ECC_PRIME239V3_OID, /* oid sum */
  1063. 1, /* cofactor */
  1064. },
  1065. #endif /* HAVE_ECC_SECPR3 */
  1066. #endif /* ECC239 */
  1067. #ifdef ECC256
  1068. #ifndef NO_ECC_SECP
  1069. {
  1070. 32, /* size/bytes */
  1071. ECC_SECP256R1, /* ID */
  1072. "SECP256R1", /* curve name */
  1073. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", /* prime */
  1074. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", /* A */
  1075. "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B", /* B */
  1076. "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551", /* order */
  1077. "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296", /* Gx */
  1078. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5", /* Gy */
  1079. ecc_oid_secp256r1, /* oid/oidSz */
  1080. ecc_oid_secp256r1_sz,
  1081. ECC_SECP256R1_OID, /* oid sum */
  1082. 1, /* cofactor */
  1083. },
  1084. #endif /* !NO_ECC_SECP */
  1085. #ifdef HAVE_ECC_KOBLITZ
  1086. {
  1087. 32, /* size/bytes */
  1088. ECC_SECP256K1, /* ID */
  1089. "SECP256K1", /* curve name */
  1090. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", /* prime */
  1091. "0000000000000000000000000000000000000000000000000000000000000000", /* A */
  1092. "0000000000000000000000000000000000000000000000000000000000000007", /* B */
  1093. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", /* order */
  1094. "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", /* Gx */
  1095. "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", /* Gy */
  1096. ecc_oid_secp256k1, /* oid/oidSz */
  1097. ecc_oid_secp256k1_sz,
  1098. ECC_SECP256K1_OID, /* oid sum */
  1099. 1, /* cofactor */
  1100. },
  1101. #endif /* HAVE_ECC_KOBLITZ */
  1102. #ifdef HAVE_ECC_BRAINPOOL
  1103. {
  1104. 32, /* size/bytes */
  1105. ECC_BRAINPOOLP256R1, /* ID */
  1106. "BRAINPOOLP256R1", /* curve name */
  1107. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  1108. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  1109. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  1110. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  1111. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  1112. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  1113. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  1114. ecc_oid_brainpoolp256r1_sz,
  1115. ECC_BRAINPOOLP256R1_OID, /* oid sum */
  1116. 1, /* cofactor */
  1117. },
  1118. #endif /* HAVE_ECC_BRAINPOOL */
  1119. #endif /* ECC256 */
  1120. #if defined(WOLFSSL_SM2)
  1121. {
  1122. 32, /* size/bytes */
  1123. ECC_SM2P256V1, /* ID */
  1124. "SM2P256V1", /* curve name */
  1125. /* bottom of draft-shen-sm2-ecdsa-02, recommended values */
  1126. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", /* prime */
  1127. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", /* A */
  1128. "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", /* B */
  1129. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", /* order */
  1130. "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", /* Gx */
  1131. "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", /* Gy */
  1132. ecc_oid_sm2p256v1, /* oid/oidSz */
  1133. ecc_oid_sm2p256v1_sz,
  1134. ECC_SM2P256V1_OID, /* oid sum */
  1135. 1, /* cofactor */
  1136. },
  1137. #endif /* WOLFSSL_SM2 */
  1138. #ifdef ECC320
  1139. #ifdef HAVE_ECC_BRAINPOOL
  1140. {
  1141. 40, /* size/bytes */
  1142. ECC_BRAINPOOLP320R1, /* ID */
  1143. "BRAINPOOLP320R1", /* curve name */
  1144. "D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E27", /* prime */
  1145. "3EE30B568FBAB0F883CCEBD46D3F3BB8A2A73513F5EB79DA66190EB085FFA9F492F375A97D860EB4", /* A */
  1146. "520883949DFDBC42D3AD198640688A6FE13F41349554B49ACC31DCCD884539816F5EB4AC8FB1F1A6", /* B */
  1147. "D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658E98691555B44C59311", /* order */
  1148. "43BD7E9AFB53D8B85289BCC48EE5BFE6F20137D10A087EB6E7871E2A10A599C710AF8D0D39E20611", /* Gx */
  1149. "14FDD05545EC1CC8AB4093247F77275E0743FFED117182EAA9C77877AAAC6AC7D35245D1692E8EE1", /* Gy */
  1150. ecc_oid_brainpoolp320r1, ecc_oid_brainpoolp320r1_sz, /* oid/oidSz */
  1151. ECC_BRAINPOOLP320R1_OID, /* oid sum */
  1152. 1, /* cofactor */
  1153. },
  1154. #endif /* HAVE_ECC_BRAINPOOL */
  1155. #endif /* ECC320 */
  1156. #ifdef ECC384
  1157. #ifndef NO_ECC_SECP
  1158. {
  1159. 48, /* size/bytes */
  1160. ECC_SECP384R1, /* ID */
  1161. "SECP384R1", /* curve name */
  1162. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF", /* prime */
  1163. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC", /* A */
  1164. "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF", /* B */
  1165. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973", /* order */
  1166. "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7", /* Gx */
  1167. "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F", /* Gy */
  1168. ecc_oid_secp384r1, ecc_oid_secp384r1_sz, /* oid/oidSz */
  1169. ECC_SECP384R1_OID, /* oid sum */
  1170. 1, /* cofactor */
  1171. },
  1172. #endif /* !NO_ECC_SECP */
  1173. #ifdef HAVE_ECC_BRAINPOOL
  1174. {
  1175. 48, /* size/bytes */
  1176. ECC_BRAINPOOLP384R1, /* ID */
  1177. "BRAINPOOLP384R1", /* curve name */
  1178. "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53", /* prime */
  1179. "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F90F8AA5814A503AD4EB04A8C7DD22CE2826", /* A */
  1180. "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62D57CB4390295DBC9943AB78696FA504C11", /* B */
  1181. "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565", /* order */
  1182. "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E", /* Gx */
  1183. "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315", /* Gy */
  1184. ecc_oid_brainpoolp384r1, ecc_oid_brainpoolp384r1_sz, /* oid/oidSz */
  1185. ECC_BRAINPOOLP384R1_OID, /* oid sum */
  1186. 1, /* cofactor */
  1187. },
  1188. #endif /* HAVE_ECC_BRAINPOOL */
  1189. #endif /* ECC384 */
  1190. #ifdef ECC512
  1191. #ifdef HAVE_ECC_BRAINPOOL
  1192. {
  1193. 64, /* size/bytes */
  1194. ECC_BRAINPOOLP512R1, /* ID */
  1195. "BRAINPOOLP512R1", /* curve name */
  1196. "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3", /* prime */
  1197. "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA", /* A */
  1198. "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723", /* B */
  1199. "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069", /* order */
  1200. "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822", /* Gx */
  1201. "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892", /* Gy */
  1202. ecc_oid_brainpoolp512r1, ecc_oid_brainpoolp512r1_sz, /* oid/oidSz */
  1203. ECC_BRAINPOOLP512R1_OID, /* oid sum */
  1204. 1, /* cofactor */
  1205. },
  1206. #endif /* HAVE_ECC_BRAINPOOL */
  1207. #endif /* ECC512 */
  1208. #ifdef ECC521
  1209. #ifndef NO_ECC_SECP
  1210. {
  1211. 66, /* size/bytes */
  1212. ECC_SECP521R1, /* ID */
  1213. "SECP521R1", /* curve name */
  1214. "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* prime */
  1215. "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", /* A */
  1216. "51953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00", /* B */
  1217. "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409", /* order */
  1218. "C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66", /* Gx */
  1219. "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650", /* Gy */
  1220. ecc_oid_secp521r1, ecc_oid_secp521r1_sz, /* oid/oidSz */
  1221. ECC_SECP521R1_OID, /* oid sum */
  1222. 1, /* cofactor */
  1223. },
  1224. #endif /* !NO_ECC_SECP */
  1225. #endif /* ECC521 */
  1226. #ifdef WOLFCRYPT_HAVE_SAKKE
  1227. {
  1228. 128,
  1229. ECC_SAKKE_1,
  1230. "SAKKE1",
  1231. "997ABB1F0A563FDA65C61198DAD0657A416C0CE19CB48261BE9AE358B3E01A2EF40AAB27E2FC0F1B228730D531A59CB0E791B39FF7C88A19356D27F4A666A6D0E26C6487326B4CD4512AC5CD65681CE1B6AFF4A831852A82A7CF3C521C3C09AA9F94D6AF56971F1FFCE3E82389857DB080C5DF10AC7ACE87666D807AFEA85FEB",
  1232. "997ABB1F0A563FDA65C61198DAD0657A416C0CE19CB48261BE9AE358B3E01A2EF40AAB27E2FC0F1B228730D531A59CB0E791B39FF7C88A19356D27F4A666A6D0E26C6487326B4CD4512AC5CD65681CE1B6AFF4A831852A82A7CF3C521C3C09AA9F94D6AF56971F1FFCE3E82389857DB080C5DF10AC7ACE87666D807AFEA85FE8",
  1233. "0",
  1234. "265EAEC7C2958FF69971846636B4195E905B0338672D20986FA6B8D62CF8068BBD02AAC9F8BF03C6C8A1CC354C69672C39E46CE7FDF222864D5B49FD2999A9B4389B1921CC9AD335144AB173595A07386DABFD2A0C614AA0A9F3CF14870F026AA7E535ABD5A5C7C7FF38FA08E2615F6C203177C42B1EB3A1D99B601EBFAA17FB",
  1235. "53FC09EE332C29AD0A7990053ED9B52A2B1A2FD60AEC69C698B2F204B6FF7CBFB5EDB6C0F6CE2308AB10DB9030B09E1043D5F22CDB9DFA55718BD9E7406CE8909760AF765DD5BCCB337C86548B72F2E1A702C3397A60DE74A7C1514DBA66910DD5CFB4CC80728D87EE9163A5B63F73EC80EC46C4967E0979880DC8ABEAE63895",
  1236. "0A8249063F6009F1F9F1F0533634A135D3E82016029906963D778D821E141178F5EA69F4654EC2B9E7F7F5E5F0DE55F66B598CCF9A140B2E416CFF0CA9E032B970DAE117AD547C6CCAD696B5B7652FE0AC6F1E80164AA989492D979FC5A4D5F213515AD7E9CB99A980BDAD5AD5BB4636ADB9B5706A67DCDE75573FD71BEF16D7",
  1237. #ifndef WOLFSSL_ECC_CURVE_STATIC
  1238. NULL, 0,
  1239. #else
  1240. {0}, 0,
  1241. #endif
  1242. 0,
  1243. 4,
  1244. },
  1245. #endif
  1246. #if defined(WOLFSSL_CUSTOM_CURVES) && defined(ECC_CACHE_CURVE)
  1247. /* place holder for custom curve index for cache */
  1248. {
  1249. 1, /* non-zero */
  1250. ECC_CURVE_CUSTOM,
  1251. #ifndef WOLFSSL_ECC_CURVE_STATIC
  1252. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1253. #else
  1254. {0},{0},{0},{0},{0},{0},{0},{0},
  1255. #endif
  1256. 0, 0, 0
  1257. },
  1258. #endif
  1259. {
  1260. 0,
  1261. ECC_CURVE_INVALID,
  1262. #ifndef WOLFSSL_ECC_CURVE_STATIC
  1263. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1264. #else
  1265. {0},{0},{0},{0},{0},{0},{0},{0},
  1266. #endif
  1267. 0, 0, 0
  1268. }
  1269. };
  1270. #define ECC_SET_COUNT (sizeof(ecc_sets)/sizeof(ecc_set_type))
  1271. const size_t ecc_sets_count = ECC_SET_COUNT - 1;
  1272. #ifdef HAVE_OID_ENCODING
  1273. /* encoded OID cache */
  1274. typedef struct {
  1275. word32 oidSz;
  1276. byte oid[ECC_MAX_OID_LEN];
  1277. } oid_cache_t;
  1278. static oid_cache_t ecc_oid_cache[ECC_SET_COUNT];
  1279. #endif
  1280. /* Forward declarations */
  1281. #if defined(HAVE_COMP_KEY) && defined(HAVE_ECC_KEY_EXPORT)
  1282. static int wc_ecc_export_x963_compressed(ecc_key* key, byte* out, word32* outLen);
  1283. #endif
  1284. #ifdef HAVE_ECC_CHECK_PUBKEY_ORDER
  1285. static int ecc_check_pubkey_order(ecc_key* key, ecc_point* pubkey, mp_int* a,
  1286. mp_int* prime, mp_int* order);
  1287. #endif
  1288. static int _ecc_validate_public_key(ecc_key* key, int partial, int priv);
  1289. #if (FIPS_VERSION_GE(5,0) || defined(WOLFSSL_VALIDATE_ECC_KEYGEN)) && \
  1290. !defined(WOLFSSL_KCAPI_ECC)
  1291. static int _ecc_pairwise_consistency_test(ecc_key* key, WC_RNG* rng);
  1292. #endif
  1293. #ifdef HAVE_COMP_KEY
  1294. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  1295. !defined(WOLFSSL_CRYPTOCELL)
  1296. #ifndef WOLFSSL_SP_MATH
  1297. #if !defined(SQRTMOD_USE_MOD_EXP)
  1298. static int mp_jacobi(mp_int* a, mp_int* n, int* c);
  1299. #endif
  1300. static int mp_sqrtmod_prime(mp_int* n, mp_int* prime, mp_int* ret);
  1301. #endif
  1302. #endif
  1303. #endif
  1304. /* Curve Specs */
  1305. typedef struct ecc_curve_spec {
  1306. const ecc_set_type* dp;
  1307. mp_int* prime;
  1308. mp_int* Af;
  1309. #ifdef USE_ECC_B_PARAM
  1310. mp_int* Bf;
  1311. #endif
  1312. mp_int* order;
  1313. mp_int* Gx;
  1314. mp_int* Gy;
  1315. #ifdef ECC_CACHE_CURVE
  1316. mp_int prime_lcl;
  1317. mp_int Af_lcl;
  1318. #ifdef USE_ECC_B_PARAM
  1319. mp_int Bf_lcl;
  1320. #endif
  1321. mp_int order_lcl;
  1322. mp_int Gx_lcl;
  1323. mp_int Gy_lcl;
  1324. #else
  1325. #ifdef WOLFSSL_SP_MATH_ALL
  1326. unsigned char* spec_ints;
  1327. #else
  1328. mp_int* spec_ints;
  1329. #endif
  1330. word32 spec_count;
  1331. word32 spec_use;
  1332. #endif
  1333. byte load_mask;
  1334. } ecc_curve_spec;
  1335. #define ECC_CURVE_FIELD_NONE 0x00
  1336. #define ECC_CURVE_FIELD_PRIME 0x01
  1337. #define ECC_CURVE_FIELD_AF 0x02
  1338. #ifdef USE_ECC_B_PARAM
  1339. #define ECC_CURVE_FIELD_BF 0x04
  1340. #endif
  1341. #define ECC_CURVE_FIELD_ORDER 0x08
  1342. #define ECC_CURVE_FIELD_GX 0x10
  1343. #define ECC_CURVE_FIELD_GY 0x20
  1344. #ifdef USE_ECC_B_PARAM
  1345. #define ECC_CURVE_FIELD_ALL 0x3F
  1346. #define ECC_CURVE_FIELD_COUNT 6
  1347. #else
  1348. #define ECC_CURVE_FIELD_ALL 0x3B
  1349. #define ECC_CURVE_FIELD_COUNT 5
  1350. #endif
  1351. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  1352. static const u32 xil_curve_type[ECC_CURVE_MAX] = {
  1353. [ECC_SECP384R1] = WOLFSSL_XSECURE_ECC_NIST_P384,
  1354. [ECC_SECP521R1] = WOLFSSL_XSECURE_ECC_NIST_P521,
  1355. };
  1356. static void buf_reverse(byte *outbuf, const byte *inbuf, word32 len)
  1357. {
  1358. word32 up, down;
  1359. up = 0;
  1360. down = len - 1;
  1361. while (up < len)
  1362. outbuf[up++] = inbuf[down--];
  1363. }
  1364. static int xil_mpi_import(mp_int *mpi,
  1365. const byte *inbuf,
  1366. word32 len,
  1367. void* heap)
  1368. {
  1369. int err;
  1370. #ifdef WOLFSSL_SMALL_STACK
  1371. byte* buf = NULL;
  1372. #else
  1373. byte buf[MAX_ECC_BYTES];
  1374. if (len > MAX_ECC_BYTES)
  1375. return BUFFER_E;
  1376. #endif
  1377. #ifdef WOLFSSL_SMALL_STACK
  1378. buf = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  1379. if (buf == NULL)
  1380. return MEMORY_E;
  1381. #endif
  1382. buf_reverse(buf, inbuf, len);
  1383. err = mp_read_unsigned_bin(mpi, buf, len);
  1384. ForceZero(buf, len);
  1385. #ifdef WOLFSSL_SMALL_STACK
  1386. XFREE(buf, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  1387. #endif
  1388. return err;
  1389. }
  1390. #endif
  1391. #ifdef ECC_CACHE_CURVE
  1392. /* cache (mp_int) of the curve parameters */
  1393. static ecc_curve_spec* ecc_curve_spec_cache[ECC_SET_COUNT];
  1394. #ifndef SINGLE_THREADED
  1395. static wolfSSL_Mutex ecc_curve_cache_mutex;
  1396. #endif
  1397. #define DECLARE_CURVE_SPECS(intcount) ecc_curve_spec* curve = NULL
  1398. #define ALLOC_CURVE_SPECS(intcount, err) WC_DO_NOTHING
  1399. #define FREE_CURVE_SPECS() WC_DO_NOTHING
  1400. #elif defined(WOLFSSL_SMALL_STACK)
  1401. #ifdef WOLFSSL_SP_MATH_ALL
  1402. #define DECLARE_CURVE_SPECS(intcount) \
  1403. unsigned char* spec_ints = NULL; \
  1404. ecc_curve_spec curve_lcl; \
  1405. ecc_curve_spec* curve = &curve_lcl; \
  1406. XMEMSET(curve, 0, sizeof(ecc_curve_spec)); \
  1407. curve->spec_count = intcount
  1408. #define ALLOC_CURVE_SPECS(intcount, err) \
  1409. spec_ints = (unsigned char*)XMALLOC(MP_INT_SIZEOF(MP_BITS_CNT( \
  1410. MAX_ECC_BITS_USE)) * (intcount), NULL, \
  1411. DYNAMIC_TYPE_ECC); \
  1412. if (spec_ints == NULL) \
  1413. (err) = MEMORY_E; \
  1414. else \
  1415. curve->spec_ints = spec_ints
  1416. #else
  1417. #define DECLARE_CURVE_SPECS(intcount) \
  1418. mp_int* spec_ints = NULL; \
  1419. ecc_curve_spec curve_lcl; \
  1420. ecc_curve_spec* curve = &curve_lcl; \
  1421. XMEMSET(curve, 0, sizeof(ecc_curve_spec)); \
  1422. curve->spec_count = intcount
  1423. #define ALLOC_CURVE_SPECS(intcount, err) \
  1424. spec_ints = (mp_int*)XMALLOC(sizeof(mp_int) * (intcount), NULL, \
  1425. DYNAMIC_TYPE_ECC); \
  1426. if (spec_ints == NULL) \
  1427. (err) = MEMORY_E; \
  1428. else \
  1429. curve->spec_ints = spec_ints
  1430. #endif
  1431. #define FREE_CURVE_SPECS() \
  1432. XFREE(spec_ints, NULL, DYNAMIC_TYPE_ECC)
  1433. #else
  1434. #ifdef WOLFSSL_SP_MATH_ALL
  1435. #define DECLARE_CURVE_SPECS(intcount) \
  1436. unsigned char spec_ints[MP_INT_SIZEOF(MP_BITS_CNT( \
  1437. MAX_ECC_BITS_USE)) * (intcount)]; \
  1438. ecc_curve_spec curve_lcl; \
  1439. ecc_curve_spec* curve = &curve_lcl; \
  1440. XMEMSET(curve, 0, sizeof(ecc_curve_spec)); \
  1441. curve->spec_ints = spec_ints; \
  1442. curve->spec_count = (intcount)
  1443. #else
  1444. #define DECLARE_CURVE_SPECS(intcount) \
  1445. mp_int spec_ints[(intcount)]; \
  1446. ecc_curve_spec curve_lcl; \
  1447. ecc_curve_spec* curve = &curve_lcl; \
  1448. XMEMSET(curve, 0, sizeof(ecc_curve_spec)); \
  1449. curve->spec_ints = spec_ints; \
  1450. curve->spec_count = (intcount)
  1451. #endif
  1452. #define ALLOC_CURVE_SPECS(intcount, err) WC_DO_NOTHING
  1453. #define FREE_CURVE_SPECS() WC_DO_NOTHING
  1454. #endif /* ECC_CACHE_CURVE */
  1455. static void wc_ecc_curve_cache_free_spec_item(ecc_curve_spec* curve, mp_int* item,
  1456. byte mask)
  1457. {
  1458. if (item) {
  1459. #ifdef HAVE_WOLF_BIGINT
  1460. wc_bigint_free(&item->raw);
  1461. #endif
  1462. mp_clear(item);
  1463. }
  1464. curve->load_mask &= ~mask;
  1465. }
  1466. static void wc_ecc_curve_cache_free_spec(ecc_curve_spec* curve)
  1467. {
  1468. if (curve == NULL) {
  1469. return;
  1470. }
  1471. if (curve->load_mask & ECC_CURVE_FIELD_PRIME)
  1472. wc_ecc_curve_cache_free_spec_item(curve, curve->prime, ECC_CURVE_FIELD_PRIME);
  1473. if (curve->load_mask & ECC_CURVE_FIELD_AF)
  1474. wc_ecc_curve_cache_free_spec_item(curve, curve->Af, ECC_CURVE_FIELD_AF);
  1475. #ifdef USE_ECC_B_PARAM
  1476. if (curve->load_mask & ECC_CURVE_FIELD_BF)
  1477. wc_ecc_curve_cache_free_spec_item(curve, curve->Bf, ECC_CURVE_FIELD_BF);
  1478. #endif
  1479. if (curve->load_mask & ECC_CURVE_FIELD_ORDER)
  1480. wc_ecc_curve_cache_free_spec_item(curve, curve->order, ECC_CURVE_FIELD_ORDER);
  1481. if (curve->load_mask & ECC_CURVE_FIELD_GX)
  1482. wc_ecc_curve_cache_free_spec_item(curve, curve->Gx, ECC_CURVE_FIELD_GX);
  1483. if (curve->load_mask & ECC_CURVE_FIELD_GY)
  1484. wc_ecc_curve_cache_free_spec_item(curve, curve->Gy, ECC_CURVE_FIELD_GY);
  1485. curve->load_mask = 0;
  1486. }
  1487. static void wc_ecc_curve_free(ecc_curve_spec* curve)
  1488. {
  1489. if (curve) {
  1490. #ifdef ECC_CACHE_CURVE
  1491. #ifdef WOLFSSL_CUSTOM_CURVES
  1492. /* only free custom curves (rest are globally cached) */
  1493. if (curve->dp && curve->dp->id == ECC_CURVE_CUSTOM) {
  1494. wc_ecc_curve_cache_free_spec(curve);
  1495. XFREE(curve, NULL, DYNAMIC_TYPE_ECC);
  1496. }
  1497. #endif
  1498. #else
  1499. wc_ecc_curve_cache_free_spec(curve);
  1500. #endif
  1501. }
  1502. }
  1503. static int wc_ecc_curve_cache_load_item(ecc_curve_spec* curve, const char* src,
  1504. mp_int** dst, byte mask)
  1505. {
  1506. int err;
  1507. #ifndef ECC_CACHE_CURVE
  1508. /* get mp_int from temp */
  1509. if (curve->spec_use >= curve->spec_count) {
  1510. WOLFSSL_MSG("Invalid DECLARE_CURVE_SPECS count");
  1511. return ECC_BAD_ARG_E;
  1512. }
  1513. #ifdef WOLFSSL_SP_MATH_ALL
  1514. *dst = (mp_int*)(curve->spec_ints + MP_INT_SIZEOF(MP_BITS_CNT(
  1515. MAX_ECC_BITS_USE)) * curve->spec_use++);
  1516. #else
  1517. *dst = &curve->spec_ints[curve->spec_use++];
  1518. #endif
  1519. #endif
  1520. #ifdef WOLFSSL_SP_MATH_ALL
  1521. err = mp_init_size(*dst, MP_BITS_CNT(MAX_ECC_BITS_USE));
  1522. #else
  1523. err = mp_init(*dst);
  1524. #endif
  1525. if (err == MP_OKAY) {
  1526. curve->load_mask |= mask;
  1527. err = mp_read_radix(*dst, src, MP_RADIX_HEX);
  1528. #ifdef HAVE_WOLF_BIGINT
  1529. if (err == MP_OKAY)
  1530. err = wc_mp_to_bigint(*dst, &(*dst)->raw);
  1531. #endif
  1532. }
  1533. return err;
  1534. }
  1535. static int wc_ecc_curve_load(const ecc_set_type* dp, ecc_curve_spec** pCurve,
  1536. byte load_mask)
  1537. {
  1538. int ret = 0;
  1539. ecc_curve_spec* curve;
  1540. byte load_items = 0; /* mask of items to load */
  1541. #ifdef ECC_CACHE_CURVE
  1542. int x;
  1543. #endif
  1544. if (dp == NULL || pCurve == NULL)
  1545. return BAD_FUNC_ARG;
  1546. #ifdef ECC_CACHE_CURVE
  1547. x = wc_ecc_get_curve_idx(dp->id);
  1548. if (x == ECC_CURVE_INVALID)
  1549. return ECC_BAD_ARG_E;
  1550. #if !defined(SINGLE_THREADED)
  1551. ret = wc_LockMutex(&ecc_curve_cache_mutex);
  1552. if (ret != 0) {
  1553. return ret;
  1554. }
  1555. #endif
  1556. /* make sure cache has been allocated */
  1557. if (ecc_curve_spec_cache[x] == NULL
  1558. #ifdef WOLFSSL_CUSTOM_CURVES
  1559. || dp->id == ECC_CURVE_CUSTOM
  1560. #endif
  1561. ) {
  1562. curve = (ecc_curve_spec*)XMALLOC(sizeof(ecc_curve_spec), NULL, DYNAMIC_TYPE_ECC);
  1563. if (curve == NULL) {
  1564. #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED)
  1565. wc_UnLockMutex(&ecc_curve_cache_mutex);
  1566. #endif
  1567. return MEMORY_E;
  1568. }
  1569. XMEMSET(curve, 0, sizeof(ecc_curve_spec));
  1570. /* set curve pointer to cache */
  1571. #ifdef WOLFSSL_CUSTOM_CURVES
  1572. if (dp->id != ECC_CURVE_CUSTOM)
  1573. #endif
  1574. {
  1575. ecc_curve_spec_cache[x] = curve;
  1576. }
  1577. }
  1578. else {
  1579. curve = ecc_curve_spec_cache[x];
  1580. }
  1581. /* return new or cached curve */
  1582. *pCurve = curve;
  1583. #else
  1584. curve = *pCurve;
  1585. #endif /* ECC_CACHE_CURVE */
  1586. /* make sure the curve is initialized */
  1587. if (curve->dp != dp) {
  1588. curve->load_mask = 0;
  1589. #ifdef ECC_CACHE_CURVE
  1590. curve->prime = &curve->prime_lcl;
  1591. curve->Af = &curve->Af_lcl;
  1592. #ifdef USE_ECC_B_PARAM
  1593. curve->Bf = &curve->Bf_lcl;
  1594. #endif
  1595. curve->order = &curve->order_lcl;
  1596. curve->Gx = &curve->Gx_lcl;
  1597. curve->Gy = &curve->Gy_lcl;
  1598. #endif
  1599. }
  1600. curve->dp = dp; /* set dp info */
  1601. /* determine items to load */
  1602. load_items = (byte)(((byte)~(word32)curve->load_mask) & load_mask);
  1603. curve->load_mask |= load_items;
  1604. /* load items */
  1605. if (load_items & ECC_CURVE_FIELD_PRIME)
  1606. ret += wc_ecc_curve_cache_load_item(curve, dp->prime, &curve->prime,
  1607. ECC_CURVE_FIELD_PRIME);
  1608. if (load_items & ECC_CURVE_FIELD_AF)
  1609. ret += wc_ecc_curve_cache_load_item(curve, dp->Af, &curve->Af,
  1610. ECC_CURVE_FIELD_AF);
  1611. #ifdef USE_ECC_B_PARAM
  1612. if (load_items & ECC_CURVE_FIELD_BF)
  1613. ret += wc_ecc_curve_cache_load_item(curve, dp->Bf, &curve->Bf,
  1614. ECC_CURVE_FIELD_BF);
  1615. #endif
  1616. if (load_items & ECC_CURVE_FIELD_ORDER)
  1617. ret += wc_ecc_curve_cache_load_item(curve, dp->order, &curve->order,
  1618. ECC_CURVE_FIELD_ORDER);
  1619. if (load_items & ECC_CURVE_FIELD_GX)
  1620. ret += wc_ecc_curve_cache_load_item(curve, dp->Gx, &curve->Gx,
  1621. ECC_CURVE_FIELD_GX);
  1622. if (load_items & ECC_CURVE_FIELD_GY)
  1623. ret += wc_ecc_curve_cache_load_item(curve, dp->Gy, &curve->Gy,
  1624. ECC_CURVE_FIELD_GY);
  1625. /* check for error */
  1626. if (ret != 0) {
  1627. wc_ecc_curve_free(curve);
  1628. ret = MP_READ_E;
  1629. }
  1630. #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED)
  1631. wc_UnLockMutex(&ecc_curve_cache_mutex);
  1632. #endif
  1633. return ret;
  1634. }
  1635. #ifdef ECC_CACHE_CURVE
  1636. int wc_ecc_curve_cache_init(void)
  1637. {
  1638. int ret = 0;
  1639. #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED)
  1640. ret = wc_InitMutex(&ecc_curve_cache_mutex);
  1641. #endif
  1642. return ret;
  1643. }
  1644. void wc_ecc_curve_cache_free(void)
  1645. {
  1646. int x;
  1647. /* free all ECC curve caches */
  1648. for (x = 0; x < (int)ECC_SET_COUNT; x++) {
  1649. if (ecc_curve_spec_cache[x]) {
  1650. wc_ecc_curve_cache_free_spec(ecc_curve_spec_cache[x]);
  1651. XFREE(ecc_curve_spec_cache[x], NULL, DYNAMIC_TYPE_ECC);
  1652. ecc_curve_spec_cache[x] = NULL;
  1653. }
  1654. }
  1655. #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED)
  1656. wc_FreeMutex(&ecc_curve_cache_mutex);
  1657. #endif
  1658. }
  1659. #endif /* ECC_CACHE_CURVE */
  1660. /* Retrieve the curve name for the ECC curve id.
  1661. *
  1662. * curve_id The id of the curve.
  1663. * returns the name stored from the curve if available, otherwise NULL.
  1664. */
  1665. const char* wc_ecc_get_name(int curve_id)
  1666. {
  1667. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  1668. if (curve_idx == ECC_CURVE_INVALID)
  1669. return NULL;
  1670. return ecc_sets[curve_idx].name;
  1671. }
  1672. int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id)
  1673. {
  1674. if (key == NULL || (keysize <= 0 && curve_id < 0)) {
  1675. return BAD_FUNC_ARG;
  1676. }
  1677. if (keysize > ECC_MAXSIZE) {
  1678. return ECC_BAD_ARG_E;
  1679. }
  1680. /* handle custom case */
  1681. if (key->idx != ECC_CUSTOM_IDX) {
  1682. int x;
  1683. /* default values */
  1684. key->idx = 0;
  1685. key->dp = NULL;
  1686. /* find ecc_set based on curve_id or key size */
  1687. for (x = 0; ecc_sets[x].size != 0; x++) {
  1688. if (curve_id > ECC_CURVE_DEF) {
  1689. if (curve_id == ecc_sets[x].id)
  1690. break;
  1691. }
  1692. else if (keysize <= ecc_sets[x].size) {
  1693. break;
  1694. }
  1695. }
  1696. if (ecc_sets[x].size == 0) {
  1697. WOLFSSL_MSG("ECC Curve not found");
  1698. return ECC_CURVE_OID_E;
  1699. }
  1700. key->idx = x;
  1701. key->dp = &ecc_sets[x];
  1702. }
  1703. return 0;
  1704. }
  1705. #ifdef ALT_ECC_SIZE
  1706. static void alt_fp_init(mp_int* a)
  1707. {
  1708. a->size = FP_SIZE_ECC;
  1709. mp_zero(a);
  1710. }
  1711. #endif /* ALT_ECC_SIZE */
  1712. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  1713. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  1714. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_PUBLIC_ECC_ADD_DBL)
  1715. static int _ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a,
  1716. mp_int* modulus, mp_digit mp);
  1717. /**
  1718. Add two ECC points
  1719. P The point to add
  1720. Q The point to add
  1721. R [out] The destination of the double
  1722. a ECC curve parameter a
  1723. modulus The modulus of the field the ECC curve is in
  1724. mp The "b" value from montgomery_setup()
  1725. return MP_OKAY on success
  1726. */
  1727. static int _ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
  1728. mp_int* a, mp_int* modulus, mp_digit mp)
  1729. {
  1730. #if !defined(WOLFSSL_SP_MATH)
  1731. DECL_MP_INT_SIZE_DYN(t1, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  1732. DECL_MP_INT_SIZE_DYN(t2, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  1733. #ifdef ALT_ECC_SIZE
  1734. DECL_MP_INT_SIZE_DYN(rx, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  1735. DECL_MP_INT_SIZE_DYN(ry, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  1736. DECL_MP_INT_SIZE_DYN(rz, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  1737. #endif
  1738. mp_int *x, *y, *z;
  1739. int err;
  1740. /* if Q == R then swap P and Q, so we don't require a local x,y,z */
  1741. if (Q == R) {
  1742. ecc_point* tPt = P;
  1743. P = Q;
  1744. Q = tPt;
  1745. }
  1746. #ifdef WOLFSSL_SMALL_STACK
  1747. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1748. if (R->key != NULL) {
  1749. t1 = R->key->t1;
  1750. t2 = R->key->t2;
  1751. #ifdef ALT_ECC_SIZE
  1752. rx = R->key->x;
  1753. ry = R->key->y;
  1754. rz = R->key->z;
  1755. #endif
  1756. }
  1757. else
  1758. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  1759. #endif /* WOLFSSL_SMALL_STACK */
  1760. {
  1761. NEW_MP_INT_SIZE(t1, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  1762. NEW_MP_INT_SIZE(t2, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  1763. #ifdef MP_INT_SIZE_CHECK_NULL
  1764. if (t1 == NULL || t2 == NULL) {
  1765. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  1766. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  1767. return MEMORY_E;
  1768. }
  1769. #endif
  1770. #ifdef ALT_ECC_SIZE
  1771. NEW_MP_INT_SIZE(rx, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  1772. NEW_MP_INT_SIZE(ry, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  1773. NEW_MP_INT_SIZE(rz, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  1774. #ifdef MP_INT_SIZE_CHECK_NULL
  1775. if (rx == NULL || ry == NULL || rz == NULL) {
  1776. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  1777. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  1778. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  1779. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  1780. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  1781. return MEMORY_E;
  1782. }
  1783. #endif
  1784. #endif
  1785. }
  1786. err = INIT_MP_INT_SIZE(t1, mp_bitsused(modulus));
  1787. if (err == MP_OKAY) {
  1788. err = INIT_MP_INT_SIZE(t2, mp_bitsused(modulus));
  1789. }
  1790. if (err != MP_OKAY) {
  1791. #ifdef WOLFSSL_SMALL_STACK
  1792. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1793. if (R->key == NULL)
  1794. #endif
  1795. #endif
  1796. {
  1797. #ifdef ALT_ECC_SIZE
  1798. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  1799. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  1800. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  1801. #endif
  1802. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  1803. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  1804. }
  1805. return err;
  1806. }
  1807. /* should we dbl instead? */
  1808. if (err == MP_OKAY) {
  1809. #ifdef ECC_TIMING_RESISTANT
  1810. err = mp_submod_ct(modulus, Q->y, modulus, t1);
  1811. #else
  1812. err = mp_sub(modulus, Q->y, t1);
  1813. #endif
  1814. }
  1815. if (err == MP_OKAY) {
  1816. if ( (mp_cmp(P->x, Q->x) == MP_EQ) &&
  1817. (get_digit_count(Q->z) && mp_cmp(P->z, Q->z) == MP_EQ) &&
  1818. (mp_cmp(P->y, Q->y) == MP_EQ || mp_cmp(P->y, t1) == MP_EQ)) {
  1819. mp_clear(t1);
  1820. mp_clear(t2);
  1821. #ifdef WOLFSSL_SMALL_STACK
  1822. #ifdef WOLFSSL_SMALL_STACK_CACHE
  1823. if (R->key == NULL)
  1824. #endif
  1825. #endif
  1826. {
  1827. #ifdef ALT_ECC_SIZE
  1828. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  1829. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  1830. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  1831. #endif
  1832. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  1833. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  1834. }
  1835. return _ecc_projective_dbl_point(P, R, a, modulus, mp);
  1836. }
  1837. }
  1838. if (err != MP_OKAY) {
  1839. goto done;
  1840. }
  1841. /* If use ALT_ECC_SIZE we need to use local stack variable since
  1842. ecc_point x,y,z is reduced size */
  1843. #ifdef ALT_ECC_SIZE
  1844. /* Use local stack variable */
  1845. x = rx;
  1846. y = ry;
  1847. z = rz;
  1848. err = INIT_MP_INT_SIZE(x, mp_bitsused(modulus));
  1849. if (err == MP_OKAY) {
  1850. err = INIT_MP_INT_SIZE(y, mp_bitsused(modulus));
  1851. }
  1852. if (err == MP_OKAY) {
  1853. err = INIT_MP_INT_SIZE(z, mp_bitsused(modulus));
  1854. }
  1855. if (err != MP_OKAY) {
  1856. goto done;
  1857. }
  1858. #else
  1859. /* Use destination directly */
  1860. x = R->x;
  1861. y = R->y;
  1862. z = R->z;
  1863. #endif
  1864. if (err == MP_OKAY)
  1865. err = mp_copy(P->x, x);
  1866. if (err == MP_OKAY)
  1867. err = mp_copy(P->y, y);
  1868. if (err == MP_OKAY)
  1869. err = mp_copy(P->z, z);
  1870. /* if Z is one then these are no-operations */
  1871. if (err == MP_OKAY) {
  1872. if (!mp_iszero(Q->z)) {
  1873. /* T1 = Z' * Z' */
  1874. err = mp_sqr(Q->z, t1);
  1875. if (err == MP_OKAY)
  1876. err = mp_montgomery_reduce(t1, modulus, mp);
  1877. /* X = X * T1 */
  1878. if (err == MP_OKAY)
  1879. err = mp_mul(t1, x, x);
  1880. if (err == MP_OKAY)
  1881. err = mp_montgomery_reduce(x, modulus, mp);
  1882. /* T1 = Z' * T1 */
  1883. if (err == MP_OKAY)
  1884. err = mp_mul(Q->z, t1, t1);
  1885. if (err == MP_OKAY)
  1886. err = mp_montgomery_reduce(t1, modulus, mp);
  1887. /* Y = Y * T1 */
  1888. if (err == MP_OKAY)
  1889. err = mp_mul(t1, y, y);
  1890. if (err == MP_OKAY)
  1891. err = mp_montgomery_reduce(y, modulus, mp);
  1892. }
  1893. }
  1894. /* T1 = Z*Z */
  1895. if (err == MP_OKAY)
  1896. err = mp_sqr(z, t1);
  1897. if (err == MP_OKAY)
  1898. err = mp_montgomery_reduce(t1, modulus, mp);
  1899. /* T2 = X' * T1 */
  1900. if (err == MP_OKAY)
  1901. err = mp_mul(Q->x, t1, t2);
  1902. if (err == MP_OKAY)
  1903. err = mp_montgomery_reduce(t2, modulus, mp);
  1904. /* T1 = Z * T1 */
  1905. if (err == MP_OKAY)
  1906. err = mp_mul(z, t1, t1);
  1907. if (err == MP_OKAY)
  1908. err = mp_montgomery_reduce(t1, modulus, mp);
  1909. /* T1 = Y' * T1 */
  1910. if (err == MP_OKAY)
  1911. err = mp_mul(Q->y, t1, t1);
  1912. if (err == MP_OKAY)
  1913. err = mp_montgomery_reduce(t1, modulus, mp);
  1914. /* Y = Y - T1 */
  1915. if (err == MP_OKAY)
  1916. err = mp_submod_ct(y, t1, modulus, y);
  1917. /* T1 = 2T1 */
  1918. if (err == MP_OKAY)
  1919. err = mp_addmod_ct(t1, t1, modulus, t1);
  1920. /* T1 = Y + T1 */
  1921. if (err == MP_OKAY)
  1922. err = mp_addmod_ct(t1, y, modulus, t1);
  1923. /* X = X - T2 */
  1924. if (err == MP_OKAY)
  1925. err = mp_submod_ct(x, t2, modulus, x);
  1926. /* T2 = 2T2 */
  1927. if (err == MP_OKAY)
  1928. err = mp_addmod_ct(t2, t2, modulus, t2);
  1929. /* T2 = X + T2 */
  1930. if (err == MP_OKAY)
  1931. err = mp_addmod_ct(t2, x, modulus, t2);
  1932. if (err == MP_OKAY) {
  1933. if (!mp_iszero(Q->z)) {
  1934. /* Z = Z * Z' */
  1935. err = mp_mul(z, Q->z, z);
  1936. if (err == MP_OKAY)
  1937. err = mp_montgomery_reduce(z, modulus, mp);
  1938. }
  1939. }
  1940. /* Z = Z * X */
  1941. if (err == MP_OKAY)
  1942. err = mp_mul(z, x, z);
  1943. if (err == MP_OKAY)
  1944. err = mp_montgomery_reduce(z, modulus, mp);
  1945. /* T1 = T1 * X */
  1946. if (err == MP_OKAY)
  1947. err = mp_mul(t1, x, t1);
  1948. if (err == MP_OKAY)
  1949. err = mp_montgomery_reduce(t1, modulus, mp);
  1950. /* X = X * X */
  1951. if (err == MP_OKAY)
  1952. err = mp_sqr(x, x);
  1953. if (err == MP_OKAY)
  1954. err = mp_montgomery_reduce(x, modulus, mp);
  1955. /* T2 = T2 * x */
  1956. if (err == MP_OKAY)
  1957. err = mp_mul(t2, x, t2);
  1958. if (err == MP_OKAY)
  1959. err = mp_montgomery_reduce(t2, modulus, mp);
  1960. /* T1 = T1 * X */
  1961. if (err == MP_OKAY)
  1962. err = mp_mul(t1, x, t1);
  1963. if (err == MP_OKAY)
  1964. err = mp_montgomery_reduce(t1, modulus, mp);
  1965. /* X = Y*Y */
  1966. if (err == MP_OKAY)
  1967. err = mp_sqr(y, x);
  1968. if (err == MP_OKAY)
  1969. err = mp_montgomery_reduce(x, modulus, mp);
  1970. /* X = X - T2 */
  1971. if (err == MP_OKAY)
  1972. err = mp_submod_ct(x, t2, modulus, x);
  1973. /* T2 = T2 - X */
  1974. if (err == MP_OKAY)
  1975. err = mp_submod_ct(t2, x, modulus, t2);
  1976. /* T2 = T2 - X */
  1977. if (err == MP_OKAY)
  1978. err = mp_submod_ct(t2, x, modulus, t2);
  1979. /* T2 = T2 * Y */
  1980. if (err == MP_OKAY)
  1981. err = mp_mul(t2, y, t2);
  1982. if (err == MP_OKAY)
  1983. err = mp_montgomery_reduce(t2, modulus, mp);
  1984. /* Y = T2 - T1 */
  1985. if (err == MP_OKAY)
  1986. err = mp_submod_ct(t2, t1, modulus, y);
  1987. /* Y = Y/2 */
  1988. if (err == MP_OKAY)
  1989. err = mp_div_2_mod_ct(y, modulus, y);
  1990. #ifdef ALT_ECC_SIZE
  1991. if (err == MP_OKAY)
  1992. err = mp_copy(x, R->x);
  1993. if (err == MP_OKAY)
  1994. err = mp_copy(y, R->y);
  1995. if (err == MP_OKAY)
  1996. err = mp_copy(z, R->z);
  1997. #endif
  1998. done:
  1999. /* clean up */
  2000. mp_clear(t1);
  2001. mp_clear(t2);
  2002. #ifdef WOLFSSL_SMALL_STACK
  2003. #ifdef WOLFSSL_SMALL_STACK_CACHE
  2004. if (R->key == NULL)
  2005. #endif
  2006. #endif
  2007. {
  2008. #ifdef ALT_ECC_SIZE
  2009. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  2010. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  2011. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  2012. #endif
  2013. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2014. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2015. }
  2016. return err;
  2017. #else
  2018. int modBits = mp_count_bits(modulus);
  2019. (void)a;
  2020. (void)mp;
  2021. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  2022. if ((modBits == 256) && (!mp_is_bit_set(modulus, 224))) {
  2023. return sp_ecc_proj_add_point_sm2_256(P->x, P->y, P->z, Q->x, Q->y, Q->z,
  2024. R->x, R->y, R->z);
  2025. }
  2026. #endif
  2027. #ifndef WOLFSSL_SP_NO_256
  2028. if (modBits == 256) {
  2029. return sp_ecc_proj_add_point_256(P->x, P->y, P->z, Q->x, Q->y, Q->z,
  2030. R->x, R->y, R->z);
  2031. }
  2032. #endif
  2033. #ifdef WOLFSSL_SP_384
  2034. if (modBits == 384) {
  2035. return sp_ecc_proj_add_point_384(P->x, P->y, P->z, Q->x, Q->y, Q->z,
  2036. R->x, R->y, R->z);
  2037. }
  2038. #endif
  2039. #ifdef WOLFSSL_SP_521
  2040. if (modBits == 521) {
  2041. return sp_ecc_proj_add_point_521(P->x, P->y, P->z, Q->x, Q->y, Q->z,
  2042. R->x, R->y, R->z);
  2043. }
  2044. #endif
  2045. return ECC_BAD_ARG_E;
  2046. #endif
  2047. }
  2048. int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
  2049. mp_int* a, mp_int* modulus, mp_digit mp)
  2050. {
  2051. if (P == NULL || Q == NULL || R == NULL || modulus == NULL) {
  2052. return ECC_BAD_ARG_E;
  2053. }
  2054. if (mp_cmp(P->x, modulus) != MP_LT ||
  2055. mp_cmp(P->y, modulus) != MP_LT ||
  2056. mp_cmp(P->z, modulus) != MP_LT ||
  2057. mp_cmp(Q->x, modulus) != MP_LT ||
  2058. mp_cmp(Q->y, modulus) != MP_LT ||
  2059. mp_cmp(Q->z, modulus) != MP_LT) {
  2060. return ECC_OUT_OF_RANGE_E;
  2061. }
  2062. return _ecc_projective_add_point(P, Q, R, a, modulus, mp);
  2063. }
  2064. /* ### Point doubling in Jacobian coordinate system ###
  2065. *
  2066. * let us have a curve: y^2 = x^3 + a*x + b
  2067. * in Jacobian coordinates it becomes: y^2 = x^3 + a*x*z^4 + b*z^6
  2068. *
  2069. * The doubling of P = (Xp, Yp, Zp) is given by R = (Xr, Yr, Zr) where:
  2070. * Xr = M^2 - 2*S
  2071. * Yr = M * (S - Xr) - 8*T
  2072. * Zr = 2 * Yp * Zp
  2073. *
  2074. * M = 3 * Xp^2 + a*Zp^4
  2075. * T = Yp^4
  2076. * S = 4 * Xp * Yp^2
  2077. *
  2078. * SPECIAL CASE: when a == 3 we can compute M as
  2079. * M = 3 * (Xp^2 - Zp^4) = 3 * (Xp + Zp^2) * (Xp - Zp^2)
  2080. */
  2081. /**
  2082. Double an ECC point
  2083. P The point to double
  2084. R [out] The destination of the double
  2085. a ECC curve parameter a
  2086. modulus The modulus of the field the ECC curve is in
  2087. mp The "b" value from montgomery_setup()
  2088. return MP_OKAY on success
  2089. */
  2090. static int _ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a,
  2091. mp_int* modulus, mp_digit mp)
  2092. {
  2093. #if !defined(WOLFSSL_SP_MATH)
  2094. DECL_MP_INT_SIZE_DYN(t1, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2095. DECL_MP_INT_SIZE_DYN(t2, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2096. #ifdef ALT_ECC_SIZE
  2097. DECL_MP_INT_SIZE_DYN(rx, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2098. DECL_MP_INT_SIZE_DYN(ry, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2099. DECL_MP_INT_SIZE_DYN(rz, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2100. #endif
  2101. mp_int *x, *y, *z;
  2102. int err;
  2103. #ifdef WOLFSSL_SMALL_STACK
  2104. #ifdef WOLFSSL_SMALL_STACK_CACHE
  2105. if (R->key != NULL) {
  2106. t1 = R->key->t1;
  2107. t2 = R->key->t2;
  2108. #ifdef ALT_ECC_SIZE
  2109. rx = R->key->x;
  2110. ry = R->key->y;
  2111. rz = R->key->z;
  2112. #endif
  2113. }
  2114. else
  2115. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  2116. #endif
  2117. {
  2118. NEW_MP_INT_SIZE(t1, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2119. NEW_MP_INT_SIZE(t2, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2120. #ifdef MP_INT_SIZE_CHECK_NULL
  2121. if (t1 == NULL || t2 == NULL) {
  2122. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2123. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2124. return MEMORY_E;
  2125. }
  2126. #endif
  2127. #ifdef ALT_ECC_SIZE
  2128. NEW_MP_INT_SIZE(rx, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2129. NEW_MP_INT_SIZE(ry, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2130. NEW_MP_INT_SIZE(rz, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2131. #ifdef MP_INT_SIZE_CHECK_NULL
  2132. if (rx == NULL || ry == NULL || rz == NULL) {
  2133. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  2134. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  2135. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  2136. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2137. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2138. return MEMORY_E;
  2139. }
  2140. #endif
  2141. #endif
  2142. }
  2143. err = INIT_MP_INT_SIZE(t1, mp_bitsused(modulus));
  2144. if (err == MP_OKAY) {
  2145. err = INIT_MP_INT_SIZE(t2, mp_bitsused(modulus));
  2146. }
  2147. if (err != MP_OKAY) {
  2148. #ifdef WOLFSSL_SMALL_STACK
  2149. #ifdef WOLFSSL_SMALL_STACK_CACHE
  2150. if (R->key == NULL)
  2151. #endif
  2152. #endif
  2153. {
  2154. #ifdef ALT_ECC_SIZE
  2155. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  2156. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  2157. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  2158. #endif
  2159. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2160. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2161. }
  2162. return err;
  2163. }
  2164. /* If use ALT_ECC_SIZE we need to use local stack variable since
  2165. ecc_point x,y,z is reduced size */
  2166. #ifdef ALT_ECC_SIZE
  2167. /* Use local stack variable */
  2168. x = rx;
  2169. y = ry;
  2170. z = rz;
  2171. err = INIT_MP_INT_SIZE(x, mp_bitsused(modulus));
  2172. if (err == MP_OKAY) {
  2173. err = INIT_MP_INT_SIZE(y, mp_bitsused(modulus));
  2174. }
  2175. if (err == MP_OKAY) {
  2176. err = INIT_MP_INT_SIZE(z, mp_bitsused(modulus));
  2177. }
  2178. if (err != MP_OKAY) {
  2179. #ifdef WOLFSSL_SMALL_STACK
  2180. #ifdef WOLFSSL_SMALL_STACK_CACHE
  2181. if (R->key == NULL)
  2182. #endif
  2183. #endif
  2184. {
  2185. #ifdef ALT_ECC_SIZE
  2186. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  2187. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  2188. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  2189. #endif
  2190. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2191. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2192. }
  2193. return err;
  2194. }
  2195. #else
  2196. /* Use destination directly */
  2197. x = R->x;
  2198. y = R->y;
  2199. z = R->z;
  2200. #endif
  2201. if (err == MP_OKAY)
  2202. err = mp_copy(P->x, x);
  2203. if (err == MP_OKAY)
  2204. err = mp_copy(P->y, y);
  2205. if (err == MP_OKAY)
  2206. err = mp_copy(P->z, z);
  2207. /* T1 = Z * Z */
  2208. if (err == MP_OKAY)
  2209. err = mp_sqr(z, t1);
  2210. if (err == MP_OKAY)
  2211. err = mp_montgomery_reduce(t1, modulus, mp);
  2212. /* Z = Y * Z */
  2213. if (err == MP_OKAY)
  2214. err = mp_mul(z, y, z);
  2215. if (err == MP_OKAY)
  2216. err = mp_montgomery_reduce(z, modulus, mp);
  2217. /* Z = 2Z */
  2218. if (err == MP_OKAY)
  2219. err = mp_addmod_ct(z, z, modulus, z);
  2220. /* Determine if curve "a" should be used in calc */
  2221. #ifdef WOLFSSL_CUSTOM_CURVES
  2222. if (err == MP_OKAY) {
  2223. /* Use a and prime to determine if a == 3 */
  2224. err = mp_submod(modulus, a, modulus, t2);
  2225. }
  2226. if (err == MP_OKAY && mp_iszero((MP_INT_SIZE*)t2)) {
  2227. /* T2 = X * X */
  2228. if (err == MP_OKAY)
  2229. err = mp_sqr(x, t2);
  2230. if (err == MP_OKAY)
  2231. err = mp_montgomery_reduce(t2, modulus, mp);
  2232. /* T1 = T2 + T1 */
  2233. if (err == MP_OKAY)
  2234. err = mp_addmod_ct(t2, t2, modulus, t1);
  2235. /* T1 = T2 + T1 */
  2236. if (err == MP_OKAY)
  2237. err = mp_addmod_ct(t1, t2, modulus, t1);
  2238. }
  2239. else if (err == MP_OKAY && mp_cmp_d(t2, 3) != MP_EQ) {
  2240. /* use "a" in calc */
  2241. /* T2 = T1 * T1 */
  2242. if (err == MP_OKAY)
  2243. err = mp_sqr(t1, t2);
  2244. if (err == MP_OKAY)
  2245. err = mp_montgomery_reduce(t2, modulus, mp);
  2246. /* T1 = T2 * a */
  2247. if (err == MP_OKAY)
  2248. err = mp_mulmod(t2, a, modulus, t1);
  2249. /* T2 = X * X */
  2250. if (err == MP_OKAY)
  2251. err = mp_sqr(x, t2);
  2252. if (err == MP_OKAY)
  2253. err = mp_montgomery_reduce(t2, modulus, mp);
  2254. /* T1 = T2 + T1 */
  2255. if (err == MP_OKAY)
  2256. err = mp_addmod_ct(t1, t2, modulus, t1);
  2257. /* T1 = T2 + T1 */
  2258. if (err == MP_OKAY)
  2259. err = mp_addmod_ct(t1, t2, modulus, t1);
  2260. /* T1 = T2 + T1 */
  2261. if (err == MP_OKAY)
  2262. err = mp_addmod_ct(t1, t2, modulus, t1);
  2263. }
  2264. else
  2265. #endif /* WOLFSSL_CUSTOM_CURVES */
  2266. {
  2267. /* assumes "a" == 3 */
  2268. (void)a;
  2269. /* T2 = X - T1 */
  2270. if (err == MP_OKAY)
  2271. err = mp_submod_ct(x, t1, modulus, t2);
  2272. /* T1 = X + T1 */
  2273. if (err == MP_OKAY)
  2274. err = mp_addmod_ct(t1, x, modulus, t1);
  2275. /* T2 = T1 * T2 */
  2276. if (err == MP_OKAY)
  2277. err = mp_mul(t1, t2, t2);
  2278. if (err == MP_OKAY)
  2279. err = mp_montgomery_reduce(t2, modulus, mp);
  2280. /* T1 = 2T2 */
  2281. if (err == MP_OKAY)
  2282. err = mp_addmod_ct(t2, t2, modulus, t1);
  2283. /* T1 = T1 + T2 */
  2284. if (err == MP_OKAY)
  2285. err = mp_addmod_ct(t1, t2, modulus, t1);
  2286. }
  2287. /* Y = 2Y */
  2288. if (err == MP_OKAY)
  2289. err = mp_addmod_ct(y, y, modulus, y);
  2290. /* Y = Y * Y */
  2291. if (err == MP_OKAY)
  2292. err = mp_sqr(y, y);
  2293. if (err == MP_OKAY)
  2294. err = mp_montgomery_reduce(y, modulus, mp);
  2295. /* T2 = Y * Y */
  2296. if (err == MP_OKAY)
  2297. err = mp_sqr(y, t2);
  2298. if (err == MP_OKAY)
  2299. err = mp_montgomery_reduce(t2, modulus, mp);
  2300. /* T2 = T2/2 */
  2301. if (err == MP_OKAY)
  2302. err = mp_div_2_mod_ct(t2, modulus, t2);
  2303. /* Y = Y * X */
  2304. if (err == MP_OKAY)
  2305. err = mp_mul(y, x, y);
  2306. if (err == MP_OKAY)
  2307. err = mp_montgomery_reduce(y, modulus, mp);
  2308. /* X = T1 * T1 */
  2309. if (err == MP_OKAY)
  2310. err = mp_sqr(t1, x);
  2311. if (err == MP_OKAY)
  2312. err = mp_montgomery_reduce(x, modulus, mp);
  2313. /* X = X - Y */
  2314. if (err == MP_OKAY)
  2315. err = mp_submod_ct(x, y, modulus, x);
  2316. /* X = X - Y */
  2317. if (err == MP_OKAY)
  2318. err = mp_submod_ct(x, y, modulus, x);
  2319. /* Y = Y - X */
  2320. if (err == MP_OKAY)
  2321. err = mp_submod_ct(y, x, modulus, y);
  2322. /* Y = Y * T1 */
  2323. if (err == MP_OKAY)
  2324. err = mp_mul(y, t1, y);
  2325. if (err == MP_OKAY)
  2326. err = mp_montgomery_reduce(y, modulus, mp);
  2327. /* Y = Y - T2 */
  2328. if (err == MP_OKAY)
  2329. err = mp_submod_ct(y, t2, modulus, y);
  2330. #ifdef ALT_ECC_SIZE
  2331. if (err == MP_OKAY)
  2332. err = mp_copy(x, R->x);
  2333. if (err == MP_OKAY)
  2334. err = mp_copy(y, R->y);
  2335. if (err == MP_OKAY)
  2336. err = mp_copy(z, R->z);
  2337. #endif
  2338. /* clean up */
  2339. mp_clear(t1);
  2340. mp_clear(t2);
  2341. #ifdef WOLFSSL_SMALL_STACK
  2342. #ifdef WOLFSSL_SMALL_STACK_CACHE
  2343. if (R->key == NULL)
  2344. #endif
  2345. #endif
  2346. {
  2347. #ifdef ALT_ECC_SIZE
  2348. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  2349. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  2350. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  2351. #endif
  2352. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2353. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2354. }
  2355. return err;
  2356. #else
  2357. int modBits = mp_count_bits(modulus);
  2358. (void)a;
  2359. (void)mp;
  2360. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  2361. if ((modBits == 256) && (!mp_is_bit_set(modulus, 224))) {
  2362. return sp_ecc_proj_dbl_point_sm2_256(P->x, P->y, P->z, R->x, R->y, R->z);
  2363. }
  2364. #endif
  2365. #ifndef WOLFSSL_SP_NO_256
  2366. if (modBits == 256) {
  2367. return sp_ecc_proj_dbl_point_256(P->x, P->y, P->z, R->x, R->y, R->z);
  2368. }
  2369. #endif
  2370. #ifdef WOLFSSL_SP_384
  2371. if (modBits == 384) {
  2372. return sp_ecc_proj_dbl_point_384(P->x, P->y, P->z, R->x, R->y, R->z);
  2373. }
  2374. #endif
  2375. #ifdef WOLFSSL_SP_521
  2376. if (modBits == 521) {
  2377. return sp_ecc_proj_dbl_point_521(P->x, P->y, P->z, R->x, R->y, R->z);
  2378. }
  2379. #endif
  2380. return ECC_BAD_ARG_E;
  2381. #endif
  2382. }
  2383. int ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a,
  2384. mp_int* modulus, mp_digit mp)
  2385. {
  2386. if (P == NULL || R == NULL || modulus == NULL)
  2387. return ECC_BAD_ARG_E;
  2388. if (mp_cmp(P->x, modulus) != MP_LT ||
  2389. mp_cmp(P->y, modulus) != MP_LT ||
  2390. mp_cmp(P->z, modulus) != MP_LT) {
  2391. return ECC_OUT_OF_RANGE_E;
  2392. }
  2393. return _ecc_projective_dbl_point(P, R, a, modulus, mp);
  2394. }
  2395. #if !defined(FREESCALE_LTC_ECC) && !defined(WOLFSSL_STM32_PKA) && \
  2396. !defined(WOLFSSL_CRYPTOCELL)
  2397. /**
  2398. Map a projective Jacobian point back to affine space
  2399. P [in/out] The point to map
  2400. modulus The modulus of the field the ECC curve is in
  2401. mp The "b" value from montgomery_setup()
  2402. ct Operation should be constant time.
  2403. return MP_OKAY on success
  2404. */
  2405. int ecc_map_ex(ecc_point* P, mp_int* modulus, mp_digit mp, int ct)
  2406. {
  2407. #if !defined(WOLFSSL_SP_MATH)
  2408. DECL_MP_INT_SIZE_DYN(t1, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2409. DECL_MP_INT_SIZE_DYN(t2, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2410. #ifdef ALT_ECC_SIZE
  2411. DECL_MP_INT_SIZE_DYN(rx, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2412. DECL_MP_INT_SIZE_DYN(ry, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2413. DECL_MP_INT_SIZE_DYN(rz, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  2414. #endif
  2415. mp_int *x, *y, *z;
  2416. int err;
  2417. (void)ct;
  2418. if (P == NULL || modulus == NULL)
  2419. return ECC_BAD_ARG_E;
  2420. /* special case for point at infinity */
  2421. if (mp_cmp_d(P->z, 0) == MP_EQ) {
  2422. err = mp_set(P->x, 0);
  2423. if (err == MP_OKAY)
  2424. err = mp_set(P->y, 0);
  2425. if (err == MP_OKAY)
  2426. err = mp_set(P->z, 1);
  2427. return err;
  2428. }
  2429. #ifdef WOLFSSL_SMALL_STACK
  2430. #ifdef WOLFSSL_SMALL_STACK_CACHE
  2431. if (P->key != NULL) {
  2432. t1 = P->key->t1;
  2433. t2 = P->key->t2;
  2434. #ifdef ALT_ECC_SIZE
  2435. rx = P->key->x;
  2436. ry = P->key->y;
  2437. rz = P->key->z;
  2438. #endif
  2439. }
  2440. else
  2441. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  2442. #endif
  2443. {
  2444. NEW_MP_INT_SIZE(t1, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2445. NEW_MP_INT_SIZE(t2, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2446. #ifdef MP_INT_SIZE_CHECK_NULL
  2447. if (t1 == NULL || t2 == NULL) {
  2448. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2449. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2450. return MEMORY_E;
  2451. }
  2452. #endif
  2453. #ifdef ALT_ECC_SIZE
  2454. NEW_MP_INT_SIZE(rx, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2455. NEW_MP_INT_SIZE(ry, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2456. NEW_MP_INT_SIZE(rz, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  2457. #ifdef MP_INT_SIZE_CHECK_NULL
  2458. if (rx == NULL || ry == NULL || rz == NULL) {
  2459. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  2460. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  2461. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  2462. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2463. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2464. return MEMORY_E;
  2465. }
  2466. #endif
  2467. #endif
  2468. }
  2469. err = INIT_MP_INT_SIZE(t1, mp_bitsused(modulus));
  2470. if (err == MP_OKAY) {
  2471. err = INIT_MP_INT_SIZE(t2, mp_bitsused(modulus));
  2472. }
  2473. if (err != MP_OKAY) {
  2474. #ifdef WOLFSSL_SMALL_STACK
  2475. #ifdef WOLFSSL_SMALL_STACK_CACHE
  2476. if (P->key == NULL)
  2477. #endif
  2478. #endif
  2479. {
  2480. #ifdef ALT_ECC_SIZE
  2481. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  2482. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  2483. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  2484. #endif
  2485. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2486. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2487. }
  2488. return MEMORY_E;
  2489. }
  2490. #ifdef ALT_ECC_SIZE
  2491. /* Use local stack variable */
  2492. x = rx;
  2493. y = ry;
  2494. z = rz;
  2495. err = INIT_MP_INT_SIZE(x, mp_bitsused(modulus));
  2496. if (err == MP_OKAY) {
  2497. err = INIT_MP_INT_SIZE(y, mp_bitsused(modulus));
  2498. }
  2499. if (err == MP_OKAY) {
  2500. err = INIT_MP_INT_SIZE(z, mp_bitsused(modulus));
  2501. }
  2502. if (err != MP_OKAY) {
  2503. goto done;
  2504. }
  2505. if (err == MP_OKAY)
  2506. err = mp_copy(P->x, x);
  2507. if (err == MP_OKAY)
  2508. err = mp_copy(P->y, y);
  2509. if (err == MP_OKAY)
  2510. err = mp_copy(P->z, z);
  2511. if (err != MP_OKAY) {
  2512. goto done;
  2513. }
  2514. #else
  2515. /* Use destination directly */
  2516. x = P->x;
  2517. y = P->y;
  2518. z = P->z;
  2519. #endif
  2520. /* get 1/z */
  2521. if (err == MP_OKAY) {
  2522. #if defined(ECC_TIMING_RESISTANT) && (defined(USE_FAST_MATH) || \
  2523. defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL))
  2524. if (ct) {
  2525. err = mp_invmod_mont_ct(z, modulus, t1, mp);
  2526. if (err == MP_OKAY)
  2527. err = mp_montgomery_reduce(t1, modulus, mp);
  2528. }
  2529. else
  2530. #endif
  2531. {
  2532. /* first map z back to normal */
  2533. err = mp_montgomery_reduce(z, modulus, mp);
  2534. if (err == MP_OKAY)
  2535. err = mp_invmod(z, modulus, t1);
  2536. }
  2537. }
  2538. /* get 1/z^2 and 1/z^3 */
  2539. if (err == MP_OKAY)
  2540. err = mp_sqr(t1, t2);
  2541. if (err == MP_OKAY)
  2542. err = mp_mod(t2, modulus, t2);
  2543. if (err == MP_OKAY)
  2544. err = mp_mul(t1, t2, t1);
  2545. if (err == MP_OKAY)
  2546. err = mp_mod(t1, modulus, t1);
  2547. /* multiply against x/y */
  2548. if (err == MP_OKAY)
  2549. err = mp_mul(x, t2, x);
  2550. if (err == MP_OKAY)
  2551. err = mp_montgomery_reduce(x, modulus, mp);
  2552. if (err == MP_OKAY)
  2553. err = mp_mul(y, t1, y);
  2554. if (err == MP_OKAY)
  2555. err = mp_montgomery_reduce(y, modulus, mp);
  2556. if (err == MP_OKAY)
  2557. err = mp_set(z, 1);
  2558. #ifdef ALT_ECC_SIZE
  2559. /* return result */
  2560. if (err == MP_OKAY)
  2561. err = mp_copy(x, P->x);
  2562. if (err == MP_OKAY)
  2563. err = mp_copy(y, P->y);
  2564. if (err == MP_OKAY)
  2565. err = mp_copy(z, P->z);
  2566. done:
  2567. #endif
  2568. /* clean up */
  2569. mp_clear(t1);
  2570. mp_clear(t2);
  2571. #ifdef WOLFSSL_SMALL_STACK
  2572. #ifdef WOLFSSL_SMALL_STACK_CACHE
  2573. if (P->key == NULL)
  2574. #endif
  2575. #endif
  2576. {
  2577. #ifdef ALT_ECC_SIZE
  2578. FREE_MP_INT_SIZE(rz, NULL, DYNAMIC_TYPE_ECC);
  2579. FREE_MP_INT_SIZE(ry, NULL, DYNAMIC_TYPE_ECC);
  2580. FREE_MP_INT_SIZE(rx, NULL, DYNAMIC_TYPE_ECC);
  2581. #endif
  2582. FREE_MP_INT_SIZE(t2, NULL, DYNAMIC_TYPE_ECC);
  2583. FREE_MP_INT_SIZE(t1, NULL, DYNAMIC_TYPE_ECC);
  2584. }
  2585. return err;
  2586. #else
  2587. if (P == NULL || modulus == NULL)
  2588. return ECC_BAD_ARG_E;
  2589. (void)mp;
  2590. (void)ct;
  2591. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  2592. if ((mp_count_bits(modulus) == 256) && (!mp_is_bit_set(modulus, 224))) {
  2593. return sp_ecc_map_sm2_256(P->x, P->y, P->z);
  2594. }
  2595. #endif
  2596. #ifndef WOLFSSL_SP_NO_256
  2597. if (mp_count_bits(modulus) == 256) {
  2598. return sp_ecc_map_256(P->x, P->y, P->z);
  2599. }
  2600. #endif
  2601. #ifdef WOLFSSL_SP_384
  2602. if (mp_count_bits(modulus) == 384) {
  2603. return sp_ecc_map_384(P->x, P->y, P->z);
  2604. }
  2605. #endif
  2606. #ifdef WOLFSSL_SP_521
  2607. if (mp_count_bits(modulus) == 521) {
  2608. return sp_ecc_map_521(P->x, P->y, P->z);
  2609. }
  2610. #endif
  2611. return ECC_BAD_ARG_E;
  2612. #endif
  2613. }
  2614. #endif /* !FREESCALE_LTC_ECC && !WOLFSSL_STM32_PKA */
  2615. int ecc_map(ecc_point* P, mp_int* modulus, mp_digit mp)
  2616. {
  2617. return ecc_map_ex(P, modulus, mp, 0);
  2618. }
  2619. #endif /* !WOLFSSL_SP_MATH || WOLFSSL_PUBLIC_ECC_ADD_DBL */
  2620. #if !defined(FREESCALE_LTC_ECC) && !defined(WOLFSSL_STM32_PKA) && \
  2621. !defined(WOLFSSL_CRYPTOCELL)
  2622. #if !defined(WOLFSSL_SP_MATH)
  2623. #ifndef ECC_TIMING_RESISTANT
  2624. /* size of sliding window, don't change this! */
  2625. #define WINSIZE 4
  2626. #define M_POINTS 8
  2627. static int ecc_mulmod(const mp_int* k, ecc_point* tG, ecc_point* R,
  2628. ecc_point** M, mp_int* a, mp_int* modulus, mp_digit mp, WC_RNG* rng)
  2629. {
  2630. int err = MP_OKAY;
  2631. int i;
  2632. int first = 1, bitbuf = 0, bitcpy = 0, j;
  2633. int bitcnt = 0, mode = 0, digidx = 0;
  2634. mp_digit buf;
  2635. int infinity;
  2636. (void)rng;
  2637. /* calc the M tab, which holds kG for k==8..15 */
  2638. /* M[0] == 8G */
  2639. if (err == MP_OKAY)
  2640. err = ecc_projective_dbl_point_safe(tG, M[0], a, modulus, mp);
  2641. if (err == MP_OKAY)
  2642. err = ecc_projective_dbl_point_safe(M[0], M[0], a, modulus, mp);
  2643. if (err == MP_OKAY)
  2644. err = ecc_projective_dbl_point_safe(M[0], M[0], a, modulus, mp);
  2645. /* now find (8+k)G for k=1..7 */
  2646. if (err == MP_OKAY)
  2647. for (j = 9; j < 16; j++) {
  2648. err = ecc_projective_add_point_safe(M[j-9], tG, M[j-M_POINTS], a,
  2649. modulus, mp, &infinity);
  2650. if (err != MP_OKAY) break;
  2651. }
  2652. /* setup sliding window */
  2653. if (err == MP_OKAY) {
  2654. mode = 0;
  2655. bitcnt = 1;
  2656. buf = 0;
  2657. digidx = get_digit_count(k) - 1;
  2658. bitcpy = bitbuf = 0;
  2659. first = 1;
  2660. /* perform ops */
  2661. for (;;) {
  2662. /* grab next digit as required */
  2663. if (--bitcnt == 0) {
  2664. if (digidx == -1) {
  2665. break;
  2666. }
  2667. buf = get_digit(k, digidx);
  2668. bitcnt = (int) DIGIT_BIT;
  2669. --digidx;
  2670. }
  2671. /* grab the next msb from the ltiplicand */
  2672. i = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  2673. buf <<= 1;
  2674. /* skip leading zero bits */
  2675. if (mode == 0 && i == 0)
  2676. continue;
  2677. /* if the bit is zero and mode == 1 then we double */
  2678. if (mode == 1 && i == 0) {
  2679. err = ecc_projective_dbl_point_safe(R, R, a, modulus, mp);
  2680. if (err != MP_OKAY) break;
  2681. continue;
  2682. }
  2683. /* else we add it to the window */
  2684. bitbuf |= (i << (WINSIZE - ++bitcpy));
  2685. mode = 2;
  2686. if (bitcpy == WINSIZE) {
  2687. /* if this is the first window we do a simple copy */
  2688. if (first == 1) {
  2689. /* R = kG [k = first window] */
  2690. err = mp_copy(M[bitbuf-M_POINTS]->x, R->x);
  2691. if (err != MP_OKAY) break;
  2692. err = mp_copy(M[bitbuf-M_POINTS]->y, R->y);
  2693. if (err != MP_OKAY) break;
  2694. err = mp_copy(M[bitbuf-M_POINTS]->z, R->z);
  2695. first = 0;
  2696. } else {
  2697. /* normal window */
  2698. /* ok window is filled so double as required and add */
  2699. /* double first */
  2700. for (j = 0; j < WINSIZE; j++) {
  2701. err = ecc_projective_dbl_point_safe(R, R, a, modulus,
  2702. mp);
  2703. if (err != MP_OKAY) break;
  2704. }
  2705. if (err != MP_OKAY) break; /* out of first for(;;) */
  2706. /* now add, bitbuf will be 8..15 [8..2^WINSIZE] guaranteed */
  2707. err = ecc_projective_add_point_safe(R, M[bitbuf-M_POINTS], R,
  2708. a, modulus, mp, &infinity);
  2709. }
  2710. if (err != MP_OKAY) break;
  2711. /* empty window and reset */
  2712. bitcpy = bitbuf = 0;
  2713. mode = 1;
  2714. }
  2715. }
  2716. }
  2717. /* if bits remain then double/add */
  2718. if (err == MP_OKAY) {
  2719. if (mode == 2 && bitcpy > 0) {
  2720. /* double then add */
  2721. for (j = 0; j < bitcpy; j++) {
  2722. /* only double if we have had at least one add first */
  2723. if (first == 0) {
  2724. err = ecc_projective_dbl_point_safe(R, R, a, modulus, mp);
  2725. if (err != MP_OKAY) break;
  2726. }
  2727. bitbuf <<= 1;
  2728. if ((bitbuf & (1 << WINSIZE)) != 0) {
  2729. if (first == 1) {
  2730. /* first add, so copy */
  2731. err = mp_copy(tG->x, R->x);
  2732. if (err != MP_OKAY) break;
  2733. err = mp_copy(tG->y, R->y);
  2734. if (err != MP_OKAY) break;
  2735. err = mp_copy(tG->z, R->z);
  2736. if (err != MP_OKAY) break;
  2737. first = 0;
  2738. } else {
  2739. /* then add */
  2740. err = ecc_projective_add_point_safe(R, tG, R, a, modulus,
  2741. mp, &infinity);
  2742. if (err != MP_OKAY) break;
  2743. }
  2744. }
  2745. }
  2746. }
  2747. }
  2748. #undef WINSIZE
  2749. return err;
  2750. }
  2751. #else
  2752. static int wc_ecc_gen_z(WC_RNG* rng, int size, ecc_point* p, mp_int* modulus,
  2753. mp_digit mp, mp_int* tx, mp_int* ty, mp_int* mu)
  2754. {
  2755. int err = MP_OKAY;
  2756. err = mp_montgomery_calc_normalization(mu, modulus);
  2757. /* Generate random value to multiply into p->z. */
  2758. if (err == MP_OKAY)
  2759. err = wc_ecc_gen_k(rng, size, ty, modulus);
  2760. /* Convert to montogmery form. */
  2761. if (err == MP_OKAY)
  2762. err = mp_mulmod(ty, mu, modulus, ty);
  2763. /* Multiply random value into p->z. */
  2764. if (err == MP_OKAY)
  2765. err = mp_mul(p->z, ty, p->z);
  2766. if (err == MP_OKAY)
  2767. err = mp_montgomery_reduce(p->z, modulus, mp);
  2768. /* Square random value for X (X' = X / Z^2). */
  2769. if (err == MP_OKAY)
  2770. err = mp_sqr(ty, tx);
  2771. if (err == MP_OKAY)
  2772. err = mp_montgomery_reduce(tx, modulus, mp);
  2773. /* Multiply square of random by random value for Y. */
  2774. if (err == MP_OKAY)
  2775. err = mp_mul(ty, tx, ty);
  2776. if (err == MP_OKAY)
  2777. err = mp_montgomery_reduce(ty, modulus, mp);
  2778. /* Multiply square into X. */
  2779. if (err == MP_OKAY)
  2780. err = mp_mul(p->x, tx, p->x);
  2781. if (err == MP_OKAY)
  2782. err = mp_montgomery_reduce(p->x, modulus, mp);
  2783. /* Multiply cube into Y (Y' = Y / Z^3). */
  2784. if (err == MP_OKAY)
  2785. err = mp_mul(p->y, ty, p->y);
  2786. if (err == MP_OKAY)
  2787. err = mp_montgomery_reduce(p->y, modulus, mp);
  2788. return err;
  2789. }
  2790. #ifndef WC_PROTECT_ENCRYPTED_MEM
  2791. #define M_POINTS 3
  2792. /* Joye double-add ladder.
  2793. * "Highly Regular Right-to-Left Algorithms for Scalar Multiplication"
  2794. * by Marc Joye (2007)
  2795. *
  2796. * Algorithm 1':
  2797. * Input: P element of curve, k = (k[t-1],..., k[0]) base 2
  2798. * Output: Q = kP
  2799. * 1: R[0] = P; R[1] = P
  2800. * 2: for j = 1 to t-1 do
  2801. * 3: b = 1 - k[j]; R[b] = 2*R[b] + R[k[j]]
  2802. * 4: end for
  2803. * 5: b = k[0]; R[b] = R[b] - P
  2804. * 6: return R[0]
  2805. *
  2806. * Assumes: k < order.
  2807. */
  2808. static int ecc_mulmod(const mp_int* k, ecc_point* P, ecc_point* Q,
  2809. ecc_point** R, mp_int* a, mp_int* modulus, mp_digit mp, WC_RNG* rng)
  2810. {
  2811. int err = MP_OKAY;
  2812. int bytes = (mp_count_bits(modulus) + 7) / 8;
  2813. int i;
  2814. int j = 1;
  2815. int cnt = DIGIT_BIT;
  2816. int t = 0;
  2817. mp_digit b;
  2818. mp_digit v = 0;
  2819. mp_int* kt = R[2]->x;
  2820. #ifndef WC_NO_CACHE_RESISTANT
  2821. /* First bit always 1 (fix at end) and swap equals first bit */
  2822. int swap = 1;
  2823. #ifdef WOLFSSL_SMALL_STACK
  2824. mp_int* tmp = NULL;
  2825. #else
  2826. mp_int tmp[1];
  2827. #endif
  2828. #endif
  2829. int infinity;
  2830. #ifndef WC_NO_CACHE_RESISTANT
  2831. #ifdef WOLFSSL_SMALL_STACK
  2832. tmp = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  2833. if (tmp == NULL) {
  2834. err = MEMORY_E;
  2835. }
  2836. #endif
  2837. if (err == MP_OKAY)
  2838. err = mp_init(tmp);
  2839. #endif
  2840. /* Step 1: R[0] = P; R[1] = P */
  2841. /* R[0] = P */
  2842. if (err == MP_OKAY)
  2843. err = mp_copy(P->x, R[0]->x);
  2844. if (err == MP_OKAY)
  2845. err = mp_copy(P->y, R[0]->y);
  2846. if (err == MP_OKAY)
  2847. err = mp_copy(P->z, R[0]->z);
  2848. /* R[1] = P */
  2849. if (err == MP_OKAY)
  2850. err = mp_copy(P->x, R[1]->x);
  2851. if (err == MP_OKAY)
  2852. err = mp_copy(P->y, R[1]->y);
  2853. if (err == MP_OKAY)
  2854. err = mp_copy(P->z, R[1]->z);
  2855. /* Randomize z ordinates to obfuscate timing. */
  2856. if ((err == MP_OKAY) && (rng != NULL))
  2857. err = wc_ecc_gen_z(rng, bytes, R[0], modulus, mp, R[2]->x, R[2]->y, kt);
  2858. if ((err == MP_OKAY) && (rng != NULL))
  2859. err = wc_ecc_gen_z(rng, bytes, R[1], modulus, mp, R[2]->x, R[2]->y, kt);
  2860. if (err == MP_OKAY) {
  2861. /* Order could be one greater than the size of the modulus. */
  2862. t = mp_count_bits(modulus) + 1;
  2863. v = k->dp[0] >> 1;
  2864. if (cnt > t) {
  2865. cnt = t;
  2866. }
  2867. err = mp_copy(k, kt);
  2868. }
  2869. if (err == MP_OKAY) {
  2870. err = mp_grow(kt, (int)modulus->used + 1);
  2871. }
  2872. /* Step 2: for j = 1 to t-1 do */
  2873. for (i = 1; (err == MP_OKAY) && (i < t); i++) {
  2874. if (--cnt == 0) {
  2875. v = kt->dp[j++];
  2876. cnt = DIGIT_BIT;
  2877. }
  2878. /* Step 3: b = 1 - k[j]; R[b] = 2*R[b] + R[k[j]] */
  2879. b = v & 1;
  2880. v >>= 1;
  2881. #ifdef WC_NO_CACHE_RESISTANT
  2882. err = ecc_projective_dbl_point_safe(R[b^1], R[b^1], a, modulus, mp);
  2883. if (err == MP_OKAY) {
  2884. err = ecc_projective_add_point_safe(R[b^1], R[b], R[b^1], a,
  2885. modulus, mp, &infinity);
  2886. }
  2887. #else
  2888. /* Swap R[0] and R[1] if other index is needed. */
  2889. swap ^= (int)b;
  2890. if (err == MP_OKAY) {
  2891. err = mp_cond_swap_ct_ex(R[0]->x, R[1]->x, (int)modulus->used, swap,
  2892. tmp);
  2893. }
  2894. if (err == MP_OKAY) {
  2895. err = mp_cond_swap_ct_ex(R[0]->y, R[1]->y, (int)modulus->used, swap,
  2896. tmp);
  2897. }
  2898. if (err == MP_OKAY) {
  2899. err = mp_cond_swap_ct_ex(R[0]->z, R[1]->z, (int)modulus->used, swap,
  2900. tmp);
  2901. }
  2902. swap = (int)b;
  2903. if (err == MP_OKAY)
  2904. err = ecc_projective_dbl_point_safe(R[0], R[0], a, modulus, mp);
  2905. if (err == MP_OKAY) {
  2906. err = ecc_projective_add_point_safe(R[0], R[1], R[0], a, modulus,
  2907. mp, &infinity);
  2908. }
  2909. #endif /* WC_NO_CACHE_RESISTANT */
  2910. }
  2911. /* Step 4: end for */
  2912. #ifndef WC_NO_CACHE_RESISTANT
  2913. /* Swap back if last bit is 0. */
  2914. swap ^= 1;
  2915. if (err == MP_OKAY) {
  2916. err = mp_cond_swap_ct_ex(R[0]->x, R[1]->x, (int)modulus->used, swap,
  2917. tmp);
  2918. }
  2919. if (err == MP_OKAY) {
  2920. err = mp_cond_swap_ct_ex(R[0]->y, R[1]->y, (int)modulus->used, swap,
  2921. tmp);
  2922. }
  2923. if (err == MP_OKAY) {
  2924. err = mp_cond_swap_ct_ex(R[0]->z, R[1]->z, (int)modulus->used, swap,
  2925. tmp);
  2926. }
  2927. #endif
  2928. /* Step 5: b = k[0]; R[b] = R[b] - P */
  2929. /* R[2] = -P */
  2930. if (err == MP_OKAY)
  2931. err = mp_copy(P->x, R[2]->x);
  2932. if (err == MP_OKAY)
  2933. err = mp_sub(modulus, P->y, R[2]->y);
  2934. if (err == MP_OKAY)
  2935. err = mp_copy(P->z, R[2]->z);
  2936. /* Subtract point by adding negative. */
  2937. if (err == MP_OKAY) {
  2938. b = k->dp[0] & 1;
  2939. #ifdef WC_NO_CACHE_RESISTANT
  2940. err = ecc_projective_add_point_safe(R[b], R[2], R[b], a, modulus, mp,
  2941. &infinity);
  2942. #else
  2943. /* Swap R[0] and R[1], if necessary, to operate on the one we want. */
  2944. err = mp_cond_swap_ct_ex(R[0]->x, R[1]->x, (int)modulus->used, (int)b,
  2945. tmp);
  2946. if (err == MP_OKAY) {
  2947. err = mp_cond_swap_ct_ex(R[0]->y, R[1]->y, (int)modulus->used,
  2948. (int)b, tmp);
  2949. }
  2950. if (err == MP_OKAY) {
  2951. err = mp_cond_swap_ct_ex(R[0]->z, R[1]->z, (int)modulus->used,
  2952. (int)b, tmp);
  2953. }
  2954. if (err == MP_OKAY)
  2955. err = ecc_projective_add_point_safe(R[0], R[2], R[0], a, modulus,
  2956. mp, &infinity);
  2957. /* Swap back if necessary. */
  2958. if (err == MP_OKAY) {
  2959. err = mp_cond_swap_ct_ex(R[0]->x, R[1]->x, (int)modulus->used,
  2960. (int)b, tmp);
  2961. }
  2962. if (err == MP_OKAY) {
  2963. err = mp_cond_swap_ct_ex(R[0]->y, R[1]->y, (int)modulus->used,
  2964. (int)b, tmp);
  2965. }
  2966. if (err == MP_OKAY) {
  2967. err = mp_cond_swap_ct_ex(R[0]->z, R[1]->z, (int)modulus->used,
  2968. (int)b, tmp);
  2969. }
  2970. #endif
  2971. }
  2972. /* Step 6: return R[0] */
  2973. if (err == MP_OKAY)
  2974. err = mp_copy(R[0]->x, Q->x);
  2975. if (err == MP_OKAY)
  2976. err = mp_copy(R[0]->y, Q->y);
  2977. if (err == MP_OKAY)
  2978. err = mp_copy(R[0]->z, Q->z);
  2979. #if defined(WOLFSSL_SMALL_STACK) && !defined(WC_NO_CACHE_RESISTANT)
  2980. XFREE(tmp, NULL, DYNAMIC_TYPE_ECC);
  2981. #endif
  2982. return err;
  2983. }
  2984. #else
  2985. /* Number of points to allocate for use during scalar multiplication. */
  2986. #define M_POINTS 5
  2987. /* Last of the points is used as a temporary during calculations. */
  2988. #define TMP_IDX M_POINTS - 1
  2989. static void mp_cond_swap_into_ct(mp_int* ra, mp_int* rb, mp_int* a, mp_int* b,
  2990. int digits, int m)
  2991. {
  2992. int i;
  2993. #if !defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_INT_NEGATIVE)
  2994. /* Only using positive numbers in ECC operations. */
  2995. ra->sign = 0;
  2996. rb->sign = 0;
  2997. #endif
  2998. /* Don't store 0 when mask is 0, it will be in a register. */
  2999. ra->used = (int)(((a->used ^ b->used) & ((mp_digit)0 - (m & 1))) ^ a->used);
  3000. rb->used = (int)(((a->used ^ b->used) & ((mp_digit)0 - (m & 1))) ^ b->used);
  3001. for (i = 0; i < digits; i++) {
  3002. ra->dp[i] = ((a->dp[i] ^ b->dp[i]) & ((mp_digit)0 - (m & 1))) ^
  3003. a->dp[i];
  3004. rb->dp[i] = ((a->dp[i] ^ b->dp[i]) & ((mp_digit)0 - (m & 1))) ^
  3005. b->dp[i];
  3006. }
  3007. }
  3008. static void ecc_cond_swap_into_ct(ecc_point* ra, ecc_point* rb, ecc_point* a,
  3009. ecc_point* b, int digits, int m)
  3010. {
  3011. /* Conditionally swap each ordinate. */
  3012. mp_cond_swap_into_ct(ra->x, rb->x, a->x, b->x, digits, m);
  3013. mp_cond_swap_into_ct(ra->y, rb->y, a->y, b->y, digits, m);
  3014. mp_cond_swap_into_ct(ra->z, rb->z, a->z, b->z, digits, m);
  3015. }
  3016. /* Joye double-add ladder.
  3017. * "Highly Regular Right-to-Left Algorithms for Scalar Multiplication"
  3018. * by Marc Joye (2007)
  3019. *
  3020. * Algorithm 1':
  3021. * Input: P element of curve, k = (k[t-1],..., k[0]) base 2
  3022. * Output: Q = kP
  3023. * 1: R[0] = P; R[1] = P
  3024. * 2: for j = 1 to t-1 do
  3025. * 3: b = 1 - k[j]; R[b] = 2*R[b] + R[k[j]]
  3026. * 4: end for
  3027. * 5: b = k[0]; R[b] = R[b] - P
  3028. * 6: return R[0]
  3029. *
  3030. * Assumes: k < order.
  3031. */
  3032. static int ecc_mulmod(const mp_int* k, ecc_point* P, ecc_point* Q,
  3033. ecc_point** R, mp_int* a, mp_int* modulus, mp_digit mp, WC_RNG* rng)
  3034. {
  3035. int err = MP_OKAY;
  3036. int bytes = (mp_count_bits(modulus) + 7) / 8;
  3037. int i;
  3038. int j = 1;
  3039. int cnt;
  3040. int t = 0;
  3041. mp_int* kt = R[TMP_IDX]->x;
  3042. /* First bit always 1 (fix at end) and swap equals first bit */
  3043. register int swap = 1;
  3044. /* Which pair of points has current value. R[0,1] or R[2,3] */
  3045. int set = 0;
  3046. int infinity;
  3047. /* Step 1: R[0] = P; R[1] = P */
  3048. /* R[0] = P */
  3049. if (err == MP_OKAY)
  3050. err = mp_copy(P->x, R[0]->x);
  3051. if (err == MP_OKAY)
  3052. err = mp_copy(P->y, R[0]->y);
  3053. if (err == MP_OKAY)
  3054. err = mp_copy(P->z, R[0]->z);
  3055. /* R[1] = P */
  3056. if (err == MP_OKAY)
  3057. err = mp_copy(P->x, R[1]->x);
  3058. if (err == MP_OKAY)
  3059. err = mp_copy(P->y, R[1]->y);
  3060. if (err == MP_OKAY)
  3061. err = mp_copy(P->z, R[1]->z);
  3062. /* Randomize z ordinates to obfuscate timing. */
  3063. if ((err == MP_OKAY) && (rng != NULL))
  3064. err = wc_ecc_gen_z(rng, bytes, R[0], modulus, mp, R[TMP_IDX]->x,
  3065. R[TMP_IDX]->y, kt);
  3066. if ((err == MP_OKAY) && (rng != NULL))
  3067. err = wc_ecc_gen_z(rng, bytes, R[1], modulus, mp, R[TMP_IDX]->x,
  3068. R[TMP_IDX]->y, kt);
  3069. if (err == MP_OKAY) {
  3070. /* Order could be one greater than the size of the modulus. */
  3071. t = mp_count_bits(modulus) + 1;
  3072. err = mp_copy(k, kt);
  3073. }
  3074. if (err == MP_OKAY) {
  3075. err = mp_grow(kt, modulus->used + 1);
  3076. }
  3077. /* Step 2: for j = 1 to t-1 do */
  3078. for (i = 1, j = 0, cnt = 0; (err == MP_OKAY) && (i < t); i++) {
  3079. if (++cnt == DIGIT_BIT) {
  3080. j++;
  3081. cnt = 0;
  3082. }
  3083. /* Step 3: b = 1 - k[j]; R[b] = 2*R[b] + R[k[j]] */
  3084. /* Swap R[0] and R[1] if other index is needed. */
  3085. /* Ensure 'swap' changes when shifted word is 0. */
  3086. swap += (kt->dp[j] >> cnt) + 2;
  3087. ecc_cond_swap_into_ct(R[(2 - set) + 0], R[(2 - set) + 1],
  3088. R[set + 0], R[set + 1], modulus->used, swap);
  3089. /* Change to operate on set copied into. */
  3090. set = 2 - set;
  3091. /* Ensure 'swap' changes to a previously unseen value. */
  3092. swap += (kt->dp[j] >> cnt) + swap;
  3093. /* R[0] = 2*R[0] */
  3094. err = ecc_projective_dbl_point_safe(R[set + 0], R[set + 0], a, modulus,
  3095. mp);
  3096. if (err == MP_OKAY) {
  3097. /* R[0] = R[1] + R[0] */
  3098. err = ecc_projective_add_point_safe(R[set + 0], R[set + 1],
  3099. R[set + 0], a, modulus, mp, &infinity);
  3100. }
  3101. /* R[1]->z * 2 - same point. */
  3102. mp_addmod_ct(R[set + 1]->z, R[set + 1]->z, modulus, R[set + 1]->z);
  3103. mp_addmod_ct(R[set + 1]->x, R[set + 1]->x, modulus, R[set + 1]->x);
  3104. mp_addmod_ct(R[set + 1]->x, R[set + 1]->x, modulus, R[set + 1]->x);
  3105. mp_addmod_ct(R[set + 1]->y, R[set + 1]->y, modulus, R[set + 1]->y);
  3106. mp_addmod_ct(R[set + 1]->y, R[set + 1]->y, modulus, R[set + 1]->y);
  3107. mp_addmod_ct(R[set + 1]->y, R[set + 1]->y, modulus, R[set + 1]->y);
  3108. }
  3109. /* Step 4: end for */
  3110. /* Swap back if last bit is 0. */
  3111. /* Ensure 'swap' changes. */
  3112. swap += 1;
  3113. if (err == MP_OKAY) {
  3114. ecc_cond_swap_into_ct(R[(2 - set) + 0], R[(2 - set) + 1],
  3115. R[set + 0], R[set + 1], modulus->used, swap);
  3116. set = 2 - set;
  3117. }
  3118. /* Step 5: b = k[0]; R[b] = R[b] - P */
  3119. /* R[TMP_IDX] = -P */
  3120. if (err == MP_OKAY)
  3121. err = mp_copy(P->x, R[TMP_IDX]->x);
  3122. if (err == MP_OKAY)
  3123. err = mp_sub(modulus, P->y, R[TMP_IDX]->y);
  3124. if (err == MP_OKAY)
  3125. err = mp_copy(P->z, R[TMP_IDX]->z);
  3126. /* Subtract point by adding negative. */
  3127. if (err == MP_OKAY) {
  3128. /* Swap R[0] and R[1], if necessary, to operate on the one we want.
  3129. * Last bit of k->dp[0] is being used to make decision to swap.
  3130. */
  3131. ecc_cond_swap_into_ct(R[(2 - set) + 0], R[(2 - set) + 1],
  3132. R[set + 0], R[set + 1], modulus->used,
  3133. (int)k->dp[0]);
  3134. set = 2 - set;
  3135. err = ecc_projective_add_point_safe(R[set + 0], R[TMP_IDX], R[set + 0],
  3136. a, modulus, mp, &infinity);
  3137. /* Swap back if necessary. */
  3138. if (err == MP_OKAY) {
  3139. ecc_cond_swap_into_ct(R[(2 - set) + 0], R[(2 - set) + 1],
  3140. R[set + 0], R[set + 1], modulus->used,
  3141. (int)k->dp[0]);
  3142. set = 2 - set;
  3143. }
  3144. }
  3145. /* Step 6: return R[0] */
  3146. if (err == MP_OKAY)
  3147. err = mp_copy(R[set + 0]->x, Q->x);
  3148. if (err == MP_OKAY)
  3149. err = mp_copy(R[set + 0]->y, Q->y);
  3150. if (err == MP_OKAY)
  3151. err = mp_copy(R[set + 0]->z, Q->z);
  3152. return err;
  3153. }
  3154. #endif
  3155. #endif
  3156. /* Convert the point to montgomery form.
  3157. *
  3158. * @param [in] p Point to convert.
  3159. * @param [out] r Point in montgomery form.
  3160. * @param [in] modulus Modulus of ordinates.
  3161. * @return 0 on success.
  3162. * @return -ve on failure.
  3163. */
  3164. static int ecc_point_to_mont(ecc_point* p, ecc_point* r, mp_int* modulus,
  3165. void* heap)
  3166. {
  3167. int err = MP_OKAY;
  3168. DECL_MP_INT_SIZE_DYN(mu, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  3169. (void)heap;
  3170. NEW_MP_INT_SIZE(mu, mp_bitsused(modulus), heap, DYNAMIC_TYPE_ECC);
  3171. #ifdef MP_INT_SIZE_CHECK_NULL
  3172. if (mu == NULL)
  3173. err = MEMORY_E;
  3174. #endif
  3175. if (err == MP_OKAY)
  3176. err = INIT_MP_INT_SIZE(mu, mp_bitsused(modulus));
  3177. if (err == MP_OKAY) {
  3178. err = mp_montgomery_calc_normalization(mu, modulus);
  3179. if (err == MP_OKAY) {
  3180. if (mp_cmp_d(mu, 1) == MP_EQ) {
  3181. err = mp_copy(p->x, r->x);
  3182. if (err == MP_OKAY)
  3183. err = mp_copy(p->y, r->y);
  3184. if (err == MP_OKAY)
  3185. err = mp_copy(p->z, r->z);
  3186. }
  3187. else {
  3188. err = mp_mulmod(p->x, mu, modulus, r->x);
  3189. if (err == MP_OKAY)
  3190. err = mp_mulmod(p->y, mu, modulus, r->y);
  3191. if (err == MP_OKAY)
  3192. err = mp_mulmod(p->z, mu, modulus, r->z);
  3193. }
  3194. }
  3195. mp_clear(mu);
  3196. }
  3197. FREE_MP_INT_SIZE(mu, heap, DYNAMIC_TYPE_ECC);
  3198. return err;
  3199. }
  3200. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3201. static int ecc_key_tmp_init(ecc_key* key, void* heap)
  3202. {
  3203. int err = MP_OKAY;
  3204. (void)heap;
  3205. if (key == NULL) {
  3206. return ECC_BAD_ARG_E;
  3207. }
  3208. XMEMSET(key, 0, sizeof(*key));
  3209. #if defined(WOLFSSL_SP_MATH_ALL) && defined(WOLFSSL_SMALL_STACK)
  3210. NEW_MP_INT_SIZE(key->t1, ECC_KEY_MAX_BITS(key), heap, DYNAMIC_TYPE_ECC);
  3211. NEW_MP_INT_SIZE(key->t2, ECC_KEY_MAX_BITS(key), heap, DYNAMIC_TYPE_ECC);
  3212. #ifdef ALT_ECC_SIZE
  3213. NEW_MP_INT_SIZE(key->x, ECC_KEY_MAX_BITS(key), heap, DYNAMIC_TYPE_ECC);
  3214. NEW_MP_INT_SIZE(key->y, ECC_KEY_MAX_BITS(key), heap, DYNAMIC_TYPE_ECC);
  3215. NEW_MP_INT_SIZE(key->z, ECC_KEY_MAX_BITS(key), heap, DYNAMIC_TYPE_ECC);
  3216. #endif
  3217. if (key->t1 == NULL || key->t2 == NULL
  3218. #ifdef ALT_ECC_SIZE
  3219. || key->x == NULL || key->y == NULL || key->z == NULL
  3220. #endif
  3221. ) {
  3222. err = MEMORY_E;
  3223. }
  3224. if (err == 0) {
  3225. err = INIT_MP_INT_SIZE(key->t1, ECC_KEY_MAX_BITS(key));
  3226. }
  3227. if (err == 0) {
  3228. err = INIT_MP_INT_SIZE(key->t2, ECC_KEY_MAX_BITS(key));
  3229. }
  3230. #ifdef ALT_ECC_SIZE
  3231. if (err == 0) {
  3232. err = INIT_MP_INT_SIZE(key->x, ECC_KEY_MAX_BITS(key));
  3233. }
  3234. if (err == 0) {
  3235. err = INIT_MP_INT_SIZE(key->y, ECC_KEY_MAX_BITS(key));
  3236. }
  3237. if (err == 0) {
  3238. err = INIT_MP_INT_SIZE(key->z, ECC_KEY_MAX_BITS(key));
  3239. }
  3240. #endif
  3241. #else
  3242. key->t1 = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  3243. key->t2 = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  3244. #ifdef ALT_ECC_SIZE
  3245. key->x = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  3246. key->y = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  3247. key->z = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  3248. #endif
  3249. if (key->t1 == NULL || key->t2 == NULL
  3250. #ifdef ALT_ECC_SIZE
  3251. || key->x == NULL || key->y == NULL || key->z == NULL
  3252. #endif
  3253. ) {
  3254. err = MEMORY_E;
  3255. }
  3256. #endif
  3257. return err;
  3258. }
  3259. static void ecc_key_tmp_final(ecc_key* key, void* heap)
  3260. {
  3261. (void)heap;
  3262. #if defined(WOLFSSL_SP_MATH_ALL) && defined(WOLFSSL_SMALL_STACK)
  3263. #ifdef ALT_ECC_SIZE
  3264. FREE_MP_INT_SIZE(key->z, heap, DYNAMIC_TYPE_ECC);
  3265. FREE_MP_INT_SIZE(key->y, heap, DYNAMIC_TYPE_ECC);
  3266. FREE_MP_INT_SIZE(key->x, heap, DYNAMIC_TYPE_ECC);
  3267. #endif
  3268. FREE_MP_INT_SIZE(key->t2, heap, DYNAMIC_TYPE_ECC);
  3269. FREE_MP_INT_SIZE(key->t1, heap, DYNAMIC_TYPE_ECC);
  3270. #else
  3271. #ifdef ALT_ECC_SIZE
  3272. if (key->z != NULL)
  3273. XFREE(key->z, heap, DYNAMIC_TYPE_ECC);
  3274. if (key->y != NULL)
  3275. XFREE(key->y, heap, DYNAMIC_TYPE_ECC);
  3276. if (key->x != NULL)
  3277. XFREE(key->x, heap, DYNAMIC_TYPE_ECC);
  3278. #endif
  3279. if (key->t2 != NULL)
  3280. XFREE(key->t2, heap, DYNAMIC_TYPE_ECC);
  3281. if (key->t1 != NULL)
  3282. XFREE(key->t1, heap, DYNAMIC_TYPE_ECC);
  3283. #endif
  3284. }
  3285. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  3286. #endif /* !WOLFSSL_SP_MATH */
  3287. #if !defined(WOLFSSL_SP_MATH) || !defined(FP_ECC)
  3288. /**
  3289. Perform a point multiplication
  3290. k The scalar to multiply by
  3291. G The base point
  3292. R [out] Destination for kG
  3293. a ECC curve parameter a
  3294. modulus The modulus of the field the ECC curve is in
  3295. map Boolean whether to map back to affine or not
  3296. (1==map, 0 == leave in projective)
  3297. return MP_OKAY on success
  3298. */
  3299. #ifdef FP_ECC
  3300. static int normal_ecc_mulmod(const mp_int* k, ecc_point *G, ecc_point *R,
  3301. mp_int* a, mp_int* modulus, WC_RNG* rng, int map,
  3302. void* heap)
  3303. #else
  3304. int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
  3305. mp_int* modulus, int map, void* heap)
  3306. #endif
  3307. #if !defined(WOLFSSL_SP_MATH)
  3308. {
  3309. ecc_point *tG, *M[M_POINTS];
  3310. #ifdef WOLFSSL_NO_MALLOC
  3311. ecc_point lcl_tG, lcl_M[M_POINTS];
  3312. #endif
  3313. int i, err;
  3314. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3315. ecc_key *key = (ecc_key *)XMALLOC(sizeof(*key), heap, DYNAMIC_TYPE_ECC);
  3316. #endif
  3317. mp_digit mp;
  3318. /* init variables */
  3319. tG = NULL;
  3320. XMEMSET(M, 0, sizeof(M));
  3321. if (k == NULL || G == NULL || R == NULL || modulus == NULL) {
  3322. err = ECC_BAD_ARG_E;
  3323. goto exit;
  3324. }
  3325. /* k can't have more bits than modulus count plus 1 */
  3326. if (mp_count_bits(k) > mp_count_bits(modulus) + 1) {
  3327. err = ECC_OUT_OF_RANGE_E;
  3328. goto exit;
  3329. }
  3330. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3331. if (key == NULL) {
  3332. err = MP_MEM;
  3333. goto exit;
  3334. }
  3335. err = ecc_key_tmp_init(key, heap);
  3336. if (err != MP_OKAY)
  3337. goto exit;
  3338. R->key = key;
  3339. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  3340. /* alloc ram for window temps */
  3341. for (i = 0; i < M_POINTS; i++) {
  3342. #ifdef WOLFSSL_NO_MALLOC
  3343. M[i] = &lcl_M[i];
  3344. #endif
  3345. err = wc_ecc_new_point_ex(&M[i], heap);
  3346. if (err != MP_OKAY) {
  3347. goto exit;
  3348. }
  3349. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3350. M[i]->key = key;
  3351. #endif
  3352. }
  3353. /* make a copy of G in case R==G */
  3354. #ifdef WOLFSSL_NO_MALLOC
  3355. tG = &lcl_tG;
  3356. #endif
  3357. err = wc_ecc_new_point_ex(&tG, heap);
  3358. if (err != MP_OKAY) {
  3359. goto exit;
  3360. }
  3361. if ((err = ecc_point_to_mont(G, tG, modulus, heap)) != MP_OKAY) {
  3362. goto exit;
  3363. }
  3364. /* init montgomery reduction */
  3365. if ((err = mp_montgomery_setup(modulus, &mp)) != MP_OKAY) {
  3366. goto exit;
  3367. }
  3368. #ifdef FP_ECC
  3369. err = ecc_mulmod(k, tG, R, M, a, modulus, mp, rng);
  3370. #else
  3371. err = ecc_mulmod(k, tG, R, M, a, modulus, mp, NULL);
  3372. #endif
  3373. /* map R back from projective space */
  3374. if (err == MP_OKAY && map)
  3375. err = ecc_map(R, modulus, mp);
  3376. exit:
  3377. /* done */
  3378. wc_ecc_del_point_ex(tG, heap);
  3379. for (i = 0; i < M_POINTS; i++) {
  3380. wc_ecc_del_point_ex(M[i], heap);
  3381. }
  3382. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3383. if (key) {
  3384. if (R)
  3385. R->key = NULL;
  3386. if (err == MP_OKAY)
  3387. ecc_key_tmp_final(key, heap);
  3388. XFREE(key, heap, DYNAMIC_TYPE_ECC);
  3389. }
  3390. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  3391. return err;
  3392. }
  3393. #else
  3394. {
  3395. if (k == NULL || G == NULL || R == NULL || modulus == NULL) {
  3396. return ECC_BAD_ARG_E;
  3397. }
  3398. (void)a;
  3399. /* For supported curves the order is the same length in bits as the modulus.
  3400. * Can't have more than order bits for the scalar.
  3401. */
  3402. if (mp_count_bits(k) > mp_count_bits(modulus)) {
  3403. return ECC_OUT_OF_RANGE_E;
  3404. }
  3405. if (mp_count_bits(G->x) > mp_count_bits(modulus) ||
  3406. mp_count_bits(G->y) > mp_count_bits(modulus) ||
  3407. mp_count_bits(G->z) > mp_count_bits(modulus)) {
  3408. return IS_POINT_E;
  3409. }
  3410. #ifdef WOLFSSL_HAVE_SP_ECC
  3411. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  3412. if ((mp_count_bits(modulus) == 256) && (!mp_is_bit_set(modulus, 224))) {
  3413. return sp_ecc_mulmod_sm2_256(k, G, R, map, heap);
  3414. }
  3415. #endif
  3416. #ifndef WOLFSSL_SP_NO_256
  3417. if (mp_count_bits(modulus) == 256) {
  3418. return sp_ecc_mulmod_256(k, G, R, map, heap);
  3419. }
  3420. #endif
  3421. #ifdef WOLFSSL_SP_384
  3422. if (mp_count_bits(modulus) == 384) {
  3423. return sp_ecc_mulmod_384(k, G, R, map, heap);
  3424. }
  3425. #endif
  3426. #ifdef WOLFSSL_SP_521
  3427. if (mp_count_bits(modulus) == 521) {
  3428. return sp_ecc_mulmod_521(k, G, R, map, heap);
  3429. }
  3430. #endif
  3431. #else
  3432. (void)map;
  3433. (void)map;
  3434. (void)heap;
  3435. #endif
  3436. return ECC_BAD_ARG_E;
  3437. }
  3438. #endif
  3439. #endif /* !WOLFSSL_SP_MATH || !FP_ECC */
  3440. #ifndef FP_ECC
  3441. #if !defined(WOLFSSL_SP_MATH)
  3442. #ifdef ECC_TIMING_RESISTANT
  3443. static int ecc_check_order_minus_1(const mp_int* k, ecc_point* tG, ecc_point* R,
  3444. mp_int* modulus, mp_int* order)
  3445. {
  3446. int err;
  3447. DECL_MP_INT_SIZE_DYN(t, mp_bitsused(order), MAX_ECC_BITS_USE);
  3448. NEW_MP_INT_SIZE(t, mp_bitsused(modulus), NULL, DYNAMIC_TYPE_ECC);
  3449. #ifdef MP_INT_SIZE_CHECK_NULL
  3450. if (t == NULL) {
  3451. err = MEMORY_E;
  3452. }
  3453. else
  3454. #endif
  3455. {
  3456. err = INIT_MP_INT_SIZE(t, mp_bitsused(modulus));
  3457. }
  3458. if (err == MP_OKAY) {
  3459. /* Check for k == order - 1. Result will be 0 point which is not correct
  3460. * Calculates order / 2 and adds order / 2 + 1 and gets infinity.
  3461. * (with constant time implementation)
  3462. */
  3463. err = mp_sub_d(order, 1, t);
  3464. if (err == MP_OKAY) {
  3465. int kIsMinusOne = (mp_cmp((mp_int*)k, t) == MP_EQ);
  3466. err = mp_cond_copy(tG->x, kIsMinusOne, R->x);
  3467. if (err == MP_OKAY) {
  3468. err = mp_sub(modulus, tG->y, t);
  3469. }
  3470. if (err == MP_OKAY) {
  3471. err = mp_cond_copy(t, kIsMinusOne, R->y);
  3472. }
  3473. if (err == MP_OKAY) {
  3474. err = mp_cond_copy(tG->z, kIsMinusOne, R->z);
  3475. }
  3476. }
  3477. mp_free(t);
  3478. }
  3479. FREE_MP_INT_SIZE(t, NULL, DYNAMIC_TYPE_ECC);
  3480. return err;
  3481. }
  3482. #endif /* ECC_TIMING_RESISTANT */
  3483. #endif
  3484. /**
  3485. Perform a point multiplication
  3486. k The scalar to multiply by
  3487. G The base point
  3488. R [out] Destination for kG
  3489. a ECC curve parameter a
  3490. modulus The modulus of the field the ECC curve is in
  3491. map Boolean whether to map back to affine or not
  3492. (1==map, 0 == leave in projective)
  3493. return MP_OKAY on success
  3494. */
  3495. int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a,
  3496. mp_int* modulus, mp_int* order, WC_RNG* rng, int map,
  3497. void* heap)
  3498. #if !defined(WOLFSSL_SP_MATH)
  3499. {
  3500. ecc_point *tG, *M[M_POINTS];
  3501. #ifdef WOLFSSL_NO_MALLOC
  3502. ecc_point lcl_tG, lcl_M[M_POINTS];
  3503. #endif
  3504. int i, err;
  3505. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3506. ecc_key key;
  3507. #endif
  3508. mp_digit mp;
  3509. if (k == NULL || G == NULL || R == NULL || modulus == NULL) {
  3510. return ECC_BAD_ARG_E;
  3511. }
  3512. #ifdef HAVE_ECC_CDH
  3513. if (mp_count_bits(modulus) > mp_count_bits(order)) {
  3514. if (mp_count_bits(k) > mp_count_bits(modulus)) {
  3515. return ECC_OUT_OF_RANGE_E;
  3516. }
  3517. }
  3518. else
  3519. #endif
  3520. /* k can't have more bits than order */
  3521. if (mp_count_bits(k) > mp_count_bits(order)) {
  3522. return ECC_OUT_OF_RANGE_E;
  3523. }
  3524. /* init variables */
  3525. tG = NULL;
  3526. XMEMSET(M, 0, sizeof(M));
  3527. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3528. err = ecc_key_tmp_init(&key, heap);
  3529. if (err != MP_OKAY)
  3530. goto exit;
  3531. R->key = &key;
  3532. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  3533. /* alloc ram for window temps */
  3534. for (i = 0; i < M_POINTS; i++) {
  3535. #ifdef WOLFSSL_NO_MALLOC
  3536. M[i] = &lcl_M[i];
  3537. #endif
  3538. err = wc_ecc_new_point_ex(&M[i], heap);
  3539. if (err != MP_OKAY) {
  3540. goto exit;
  3541. }
  3542. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3543. M[i]->key = &key;
  3544. #endif
  3545. }
  3546. /* make a copy of G in case R==G */
  3547. #ifdef WOLFSSL_NO_MALLOC
  3548. tG = &lcl_tG;
  3549. #endif
  3550. err = wc_ecc_new_point_ex(&tG, heap);
  3551. if (err != MP_OKAY) {
  3552. goto exit;
  3553. }
  3554. if ((err = ecc_point_to_mont(G, tG, modulus, heap)) != MP_OKAY) {
  3555. goto exit;
  3556. }
  3557. /* init montgomery reduction */
  3558. if ((err = mp_montgomery_setup(modulus, &mp)) != MP_OKAY) {
  3559. goto exit;
  3560. }
  3561. err = ecc_mulmod(k, tG, R, M, a, modulus, mp, rng);
  3562. #ifdef ECC_TIMING_RESISTANT
  3563. if (err == MP_OKAY) {
  3564. err = ecc_check_order_minus_1(k, tG, R, modulus, order);
  3565. }
  3566. #else
  3567. (void)order;
  3568. #endif
  3569. /* map R back from projective space */
  3570. if (err == MP_OKAY && map)
  3571. err = ecc_map(R, modulus, mp);
  3572. exit:
  3573. /* done */
  3574. wc_ecc_del_point_ex(tG, heap);
  3575. for (i = 0; i < M_POINTS; i++) {
  3576. wc_ecc_del_point_ex(M[i], heap);
  3577. }
  3578. #ifdef WOLFSSL_SMALL_STACK_CACHE
  3579. R->key = NULL;
  3580. ecc_key_tmp_final(&key, heap);
  3581. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  3582. return err;
  3583. }
  3584. #else
  3585. {
  3586. if (k == NULL || G == NULL || R == NULL || modulus == NULL) {
  3587. return ECC_BAD_ARG_E;
  3588. }
  3589. if (mp_count_bits(G->x) > mp_count_bits(modulus) ||
  3590. mp_count_bits(G->y) > mp_count_bits(modulus) ||
  3591. mp_count_bits(G->z) > mp_count_bits(modulus)) {
  3592. return IS_POINT_E;
  3593. }
  3594. (void)a;
  3595. (void)order;
  3596. (void)rng;
  3597. #ifdef WOLFSSL_HAVE_SP_ECC
  3598. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  3599. if ((mp_count_bits(modulus) == 256) && (!mp_is_bit_set(modulus, 224))) {
  3600. return sp_ecc_mulmod_sm2_256(k, G, R, map, heap);
  3601. }
  3602. #endif
  3603. #ifndef WOLFSSL_SP_NO_256
  3604. if (mp_count_bits(modulus) == 256) {
  3605. return sp_ecc_mulmod_256(k, G, R, map, heap);
  3606. }
  3607. #endif
  3608. #ifdef WOLFSSL_SP_384
  3609. if (mp_count_bits(modulus) == 384) {
  3610. return sp_ecc_mulmod_384(k, G, R, map, heap);
  3611. }
  3612. #endif
  3613. #ifdef WOLFSSL_SP_521
  3614. if (mp_count_bits(modulus) == 521) {
  3615. return sp_ecc_mulmod_521(k, G, R, map, heap);
  3616. }
  3617. #endif
  3618. #else
  3619. (void)map;
  3620. (void)heap;
  3621. #endif
  3622. return ECC_BAD_ARG_E;
  3623. }
  3624. #endif /* !WOLFSSL_SP_MATH */
  3625. #endif /* !FP_ECC */
  3626. #endif /* !FREESCALE_LTC_ECC && !WOLFSSL_STM32_PKA */
  3627. /** ECC Fixed Point mulmod global
  3628. k The multiplicand
  3629. G Base point to multiply
  3630. R [out] Destination of product
  3631. a ECC curve parameter a
  3632. modulus The modulus for the curve
  3633. map [boolean] If non-zero maps the point back to affine coordinates,
  3634. otherwise it's left in jacobian-montgomery form
  3635. return MP_OKAY if successful
  3636. */
  3637. int wc_ecc_mulmod(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
  3638. mp_int* modulus, int map)
  3639. {
  3640. return wc_ecc_mulmod_ex(k, G, R, a, modulus, map, NULL);
  3641. }
  3642. #endif /* !WOLFSSL_ATECC508A */
  3643. /**
  3644. * Allocate a new ECC point (if one not provided)
  3645. * use a heap hint when creating new ecc_point
  3646. * return an allocated point on success or NULL on failure
  3647. */
  3648. static int wc_ecc_new_point_ex(ecc_point** point, void* heap)
  3649. {
  3650. int err = MP_OKAY;
  3651. ecc_point* p;
  3652. if (point == NULL) {
  3653. return BAD_FUNC_ARG;
  3654. }
  3655. p = *point;
  3656. #ifndef WOLFSSL_NO_MALLOC
  3657. if (p == NULL) {
  3658. p = (ecc_point*)XMALLOC(sizeof(ecc_point), heap, DYNAMIC_TYPE_ECC);
  3659. }
  3660. #endif
  3661. if (p == NULL) {
  3662. return MEMORY_E;
  3663. }
  3664. XMEMSET(p, 0, sizeof(ecc_point));
  3665. #ifndef ALT_ECC_SIZE
  3666. err = mp_init_multi(p->x, p->y, p->z, NULL, NULL, NULL);
  3667. if (err != MP_OKAY) {
  3668. #ifndef WOLFSSL_NO_MALLOC
  3669. XFREE(p, heap, DYNAMIC_TYPE_ECC);
  3670. #endif
  3671. return err;
  3672. }
  3673. #else
  3674. p->x = (mp_int*)&p->xyz[0];
  3675. p->y = (mp_int*)&p->xyz[1];
  3676. p->z = (mp_int*)&p->xyz[2];
  3677. alt_fp_init(p->x);
  3678. alt_fp_init(p->y);
  3679. alt_fp_init(p->z);
  3680. #endif
  3681. *point = p;
  3682. (void)heap;
  3683. return err;
  3684. }
  3685. ecc_point* wc_ecc_new_point_h(void* heap)
  3686. {
  3687. ecc_point* p = NULL;
  3688. (void)wc_ecc_new_point_ex(&p, heap);
  3689. return p;
  3690. }
  3691. ecc_point* wc_ecc_new_point(void)
  3692. {
  3693. ecc_point* p = NULL;
  3694. (void)wc_ecc_new_point_ex(&p, NULL);
  3695. return p;
  3696. }
  3697. /** Free an ECC point from memory
  3698. p The point to free
  3699. */
  3700. static void wc_ecc_del_point_ex(ecc_point* p, void* heap)
  3701. {
  3702. if (p != NULL) {
  3703. mp_clear(p->x);
  3704. mp_clear(p->y);
  3705. mp_clear(p->z);
  3706. #ifndef WOLFSSL_NO_MALLOC
  3707. XFREE(p, heap, DYNAMIC_TYPE_ECC);
  3708. #endif
  3709. }
  3710. (void)heap;
  3711. }
  3712. void wc_ecc_del_point_h(ecc_point* p, void* heap)
  3713. {
  3714. wc_ecc_del_point_ex(p, heap);
  3715. }
  3716. void wc_ecc_del_point(ecc_point* p)
  3717. {
  3718. wc_ecc_del_point_ex(p, NULL);
  3719. }
  3720. void wc_ecc_forcezero_point(ecc_point* p)
  3721. {
  3722. if (p != NULL) {
  3723. mp_forcezero(p->x);
  3724. mp_forcezero(p->y);
  3725. mp_forcezero(p->z);
  3726. }
  3727. }
  3728. /** Copy the value of a point to an other one
  3729. p The point to copy
  3730. r The created point
  3731. */
  3732. int wc_ecc_copy_point(const ecc_point* p, ecc_point *r)
  3733. {
  3734. int ret;
  3735. /* prevents null arguments */
  3736. if (p == NULL || r == NULL)
  3737. return ECC_BAD_ARG_E;
  3738. ret = mp_copy(p->x, r->x);
  3739. if (ret != MP_OKAY)
  3740. return ret;
  3741. ret = mp_copy(p->y, r->y);
  3742. if (ret != MP_OKAY)
  3743. return ret;
  3744. ret = mp_copy(p->z, r->z);
  3745. if (ret != MP_OKAY)
  3746. return ret;
  3747. return MP_OKAY;
  3748. }
  3749. /** Compare the value of a point with an other one
  3750. a The point to compare
  3751. b The other point to compare
  3752. return MP_EQ if equal, MP_LT/MP_GT if not, < 0 in case of error
  3753. */
  3754. int wc_ecc_cmp_point(ecc_point* a, ecc_point *b)
  3755. {
  3756. int ret;
  3757. /* prevents null arguments */
  3758. if (a == NULL || b == NULL)
  3759. return BAD_FUNC_ARG;
  3760. ret = mp_cmp(a->x, b->x);
  3761. if (ret != MP_EQ)
  3762. return ret;
  3763. ret = mp_cmp(a->y, b->y);
  3764. if (ret != MP_EQ)
  3765. return ret;
  3766. ret = mp_cmp(a->z, b->z);
  3767. if (ret != MP_EQ)
  3768. return ret;
  3769. return MP_EQ;
  3770. }
  3771. /** Returns whether an ECC idx is valid or not
  3772. n The idx number to check
  3773. return 1 if valid, 0 if not
  3774. */
  3775. int wc_ecc_is_valid_idx(int n)
  3776. {
  3777. int x;
  3778. if (n >= (int)ECC_SET_COUNT)
  3779. return 0;
  3780. for (x = 0; ecc_sets[x].size != 0; x++)
  3781. ;
  3782. /* -1 is a valid index --- indicating that the domain params
  3783. were supplied by the user */
  3784. if ((n >= ECC_CUSTOM_IDX) && (n < x)) {
  3785. return 1;
  3786. }
  3787. return 0;
  3788. }
  3789. int wc_ecc_get_curve_idx(int curve_id)
  3790. {
  3791. int curve_idx;
  3792. for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) {
  3793. if (curve_id == ecc_sets[curve_idx].id)
  3794. break;
  3795. }
  3796. if (ecc_sets[curve_idx].size == 0) {
  3797. return ECC_CURVE_INVALID;
  3798. }
  3799. return curve_idx;
  3800. }
  3801. int wc_ecc_get_curve_id(int curve_idx)
  3802. {
  3803. if (wc_ecc_is_valid_idx(curve_idx)) {
  3804. return ecc_sets[curve_idx].id;
  3805. }
  3806. return ECC_CURVE_INVALID;
  3807. }
  3808. /* Returns the curve size that corresponds to a given ecc_curve_id identifier
  3809. *
  3810. * id curve id, from ecc_curve_id enum in ecc.h
  3811. * return curve size, from ecc_sets[] on success, negative on error
  3812. */
  3813. int wc_ecc_get_curve_size_from_id(int curve_id)
  3814. {
  3815. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  3816. if (curve_idx == ECC_CURVE_INVALID)
  3817. return ECC_BAD_ARG_E;
  3818. return ecc_sets[curve_idx].size;
  3819. }
  3820. /* Returns the curve index that corresponds to a given curve name in
  3821. * ecc_sets[] of ecc.c
  3822. *
  3823. * name curve name, from ecc_sets[].name in ecc.c
  3824. * return curve index in ecc_sets[] on success, negative on error
  3825. */
  3826. int wc_ecc_get_curve_idx_from_name(const char* curveName)
  3827. {
  3828. int curve_idx;
  3829. if (curveName == NULL)
  3830. return BAD_FUNC_ARG;
  3831. for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) {
  3832. if (
  3833. #ifndef WOLFSSL_ECC_CURVE_STATIC
  3834. ecc_sets[curve_idx].name &&
  3835. #endif
  3836. XSTRCASECMP(ecc_sets[curve_idx].name, curveName) == 0) {
  3837. break;
  3838. }
  3839. }
  3840. if (ecc_sets[curve_idx].size == 0) {
  3841. WOLFSSL_MSG("ecc_set curve name not found");
  3842. return ECC_CURVE_INVALID;
  3843. }
  3844. return curve_idx;
  3845. }
  3846. /* Returns the curve size that corresponds to a given curve name,
  3847. * as listed in ecc_sets[] of ecc.c.
  3848. *
  3849. * name curve name, from ecc_sets[].name in ecc.c
  3850. * return curve size, from ecc_sets[] on success, negative on error
  3851. */
  3852. int wc_ecc_get_curve_size_from_name(const char* curveName)
  3853. {
  3854. int curve_idx;
  3855. if (curveName == NULL)
  3856. return BAD_FUNC_ARG;
  3857. curve_idx = wc_ecc_get_curve_idx_from_name(curveName);
  3858. if (curve_idx < 0)
  3859. return curve_idx;
  3860. return ecc_sets[curve_idx].size;
  3861. }
  3862. /* Returns the curve id that corresponds to a given curve name,
  3863. * as listed in ecc_sets[] of ecc.c.
  3864. *
  3865. * name curve name, from ecc_sets[].name in ecc.c
  3866. * return curve id, from ecc_sets[] on success, negative on error
  3867. */
  3868. int wc_ecc_get_curve_id_from_name(const char* curveName)
  3869. {
  3870. int curve_idx;
  3871. if (curveName == NULL)
  3872. return BAD_FUNC_ARG;
  3873. curve_idx = wc_ecc_get_curve_idx_from_name(curveName);
  3874. if (curve_idx < 0)
  3875. return curve_idx;
  3876. return ecc_sets[curve_idx].id;
  3877. }
  3878. /* Compares a curve parameter (hex, from ecc_sets[]) to given input
  3879. * parameter for equality.
  3880. * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR
  3881. * Returns MP_EQ on success, negative on error */
  3882. static int wc_ecc_cmp_param(const char* curveParam,
  3883. const byte* param, word32 paramSz, int encType)
  3884. {
  3885. int err = MP_OKAY;
  3886. #ifdef WOLFSSL_SMALL_STACK
  3887. mp_int* a = NULL;
  3888. mp_int* b = NULL;
  3889. #else
  3890. mp_int a[1], b[1];
  3891. #endif
  3892. if (param == NULL || curveParam == NULL)
  3893. return BAD_FUNC_ARG;
  3894. if (encType == WC_TYPE_HEX_STR)
  3895. return XSTRNCMP(curveParam, (char*) param, paramSz);
  3896. #ifdef WOLFSSL_SMALL_STACK
  3897. a = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  3898. if (a == NULL)
  3899. return MEMORY_E;
  3900. b = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  3901. if (b == NULL) {
  3902. XFREE(a, NULL, DYNAMIC_TYPE_ECC);
  3903. return MEMORY_E;
  3904. }
  3905. #endif
  3906. if ((err = mp_init_multi(a, b, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  3907. #ifdef WOLFSSL_SMALL_STACK
  3908. XFREE(a, NULL, DYNAMIC_TYPE_ECC);
  3909. XFREE(b, NULL, DYNAMIC_TYPE_ECC);
  3910. #endif
  3911. return err;
  3912. }
  3913. if (err == MP_OKAY) {
  3914. err = mp_read_unsigned_bin(a, param, paramSz);
  3915. }
  3916. if (err == MP_OKAY)
  3917. err = mp_read_radix(b, curveParam, MP_RADIX_HEX);
  3918. if (err == MP_OKAY) {
  3919. if (mp_cmp(a, b) != MP_EQ) {
  3920. err = -1;
  3921. } else {
  3922. err = MP_EQ;
  3923. }
  3924. }
  3925. mp_clear(a);
  3926. mp_clear(b);
  3927. #ifdef WOLFSSL_SMALL_STACK
  3928. XFREE(b, NULL, DYNAMIC_TYPE_ECC);
  3929. XFREE(a, NULL, DYNAMIC_TYPE_ECC);
  3930. #endif
  3931. return err;
  3932. }
  3933. /* Returns the curve id in ecc_sets[] that corresponds to a given set of
  3934. * curve parameters.
  3935. *
  3936. * fieldSize the field size in bits
  3937. * prime prime of the finite field
  3938. * primeSz size of prime in octets
  3939. * Af first coefficient a of the curve
  3940. * AfSz size of Af in octets
  3941. * Bf second coefficient b of the curve
  3942. * BfSz size of Bf in octets
  3943. * order curve order
  3944. * orderSz size of curve in octets
  3945. * Gx affine x coordinate of base point
  3946. * GxSz size of Gx in octets
  3947. * Gy affine y coordinate of base point
  3948. * GySz size of Gy in octets
  3949. * cofactor curve cofactor
  3950. *
  3951. * return curve id, from ecc_sets[] on success, negative on error
  3952. */
  3953. int wc_ecc_get_curve_id_from_params(int fieldSize,
  3954. const byte* prime, word32 primeSz, const byte* Af, word32 AfSz,
  3955. const byte* Bf, word32 BfSz, const byte* order, word32 orderSz,
  3956. const byte* Gx, word32 GxSz, const byte* Gy, word32 GySz, int cofactor)
  3957. {
  3958. int idx;
  3959. int curveSz;
  3960. if (prime == NULL || Af == NULL || Bf == NULL || order == NULL ||
  3961. Gx == NULL || Gy == NULL)
  3962. return BAD_FUNC_ARG;
  3963. curveSz = (fieldSize + 1) / 8; /* round up */
  3964. for (idx = 0; ecc_sets[idx].size != 0; idx++) {
  3965. if (curveSz == ecc_sets[idx].size) {
  3966. if ((wc_ecc_cmp_param(ecc_sets[idx].prime, prime,
  3967. primeSz, WC_TYPE_UNSIGNED_BIN) == MP_EQ) &&
  3968. (wc_ecc_cmp_param(ecc_sets[idx].Af, Af, AfSz,
  3969. WC_TYPE_UNSIGNED_BIN) == MP_EQ) &&
  3970. (wc_ecc_cmp_param(ecc_sets[idx].Bf, Bf, BfSz,
  3971. WC_TYPE_UNSIGNED_BIN) == MP_EQ) &&
  3972. (wc_ecc_cmp_param(ecc_sets[idx].order, order,
  3973. orderSz, WC_TYPE_UNSIGNED_BIN) == MP_EQ) &&
  3974. (wc_ecc_cmp_param(ecc_sets[idx].Gx, Gx, GxSz,
  3975. WC_TYPE_UNSIGNED_BIN) == MP_EQ) &&
  3976. (wc_ecc_cmp_param(ecc_sets[idx].Gy, Gy, GySz,
  3977. WC_TYPE_UNSIGNED_BIN) == MP_EQ) &&
  3978. (cofactor == ecc_sets[idx].cofactor)) {
  3979. break;
  3980. }
  3981. }
  3982. }
  3983. if (ecc_sets[idx].size == 0)
  3984. return ECC_CURVE_INVALID;
  3985. return ecc_sets[idx].id;
  3986. }
  3987. /* Returns the curve id in ecc_sets[] that corresponds
  3988. * to a given domain parameters pointer.
  3989. *
  3990. * dp domain parameters pointer
  3991. *
  3992. * return curve id, from ecc_sets[] on success, negative on error
  3993. */
  3994. int wc_ecc_get_curve_id_from_dp_params(const ecc_set_type* dp)
  3995. {
  3996. int idx;
  3997. if (dp == NULL
  3998. #ifndef WOLFSSL_ECC_CURVE_STATIC
  3999. || dp->prime == NULL || dp->Af == NULL ||
  4000. dp->Bf == NULL || dp->order == NULL || dp->Gx == NULL || dp->Gy == NULL
  4001. #endif
  4002. ) {
  4003. return BAD_FUNC_ARG;
  4004. }
  4005. for (idx = 0; ecc_sets[idx].size != 0; idx++) {
  4006. if (dp->size == ecc_sets[idx].size) {
  4007. if ((wc_ecc_cmp_param(ecc_sets[idx].prime, (const byte*)dp->prime,
  4008. (word32)XSTRLEN(dp->prime), WC_TYPE_HEX_STR) == MP_EQ) &&
  4009. (wc_ecc_cmp_param(ecc_sets[idx].Af, (const byte*)dp->Af,
  4010. (word32)XSTRLEN(dp->Af),WC_TYPE_HEX_STR) == MP_EQ) &&
  4011. (wc_ecc_cmp_param(ecc_sets[idx].Bf, (const byte*)dp->Bf,
  4012. (word32)XSTRLEN(dp->Bf),WC_TYPE_HEX_STR) == MP_EQ) &&
  4013. (wc_ecc_cmp_param(ecc_sets[idx].order, (const byte*)dp->order,
  4014. (word32)XSTRLEN(dp->order),WC_TYPE_HEX_STR) == MP_EQ) &&
  4015. (wc_ecc_cmp_param(ecc_sets[idx].Gx, (const byte*)dp->Gx,
  4016. (word32)XSTRLEN(dp->Gx),WC_TYPE_HEX_STR) == MP_EQ) &&
  4017. (wc_ecc_cmp_param(ecc_sets[idx].Gy, (const byte*)dp->Gy,
  4018. (word32)XSTRLEN(dp->Gy),WC_TYPE_HEX_STR) == MP_EQ) &&
  4019. (dp->cofactor == ecc_sets[idx].cofactor)) {
  4020. break;
  4021. }
  4022. }
  4023. }
  4024. if (ecc_sets[idx].size == 0)
  4025. return ECC_CURVE_INVALID;
  4026. return ecc_sets[idx].id;
  4027. }
  4028. /* Returns the curve id that corresponds to a given OID,
  4029. * as listed in ecc_sets[] of ecc.c.
  4030. *
  4031. * oid OID, from ecc_sets[].name in ecc.c
  4032. * len OID len, from ecc_sets[].name in ecc.c
  4033. * return curve id, from ecc_sets[] on success, negative on error
  4034. */
  4035. int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len)
  4036. {
  4037. int curve_idx;
  4038. #if defined(HAVE_OID_DECODING) || defined(HAVE_OID_ENCODING)
  4039. int ret;
  4040. #ifdef HAVE_OID_DECODING
  4041. word16 decOid[MAX_OID_SZ/sizeof(word16)];
  4042. #else
  4043. byte decOid[MAX_OID_SZ];
  4044. #endif
  4045. word32 decOidSz;
  4046. #endif
  4047. if (oid == NULL)
  4048. return BAD_FUNC_ARG;
  4049. #ifdef HAVE_OID_DECODING
  4050. decOidSz = (word32)sizeof(decOid);
  4051. ret = DecodeObjectId(oid, len, decOid, &decOidSz);
  4052. if (ret != 0) {
  4053. return ret;
  4054. }
  4055. #endif
  4056. #if !defined(HAVE_OID_ENCODING) && !defined(HAVE_OID_DECODING)
  4057. if (len == 0) {
  4058. /* SAKKE has zero oidSz and will otherwise match with len==0. */
  4059. WOLFSSL_MSG("zero oidSz");
  4060. return ECC_CURVE_INVALID;
  4061. }
  4062. #endif
  4063. for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) {
  4064. #if defined(HAVE_OID_ENCODING) && !defined(HAVE_OID_DECODING)
  4065. decOidSz = (word32)sizeof(decOid);
  4066. ret = EncodeObjectId(ecc_sets[curve_idx].oid, ecc_sets[curve_idx].oidSz,
  4067. decOid, &decOidSz);
  4068. if (ret != 0) {
  4069. continue;
  4070. }
  4071. #endif
  4072. if (
  4073. #ifndef WOLFSSL_ECC_CURVE_STATIC
  4074. ecc_sets[curve_idx].oid &&
  4075. #endif
  4076. #if defined(HAVE_OID_ENCODING) && !defined(HAVE_OID_DECODING)
  4077. decOidSz == len &&
  4078. XMEMCMP(decOid, oid, len) == 0
  4079. #elif defined(HAVE_OID_ENCODING) && defined(HAVE_OID_DECODING)
  4080. /* We double because decOidSz is a count of word16 elements. */
  4081. ecc_sets[curve_idx].oidSz == decOidSz &&
  4082. XMEMCMP(ecc_sets[curve_idx].oid, decOid, decOidSz * 2) == 0
  4083. #else
  4084. ecc_sets[curve_idx].oidSz == len &&
  4085. XMEMCMP(ecc_sets[curve_idx].oid, oid, len) == 0
  4086. #endif
  4087. ) {
  4088. break;
  4089. }
  4090. }
  4091. if (ecc_sets[curve_idx].size == 0) {
  4092. WOLFSSL_MSG("ecc_set curve name not found");
  4093. return ECC_CURVE_INVALID;
  4094. }
  4095. return ecc_sets[curve_idx].id;
  4096. }
  4097. /* Get curve parameters using curve index */
  4098. const ecc_set_type* wc_ecc_get_curve_params(int curve_idx)
  4099. {
  4100. const ecc_set_type* ecc_set = NULL;
  4101. if (curve_idx >= 0 && curve_idx < (int)ECC_SET_COUNT) {
  4102. ecc_set = &ecc_sets[curve_idx];
  4103. }
  4104. return ecc_set;
  4105. }
  4106. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  4107. static WC_INLINE int wc_ecc_alloc_mpint(ecc_key* key, mp_int** mp)
  4108. {
  4109. if (key == NULL || mp == NULL)
  4110. return BAD_FUNC_ARG;
  4111. if (*mp == NULL) {
  4112. *mp = (mp_int*)XMALLOC(sizeof(mp_int), key->heap, DYNAMIC_TYPE_BIGINT);
  4113. if (*mp == NULL) {
  4114. return MEMORY_E;
  4115. }
  4116. XMEMSET(*mp, 0, sizeof(mp_int));
  4117. }
  4118. return 0;
  4119. }
  4120. static WC_INLINE void wc_ecc_free_mpint(ecc_key* key, mp_int** mp)
  4121. {
  4122. if (key && mp && *mp) {
  4123. mp_clear(*mp);
  4124. XFREE(*mp, key->heap, DYNAMIC_TYPE_BIGINT);
  4125. *mp = NULL;
  4126. }
  4127. }
  4128. static int wc_ecc_alloc_async(ecc_key* key)
  4129. {
  4130. int err = wc_ecc_alloc_mpint(key, &key->r);
  4131. if (err == 0)
  4132. err = wc_ecc_alloc_mpint(key, &key->s);
  4133. return err;
  4134. }
  4135. static void wc_ecc_free_async(ecc_key* key)
  4136. {
  4137. wc_ecc_free_mpint(key, &key->r);
  4138. wc_ecc_free_mpint(key, &key->s);
  4139. #ifdef HAVE_CAVIUM_V
  4140. wc_ecc_free_mpint(key, &key->e);
  4141. wc_ecc_free_mpint(key, &key->signK);
  4142. #endif /* HAVE_CAVIUM_V */
  4143. }
  4144. #endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
  4145. #ifdef HAVE_ECC_DHE
  4146. /**
  4147. Create an ECC shared secret between two keys
  4148. private_key The private ECC key (heap hint based off of private key)
  4149. public_key The public key
  4150. out [out] Destination of the shared secret
  4151. Conforms to EC-DH from ANSI X9.63
  4152. outlen [in/out] The max size and resulting size of the shared secret
  4153. return MP_OKAY if successful
  4154. */
  4155. WOLFSSL_ABI
  4156. int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
  4157. word32* outlen)
  4158. {
  4159. int err = 0;
  4160. #if defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_ATECC508A) && \
  4161. !defined(WOLFSSL_ATECC608A)
  4162. CRYS_ECDH_TempData_t tempBuff;
  4163. #endif
  4164. (void)err;
  4165. if (private_key == NULL || public_key == NULL || out == NULL ||
  4166. outlen == NULL) {
  4167. return BAD_FUNC_ARG;
  4168. }
  4169. #ifdef WOLF_CRYPTO_CB
  4170. #ifndef WOLF_CRYPTO_CB_FIND
  4171. if (private_key->devId != INVALID_DEVID)
  4172. #endif
  4173. {
  4174. err = wc_CryptoCb_Ecdh(private_key, public_key, out, outlen);
  4175. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  4176. if (err != CRYPTOCB_UNAVAILABLE)
  4177. return err;
  4178. /* fall-through when unavailable */
  4179. #endif
  4180. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  4181. if (err == CRYPTOCB_UNAVAILABLE) {
  4182. err = NO_VALID_DEVID;
  4183. }
  4184. #endif
  4185. }
  4186. #endif
  4187. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  4188. /* type valid? */
  4189. if (private_key->type != ECC_PRIVATEKEY &&
  4190. private_key->type != ECC_PRIVATEKEY_ONLY) {
  4191. return ECC_BAD_ARG_E;
  4192. }
  4193. /* Verify domain params supplied */
  4194. if (wc_ecc_is_valid_idx(private_key->idx) == 0 || private_key->dp == NULL ||
  4195. wc_ecc_is_valid_idx(public_key->idx) == 0 || public_key->dp == NULL) {
  4196. return ECC_BAD_ARG_E;
  4197. }
  4198. /* Verify curve id matches */
  4199. if (private_key->dp->id != public_key->dp->id) {
  4200. return ECC_BAD_ARG_E;
  4201. }
  4202. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  4203. /* For SECP256R1 use hardware */
  4204. if (private_key->dp->id == ECC_SECP256R1) {
  4205. err = atmel_ecc_create_pms(private_key->slot, public_key->pubkey_raw, out);
  4206. *outlen = private_key->dp->size;
  4207. }
  4208. else {
  4209. err = NOT_COMPILED_IN;
  4210. }
  4211. #elif defined(WOLFSSL_CRYPTOCELL)
  4212. /* generate a secret*/
  4213. err = CRYS_ECDH_SVDP_DH(&public_key->ctx.pubKey,
  4214. &private_key->ctx.privKey,
  4215. out,
  4216. (uint32_t*)outlen,
  4217. &tempBuff);
  4218. if (err != SA_SILIB_RET_OK){
  4219. WOLFSSL_MSG("CRYS_ECDH_SVDP_DH for secret failed");
  4220. return err;
  4221. }
  4222. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  4223. err = silabs_ecc_shared_secret(private_key, public_key, out, outlen);
  4224. #elif defined(WOLFSSL_KCAPI_ECC)
  4225. err = KcapiEcc_SharedSecret(private_key, public_key, out, outlen);
  4226. #elif defined(WOLFSSL_SE050)
  4227. err = se050_ecc_shared_secret(private_key, public_key, out, outlen);
  4228. #else
  4229. err = wc_ecc_shared_secret_ex(private_key, &public_key->pubkey, out, outlen);
  4230. #endif /* WOLFSSL_ATECC508A */
  4231. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  4232. return err;
  4233. }
  4234. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  4235. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_KCAPI_ECC) && \
  4236. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  4237. int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
  4238. byte* out, word32* outlen)
  4239. {
  4240. int err = MP_OKAY;
  4241. mp_int* k = private_key->k;
  4242. #ifdef HAVE_ECC_CDH
  4243. #ifdef WOLFSSL_SMALL_STACK
  4244. mp_int *k_lcl = NULL;
  4245. #else
  4246. mp_int k_lcl[1];
  4247. #endif
  4248. #endif
  4249. #if defined(WOLFSSL_HAVE_SP_ECC) && defined(WC_ECC_NONBLOCK) && \
  4250. defined(WC_ECC_NONBLOCK_ONLY)
  4251. ecc_nb_ctx_t nb_ctx;
  4252. XMEMSET(&nb_ctx, 0, sizeof(nb_ctx));
  4253. #endif /* WOLFSSL_HAVE_SP_ECC && WC_ECC_NONBLOCK && WC_ECC_NONBLOCK_ONLY */
  4254. #ifdef HAVE_ECC_CDH
  4255. /* if cofactor flag has been set */
  4256. if (private_key->flags & WC_ECC_FLAG_COFACTOR) {
  4257. mp_digit cofactor = (mp_digit)private_key->dp->cofactor;
  4258. /* only perform cofactor calc if not equal to 1 */
  4259. if (cofactor != 1) {
  4260. #ifdef WOLFSSL_SMALL_STACK
  4261. if ((k_lcl = (mp_int *)XMALLOC(sizeof(*k_lcl), private_key->heap, DYNAMIC_TYPE_ECC_BUFFER)) == NULL)
  4262. return MEMORY_E;
  4263. #endif
  4264. k = k_lcl;
  4265. if (mp_init(k) != MP_OKAY) {
  4266. err = MEMORY_E;
  4267. goto errout;
  4268. }
  4269. /* multiply cofactor times private key "k" */
  4270. err = mp_mul_d(private_key->k, cofactor, k);
  4271. if (err != MP_OKAY)
  4272. goto errout;
  4273. }
  4274. }
  4275. #endif
  4276. #ifdef WOLFSSL_HAVE_SP_ECC
  4277. #ifndef WOLFSSL_SP_NO_256
  4278. if (private_key->idx != ECC_CUSTOM_IDX &&
  4279. ecc_sets[private_key->idx].id == ECC_SECP256R1) {
  4280. #ifndef WC_ECC_NONBLOCK
  4281. err = sp_ecc_secret_gen_256(k, point, out, outlen, private_key->heap);
  4282. #else
  4283. if (private_key->nb_ctx) {
  4284. err = sp_ecc_secret_gen_256_nb(&private_key->nb_ctx->sp_ctx, k,
  4285. point, out, outlen,
  4286. private_key->heap);
  4287. }
  4288. else {
  4289. #ifdef WC_ECC_NONBLOCK_ONLY
  4290. do { /* perform blocking call to non-blocking function */
  4291. err = sp_ecc_secret_gen_256_nb(&nb_ctx.sp_ctx, k, point, out,
  4292. outlen, private_key->heap);
  4293. } while (err == FP_WOULDBLOCK);
  4294. #else
  4295. err = sp_ecc_secret_gen_256(k, point, out, outlen,
  4296. private_key->heap);
  4297. #endif /* WC_ECC_NONBLOCK_ONLY */
  4298. }
  4299. #endif /* !WC_ECC_NONBLOCK */
  4300. }
  4301. else
  4302. #endif /* ! WOLFSSL_SP_NO_256 */
  4303. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  4304. if (private_key->idx != ECC_CUSTOM_IDX &&
  4305. ecc_sets[private_key->idx].id == ECC_SM2P256V1) {
  4306. err = sp_ecc_secret_gen_sm2_256(k, point, out, outlen,
  4307. private_key->heap);
  4308. }
  4309. else
  4310. #endif
  4311. #ifdef WOLFSSL_SP_384
  4312. if (private_key->idx != ECC_CUSTOM_IDX &&
  4313. ecc_sets[private_key->idx].id == ECC_SECP384R1) {
  4314. #ifndef WC_ECC_NONBLOCK
  4315. err = sp_ecc_secret_gen_384(k, point, out, outlen, private_key->heap);
  4316. #else
  4317. if (private_key->nb_ctx) {
  4318. err = sp_ecc_secret_gen_384_nb(&private_key->nb_ctx->sp_ctx, k,
  4319. point, out, outlen,
  4320. private_key->heap);
  4321. }
  4322. else {
  4323. #ifdef WC_ECC_NONBLOCK_ONLY
  4324. do { /* perform blocking call to non-blocking function */
  4325. err = sp_ecc_secret_gen_384_nb(&nb_ctx.sp_ctx, k, point, out,
  4326. outlen, private_key->heap);
  4327. } while (err == FP_WOULDBLOCK);
  4328. #else
  4329. err = sp_ecc_secret_gen_384(k, point, out, outlen,
  4330. private_key->heap);
  4331. #endif /* WC_ECC_NONBLOCK_ONLY */
  4332. }
  4333. #endif /* !WC_ECC_NONBLOCK */
  4334. }
  4335. else
  4336. #endif /* WOLFSSL_SP_384 */
  4337. #ifdef WOLFSSL_SP_521
  4338. if (private_key->idx != ECC_CUSTOM_IDX &&
  4339. ecc_sets[private_key->idx].id == ECC_SECP521R1) {
  4340. #ifndef WC_ECC_NONBLOCK
  4341. err = sp_ecc_secret_gen_521(k, point, out, outlen, private_key->heap);
  4342. #else
  4343. if (private_key->nb_ctx) {
  4344. err = sp_ecc_secret_gen_521_nb(&private_key->nb_ctx->sp_ctx, k,
  4345. point, out, outlen,
  4346. private_key->heap);
  4347. }
  4348. else {
  4349. #ifdef WC_ECC_NONBLOCK_ONLY
  4350. do { /* perform blocking call to non-blocking function */
  4351. err = sp_ecc_secret_gen_521_nb(&nb_ctx.sp_ctx, k, point, out,
  4352. outlen, private_key->heap);
  4353. } while (err == FP_WOULDBLOCK);
  4354. #else
  4355. err = sp_ecc_secret_gen_521(k, point, out, outlen,
  4356. private_key->heap);
  4357. #endif /* WC_ECC_NONBLOCK_ONLY */
  4358. }
  4359. #endif /* !WC_ECC_NONBLOCK */
  4360. }
  4361. else
  4362. #endif /* WOLFSSL_SP_521 */
  4363. #else
  4364. (void)point;
  4365. (void)out;
  4366. (void)outlen;
  4367. (void)k;
  4368. #endif
  4369. #if defined(WOLFSSL_SP_MATH)
  4370. {
  4371. err = WC_KEY_SIZE_E;
  4372. goto errout;
  4373. }
  4374. #else
  4375. {
  4376. ecc_point* result = NULL;
  4377. #ifdef WOLFSSL_NO_MALLOC
  4378. ecc_point lcl_result;
  4379. #endif
  4380. int x = 0;
  4381. mp_digit mp = 0;
  4382. DECLARE_CURVE_SPECS(3);
  4383. /* load curve info */
  4384. ALLOC_CURVE_SPECS(3, err);
  4385. if (err == MP_OKAY) {
  4386. err = wc_ecc_curve_load(private_key->dp, &curve,
  4387. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
  4388. ECC_CURVE_FIELD_ORDER));
  4389. }
  4390. if (err != MP_OKAY) {
  4391. FREE_CURVE_SPECS();
  4392. goto errout;
  4393. }
  4394. /* make new point */
  4395. #ifdef WOLFSSL_NO_MALLOC
  4396. result = &lcl_result;
  4397. #endif
  4398. err = wc_ecc_new_point_ex(&result, private_key->heap);
  4399. if (err != MP_OKAY) {
  4400. wc_ecc_curve_free(curve);
  4401. FREE_CURVE_SPECS();
  4402. goto errout;
  4403. }
  4404. #ifdef ECC_TIMING_RESISTANT
  4405. if (private_key->rng == NULL) {
  4406. err = MISSING_RNG_E;
  4407. }
  4408. #endif
  4409. if (err == MP_OKAY) {
  4410. /* Map in a separate call as this should be constant time */
  4411. #ifdef ECC_TIMING_RESISTANT
  4412. err = wc_ecc_mulmod_ex2(k, point, result, curve->Af, curve->prime,
  4413. curve->order, private_key->rng, 0,
  4414. private_key->heap);
  4415. #else
  4416. err = wc_ecc_mulmod_ex2(k, point, result, curve->Af, curve->prime,
  4417. curve->order, NULL, 0, private_key->heap);
  4418. #endif
  4419. }
  4420. if (err == MP_OKAY) {
  4421. #ifdef WOLFSSL_CHECK_MEM_ZERO
  4422. mp_memzero_add("wc_ecc_shared_secret_gen_sync result->x",
  4423. result->x);
  4424. mp_memzero_add("wc_ecc_shared_secret_gen_sync result->y",
  4425. result->y);
  4426. #endif
  4427. err = mp_montgomery_setup(curve->prime, &mp);
  4428. }
  4429. if (err == MP_OKAY) {
  4430. /* Use constant time map if compiled in */
  4431. err = ecc_map_ex(result, curve->prime, mp, 1);
  4432. }
  4433. if (err == MP_OKAY) {
  4434. x = mp_unsigned_bin_size(curve->prime);
  4435. if (*outlen < (word32)x || x < mp_unsigned_bin_size(result->x)) {
  4436. err = BUFFER_E;
  4437. }
  4438. }
  4439. if (err == MP_OKAY) {
  4440. XMEMSET(out, 0, (size_t)x);
  4441. err = mp_to_unsigned_bin(result->x, out +
  4442. (x - mp_unsigned_bin_size(result->x)));
  4443. }
  4444. *outlen = (word32)x;
  4445. mp_forcezero(result->x);
  4446. mp_forcezero(result->y);
  4447. wc_ecc_del_point_ex(result, private_key->heap);
  4448. wc_ecc_curve_free(curve);
  4449. FREE_CURVE_SPECS();
  4450. }
  4451. #endif
  4452. errout:
  4453. #ifdef HAVE_ECC_CDH
  4454. if (k == k_lcl)
  4455. mp_clear(k);
  4456. #ifdef WOLFSSL_SMALL_STACK
  4457. if (k_lcl != NULL)
  4458. XFREE(k_lcl, private_key->heap, DYNAMIC_TYPE_ECC_BUFFER);
  4459. #endif
  4460. #endif
  4461. return err;
  4462. }
  4463. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  4464. static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
  4465. ecc_point* point, byte* out, word32 *outlen)
  4466. {
  4467. int err = 0;
  4468. #if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
  4469. DECLARE_CURVE_SPECS(3);
  4470. /* load curve info */
  4471. ALLOC_CURVE_SPECS(3, err);
  4472. if (err == MP_OKAY) {
  4473. err = wc_ecc_curve_load(private_key->dp, &curve,
  4474. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
  4475. ECC_CURVE_FIELD_ORDER));
  4476. }
  4477. if (err != MP_OKAY) {
  4478. FREE_CURVE_SPECS();
  4479. return err;
  4480. }
  4481. if (private_key->dp
  4482. #ifdef WOLFSSL_CUSTOM_CURVES
  4483. && private_key->dp->id != ECC_CURVE_CUSTOM
  4484. #endif
  4485. #ifdef HAVE_CAVIUM_V
  4486. /* verify the curve is supported by hardware */
  4487. && NitroxEccIsCurveSupported(private_key)
  4488. #endif
  4489. ) {
  4490. word32 keySz = private_key->dp->size;
  4491. /* sync public key x/y */
  4492. err = wc_mp_to_bigint_sz(private_key->k, &private_key->k->raw, keySz);
  4493. if (err == MP_OKAY)
  4494. err = wc_mp_to_bigint_sz(point->x, &point->x->raw, keySz);
  4495. if (err == MP_OKAY)
  4496. err = wc_mp_to_bigint_sz(point->y, &point->y->raw, keySz);
  4497. #ifdef HAVE_CAVIUM_V
  4498. /* allocate buffer for output */
  4499. if (err == MP_OKAY)
  4500. err = wc_ecc_alloc_mpint(private_key, &private_key->e);
  4501. if (err == MP_OKAY)
  4502. err = wc_bigint_alloc(&private_key->e->raw,
  4503. NitroxEccGetSize(private_key)*2);
  4504. if (err == MP_OKAY)
  4505. err = NitroxEcdh(private_key,
  4506. &private_key->k->raw, &point->x->raw, &point->y->raw,
  4507. private_key->e->raw.buf, &private_key->e->raw.len,
  4508. &curve->prime->raw);
  4509. #else
  4510. if (err == MP_OKAY)
  4511. err = wc_ecc_curve_load(private_key->dp, &curve, ECC_CURVE_FIELD_BF);
  4512. if (err == MP_OKAY)
  4513. err = IntelQaEcdh(&private_key->asyncDev,
  4514. &private_key->k->raw, &point->x->raw, &point->y->raw,
  4515. out, outlen,
  4516. &curve->Af->raw, &curve->Bf->raw, &curve->prime->raw,
  4517. private_key->dp->cofactor);
  4518. #endif
  4519. }
  4520. else
  4521. #elif defined(WOLFSSL_ASYNC_CRYPT_SW)
  4522. if (wc_AsyncSwInit(&private_key->asyncDev, ASYNC_SW_ECC_SHARED_SEC)) {
  4523. WC_ASYNC_SW* sw = &private_key->asyncDev.sw;
  4524. sw->eccSharedSec.private_key = private_key;
  4525. sw->eccSharedSec.public_point = point;
  4526. sw->eccSharedSec.out = out;
  4527. sw->eccSharedSec.outLen = outlen;
  4528. err = WC_PENDING_E;
  4529. }
  4530. else
  4531. #endif
  4532. {
  4533. /* use sync in other cases */
  4534. err = wc_ecc_shared_secret_gen_sync(private_key, point, out, outlen);
  4535. }
  4536. if (err == WC_PENDING_E) {
  4537. private_key->state++;
  4538. }
  4539. #if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
  4540. wc_ecc_curve_free(curve);
  4541. FREE_CURVE_SPECS();
  4542. #endif
  4543. return err;
  4544. }
  4545. #endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
  4546. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  4547. /**
  4548. Create an ECC shared secret between private key and public point
  4549. private_key The private ECC key (heap hint based on private key)
  4550. point The point to use (public key)
  4551. out [out] Destination of the shared secret
  4552. Conforms to EC-DH from ANSI X9.63
  4553. outlen [in/out] The max size and resulting size of the shared secret
  4554. return MP_OKAY if successful
  4555. */
  4556. int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
  4557. byte* out, word32 *outlen)
  4558. {
  4559. int err;
  4560. if (private_key == NULL || point == NULL || out == NULL ||
  4561. outlen == NULL) {
  4562. return BAD_FUNC_ARG;
  4563. }
  4564. /* type valid? */
  4565. if (private_key->type != ECC_PRIVATEKEY &&
  4566. private_key->type != ECC_PRIVATEKEY_ONLY) {
  4567. WOLFSSL_MSG("ECC_BAD_ARG_E");
  4568. return ECC_BAD_ARG_E;
  4569. }
  4570. /* Verify domain params supplied */
  4571. if (wc_ecc_is_valid_idx(private_key->idx) == 0 || private_key->dp == NULL) {
  4572. WOLFSSL_MSG("wc_ecc_is_valid_idx failed");
  4573. return ECC_BAD_ARG_E;
  4574. }
  4575. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  4576. switch (private_key->state) {
  4577. case ECC_STATE_NONE:
  4578. case ECC_STATE_SHARED_SEC_GEN:
  4579. private_key->state = ECC_STATE_SHARED_SEC_GEN;
  4580. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  4581. if (private_key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  4582. err = wc_ecc_shared_secret_gen_async(private_key, point,
  4583. out, outlen);
  4584. }
  4585. else
  4586. #endif
  4587. {
  4588. err = wc_ecc_shared_secret_gen_sync(private_key, point,
  4589. out, outlen);
  4590. }
  4591. if (err < 0) {
  4592. break;
  4593. }
  4594. FALL_THROUGH;
  4595. case ECC_STATE_SHARED_SEC_RES:
  4596. private_key->state = ECC_STATE_SHARED_SEC_RES;
  4597. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  4598. if (private_key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  4599. #ifdef HAVE_CAVIUM_V
  4600. /* verify the curve is supported by hardware */
  4601. if (NitroxEccIsCurveSupported(private_key)) {
  4602. /* copy output */
  4603. *outlen = private_key->dp->size;
  4604. XMEMCPY(out, private_key->e->raw.buf, *outlen);
  4605. }
  4606. #endif /* HAVE_CAVIUM_V */
  4607. }
  4608. #endif /* WOLFSSL_ASYNC_CRYPT */
  4609. err = 0;
  4610. break;
  4611. default:
  4612. err = BAD_STATE_E;
  4613. } /* switch */
  4614. RESTORE_VECTOR_REGISTERS();
  4615. /* if async pending then return and skip done cleanup below */
  4616. if (err == WC_PENDING_E) {
  4617. return err;
  4618. }
  4619. /* cleanup */
  4620. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  4621. wc_ecc_free_async(private_key);
  4622. #endif
  4623. private_key->state = ECC_STATE_NONE;
  4624. return err;
  4625. }
  4626. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  4627. #elif defined(WOLFSSL_KCAPI_ECC)
  4628. int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
  4629. byte* out, word32 *outlen)
  4630. {
  4631. int err;
  4632. ecc_key public_key;
  4633. err = wc_ecc_init_ex(&public_key, private_key->heap, INVALID_DEVID);
  4634. if (err == MP_OKAY) {
  4635. err = wc_ecc_set_curve(&public_key, private_key->dp->size,
  4636. private_key->dp->id);
  4637. if (err == MP_OKAY) {
  4638. err = mp_copy(point->x, public_key.pubkey.x);
  4639. }
  4640. if (err == MP_OKAY) {
  4641. err = mp_copy(point->y, public_key.pubkey.y);
  4642. }
  4643. if (err == MP_OKAY) {
  4644. err = wc_ecc_shared_secret(private_key, &public_key, out, outlen);
  4645. }
  4646. wc_ecc_free(&public_key);
  4647. }
  4648. return err;
  4649. }
  4650. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_CRYPTOCELL && !WOLFSSL_KCAPI_ECC */
  4651. #endif /* HAVE_ECC_DHE */
  4652. #ifdef USE_ECC_B_PARAM
  4653. /* Checks if a point p lies on the curve with index curve_idx */
  4654. int wc_ecc_point_is_on_curve(ecc_point *p, int curve_idx)
  4655. {
  4656. int err = MP_OKAY;
  4657. DECLARE_CURVE_SPECS(3);
  4658. if (p == NULL)
  4659. return BAD_FUNC_ARG;
  4660. /* is the IDX valid ? */
  4661. if (wc_ecc_is_valid_idx(curve_idx) == 0) {
  4662. return ECC_BAD_ARG_E;
  4663. }
  4664. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  4665. ALLOC_CURVE_SPECS(3, err);
  4666. if (err == MP_OKAY) {
  4667. err = wc_ecc_curve_load(wc_ecc_get_curve_params(curve_idx), &curve,
  4668. ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
  4669. ECC_CURVE_FIELD_BF);
  4670. }
  4671. if (err == MP_OKAY) {
  4672. err = wc_ecc_is_point(p, curve->Af, curve->Bf, curve->prime);
  4673. }
  4674. wc_ecc_curve_free(curve);
  4675. FREE_CURVE_SPECS();
  4676. RESTORE_VECTOR_REGISTERS();
  4677. return err;
  4678. }
  4679. #endif /* USE_ECC_B_PARAM */
  4680. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  4681. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  4682. /* return 1 if point is at infinity, 0 if not, < 0 on error */
  4683. int wc_ecc_point_is_at_infinity(ecc_point* p)
  4684. {
  4685. if (p == NULL)
  4686. return BAD_FUNC_ARG;
  4687. if (mp_iszero(p->x) && mp_iszero(p->y))
  4688. return 1;
  4689. return 0;
  4690. }
  4691. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_CRYPTOCELL */
  4692. /* generate random and ensure its greater than 0 and less than order */
  4693. int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order)
  4694. {
  4695. #ifndef WC_NO_RNG
  4696. int err;
  4697. byte buf[ECC_MAXSIZE_GEN];
  4698. if (rng == NULL || size < 0 || size + 8 > ECC_MAXSIZE_GEN || k == NULL ||
  4699. order == NULL) {
  4700. return BAD_FUNC_ARG;
  4701. }
  4702. /* generate 8 extra bytes to mitigate bias from the modulo operation below */
  4703. /* see section A.1.2 in 'Suite B Implementor's Guide to FIPS 186-3 (ECDSA)' */
  4704. size += 8;
  4705. /* make up random string */
  4706. err = wc_RNG_GenerateBlock(rng, buf, (word32)size);
  4707. #ifdef WOLFSSL_CHECK_MEM_ZERO
  4708. wc_MemZero_Add("wc_ecc_gen_k buf", buf, size);
  4709. #endif
  4710. /* load random buffer data into k */
  4711. if (err == 0)
  4712. err = mp_read_unsigned_bin(k, buf, (word32)size);
  4713. /* the key should be smaller than the order of base point */
  4714. if (err == MP_OKAY) {
  4715. if (mp_cmp(k, order) != MP_LT) {
  4716. err = mp_mod(k, order, k);
  4717. }
  4718. }
  4719. /* quick sanity check to make sure we're not dealing with a 0 key */
  4720. if (err == MP_OKAY) {
  4721. if (mp_iszero(k) == MP_YES)
  4722. err = MP_ZERO_E;
  4723. }
  4724. ForceZero(buf, ECC_MAXSIZE_GEN);
  4725. #ifdef WOLFSSL_CHECK_MEM_ZERO
  4726. wc_MemZero_Check(buf, ECC_MAXSIZE_GEN);
  4727. #endif
  4728. return err;
  4729. #else
  4730. (void)rng;
  4731. (void)size;
  4732. (void)k;
  4733. (void)order;
  4734. return NOT_COMPILED_IN;
  4735. #endif /* !WC_NO_RNG */
  4736. }
  4737. static WC_INLINE void wc_ecc_reset(ecc_key* key)
  4738. {
  4739. /* make sure required key variables are reset */
  4740. key->state = ECC_STATE_NONE;
  4741. }
  4742. /* create the public ECC key from a private key
  4743. *
  4744. * key an initialized private key to generate public part from
  4745. * curve [in]curve for key, cannot be NULL
  4746. * pubOut [out]ecc_point holding the public key, if NULL then public key part
  4747. * is cached in key instead.
  4748. *
  4749. * Note this function is local to the file because of the argument type
  4750. * ecc_curve_spec. Having this argument allows for not having to load the
  4751. * curve type multiple times when generating a key with wc_ecc_make_key().
  4752. * For async the results are placed directly into pubOut, so this function
  4753. * does not need to be called again
  4754. *
  4755. * returns MP_OKAY on success
  4756. */
  4757. static int ecc_make_pub_ex(ecc_key* key, ecc_curve_spec* curve,
  4758. ecc_point* pubOut, WC_RNG* rng)
  4759. {
  4760. int err = MP_OKAY;
  4761. #ifdef HAVE_ECC_MAKE_PUB
  4762. ecc_point* pub;
  4763. #endif /* HAVE_ECC_MAKE_PUB */
  4764. (void)rng;
  4765. if (key == NULL) {
  4766. return BAD_FUNC_ARG;
  4767. }
  4768. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  4769. #ifdef HAVE_ECC_MAKE_PUB
  4770. /* if ecc_point passed in then use it as output for public key point */
  4771. if (pubOut != NULL) {
  4772. pub = pubOut;
  4773. }
  4774. else {
  4775. /* caching public key making it a ECC_PRIVATEKEY instead of
  4776. ECC_PRIVATEKEY_ONLY */
  4777. pub = &key->pubkey;
  4778. key->type = ECC_PRIVATEKEY_ONLY;
  4779. }
  4780. if ((err == MP_OKAY) && (mp_iszero(key->k) || mp_isneg(key->k) ||
  4781. (mp_cmp(key->k, curve->order) != MP_LT)))
  4782. {
  4783. err = ECC_PRIV_KEY_E;
  4784. }
  4785. if (err == MP_OKAY) {
  4786. #ifndef ALT_ECC_SIZE
  4787. err = mp_init_multi(pub->x, pub->y, pub->z, NULL, NULL, NULL);
  4788. #else
  4789. pub->x = (mp_int*)&pub->xyz[0];
  4790. pub->y = (mp_int*)&pub->xyz[1];
  4791. pub->z = (mp_int*)&pub->xyz[2];
  4792. alt_fp_init(pub->x);
  4793. alt_fp_init(pub->y);
  4794. alt_fp_init(pub->z);
  4795. #endif
  4796. }
  4797. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC_KEYGEN) && \
  4798. defined(HAVE_INTEL_QA)
  4799. if (err == MP_OKAY && key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  4800. word32 keySz = key->dp->size;
  4801. /* sync private key to raw */
  4802. err = wc_mp_to_bigint_sz(key->k, &key->k->raw, keySz);
  4803. if (err == MP_OKAY) {
  4804. err = IntelQaEccPointMul(&key->asyncDev,
  4805. &key->k->raw, pub->x, pub->y, pub->z,
  4806. &curve->Gx->raw, &curve->Gy->raw,
  4807. &curve->Af->raw, &curve->Bf->raw, &curve->prime->raw,
  4808. key->dp->cofactor);
  4809. }
  4810. }
  4811. else
  4812. #endif
  4813. { /* BEGIN: Software Crypto */
  4814. #ifdef WOLFSSL_HAVE_SP_ECC
  4815. /* Single-Precision Math (optimized for specific curves) */
  4816. if (err != MP_OKAY) {
  4817. }
  4818. else
  4819. #ifndef WOLFSSL_SP_NO_256
  4820. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1) {
  4821. err = sp_ecc_mulmod_base_256(key->k, pub, 1, key->heap);
  4822. }
  4823. else
  4824. #endif /* WOLFSSL_SP_NO_256 */
  4825. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  4826. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SM2P256V1) {
  4827. err = sp_ecc_mulmod_base_sm2_256(key->k, pub, 1, key->heap);
  4828. }
  4829. else
  4830. #endif
  4831. #ifdef WOLFSSL_SP_384
  4832. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP384R1) {
  4833. err = sp_ecc_mulmod_base_384(key->k, pub, 1, key->heap);
  4834. }
  4835. else
  4836. #endif
  4837. #ifdef WOLFSSL_SP_521
  4838. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP521R1) {
  4839. err = sp_ecc_mulmod_base_521(key->k, pub, 1, key->heap);
  4840. }
  4841. else
  4842. #endif
  4843. #endif /* WOLFSSL_HAVE_SP_ECC */
  4844. #if defined(WOLFSSL_SP_MATH)
  4845. err = WC_KEY_SIZE_E;
  4846. #else
  4847. if (err == MP_OKAY) {
  4848. /* Multi-Precision Math: compute public curve */
  4849. mp_digit mp = 0;
  4850. ecc_point* base = NULL;
  4851. #ifdef WOLFSSL_NO_MALLOC
  4852. ecc_point lcl_base;
  4853. base = &lcl_base;
  4854. #endif
  4855. err = wc_ecc_new_point_ex(&base, key->heap);
  4856. /* read in the x/y for this key */
  4857. if (err == MP_OKAY)
  4858. err = mp_copy(curve->Gx, base->x);
  4859. if (err == MP_OKAY)
  4860. err = mp_copy(curve->Gy, base->y);
  4861. if (err == MP_OKAY)
  4862. err = mp_montgomery_setup(curve->prime, &mp);
  4863. if (err == MP_OKAY)
  4864. err = mp_set(base->z, 1);
  4865. /* make the public key */
  4866. if (err == MP_OKAY) {
  4867. /* Map in a separate call as this should be constant time */
  4868. err = wc_ecc_mulmod_ex2(key->k, base, pub, curve->Af, curve->prime,
  4869. curve->order, rng, 0, key->heap);
  4870. if (err == MP_MEM) {
  4871. err = MEMORY_E;
  4872. }
  4873. }
  4874. if (err == MP_OKAY) {
  4875. /* Use constant time map if compiled in */
  4876. err = ecc_map_ex(pub, curve->prime, mp, 1);
  4877. }
  4878. wc_ecc_del_point_ex(base, key->heap);
  4879. }
  4880. #endif /* WOLFSSL_SP_MATH */
  4881. } /* END: Software Crypto */
  4882. if (err != MP_OKAY
  4883. #ifdef WOLFSSL_ASYNC_CRYPT
  4884. && err != WC_PENDING_E
  4885. #endif
  4886. ) {
  4887. /* clean up if failed */
  4888. #ifndef ALT_ECC_SIZE
  4889. mp_clear(pub->x);
  4890. mp_clear(pub->y);
  4891. mp_clear(pub->z);
  4892. #endif
  4893. }
  4894. #else
  4895. /* Using hardware crypto, that does not support ecc_make_pub_ex */
  4896. (void)curve;
  4897. err = NOT_COMPILED_IN;
  4898. #endif /* HAVE_ECC_MAKE_PUB */
  4899. /* change key state if public part is cached */
  4900. if (key->type == ECC_PRIVATEKEY_ONLY && pubOut == NULL) {
  4901. key->type = ECC_PRIVATEKEY;
  4902. }
  4903. RESTORE_VECTOR_REGISTERS();
  4904. return err;
  4905. }
  4906. /* create the public ECC key from a private key
  4907. *
  4908. * key an initialized private key to generate public part from
  4909. * pubOut [out]ecc_point holding the public key, if NULL then public key part
  4910. * is cached in key instead.
  4911. *
  4912. *
  4913. * returns MP_OKAY on success
  4914. */
  4915. int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut)
  4916. {
  4917. WOLFSSL_ENTER("wc_ecc_make_pub");
  4918. return wc_ecc_make_pub_ex(key, pubOut, NULL);
  4919. }
  4920. /* create the public ECC key from a private key - mask timing use random z
  4921. *
  4922. * key an initialized private key to generate public part from
  4923. * pubOut [out]ecc_point holding the public key, if NULL then public key part
  4924. * is cached in key instead.
  4925. *
  4926. *
  4927. * returns MP_OKAY on success
  4928. */
  4929. int wc_ecc_make_pub_ex(ecc_key* key, ecc_point* pubOut, WC_RNG* rng)
  4930. {
  4931. int err = MP_OKAY;
  4932. DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT);
  4933. WOLFSSL_ENTER("wc_ecc_make_pub_ex");
  4934. if (key == NULL) {
  4935. return BAD_FUNC_ARG;
  4936. }
  4937. /* load curve info */
  4938. ALLOC_CURVE_SPECS(ECC_CURVE_FIELD_COUNT, err);
  4939. if (err == MP_OKAY) {
  4940. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  4941. }
  4942. if (err == MP_OKAY) {
  4943. err = ecc_make_pub_ex(key, curve, pubOut, rng);
  4944. }
  4945. wc_ecc_curve_free(curve);
  4946. FREE_CURVE_SPECS();
  4947. return err;
  4948. }
  4949. static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
  4950. int curve_id, int flags)
  4951. {
  4952. int err = 0;
  4953. #if defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_ATECC508A) && \
  4954. !defined(WOLFSSL_ATECC608A)
  4955. const CRYS_ECPKI_Domain_t* pDomain;
  4956. CRYS_ECPKI_KG_TempData_t tempBuff;
  4957. CRYS_ECPKI_KG_FipsContext_t fipsCtx;
  4958. byte ucompressed_key[ECC_MAX_CRYPTO_HW_SIZE*2 + 1];
  4959. word32 raw_size = 0;
  4960. #endif
  4961. #if defined(WOLFSSL_HAVE_SP_ECC) && defined(WC_ECC_NONBLOCK) && \
  4962. defined(WC_ECC_NONBLOCK_ONLY)
  4963. ecc_nb_ctx_t nb_ctx;
  4964. XMEMSET(&nb_ctx, 0, sizeof(nb_ctx));
  4965. #endif /* WOLFSSL_HAVE_SP_ECC && WC_ECC_NONBLOCK && WC_ECC_NONBLOCK_ONLY */
  4966. if (key == NULL || rng == NULL) {
  4967. return BAD_FUNC_ARG;
  4968. }
  4969. /* make sure required variables are reset */
  4970. wc_ecc_reset(key);
  4971. err = wc_ecc_set_curve(key, keysize, curve_id);
  4972. if (err != 0) {
  4973. return err;
  4974. }
  4975. key->flags = (byte)flags;
  4976. #ifdef WOLF_CRYPTO_CB
  4977. #ifndef WOLF_CRYPTO_CB_FIND
  4978. if (key->devId != INVALID_DEVID)
  4979. #endif
  4980. {
  4981. err = wc_CryptoCb_MakeEccKey(rng, keysize, key, curve_id);
  4982. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  4983. if (err != CRYPTOCB_UNAVAILABLE)
  4984. return err;
  4985. /* fall-through when unavailable */
  4986. #endif
  4987. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  4988. if (err == CRYPTOCB_UNAVAILABLE) {
  4989. return NO_VALID_DEVID;
  4990. }
  4991. return err;
  4992. #endif
  4993. }
  4994. #endif
  4995. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  4996. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  4997. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  4998. #ifdef HAVE_CAVIUM
  4999. /* TODO: Not implemented */
  5000. #elif defined(HAVE_INTEL_QA)
  5001. /* Implemented in ecc_make_pub_ex for the pub calc */
  5002. #elif defined(WOLFSSL_ASYNC_CRYPT_SW)
  5003. if (wc_AsyncSwInit(&key->asyncDev, ASYNC_SW_ECC_MAKE)) {
  5004. WC_ASYNC_SW* sw = &key->asyncDev.sw;
  5005. sw->eccMake.rng = rng;
  5006. sw->eccMake.key = key;
  5007. sw->eccMake.size = keysize;
  5008. sw->eccMake.curve_id = curve_id;
  5009. return WC_PENDING_E;
  5010. }
  5011. #endif
  5012. }
  5013. #endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
  5014. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  5015. if (key->dp->id == ECC_SECP256R1) {
  5016. key->type = ECC_PRIVATEKEY;
  5017. key->slot = atmel_ecc_alloc(ATMEL_SLOT_ECDHE);
  5018. err = atmel_ecc_create_key(key->slot, key->pubkey_raw);
  5019. /* populate key->pubkey */
  5020. if (err == 0
  5021. #ifdef ALT_ECC_SIZE
  5022. && key->pubkey.x
  5023. #endif
  5024. ) {
  5025. err = mp_read_unsigned_bin(key->pubkey.x, key->pubkey_raw,
  5026. ECC_MAX_CRYPTO_HW_SIZE);
  5027. }
  5028. if (err == 0
  5029. #ifdef ALT_ECC_SIZE
  5030. && key->pubkey.y
  5031. #endif
  5032. ) {
  5033. err = mp_read_unsigned_bin(key->pubkey.y,
  5034. key->pubkey_raw + ECC_MAX_CRYPTO_HW_SIZE,
  5035. ECC_MAX_CRYPTO_HW_SIZE);
  5036. }
  5037. }
  5038. else {
  5039. err = NOT_COMPILED_IN;
  5040. }
  5041. #elif defined(WOLFSSL_SE050)
  5042. err = se050_ecc_create_key(key, key->dp->id, key->dp->size);
  5043. key->type = ECC_PRIVATEKEY;
  5044. #elif defined(WOLFSSL_CRYPTOCELL)
  5045. pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id));
  5046. raw_size = (word32)(key->dp->size)*2 + 1;
  5047. /* generate first key pair */
  5048. err = CRYS_ECPKI_GenKeyPair(&wc_rndState,
  5049. wc_rndGenVectFunc,
  5050. pDomain,
  5051. &key->ctx.privKey,
  5052. &key->ctx.pubKey,
  5053. &tempBuff,
  5054. &fipsCtx);
  5055. if (err != SA_SILIB_RET_OK){
  5056. WOLFSSL_MSG("CRYS_ECPKI_GenKeyPair for key pair failed");
  5057. return err;
  5058. }
  5059. key->type = ECC_PRIVATEKEY;
  5060. err = CRYS_ECPKI_ExportPublKey(&key->ctx.pubKey,
  5061. CRYS_EC_PointUncompressed,
  5062. &ucompressed_key[0],
  5063. (uint32_t*)&raw_size);
  5064. if (err == SA_SILIB_RET_OK && key->pubkey.x && key->pubkey.y) {
  5065. err = mp_read_unsigned_bin(key->pubkey.x,
  5066. &ucompressed_key[1], key->dp->size);
  5067. if (err == MP_OKAY) {
  5068. err = mp_read_unsigned_bin(key->pubkey.y,
  5069. &ucompressed_key[1+key->dp->size],key->dp->size);
  5070. }
  5071. }
  5072. raw_size = key->dp->size;
  5073. if (err == MP_OKAY) {
  5074. err = CRYS_ECPKI_ExportPrivKey(&key->ctx.privKey,
  5075. ucompressed_key,
  5076. (uint32_t*)&raw_size);
  5077. }
  5078. if (err == SA_SILIB_RET_OK) {
  5079. err = mp_read_unsigned_bin(key->k, ucompressed_key, raw_size);
  5080. }
  5081. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  5082. return silabs_ecc_make_key(key, keysize);
  5083. #elif defined(WOLFSSL_KCAPI_ECC)
  5084. err = KcapiEcc_MakeKey(key, keysize, curve_id);
  5085. (void)rng;
  5086. #elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  5087. if (xil_curve_type[key->dp->id] == 0)
  5088. return ECC_CURVE_OID_E;
  5089. err = wc_RNG_GenerateBlock(rng, key->privKey, key->dp->size);
  5090. if (err)
  5091. return err;
  5092. /* Make sure that private key is max. 521 bits */
  5093. if (key->dp->size == 66)
  5094. key->privKey[65] &= 0x1U;
  5095. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(key->privKey), key->dp->size);
  5096. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(key->keyRaw),
  5097. 2 * key->dp->size);
  5098. err = XSecure_EllipticGenerateKey(&(key->xSec.cinst),
  5099. xil_curve_type[key->dp->id],
  5100. XIL_CAST_U64(key->privKey),
  5101. XIL_CAST_U64(key->keyRaw));
  5102. if (err != XST_SUCCESS) {
  5103. WOLFSSL_XIL_ERROR("Generate ECC key failed", err);
  5104. err = WC_HW_E;
  5105. }
  5106. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(key->keyRaw),
  5107. 2 * key->dp->size);
  5108. #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN
  5109. if (err == 0)
  5110. err = XSecure_EllipticValidateKey(&(key->xSec.cinst),
  5111. xil_curve_type[key->dp->id],
  5112. XIL_CAST_U64(key->keyRaw));
  5113. #endif
  5114. if (err == 0)
  5115. err = xil_mpi_import(key->pubkey.x, key->keyRaw, key->dp->size,
  5116. key->heap);
  5117. if (err == 0)
  5118. err = xil_mpi_import(key->pubkey.y, key->keyRaw + key->dp->size,
  5119. key->dp->size, key->heap);
  5120. if (err == 0)
  5121. err = xil_mpi_import(key->k, key->privKey, key->dp->size, key->heap);
  5122. if (err == 0)
  5123. err = mp_set(key->pubkey.z, 1);
  5124. if (err) {
  5125. key->privKey = NULL;
  5126. XMEMSET(key->keyRaw, 0, sizeof(key->keyRaw));
  5127. return err;
  5128. }
  5129. key->type = ECC_PRIVATEKEY;
  5130. #else
  5131. #ifdef WOLFSSL_HAVE_SP_ECC
  5132. #ifndef WOLFSSL_SP_NO_256
  5133. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1) {
  5134. #ifndef WC_ECC_NONBLOCK
  5135. err = sp_ecc_make_key_256(rng, key->k, &key->pubkey, key->heap);
  5136. #else
  5137. if (key->nb_ctx) {
  5138. err = sp_ecc_make_key_256_nb(&key->nb_ctx->sp_ctx, rng, key->k,
  5139. &key->pubkey, key->heap);
  5140. }
  5141. else {
  5142. #ifdef WC_ECC_NONBLOCK_ONLY
  5143. do { /* perform blocking call to non-blocking function */
  5144. err = sp_ecc_make_key_256_nb(&nb_ctx.sp_ctx, rng, key->k,
  5145. &key->pubkey, key->heap);
  5146. } while (err == FP_WOULDBLOCK);
  5147. #else
  5148. err = sp_ecc_make_key_256(rng, key->k, &key->pubkey, key->heap);
  5149. #endif /* WC_ECC_NONBLOCK_ONLY */
  5150. }
  5151. #endif /* !WC_ECC_NONBLOCK */
  5152. if (err == MP_OKAY) {
  5153. key->type = ECC_PRIVATEKEY;
  5154. }
  5155. }
  5156. else
  5157. #endif /* !WOLFSSL_SP_NO_256 */
  5158. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  5159. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SM2P256V1) {
  5160. err = sp_ecc_make_key_sm2_256(rng, key->k, &key->pubkey, key->heap);
  5161. if (err == MP_OKAY) {
  5162. key->type = ECC_PRIVATEKEY;
  5163. }
  5164. }
  5165. else
  5166. #endif
  5167. #ifdef WOLFSSL_SP_384
  5168. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP384R1) {
  5169. #ifndef WC_ECC_NONBLOCK
  5170. err = sp_ecc_make_key_384(rng, key->k, &key->pubkey, key->heap);
  5171. #else
  5172. if (key->nb_ctx) {
  5173. err = sp_ecc_make_key_384_nb(&key->nb_ctx->sp_ctx, rng, key->k,
  5174. &key->pubkey, key->heap);
  5175. }
  5176. else {
  5177. #ifdef WC_ECC_NONBLOCK_ONLY
  5178. do { /* perform blocking call to non-blocking function */
  5179. err = sp_ecc_make_key_384_nb(&nb_ctx.sp_ctx, rng, key->k,
  5180. &key->pubkey, key->heap);
  5181. } while (err == FP_WOULDBLOCK);
  5182. #else
  5183. err = sp_ecc_make_key_384(rng, key->k, &key->pubkey, key->heap);
  5184. #endif /* WC_ECC_NONBLOCK_ONLY */
  5185. }
  5186. #endif /* !WC_ECC_NONBLOCK */
  5187. if (err == MP_OKAY) {
  5188. key->type = ECC_PRIVATEKEY;
  5189. }
  5190. }
  5191. else
  5192. #endif /* WOLFSSL_SP_384 */
  5193. #ifdef WOLFSSL_SP_521
  5194. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP521R1) {
  5195. #ifndef WC_ECC_NONBLOCK
  5196. err = sp_ecc_make_key_521(rng, key->k, &key->pubkey, key->heap);
  5197. #else
  5198. if (key->nb_ctx) {
  5199. err = sp_ecc_make_key_521_nb(&key->nb_ctx->sp_ctx, rng, key->k,
  5200. &key->pubkey, key->heap);
  5201. }
  5202. else {
  5203. #ifdef WC_ECC_NONBLOCK_ONLY
  5204. do { /* perform blocking call to non-blocking function */
  5205. err = sp_ecc_make_key_521_nb(&nb_ctx.sp_ctx, rng, key->k,
  5206. &key->pubkey, key->heap);
  5207. } while (err == FP_WOULDBLOCK);
  5208. #else
  5209. err = sp_ecc_make_key_521(rng, key->k, &key->pubkey, key->heap);
  5210. #endif /* WC_ECC_NONBLOCK_ONLY */
  5211. }
  5212. #endif /* !WC_ECC_NONBLOCK */
  5213. if (err == MP_OKAY) {
  5214. key->type = ECC_PRIVATEKEY;
  5215. }
  5216. }
  5217. else
  5218. #endif /* WOLFSSL_SP_521 */
  5219. #endif /* WOLFSSL_HAVE_SP_ECC */
  5220. { /* software key gen */
  5221. #if defined(WOLFSSL_SP_MATH)
  5222. err = WC_KEY_SIZE_E;
  5223. #else
  5224. DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT);
  5225. /* setup the key variables */
  5226. #ifndef ALT_ECC_SIZE
  5227. err = mp_init(key->k);
  5228. #else
  5229. err = 0;
  5230. key->k = (mp_int*)key->ka;
  5231. alt_fp_init(key->k);
  5232. #endif
  5233. /* load curve info */
  5234. if (err == MP_OKAY) {
  5235. ALLOC_CURVE_SPECS(ECC_CURVE_FIELD_COUNT, err);
  5236. }
  5237. if (err == MP_OKAY) {
  5238. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  5239. }
  5240. /* generate k */
  5241. if (err == MP_OKAY) {
  5242. err = wc_ecc_gen_k(rng, key->dp->size, key->k, curve->order);
  5243. }
  5244. /* generate public key from k */
  5245. if (err == MP_OKAY) {
  5246. err = ecc_make_pub_ex(key, curve, NULL, rng);
  5247. }
  5248. if (err == MP_OKAY
  5249. #ifdef WOLFSSL_ASYNC_CRYPT
  5250. || err == WC_PENDING_E
  5251. #endif
  5252. ) {
  5253. key->type = ECC_PRIVATEKEY;
  5254. }
  5255. else {
  5256. /* cleanup these on failure case only */
  5257. mp_forcezero(key->k);
  5258. }
  5259. /* cleanup allocations */
  5260. wc_ecc_curve_free(curve);
  5261. FREE_CURVE_SPECS();
  5262. #endif /* WOLFSSL_SP_MATH */
  5263. }
  5264. #ifdef HAVE_WOLF_BIGINT
  5265. if (err == MP_OKAY)
  5266. err = wc_mp_to_bigint(key->k, &key->k->raw);
  5267. if (err == MP_OKAY)
  5268. err = wc_mp_to_bigint(key->pubkey.x, &key->pubkey.x->raw);
  5269. if (err == MP_OKAY)
  5270. err = wc_mp_to_bigint(key->pubkey.y, &key->pubkey.y->raw);
  5271. if (err == MP_OKAY)
  5272. err = wc_mp_to_bigint(key->pubkey.z, &key->pubkey.z->raw);
  5273. #endif
  5274. #endif /* HAVE_ECC_MAKE_PUB */
  5275. return err;
  5276. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  5277. }
  5278. int wc_ecc_make_key_ex2(WC_RNG* rng, int keysize, ecc_key* key, int curve_id,
  5279. int flags)
  5280. {
  5281. int err;
  5282. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  5283. err = _ecc_make_key_ex(rng, keysize, key, curve_id, flags);
  5284. #if (FIPS_VERSION_GE(5,0) || defined(WOLFSSL_VALIDATE_ECC_KEYGEN)) && \
  5285. !defined(WOLFSSL_KCAPI_ECC)
  5286. if (err == MP_OKAY) {
  5287. err = _ecc_validate_public_key(key, 0, 0);
  5288. }
  5289. if (err == MP_OKAY
  5290. #if defined(WOLF_CRYPTO_CB)
  5291. /* even if WOLF_CRYPTO_CB we generate the key if the devId is invalid */
  5292. && key->devId == INVALID_DEVID
  5293. #endif
  5294. ) {
  5295. err = _ecc_pairwise_consistency_test(key, rng);
  5296. }
  5297. #endif
  5298. RESTORE_VECTOR_REGISTERS();
  5299. return err;
  5300. }
  5301. WOLFSSL_ABI
  5302. int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id)
  5303. {
  5304. return wc_ecc_make_key_ex2(rng, keysize, key, curve_id, WC_ECC_FLAG_NONE);
  5305. }
  5306. #ifdef ECC_DUMP_OID
  5307. /* Optional dump of encoded OID for adding new curves */
  5308. static int mOidDumpDone;
  5309. static void wc_ecc_dump_oids(void)
  5310. {
  5311. int x;
  5312. if (mOidDumpDone) {
  5313. return;
  5314. }
  5315. /* find matching OID sum (based on encoded value) */
  5316. for (x = 0; ecc_sets[x].size != 0; x++) {
  5317. int i;
  5318. byte* oid;
  5319. word32 oidSz, sum = 0;
  5320. printf("ECC %s (%d):\n", ecc_sets[x].name, x);
  5321. #ifdef HAVE_OID_ENCODING
  5322. byte oidEnc[ECC_MAX_OID_LEN];
  5323. oid = oidEnc;
  5324. oidSz = ECC_MAX_OID_LEN;
  5325. printf("OID: ");
  5326. for (i = 0; i < (int)ecc_sets[x].oidSz; i++) {
  5327. printf("%d.", ecc_sets[x].oid[i]);
  5328. }
  5329. printf("\n");
  5330. EncodeObjectId(ecc_sets[x].oid, ecc_sets[x].oidSz, oidEnc, &oidSz);
  5331. #else
  5332. oid = (byte*)ecc_sets[x].oid;
  5333. oidSz = ecc_sets[x].oidSz;
  5334. #endif
  5335. printf("OID Encoded: ");
  5336. for (i = 0; i < (int)oidSz; i++) {
  5337. printf("0x%02X,", oid[i]);
  5338. }
  5339. printf("\n");
  5340. for (i = 0; i < (int)oidSz; i++) {
  5341. sum += oid[i];
  5342. }
  5343. printf("Sum: %u\n", sum);
  5344. /* validate sum */
  5345. if (ecc_sets[x].oidSum != sum) {
  5346. fprintf(stderr, " Sum %u Not Valid!\n", ecc_sets[x].oidSum);
  5347. }
  5348. }
  5349. mOidDumpDone = 1;
  5350. }
  5351. #endif /* ECC_DUMP_OID */
  5352. WOLFSSL_ABI
  5353. ecc_key* wc_ecc_key_new(void* heap)
  5354. {
  5355. int devId = INVALID_DEVID;
  5356. ecc_key* key;
  5357. #if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  5358. /* assume all keys are using CAAM for ECC unless explicitly set otherwise */
  5359. devId = WOLFSSL_CAAM_DEVID;
  5360. #endif
  5361. key = (ecc_key*)XMALLOC(sizeof(ecc_key), heap, DYNAMIC_TYPE_ECC);
  5362. if (key) {
  5363. if (wc_ecc_init_ex(key, heap, devId) != 0) {
  5364. XFREE(key, heap, DYNAMIC_TYPE_ECC);
  5365. key = NULL;
  5366. }
  5367. }
  5368. return key;
  5369. }
  5370. WOLFSSL_ABI
  5371. void wc_ecc_key_free(ecc_key* key)
  5372. {
  5373. if (key) {
  5374. void* heap = key->heap;
  5375. wc_ecc_free(key);
  5376. ForceZero(key, sizeof(ecc_key));
  5377. XFREE(key, heap, DYNAMIC_TYPE_ECC);
  5378. (void)heap;
  5379. }
  5380. }
  5381. /**
  5382. Make a new ECC key
  5383. rng An active RNG state
  5384. keysize The keysize for the new key (in octets from 20 to 65 bytes)
  5385. key [out] Destination of the newly created key
  5386. return MP_OKAY if successful,
  5387. upon error all allocated memory will be freed
  5388. */
  5389. WOLFSSL_ABI
  5390. int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key)
  5391. {
  5392. return wc_ecc_make_key_ex(rng, keysize, key, ECC_CURVE_DEF);
  5393. }
  5394. /* Setup dynamic pointers if using normal math for proper freeing */
  5395. WOLFSSL_ABI
  5396. int wc_ecc_init_ex(ecc_key* key, void* heap, int devId)
  5397. {
  5398. int ret = 0;
  5399. #if defined(HAVE_PKCS11)
  5400. int isPkcs11 = 0;
  5401. #endif
  5402. if (key == NULL) {
  5403. return BAD_FUNC_ARG;
  5404. }
  5405. #if defined(HAVE_PKCS11)
  5406. if (key->isPkcs11) {
  5407. isPkcs11 = 1;
  5408. }
  5409. #endif
  5410. #ifdef ECC_DUMP_OID
  5411. wc_ecc_dump_oids();
  5412. #endif
  5413. XMEMSET(key, 0, sizeof(ecc_key));
  5414. key->state = ECC_STATE_NONE;
  5415. #if defined(PLUTON_CRYPTO_ECC) || defined(WOLF_CRYPTO_CB)
  5416. key->devId = devId;
  5417. #else
  5418. (void)devId;
  5419. #endif
  5420. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  5421. key->slot = ATECC_INVALID_SLOT;
  5422. #elif defined(WOLFSSL_KCAPI_ECC)
  5423. key->handle = NULL;
  5424. #else
  5425. #ifdef ALT_ECC_SIZE
  5426. key->pubkey.x = (mp_int*)&key->pubkey.xyz[0];
  5427. key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
  5428. key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
  5429. alt_fp_init(key->pubkey.x);
  5430. alt_fp_init(key->pubkey.y);
  5431. alt_fp_init(key->pubkey.z);
  5432. key->k = (mp_int*)key->ka;
  5433. alt_fp_init(key->k);
  5434. #else
  5435. ret = mp_init_multi(key->k, key->pubkey.x, key->pubkey.y, key->pubkey.z,
  5436. NULL, NULL);
  5437. if (ret != MP_OKAY) {
  5438. return MEMORY_E;
  5439. }
  5440. #endif /* ALT_ECC_SIZE */
  5441. #endif /* WOLFSSL_ATECC508A */
  5442. #if (defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP) || \
  5443. defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  5444. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)) && \
  5445. defined(WOLFSSL_NO_MALLOC)
  5446. ret = mp_init(key->sign_k);
  5447. if (ret != MP_OKAY) {
  5448. return MEMORY_E;
  5449. }
  5450. #endif
  5451. #ifdef WOLFSSL_HEAP_TEST
  5452. key->heap = (void*)WOLFSSL_HEAP_TEST;
  5453. #else
  5454. key->heap = heap;
  5455. #endif
  5456. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  5457. #if defined(HAVE_PKCS11)
  5458. if (!isPkcs11)
  5459. #endif
  5460. {
  5461. /* handle as async */
  5462. ret = wolfAsync_DevCtxInit(&key->asyncDev, WOLFSSL_ASYNC_MARKER_ECC,
  5463. key->heap, devId);
  5464. }
  5465. #elif defined(HAVE_PKCS11)
  5466. (void)isPkcs11;
  5467. #endif
  5468. #if defined(WOLFSSL_DSP)
  5469. key->handle = -1;
  5470. #endif
  5471. #ifdef WOLFSSL_SE050
  5472. key->keyId = 0;
  5473. key->keyIdSet = 0;
  5474. #endif
  5475. #ifdef WOLFSSL_CHECK_MEM_ZERO
  5476. mp_memzero_add("ECC k", key->k);
  5477. #endif
  5478. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  5479. key->privKey = key->keyRaw + (2 * ECC_MAX_CRYPTO_HW_SIZE);
  5480. if (wc_InitXsecure(&(key->xSec))) {
  5481. WOLFSSL_MSG("Can't initialize Xsecure");
  5482. return WC_HW_E;
  5483. }
  5484. #endif
  5485. return ret;
  5486. }
  5487. WOLFSSL_ABI
  5488. int wc_ecc_init(ecc_key* key)
  5489. {
  5490. #if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  5491. return wc_ecc_init_ex(key, NULL, WOLFSSL_CAAM_DEVID);
  5492. #else
  5493. return wc_ecc_init_ex(key, NULL, INVALID_DEVID);
  5494. #endif
  5495. }
  5496. #ifdef WOLF_PRIVATE_KEY_ID
  5497. int wc_ecc_init_id(ecc_key* key, unsigned char* id, int len, void* heap,
  5498. int devId)
  5499. {
  5500. int ret = 0;
  5501. #ifdef WOLFSSL_SE050
  5502. /* SE050 TLS users store a word32 at id, need to cast back */
  5503. word32* keyPtr = NULL;
  5504. #endif
  5505. if (key == NULL)
  5506. ret = BAD_FUNC_ARG;
  5507. if (ret == 0 && (len < 0 || len > ECC_MAX_ID_LEN))
  5508. ret = BUFFER_E;
  5509. #if defined(HAVE_PKCS11)
  5510. XMEMSET(key, 0, sizeof(ecc_key));
  5511. key->isPkcs11 = 1;
  5512. #endif
  5513. if (ret == 0)
  5514. ret = wc_ecc_init_ex(key, heap, devId);
  5515. if (ret == 0 && id != NULL && len != 0) {
  5516. XMEMCPY(key->id, id, (size_t)len);
  5517. key->idLen = len;
  5518. #ifdef WOLFSSL_SE050
  5519. /* Set SE050 ID from word32, populate ecc_key with public from SE050 */
  5520. if (len == (int)sizeof(word32)) {
  5521. keyPtr = (word32*)key->id;
  5522. ret = wc_ecc_use_key_id(key, *keyPtr, 0);
  5523. }
  5524. #endif
  5525. }
  5526. return ret;
  5527. }
  5528. int wc_ecc_init_label(ecc_key* key, const char* label, void* heap, int devId)
  5529. {
  5530. int ret = 0;
  5531. int labelLen = 0;
  5532. if (key == NULL || label == NULL)
  5533. ret = BAD_FUNC_ARG;
  5534. if (ret == 0) {
  5535. labelLen = (int)XSTRLEN(label);
  5536. if (labelLen == 0 || labelLen > ECC_MAX_LABEL_LEN)
  5537. ret = BUFFER_E;
  5538. }
  5539. #if defined(HAVE_PKCS11)
  5540. XMEMSET(key, 0, sizeof(ecc_key));
  5541. key->isPkcs11 = 1;
  5542. #endif
  5543. if (ret == 0)
  5544. ret = wc_ecc_init_ex(key, heap, devId);
  5545. if (ret == 0) {
  5546. XMEMCPY(key->label, label, (size_t)labelLen);
  5547. key->labelLen = labelLen;
  5548. }
  5549. return ret;
  5550. }
  5551. #endif /* WOLF_PRIVATE_KEY_ID */
  5552. int wc_ecc_set_flags(ecc_key* key, word32 flags)
  5553. {
  5554. if (key == NULL) {
  5555. return BAD_FUNC_ARG;
  5556. }
  5557. key->flags |= flags;
  5558. return 0;
  5559. }
  5560. static int wc_ecc_get_curve_order_bit_count(const ecc_set_type* dp)
  5561. {
  5562. int err = MP_OKAY;
  5563. int orderBits;
  5564. DECLARE_CURVE_SPECS(1);
  5565. ALLOC_CURVE_SPECS(1, err);
  5566. if (err == MP_OKAY) {
  5567. err = wc_ecc_curve_load(dp, &curve, ECC_CURVE_FIELD_ORDER);
  5568. }
  5569. if (err != 0) {
  5570. FREE_CURVE_SPECS();
  5571. return err;
  5572. }
  5573. orderBits = mp_count_bits(curve->order);
  5574. wc_ecc_curve_free(curve);
  5575. FREE_CURVE_SPECS();
  5576. return orderBits;
  5577. }
  5578. #ifdef HAVE_ECC_SIGN
  5579. #ifndef NO_ASN
  5580. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  5581. defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL) || \
  5582. defined(WOLFSSL_SILABS_SE_ACCEL) || defined(WOLFSSL_KCAPI_ECC) || \
  5583. defined(WOLFSSL_SE050) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  5584. static int wc_ecc_sign_hash_hw(const byte* in, word32 inlen,
  5585. mp_int* r, mp_int* s, byte* out, word32 *outlen, WC_RNG* rng,
  5586. ecc_key* key)
  5587. {
  5588. int err;
  5589. #ifdef PLUTON_CRYPTO_ECC
  5590. if (key->devId != INVALID_DEVID) /* use hardware */
  5591. #endif
  5592. {
  5593. #if defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_ATECC508A) && \
  5594. !defined(WOLFSSL_ATECC608A)
  5595. CRYS_ECDSA_SignUserContext_t sigCtxTemp;
  5596. word32 raw_sig_size = *outlen;
  5597. word32 msgLenInBytes = inlen;
  5598. CRYS_ECPKI_HASH_OpMode_t hash_mode;
  5599. #endif
  5600. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  5601. #ifdef WOLFSSL_SMALL_STACK
  5602. byte* K = NULL;
  5603. byte* incopy = NULL;
  5604. #else
  5605. byte K[MAX_ECC_BYTES] = {0};
  5606. byte incopy[MAX_ECC_BYTES] = {0};
  5607. #endif
  5608. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  5609. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  5610. word32 Ksize;
  5611. #endif
  5612. #endif
  5613. word32 keysize = (word32)key->dp->size;
  5614. #ifdef PLUTON_CRYPTO_ECC
  5615. word32 orderBits = wc_ecc_get_curve_order_bit_count(key->dp);
  5616. #endif
  5617. #ifndef WOLFSSL_KCAPI_ECC
  5618. /* Check args */
  5619. if (keysize > ECC_MAX_CRYPTO_HW_SIZE || *outlen < keysize*2) {
  5620. return ECC_BAD_ARG_E;
  5621. }
  5622. #endif
  5623. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  5624. /* Sign: Result is 32-bytes of R then 32-bytes of S */
  5625. err = atmel_ecc_sign(key->slot, in, out);
  5626. if (err != 0) {
  5627. return err;
  5628. }
  5629. #elif defined(PLUTON_CRYPTO_ECC)
  5630. {
  5631. /* if the input is larger than curve order, we must truncate */
  5632. if ((inlen * WOLFSSL_BIT_SIZE) > orderBits) {
  5633. inlen = (orderBits + WOLFSSL_BIT_SIZE - 1) / WOLFSSL_BIT_SIZE;
  5634. }
  5635. /* perform ECC sign */
  5636. word32 raw_sig_size = *outlen;
  5637. err = Crypto_EccSign(in, inlen, out, &raw_sig_size);
  5638. if (err != CRYPTO_RES_SUCCESS || raw_sig_size != keysize*2){
  5639. return BAD_COND_E;
  5640. }
  5641. }
  5642. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  5643. err = silabs_ecc_sign_hash(in, inlen, out, outlen, key);
  5644. if (err != 0) {
  5645. return WC_HW_E;
  5646. }
  5647. #elif defined(WOLFSSL_CRYPTOCELL)
  5648. /* truncate if hash is longer than key size */
  5649. if (msgLenInBytes > keysize) {
  5650. msgLenInBytes = keysize;
  5651. }
  5652. hash_mode = cc310_hashModeECC(msgLenInBytes);
  5653. if (hash_mode == CRYS_ECPKI_HASH_OpModeLast) {
  5654. (void)cc310_hashModeECC(keysize);
  5655. /* Ignoring returned value */
  5656. hash_mode = CRYS_ECPKI_HASH_SHA256_mode;
  5657. }
  5658. /* create signature from an input buffer using a private key*/
  5659. err = CRYS_ECDSA_Sign(&wc_rndState,
  5660. wc_rndGenVectFunc,
  5661. &sigCtxTemp,
  5662. &key->ctx.privKey,
  5663. hash_mode,
  5664. (byte*)in,
  5665. msgLenInBytes,
  5666. out,
  5667. (uint32_t*)&raw_sig_size);
  5668. if (err != SA_SILIB_RET_OK){
  5669. WOLFSSL_MSG("CRYS_ECDSA_Sign failed");
  5670. return err;
  5671. }
  5672. #elif defined(WOLFSSL_KCAPI_ECC)
  5673. err = KcapiEcc_Sign(key, in, inlen, out, *outlen);
  5674. if (err != MP_OKAY) {
  5675. return err;
  5676. }
  5677. (void)rng;
  5678. #elif defined(WOLFSSL_SE050)
  5679. err = se050_ecc_sign_hash_ex(in, inlen, r, s, out, outlen, key);
  5680. if (err != MP_OKAY) {
  5681. return err;
  5682. }
  5683. (void)rng;
  5684. #elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  5685. #ifdef WOLFSSL_SMALL_STACK
  5686. K = (byte*)XMALLOC(keysize, key->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5687. incopy = (byte*)XMALLOC(inlen, key->heap, DYNAMIC_TYPE_HASH_TMP);
  5688. if (K == NULL || incopy == NULL) {
  5689. XFREE(incopy, key->heap, DYNAMIC_TYPE_HASH_TMP);
  5690. XFREE(K, key->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5691. return MEMORY_E;
  5692. }
  5693. #else
  5694. if (inlen > sizeof(incopy))
  5695. return ECC_BAD_ARG_E;
  5696. #endif
  5697. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  5698. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  5699. err = deterministic_sign_helper(in, inlen, key);
  5700. if (err)
  5701. return err;
  5702. Ksize = mp_unsigned_bin_size(key->sign_k);
  5703. if (Ksize > keysize) {
  5704. err = BUFFER_E;
  5705. goto error_out;
  5706. }
  5707. err = mp_to_unsigned_bin(key->sign_k, K);
  5708. if (err)
  5709. goto error_out;
  5710. mp_reverse(K, Ksize);
  5711. #else
  5712. err = wc_RNG_GenerateBlock(rng, K, keysize);
  5713. if (err)
  5714. goto error_out;
  5715. /* Make sure that K is max. 521 bits */
  5716. if (keysize == 66)
  5717. K[65] &= 0x1;
  5718. #endif
  5719. buf_reverse(incopy, in, inlen < keysize ? inlen : keysize);
  5720. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(incopy), keysize);
  5721. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(key->privKey), keysize);
  5722. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(K), keysize);
  5723. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(out), keysize * 2);
  5724. err = XSecure_EllipticGenerateSign(&(key->xSec.cinst),
  5725. xil_curve_type[key->dp->id],
  5726. XIL_CAST_U64(incopy), keysize,
  5727. XIL_CAST_U64(key->privKey),
  5728. XIL_CAST_U64(K),
  5729. XIL_CAST_U64(out));
  5730. if (err) {
  5731. WOLFSSL_XIL_ERROR("Generate ECC signature failed", err);
  5732. err = WC_HW_E;
  5733. }
  5734. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(out), keysize * 2);
  5735. mp_reverse(&out[0], keysize);
  5736. mp_reverse(&out[keysize], keysize);
  5737. error_out:
  5738. ForceZero(K, MAX_ECC_BYTES);
  5739. #ifdef WOLFSSL_SMALL_STACK
  5740. XFREE(incopy, key->heap, DYNAMIC_TYPE_HASH_TMP);
  5741. XFREE(K, key->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5742. #endif
  5743. if (err) {
  5744. ForceZero(out, keysize * 2);
  5745. return err;
  5746. }
  5747. #endif /* HW-specific #if-#elif chain */
  5748. #ifndef WOLFSSL_SE050
  5749. /* Load R and S, SE050 does this in port layer */
  5750. err = mp_read_unsigned_bin(r, &out[0], keysize);
  5751. if (err != MP_OKAY) {
  5752. return err;
  5753. }
  5754. err = mp_read_unsigned_bin(s, &out[keysize], keysize);
  5755. if (err != MP_OKAY) {
  5756. return err;
  5757. }
  5758. #endif
  5759. /* Check for zeros */
  5760. if (mp_iszero(r) || mp_iszero(s)) {
  5761. return MP_ZERO_E;
  5762. }
  5763. }
  5764. #ifdef PLUTON_CRYPTO_ECC
  5765. else {
  5766. err = wc_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
  5767. }
  5768. #endif
  5769. (void)rng;
  5770. return err;
  5771. }
  5772. #endif /* WOLFSSL_ATECC508A || PLUTON_CRYPTO_ECC || WOLFSSL_CRYPTOCELL */
  5773. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  5774. static int wc_ecc_sign_hash_async(const byte* in, word32 inlen, byte* out,
  5775. word32 *outlen, WC_RNG* rng, ecc_key* key)
  5776. {
  5777. int err;
  5778. mp_int *r = NULL, *s = NULL;
  5779. if (in == NULL || out == NULL || outlen == NULL || key == NULL ||
  5780. rng == NULL) {
  5781. return ECC_BAD_ARG_E;
  5782. }
  5783. err = wc_ecc_alloc_async(key);
  5784. if (err != 0) {
  5785. return err;
  5786. }
  5787. r = key->r;
  5788. s = key->s;
  5789. switch (key->state) {
  5790. case ECC_STATE_NONE:
  5791. case ECC_STATE_SIGN_DO:
  5792. key->state = ECC_STATE_SIGN_DO;
  5793. if ((err = mp_init_multi(r, s, NULL, NULL, NULL, NULL)) != MP_OKAY){
  5794. break;
  5795. }
  5796. err = wc_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
  5797. if (err < 0) {
  5798. break;
  5799. }
  5800. FALL_THROUGH;
  5801. case ECC_STATE_SIGN_ENCODE:
  5802. key->state = ECC_STATE_SIGN_ENCODE;
  5803. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  5804. #if !defined(WOLFSSL_ASYNC_CRYPT_SW) && defined(HAVE_ECC_CDH)
  5805. DECLARE_CURVE_SPECS(1);
  5806. ALLOC_CURVE_SPECS(1, err);
  5807. /* get curve order */
  5808. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ORDER);
  5809. #endif
  5810. #ifdef HAVE_CAVIUM_V
  5811. /* Nitrox requires r and s in sep buffer, so split it */
  5812. NitroxEccRsSplit(key, &r->raw, &s->raw);
  5813. #endif
  5814. #ifndef WOLFSSL_ASYNC_CRYPT_SW
  5815. /* only do this if not software, since it overwrites result */
  5816. wc_bigint_to_mp(&r->raw, r);
  5817. wc_bigint_to_mp(&s->raw, s);
  5818. /* if using a curve with cofactor != 1 then reduce by mod order */
  5819. #ifdef HAVE_ECC_CDH
  5820. /* if r is not less than order than reduce */
  5821. if (err == 0 && mp_count_bits(r) > mp_count_bits(curve->order)) {
  5822. err = mp_mod(r, curve->order, r);
  5823. }
  5824. wc_ecc_curve_free(curve);
  5825. FREE_CURVE_SPECS();
  5826. #endif
  5827. #endif /* !WOLFSSL_ASYNC_CRYPT_SW */
  5828. }
  5829. /* encoded with DSA header */
  5830. if (err == 0) {
  5831. err = StoreECC_DSA_Sig(out, outlen, r, s);
  5832. }
  5833. /* done with R/S */
  5834. mp_clear(r);
  5835. mp_clear(s);
  5836. break;
  5837. default:
  5838. err = BAD_STATE_E;
  5839. break;
  5840. }
  5841. /* if async pending then return and skip done cleanup below */
  5842. if (err == WC_PENDING_E) {
  5843. key->state++;
  5844. return err;
  5845. }
  5846. /* cleanup */
  5847. wc_ecc_free_async(key);
  5848. key->state = ECC_STATE_NONE;
  5849. return err;
  5850. }
  5851. #endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
  5852. /**
  5853. Sign a message digest
  5854. in The message digest to sign
  5855. inlen The length of the digest
  5856. out [out] The destination for the signature
  5857. outlen [in/out] The max size and resulting size of the signature
  5858. key A private ECC key
  5859. return MP_OKAY if successful
  5860. */
  5861. WOLFSSL_ABI
  5862. int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
  5863. WC_RNG* rng, ecc_key* key)
  5864. {
  5865. int err;
  5866. #if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(WC_ASYNC_ENABLE_ECC)
  5867. DECL_MP_INT_SIZE_DYN(r, ECC_KEY_MAX_BITS(key), MAX_ECC_BITS_USE);
  5868. DECL_MP_INT_SIZE_DYN(s, ECC_KEY_MAX_BITS(key), MAX_ECC_BITS_USE);
  5869. #endif
  5870. if (in == NULL || out == NULL || outlen == NULL || key == NULL) {
  5871. return ECC_BAD_ARG_E;
  5872. }
  5873. #ifdef WOLF_CRYPTO_CB
  5874. #ifndef WOLF_CRYPTO_CB_FIND
  5875. if (key->devId != INVALID_DEVID)
  5876. #endif
  5877. {
  5878. err = wc_CryptoCb_EccSign(in, inlen, out, outlen, rng, key);
  5879. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  5880. if (err != CRYPTOCB_UNAVAILABLE)
  5881. return err;
  5882. /* fall-through when unavailable */
  5883. #endif
  5884. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  5885. if (err == CRYPTOCB_UNAVAILABLE) {
  5886. err = NO_VALID_DEVID;
  5887. }
  5888. #endif
  5889. }
  5890. #endif
  5891. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  5892. if (rng == NULL) {
  5893. WOLFSSL_MSG("ECC sign RNG missing");
  5894. return ECC_BAD_ARG_E;
  5895. }
  5896. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  5897. /* handle async cases */
  5898. err = wc_ecc_sign_hash_async(in, inlen, out, outlen, rng, key);
  5899. #else
  5900. NEW_MP_INT_SIZE(r, ECC_KEY_MAX_BITS(key), key->heap, DYNAMIC_TYPE_ECC);
  5901. #ifdef MP_INT_SIZE_CHECK_NULL
  5902. if (r == NULL)
  5903. return MEMORY_E;
  5904. #endif
  5905. NEW_MP_INT_SIZE(s, ECC_KEY_MAX_BITS(key), key->heap, DYNAMIC_TYPE_ECC);
  5906. #ifdef MP_INT_SIZE_CHECK_NULL
  5907. if (s == NULL) {
  5908. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  5909. return MEMORY_E;
  5910. }
  5911. #endif
  5912. err = INIT_MP_INT_SIZE(r, ECC_KEY_MAX_BITS(key));
  5913. if (err != 0) {
  5914. FREE_MP_INT_SIZE(s, key->heap, DYNAMIC_TYPE_ECC);
  5915. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  5916. return err;
  5917. }
  5918. err = INIT_MP_INT_SIZE(s, ECC_KEY_MAX_BITS(key));
  5919. if (err != 0) {
  5920. FREE_MP_INT_SIZE(s, key->heap, DYNAMIC_TYPE_ECC);
  5921. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  5922. return err;
  5923. }
  5924. /* hardware crypto */
  5925. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  5926. defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL) || \
  5927. defined(WOLFSSL_SILABS_SE_ACCEL) || defined(WOLFSSL_KCAPI_ECC) || \
  5928. defined(WOLFSSL_SE050) || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  5929. err = wc_ecc_sign_hash_hw(in, inlen, r, s, out, outlen, rng, key);
  5930. #else
  5931. err = wc_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
  5932. #endif
  5933. if (err < 0) {
  5934. mp_clear(r);
  5935. mp_clear(s);
  5936. FREE_MP_INT_SIZE(s, key->heap, DYNAMIC_TYPE_ECC);
  5937. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  5938. return err;
  5939. }
  5940. /* encoded with DSA header */
  5941. err = StoreECC_DSA_Sig(out, outlen, r, s);
  5942. /* cleanup */
  5943. mp_clear(r);
  5944. mp_clear(s);
  5945. FREE_MP_INT_SIZE(s, key->heap, DYNAMIC_TYPE_ECC);
  5946. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  5947. #endif /* WOLFSSL_ASYNC_CRYPT */
  5948. #else
  5949. (void)rng;
  5950. (void)inlen;
  5951. (void)s;
  5952. (void)r;
  5953. (void)err;
  5954. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  5955. return err;
  5956. }
  5957. #endif /* !NO_ASN */
  5958. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  5959. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  5960. /* returns MP_OKAY on success */
  5961. static int deterministic_sign_helper(const byte* in, word32 inlen, ecc_key* key)
  5962. {
  5963. int err = MP_OKAY;
  5964. DECLARE_CURVE_SPECS(1);
  5965. ALLOC_CURVE_SPECS(1, err);
  5966. /* get curve order */
  5967. if (err == MP_OKAY) {
  5968. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ORDER);
  5969. }
  5970. if (err == MP_OKAY) {
  5971. #ifndef WOLFSSL_NO_MALLOC
  5972. /* if key->sign_k is NULL then create a buffer for the mp_int
  5973. * if not NULL then assume the user correctly set deterministic flag and
  5974. * that the key->sign_k holds a previously malloc'd mp_int buffer */
  5975. if (key->sign_k == NULL) {
  5976. key->sign_k = (mp_int*)XMALLOC(sizeof(mp_int), key->heap,
  5977. DYNAMIC_TYPE_ECC);
  5978. }
  5979. if (key->sign_k != NULL) {
  5980. /* currently limiting to SHA256 for auto create */
  5981. if (mp_init(key->sign_k) != MP_OKAY ||
  5982. wc_ecc_gen_deterministic_k(in, inlen,
  5983. WC_HASH_TYPE_SHA256, key->k, key->sign_k,
  5984. curve->order, key->heap) != 0) {
  5985. mp_free(key->sign_k);
  5986. XFREE(key->sign_k, key->heap, DYNAMIC_TYPE_ECC);
  5987. key->sign_k = NULL;
  5988. err = ECC_PRIV_KEY_E;
  5989. }
  5990. #ifdef WOLFSSL_CHECK_MEM_ZERO
  5991. else {
  5992. mp_memzero_add("deterministic_sign_helper sign_k", key->sign_k);
  5993. }
  5994. #endif
  5995. }
  5996. else {
  5997. err = MEMORY_E;
  5998. }
  5999. #else
  6000. key->sign_k_set = 0;
  6001. /* currently limiting to SHA256 for auto create */
  6002. if (wc_ecc_gen_deterministic_k(in, inlen, WC_HASH_TYPE_SHA256, key->k,
  6003. key->sign_k, curve->order, key->heap) != 0) {
  6004. err = ECC_PRIV_KEY_E;
  6005. }
  6006. else {
  6007. key->sign_k_set = 1;
  6008. }
  6009. #endif
  6010. }
  6011. wc_ecc_curve_free(curve);
  6012. FREE_CURVE_SPECS();
  6013. return err;
  6014. }
  6015. #endif /* WOLFSSL_ECDSA_DETERMINISTIC_K ||
  6016. WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT */
  6017. #if defined(WOLFSSL_STM32_PKA)
  6018. int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
  6019. ecc_key* key, mp_int *r, mp_int *s)
  6020. {
  6021. return stm32_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
  6022. }
  6023. #elif !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  6024. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_KCAPI_ECC)
  6025. #ifndef WOLFSSL_SP_MATH
  6026. static int ecc_sign_hash_sw(ecc_key* key, ecc_key* pubkey, WC_RNG* rng,
  6027. ecc_curve_spec* curve, mp_int* e, mp_int* r,
  6028. mp_int* s)
  6029. {
  6030. int err = MP_OKAY;
  6031. int loop_check = 0;
  6032. DECL_MP_INT_SIZE_DYN(b, ECC_KEY_MAX_BITS(key), MAX_ECC_BITS_USE);
  6033. NEW_MP_INT_SIZE(b, ECC_KEY_MAX_BITS(key), key->heap, DYNAMIC_TYPE_ECC);
  6034. #ifdef MP_INT_SIZE_CHECK_NULL
  6035. if (b == NULL)
  6036. err = MEMORY_E;
  6037. #endif
  6038. if (err == MP_OKAY) {
  6039. err = INIT_MP_INT_SIZE(b, ECC_KEY_MAX_BITS(key));
  6040. }
  6041. #ifdef WOLFSSL_CUSTOM_CURVES
  6042. /* if custom curve, apply params to pubkey */
  6043. if (err == MP_OKAY && key->idx == ECC_CUSTOM_IDX) {
  6044. err = wc_ecc_set_custom_curve(pubkey, key->dp);
  6045. }
  6046. #endif
  6047. if (err == MP_OKAY) {
  6048. /* Generate blinding value - non-zero value. */
  6049. do {
  6050. if (++loop_check > 64) {
  6051. err = RNG_FAILURE_E;
  6052. break;
  6053. }
  6054. err = wc_ecc_gen_k(rng, key->dp->size, b, curve->order);
  6055. }
  6056. while (err == MP_ZERO_E);
  6057. loop_check = 0;
  6058. }
  6059. #ifdef WOLFSSL_CHECK_MEM_ZERO
  6060. if (err == MP_OKAY) {
  6061. mp_memzero_add("ecc_sign_hash_sw b", b);
  6062. }
  6063. #endif
  6064. for (; err == MP_OKAY;) {
  6065. if (++loop_check > 64) {
  6066. err = RNG_FAILURE_E;
  6067. break;
  6068. }
  6069. #if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP) || \
  6070. defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  6071. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  6072. #ifndef WOLFSSL_NO_MALLOC
  6073. if (key->sign_k != NULL)
  6074. #else
  6075. if (key->sign_k_set)
  6076. #endif
  6077. {
  6078. if (loop_check > 1) {
  6079. err = RNG_FAILURE_E;
  6080. break;
  6081. }
  6082. /* use provided sign_k */
  6083. err = mp_copy(key->sign_k, pubkey->k);
  6084. if (err != MP_OKAY) break;
  6085. /* free sign_k, so only used once */
  6086. mp_forcezero(key->sign_k);
  6087. #ifndef WOLFSSL_NO_MALLOC
  6088. mp_free(key->sign_k);
  6089. XFREE(key->sign_k, key->heap, DYNAMIC_TYPE_ECC);
  6090. key->sign_k = NULL;
  6091. #else
  6092. key->sign_k_set = 0;
  6093. #endif
  6094. #ifdef WOLFSSL_ECDSA_SET_K_ONE_LOOP
  6095. loop_check = 64;
  6096. #endif
  6097. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  6098. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  6099. if (key->deterministic == 1) {
  6100. /* sign_k generated earlier in function for SP calls.
  6101. * Only go through the loop once and fail if error */
  6102. loop_check = 64;
  6103. }
  6104. #endif
  6105. /* compute public key based on provided "k" */
  6106. err = ecc_make_pub_ex(pubkey, curve, NULL, rng);
  6107. }
  6108. else
  6109. #endif
  6110. {
  6111. err = _ecc_make_key_ex(rng, key->dp->size, pubkey, key->dp->id,
  6112. WC_ECC_FLAG_NONE);
  6113. }
  6114. #ifdef WOLFSSL_CHECK_MEM_ZERO
  6115. if (err == MP_OKAY) {
  6116. mp_memzero_add("ecc_sign_hash_sw k", pubkey->k);
  6117. }
  6118. #endif
  6119. #ifdef WOLFSSL_ASYNC_CRYPT
  6120. /* for async do blocking wait here */
  6121. err = wc_AsyncWait(err, &pubkey->asyncDev, WC_ASYNC_FLAG_NONE);
  6122. #endif
  6123. if (err != MP_OKAY) break;
  6124. /* find r = x1 mod n */
  6125. err = mp_mod(pubkey->pubkey.x, curve->order, r);
  6126. if (err != MP_OKAY) break;
  6127. if (mp_iszero(r) == MP_NO) {
  6128. mp_int* ep = pubkey->k;
  6129. mp_int* kp = pubkey->k;
  6130. mp_int* x = key->k;
  6131. /* find s = (e + xr)/k
  6132. = b.(e/k.b + x.r/k.b) */
  6133. /* k' = k.b */
  6134. err = mp_mulmod(pubkey->k, b, curve->order, kp);
  6135. if (err != MP_OKAY) break;
  6136. /* k' = 1/k.b
  6137. = 1/k' */
  6138. err = mp_invmod(kp, curve->order, kp);
  6139. if (err != MP_OKAY) break;
  6140. /* s = x.r */
  6141. err = mp_mulmod(x, r, curve->order, s);
  6142. if (err != MP_OKAY) break;
  6143. /* s = x.r/k.b
  6144. = k'.s */
  6145. err = mp_mulmod(kp, s, curve->order, s);
  6146. if (err != MP_OKAY) break;
  6147. /* e' = e/k.b
  6148. = e.k' */
  6149. err = mp_mulmod(kp, e, curve->order, ep);
  6150. if (err != MP_OKAY) break;
  6151. /* s = e/k.b + x.r/k.b = (e + x.r)/k.b
  6152. = e' + s */
  6153. err = mp_addmod_ct(ep, s, curve->order, s);
  6154. if (err != MP_OKAY) break;
  6155. /* s = b.(e + x.r)/k.b = (e + x.r)/k
  6156. = b.s */
  6157. err = mp_mulmod(s, b, curve->order, s);
  6158. if (err != MP_OKAY) break;
  6159. if (mp_iszero(s) == MP_NO) {
  6160. /* sign successful */
  6161. break;
  6162. }
  6163. }
  6164. #ifndef ALT_ECC_SIZE
  6165. mp_clear(pubkey->pubkey.x);
  6166. mp_clear(pubkey->pubkey.y);
  6167. mp_clear(pubkey->pubkey.z);
  6168. #endif
  6169. mp_forcezero(pubkey->k);
  6170. }
  6171. mp_forcezero(b);
  6172. FREE_MP_INT_SIZE(b, key->heap, DYNAMIC_TYPE_ECC);
  6173. #if !defined(WOLFSSL_SMALL_STACK) && defined(WOLFSSL_CHECK_MEM_ZERO)
  6174. mp_memzero_check(b);
  6175. #endif
  6176. return err;
  6177. }
  6178. #endif
  6179. #ifdef WOLFSSL_HAVE_SP_ECC
  6180. static int ecc_sign_hash_sp(const byte* in, word32 inlen, WC_RNG* rng,
  6181. ecc_key* key, mp_int *r, mp_int *s)
  6182. {
  6183. if (key->idx != ECC_CUSTOM_IDX) {
  6184. #if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP) \
  6185. || defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  6186. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  6187. mp_int* sign_k = key->sign_k;
  6188. #else
  6189. mp_int* sign_k = NULL;
  6190. #endif
  6191. #if defined(WC_ECC_NONBLOCK) && defined(WC_ECC_NONBLOCK_ONLY)
  6192. /* perform blocking call to non-blocking function */
  6193. ecc_nb_ctx_t nb_ctx;
  6194. XMEMSET(&nb_ctx, 0, sizeof(nb_ctx));
  6195. #endif
  6196. #ifndef WOLFSSL_SP_NO_256
  6197. if (ecc_sets[key->idx].id == ECC_SECP256R1) {
  6198. #ifdef WC_ECC_NONBLOCK
  6199. #ifdef WC_ECC_NONBLOCK_ONLY
  6200. int err;
  6201. #endif
  6202. if (key->nb_ctx) {
  6203. return sp_ecc_sign_256_nb(&key->nb_ctx->sp_ctx, in, inlen, rng,
  6204. key->k, r, s, sign_k, key->heap);
  6205. }
  6206. #ifdef WC_ECC_NONBLOCK_ONLY
  6207. do { /* perform blocking call to non-blocking function */
  6208. err = sp_ecc_sign_256_nb(&nb_ctx.sp_ctx, in, inlen, rng,
  6209. key->k, r, s, sign_k, key->heap);
  6210. } while (err == FP_WOULDBLOCK);
  6211. return err;
  6212. #endif
  6213. #endif /* WC_ECC_NONBLOCK */
  6214. #if !defined(WC_ECC_NONBLOCK) || (defined(WC_ECC_NONBLOCK) && !defined(WC_ECC_NONBLOCK_ONLY))
  6215. {
  6216. int ret;
  6217. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  6218. ret = sp_ecc_sign_256(in, inlen, rng, key->k, r, s, sign_k,
  6219. key->heap);
  6220. RESTORE_VECTOR_REGISTERS();
  6221. return ret;
  6222. }
  6223. #endif
  6224. }
  6225. #endif
  6226. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  6227. if (ecc_sets[key->idx].id == ECC_SM2P256V1) {
  6228. int ret;
  6229. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  6230. ret = sp_ecc_sign_sm2_256(in, inlen, rng, key->k, r, s, sign_k,
  6231. key->heap);
  6232. RESTORE_VECTOR_REGISTERS();
  6233. return ret;
  6234. }
  6235. #endif
  6236. #ifdef WOLFSSL_SP_384
  6237. if (ecc_sets[key->idx].id == ECC_SECP384R1) {
  6238. #ifdef WC_ECC_NONBLOCK
  6239. #ifdef WC_ECC_NONBLOCK_ONLY
  6240. int err;
  6241. #endif
  6242. if (key->nb_ctx) {
  6243. return sp_ecc_sign_384_nb(&key->nb_ctx->sp_ctx, in, inlen, rng,
  6244. key->k, r, s, sign_k, key->heap);
  6245. }
  6246. #ifdef WC_ECC_NONBLOCK_ONLY
  6247. do { /* perform blocking call to non-blocking function */
  6248. err = sp_ecc_sign_384_nb(&nb_ctx.sp_ctx, in, inlen, rng,
  6249. key->k, r, s, sign_k, key->heap);
  6250. } while (err == FP_WOULDBLOCK);
  6251. return err;
  6252. #endif
  6253. #endif /* WC_ECC_NONBLOCK */
  6254. #if !defined(WC_ECC_NONBLOCK) || (defined(WC_ECC_NONBLOCK) && !defined(WC_ECC_NONBLOCK_ONLY))
  6255. {
  6256. int ret;
  6257. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  6258. ret = sp_ecc_sign_384(in, inlen, rng, key->k, r, s, sign_k,
  6259. key->heap);
  6260. RESTORE_VECTOR_REGISTERS();
  6261. return ret;
  6262. }
  6263. #endif
  6264. }
  6265. #endif
  6266. #ifdef WOLFSSL_SP_521
  6267. if (ecc_sets[key->idx].id == ECC_SECP521R1) {
  6268. #ifdef WC_ECC_NONBLOCK
  6269. #ifdef WC_ECC_NONBLOCK_ONLY
  6270. int err;
  6271. #endif
  6272. if (key->nb_ctx) {
  6273. return sp_ecc_sign_521_nb(&key->nb_ctx->sp_ctx, in, inlen, rng,
  6274. key->k, r, s, sign_k, key->heap);
  6275. }
  6276. #ifdef WC_ECC_NONBLOCK_ONLY
  6277. do { /* perform blocking call to non-blocking function */
  6278. err = sp_ecc_sign_521_nb(&nb_ctx.sp_ctx, in, inlen, rng,
  6279. key->k, r, s, sign_k, key->heap);
  6280. } while (err == FP_WOULDBLOCK);
  6281. return err;
  6282. #endif
  6283. #endif /* WC_ECC_NONBLOCK */
  6284. #if !defined(WC_ECC_NONBLOCK) || (defined(WC_ECC_NONBLOCK) && !defined(WC_ECC_NONBLOCK_ONLY))
  6285. {
  6286. int ret;
  6287. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  6288. ret = sp_ecc_sign_521(in, inlen, rng, key->k, r, s, sign_k,
  6289. key->heap);
  6290. RESTORE_VECTOR_REGISTERS();
  6291. return ret;
  6292. }
  6293. #endif
  6294. }
  6295. #endif
  6296. (void)sign_k;
  6297. }
  6298. /* SP doesn't support curve. */
  6299. return WC_KEY_SIZE_E;
  6300. }
  6301. #endif
  6302. /**
  6303. Sign a message digest
  6304. in The message digest to sign
  6305. inlen The length of the digest
  6306. key A private ECC key
  6307. r [out] The destination for r component of the signature
  6308. s [out] The destination for s component of the signature
  6309. return MP_OKAY if successful
  6310. */
  6311. int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
  6312. ecc_key* key, mp_int *r, mp_int *s)
  6313. {
  6314. int err = 0;
  6315. #if !defined(WOLFSSL_SP_MATH)
  6316. mp_int* e;
  6317. #if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(HAVE_CAVIUM_V)
  6318. DECL_MP_INT_SIZE_DYN(e_lcl, ECC_KEY_MAX_BITS(key), MAX_ECC_BITS_USE);
  6319. #endif
  6320. #if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP) || \
  6321. defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  6322. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT) || \
  6323. (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  6324. (defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)))
  6325. DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT);
  6326. #else
  6327. DECLARE_CURVE_SPECS(1);
  6328. #endif
  6329. #endif /* !WOLFSSL_SP_MATH */
  6330. if (in == NULL || r == NULL || s == NULL || key == NULL || rng == NULL) {
  6331. return ECC_BAD_ARG_E;
  6332. }
  6333. /* is this a private key? */
  6334. if (key->type != ECC_PRIVATEKEY && key->type != ECC_PRIVATEKEY_ONLY) {
  6335. return ECC_BAD_ARG_E;
  6336. }
  6337. /* is the IDX valid ? */
  6338. if (wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL) {
  6339. return ECC_BAD_ARG_E;
  6340. }
  6341. #if defined(WOLFSSL_SP_MATH)
  6342. if (key->idx == ECC_CUSTOM_IDX || (1
  6343. #ifndef WOLFSSL_SP_NO_256
  6344. && ecc_sets[key->idx].id != ECC_SECP256R1
  6345. #endif
  6346. #ifdef WOLFSSL_SP_SM2
  6347. && ecc_sets[key->idx].id != ECC_SM2P256V1
  6348. #endif
  6349. #ifdef WOLFSSL_SP_384
  6350. && ecc_sets[key->idx].id != ECC_SECP384R1
  6351. #endif
  6352. #ifdef WOLFSSL_SP_521
  6353. && ecc_sets[key->idx].id != ECC_SECP521R1
  6354. #endif
  6355. )) {
  6356. return WC_KEY_SIZE_E;
  6357. }
  6358. #endif
  6359. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  6360. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  6361. /* generate deterministic 'k' value to be used either with SP or normal */
  6362. if (key->deterministic == 1) {
  6363. if (deterministic_sign_helper(in, inlen, key)) {
  6364. WOLFSSL_MSG("Error generating deterministic k to sign");
  6365. return ECC_PRIV_KEY_E;
  6366. }
  6367. }
  6368. #endif
  6369. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  6370. defined(WOLFSSL_ASYNC_CRYPT_SW)
  6371. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  6372. if (wc_AsyncSwInit(&key->asyncDev, ASYNC_SW_ECC_SIGN)) {
  6373. WC_ASYNC_SW* sw = &key->asyncDev.sw;
  6374. sw->eccSign.in = in;
  6375. sw->eccSign.inSz = inlen;
  6376. sw->eccSign.rng = rng;
  6377. sw->eccSign.key = key;
  6378. sw->eccSign.r = r;
  6379. sw->eccSign.s = s;
  6380. return WC_PENDING_E;
  6381. }
  6382. }
  6383. #endif
  6384. #if defined(WOLFSSL_HAVE_SP_ECC)
  6385. err = ecc_sign_hash_sp(in, inlen, rng, key, r, s);
  6386. if (err != WC_KEY_SIZE_E) {
  6387. return err;
  6388. }
  6389. #else
  6390. (void)inlen;
  6391. #endif
  6392. #if !defined(WOLFSSL_SP_MATH)
  6393. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_CAVIUM_V)
  6394. err = wc_ecc_alloc_mpint(key, &key->e);
  6395. if (err != 0) {
  6396. return err;
  6397. }
  6398. e = key->e;
  6399. #else
  6400. NEW_MP_INT_SIZE(e_lcl, ECC_KEY_MAX_BITS(key), key->heap, DYNAMIC_TYPE_ECC);
  6401. #ifdef MP_INT_SIZE_CHECK_NULL
  6402. if (e_lcl == NULL) {
  6403. return MEMORY_E;
  6404. }
  6405. #endif
  6406. e = e_lcl;
  6407. #endif
  6408. /* get the hash and load it as a bignum into 'e' */
  6409. /* init the bignums */
  6410. if ((err = INIT_MP_INT_SIZE(e, ECC_KEY_MAX_BITS(key))) != MP_OKAY) {
  6411. FREE_MP_INT_SIZE(e_lcl, key->heap, DYNAMIC_TYPE_ECC);
  6412. return err;
  6413. }
  6414. /* load curve info */
  6415. #if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP) || \
  6416. defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  6417. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  6418. ALLOC_CURVE_SPECS(ECC_CURVE_FIELD_COUNT, err);
  6419. if (err == MP_OKAY)
  6420. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  6421. #else
  6422. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  6423. (defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA))
  6424. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  6425. ALLOC_CURVE_SPECS(ECC_CURVE_FIELD_COUNT, err);
  6426. if (err == MP_OKAY)
  6427. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  6428. }
  6429. else
  6430. #endif
  6431. {
  6432. ALLOC_CURVE_SPECS(1, err);
  6433. if (err == MP_OKAY)
  6434. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ORDER);
  6435. }
  6436. #endif
  6437. /* load digest into e */
  6438. if (err == MP_OKAY) {
  6439. /* we may need to truncate if hash is longer than key size */
  6440. word32 orderBits = (word32)mp_count_bits(curve->order);
  6441. /* truncate down to byte size, may be all that's needed */
  6442. if ((WOLFSSL_BIT_SIZE * inlen) > orderBits)
  6443. inlen = (orderBits + WOLFSSL_BIT_SIZE - 1) / WOLFSSL_BIT_SIZE;
  6444. err = mp_read_unsigned_bin(e, in, inlen);
  6445. /* may still need bit truncation too */
  6446. if (err == MP_OKAY && (WOLFSSL_BIT_SIZE * inlen) > orderBits)
  6447. mp_rshb(e, (int)(WOLFSSL_BIT_SIZE - (orderBits & 0x7)));
  6448. }
  6449. /* make up a key and export the public copy */
  6450. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  6451. if ((err == MP_OKAY) && (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC)) {
  6452. #if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
  6453. #ifdef HAVE_CAVIUM_V
  6454. if (NitroxEccIsCurveSupported(key))
  6455. #endif
  6456. {
  6457. word32 keySz = key->dp->size;
  6458. mp_int* k;
  6459. #ifdef HAVE_CAVIUM_V
  6460. err = wc_ecc_alloc_mpint(key, &key->signK);
  6461. if (err != 0)
  6462. return err;
  6463. k = key->signK;
  6464. #else
  6465. mp_int k_lcl;
  6466. k = &k_lcl;
  6467. #endif
  6468. err = mp_init(k);
  6469. /* make sure r and s are allocated */
  6470. #ifdef HAVE_CAVIUM_V
  6471. /* Nitrox V needs single buffer for R and S */
  6472. if (err == MP_OKAY)
  6473. err = wc_bigint_alloc(&key->r->raw, NitroxEccGetSize(key)*2);
  6474. /* Nitrox V only needs Prime and Order */
  6475. if (err == MP_OKAY)
  6476. err = wc_ecc_curve_load(key->dp, &curve,
  6477. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_ORDER));
  6478. #else
  6479. if (err == MP_OKAY)
  6480. err = wc_bigint_alloc(&key->r->raw, key->dp->size);
  6481. if (err == MP_OKAY)
  6482. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  6483. #endif
  6484. if (err == MP_OKAY)
  6485. err = wc_bigint_alloc(&key->s->raw, key->dp->size);
  6486. /* load e and k */
  6487. if (err == MP_OKAY)
  6488. err = wc_mp_to_bigint_sz(e, &e->raw, keySz);
  6489. if (err == MP_OKAY)
  6490. err = wc_mp_to_bigint_sz(key->k, &key->k->raw, keySz);
  6491. if (err == MP_OKAY)
  6492. err = wc_ecc_gen_k(rng, key->dp->size, k, curve->order);
  6493. if (err == MP_OKAY)
  6494. err = wc_mp_to_bigint_sz(k, &k->raw, keySz);
  6495. #ifdef HAVE_CAVIUM_V
  6496. if (err == MP_OKAY)
  6497. err = NitroxEcdsaSign(key, &e->raw, &key->k->raw, &k->raw,
  6498. &r->raw, &s->raw, &curve->prime->raw, &curve->order->raw);
  6499. #else
  6500. if (err == MP_OKAY)
  6501. err = IntelQaEcdsaSign(&key->asyncDev, &e->raw, &key->k->raw,
  6502. &k->raw, &r->raw, &s->raw, &curve->Af->raw, &curve->Bf->raw,
  6503. &curve->prime->raw, &curve->order->raw, &curve->Gx->raw,
  6504. &curve->Gy->raw);
  6505. #endif
  6506. #ifndef HAVE_CAVIUM_V
  6507. mp_clear(e);
  6508. mp_clear(k);
  6509. #endif
  6510. wc_ecc_curve_free(curve);
  6511. FREE_CURVE_SPECS();
  6512. return err;
  6513. }
  6514. #endif /* HAVE_CAVIUM_V || HAVE_INTEL_QA */
  6515. }
  6516. #endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
  6517. if (err == MP_OKAY) {
  6518. #ifdef WOLFSSL_SMALL_STACK
  6519. ecc_key* pubkey;
  6520. #else
  6521. ecc_key pubkey[1];
  6522. #endif
  6523. #ifdef WOLFSSL_SMALL_STACK
  6524. pubkey = (ecc_key*)XMALLOC(sizeof(ecc_key), key->heap, DYNAMIC_TYPE_ECC);
  6525. if (pubkey == NULL)
  6526. err = MEMORY_E;
  6527. #endif
  6528. /* don't use async for key, since we don't support async return here */
  6529. if (err == MP_OKAY) {
  6530. err = wc_ecc_init_ex(pubkey, key->heap, INVALID_DEVID);
  6531. if (err == MP_OKAY) {
  6532. err = ecc_sign_hash_sw(key, pubkey, rng, curve, e, r, s);
  6533. wc_ecc_free(pubkey);
  6534. #ifdef WOLFSSL_SMALL_STACK
  6535. XFREE(pubkey, key->heap, DYNAMIC_TYPE_ECC);
  6536. #endif
  6537. }
  6538. }
  6539. }
  6540. mp_clear(e);
  6541. wc_ecc_curve_free(curve);
  6542. FREE_MP_INT_SIZE(e_lcl, key->heap, DYNAMIC_TYPE_ECC);
  6543. FREE_CURVE_SPECS();
  6544. #endif /* !WOLFSSL_SP_MATH */
  6545. return err;
  6546. }
  6547. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  6548. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  6549. /* helper function to do HMAC operations
  6550. * returns 0 on success and updates "out" buffer
  6551. */
  6552. static int _HMAC_K(byte* K, word32 KSz, byte* V, word32 VSz,
  6553. const byte* h1, word32 h1Sz, byte* x, word32 xSz, byte* oct,
  6554. byte* out, enum wc_HashType hashType, void* heap)
  6555. {
  6556. Hmac hmac;
  6557. int ret, init;
  6558. ret = init = wc_HmacInit(&hmac, heap, 0);
  6559. if (ret == 0)
  6560. ret = wc_HmacSetKey(&hmac, hashType, K, KSz);
  6561. if (ret == 0)
  6562. ret = wc_HmacUpdate(&hmac, V, VSz);
  6563. if (ret == 0 && oct != NULL)
  6564. ret = wc_HmacUpdate(&hmac, oct, 1);
  6565. if (ret == 0)
  6566. ret = wc_HmacUpdate(&hmac, x, xSz);
  6567. if (ret == 0)
  6568. ret = wc_HmacUpdate(&hmac, h1, h1Sz);
  6569. if (ret == 0)
  6570. ret = wc_HmacFinal(&hmac, out);
  6571. if (init == 0)
  6572. wc_HmacFree(&hmac);
  6573. return ret;
  6574. }
  6575. /* Generates a deterministic key based of the message using RFC6979
  6576. * @param [in] hash Hash value to sign
  6577. * @param [in] hashSz Size of 'hash' buffer passed in
  6578. * @param [in] hashType Type of hash to use with deterministic k gen, i.e.
  6579. * WC_HASH_TYPE_SHA256
  6580. * @param [in] priv Current ECC private key set
  6581. * @param [out] k An initialized mp_int to set the k value generated in
  6582. * @param [in] order ECC order parameter to use with generation
  6583. * @return 0 on success.
  6584. */
  6585. int wc_ecc_gen_deterministic_k(const byte* hash, word32 hashSz,
  6586. enum wc_HashType hashType, mp_int* priv, mp_int* k, mp_int* order,
  6587. void* heap)
  6588. {
  6589. int ret = 0, qbits = 0;
  6590. #ifndef WOLFSSL_SMALL_STACK
  6591. byte h1[MAX_ECC_BYTES];
  6592. byte V[WC_MAX_DIGEST_SIZE];
  6593. byte K[WC_MAX_DIGEST_SIZE];
  6594. byte x[MAX_ECC_BYTES];
  6595. mp_int z1[1];
  6596. #else
  6597. byte *h1 = NULL;
  6598. byte *V = NULL;
  6599. byte *K = NULL;
  6600. byte *x = NULL;
  6601. mp_int *z1 = NULL;
  6602. #endif
  6603. word32 xSz, VSz, KSz, h1len, qLen;
  6604. byte intOct;
  6605. if (hash == NULL || k == NULL || order == NULL) {
  6606. return BAD_FUNC_ARG;
  6607. }
  6608. if (hashSz > WC_MAX_DIGEST_SIZE) {
  6609. WOLFSSL_MSG("hash size was too large!");
  6610. return BAD_FUNC_ARG;
  6611. }
  6612. if (hashSz != WC_SHA256_DIGEST_SIZE) {
  6613. WOLFSSL_MSG("Currently only SHA256 digest is supported");
  6614. return BAD_FUNC_ARG;
  6615. }
  6616. if (mp_unsigned_bin_size(priv) > MAX_ECC_BYTES) {
  6617. WOLFSSL_MSG("private key larger than max expected!");
  6618. return BAD_FUNC_ARG;
  6619. }
  6620. #ifdef WOLFSSL_SMALL_STACK
  6621. h1 = (byte*)XMALLOC(MAX_ECC_BYTES, heap, DYNAMIC_TYPE_DIGEST);
  6622. if (h1 == NULL) {
  6623. ret = MEMORY_E;
  6624. }
  6625. if (ret == 0) {
  6626. V = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6627. if (V == NULL)
  6628. ret = MEMORY_E;
  6629. }
  6630. if (ret == 0) {
  6631. K = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6632. if (K == NULL)
  6633. ret = MEMORY_E;
  6634. }
  6635. if (ret == 0) {
  6636. x = (byte*)XMALLOC(MAX_ECC_BYTES, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6637. if (x == NULL)
  6638. ret = MEMORY_E;
  6639. }
  6640. if (ret == 0) {
  6641. z1 = (mp_int *)XMALLOC(sizeof(*z1), heap, DYNAMIC_TYPE_ECC_BUFFER);
  6642. if (z1 == NULL)
  6643. ret = MEMORY_E;
  6644. }
  6645. /* bail out if any error has been hit at this point */
  6646. if (ret != 0) {
  6647. if (x != NULL)
  6648. XFREE(x, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6649. if (K != NULL)
  6650. XFREE(K, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6651. if (V != NULL)
  6652. XFREE(V, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6653. if (h1 != NULL)
  6654. XFREE(h1, heap, DYNAMIC_TYPE_DIGEST);
  6655. return ret;
  6656. }
  6657. #endif
  6658. VSz = KSz = hashSz;
  6659. qLen = xSz = h1len = (word32)mp_unsigned_bin_size(order);
  6660. /* 3.2 b. Set V = 0x01 0x01 ... */
  6661. XMEMSET(V, 0x01, VSz);
  6662. /* 3.2 c. Set K = 0x00 0x00 ... */
  6663. XMEMSET(K, 0x00, KSz);
  6664. mp_init(z1); /* always init z1 and free z1 */
  6665. ret = mp_to_unsigned_bin_len(priv, x, (int)qLen);
  6666. if (ret == 0) {
  6667. #ifdef WOLFSSL_CHECK_MEM_ZERO
  6668. wc_MemZero_Add("wc_ecc_gen_deterministic_k x", x, qLen);
  6669. #endif
  6670. qbits = mp_count_bits(order);
  6671. ret = mp_read_unsigned_bin(z1, hash, hashSz);
  6672. }
  6673. /* bits2octets on h1 */
  6674. if (ret == 0) {
  6675. XMEMSET(h1, 0, MAX_ECC_BYTES);
  6676. #if !defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  6677. /* mod reduce by order using conditional subtract
  6678. * RFC6979 lists a variant that uses the hash directly instead of
  6679. * doing bits2octets(H(m)), when variant macro is used avoid this
  6680. * bits2octets operation */
  6681. if (mp_cmp(z1, order) == MP_GT) {
  6682. int z1Sz;
  6683. mp_sub(z1, order, z1);
  6684. z1Sz = mp_unsigned_bin_size(z1);
  6685. if (z1Sz < 0 || z1Sz > MAX_ECC_BYTES) {
  6686. ret = BUFFER_E;
  6687. }
  6688. else {
  6689. ret = mp_to_unsigned_bin_len(z1, h1, h1len);
  6690. }
  6691. }
  6692. else
  6693. #endif
  6694. {
  6695. /* use original hash and keep leading 0's */
  6696. mp_to_unsigned_bin_len(z1, h1, (int)h1len);
  6697. }
  6698. }
  6699. mp_free(z1);
  6700. /* 3.2 step d. K = HMAC_K(V || 0x00 || int2octests(x) || bits2octests(h1) */
  6701. if (ret == 0) {
  6702. intOct = 0x00;
  6703. ret = _HMAC_K(K, KSz, V, VSz, h1, h1len, x, xSz, &intOct, K,
  6704. hashType, heap);
  6705. }
  6706. /* 3.2 step e. V = HMAC_K(V) */
  6707. if (ret == 0) {
  6708. ret = _HMAC_K(K, KSz, V, VSz, NULL, 0, NULL, 0, NULL, V, hashType,
  6709. heap);
  6710. }
  6711. /* 3.2 step f. K = HMAC_K(V || 0x01 || int2octests(x) || bits2octests(h1) */
  6712. if (ret == 0) {
  6713. intOct = 0x01;
  6714. ret = _HMAC_K(K, KSz, V, VSz, h1, h1len, x, xSz, &intOct, K, hashType,
  6715. heap);
  6716. }
  6717. /* 3.2 step g. V = HMAC_K(V) */
  6718. if (ret == 0) {
  6719. ret = _HMAC_K(K, KSz, V, VSz, NULL, 0, NULL, 0, NULL, V, hashType,
  6720. heap);
  6721. }
  6722. /* 3.2 step h. loop through the next steps until a valid value is found */
  6723. if (ret == 0 ) {
  6724. int err;
  6725. intOct = 0x00;
  6726. do {
  6727. xSz = 0; /* used as tLen */
  6728. err = 0; /* start as good until generated k is tested */
  6729. /* 3.2 step h.2 when tlen < qlen do V = HMAC_K(V); T = T || V */
  6730. while (xSz < qLen) {
  6731. ret = _HMAC_K(K, KSz, V, VSz, NULL, 0, NULL, 0, NULL, V,
  6732. hashType, heap);
  6733. if (ret == 0) {
  6734. int sz;
  6735. sz = (int)MIN(qLen - xSz, (size_t)VSz);
  6736. XMEMCPY(x + xSz, V, (size_t)sz);
  6737. xSz += (word32)sz;
  6738. }
  6739. else {
  6740. break; /* error case */
  6741. }
  6742. }
  6743. if (ret == 0) {
  6744. mp_clear(k); /* 3.2 step h.1 clear T */
  6745. ret = mp_read_unsigned_bin(k, x, xSz);
  6746. }
  6747. if ((ret == 0) && ((int)(xSz * WOLFSSL_BIT_SIZE) != qbits)) {
  6748. /* handle odd case where shift of 'k' is needed with RFC 6979
  6749. * k = bits2int(T) in section 3.2 h.3 */
  6750. mp_rshb(k, ((int)xSz * WOLFSSL_BIT_SIZE) - qbits);
  6751. }
  6752. /* 3.2 step h.3 the key should be smaller than the order of base
  6753. * point */
  6754. if (ret == 0) {
  6755. if (mp_cmp(k, order) != MP_LT) {
  6756. err = MP_VAL;
  6757. } else if (mp_iszero(k) == MP_YES) {
  6758. /* no 0 key's */
  6759. err = MP_ZERO_E;
  6760. }
  6761. }
  6762. /* 3.2 step h.3 if there was a problem with 'k' generated then try
  6763. * again K = HMAC_K(V || 0x00) and V = HMAC_K(V) */
  6764. if (ret == 0 && err != 0) {
  6765. ret = _HMAC_K(K, KSz, V, VSz, NULL, 0, NULL, 0, &intOct, K,
  6766. hashType, heap);
  6767. if (ret == 0) {
  6768. ret = _HMAC_K(K, KSz, V, VSz, NULL, 0, NULL, 0, NULL, V,
  6769. hashType, heap);
  6770. }
  6771. }
  6772. } while (ret == 0 && err != 0);
  6773. }
  6774. ForceZero(x, MAX_ECC_BYTES);
  6775. #ifdef WOLFSSL_SMALL_STACK
  6776. if (z1 != NULL)
  6777. XFREE(z1, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6778. if (x != NULL)
  6779. XFREE(x, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6780. if (K != NULL)
  6781. XFREE(K, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6782. if (V != NULL)
  6783. XFREE(V, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6784. if (h1 != NULL)
  6785. XFREE(h1, heap, DYNAMIC_TYPE_DIGEST);
  6786. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  6787. wc_MemZero_Check(x, MAX_ECC_BYTES);
  6788. #endif
  6789. return ret;
  6790. }
  6791. /* Sets the deterministic flag for 'k' generation with sign.
  6792. * returns 0 on success
  6793. */
  6794. int wc_ecc_set_deterministic(ecc_key* key, byte flag)
  6795. {
  6796. if (key == NULL) {
  6797. return BAD_FUNC_ARG;
  6798. }
  6799. key->deterministic = flag ? 1 : 0;
  6800. return 0;
  6801. }
  6802. #endif /* end sign_ex and deterministic sign */
  6803. #if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP)
  6804. int wc_ecc_sign_set_k(const byte* k, word32 klen, ecc_key* key)
  6805. {
  6806. int ret = MP_OKAY;
  6807. DECLARE_CURVE_SPECS(1);
  6808. if (k == NULL || klen == 0 || key == NULL) {
  6809. return BAD_FUNC_ARG;
  6810. }
  6811. ALLOC_CURVE_SPECS(1, ret);
  6812. if (ret == MP_OKAY) {
  6813. ret = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ORDER);
  6814. }
  6815. if (ret != 0) {
  6816. FREE_CURVE_SPECS();
  6817. return ret;
  6818. }
  6819. #ifndef WOLFSSL_NO_MALLOC
  6820. if (key->sign_k == NULL) {
  6821. key->sign_k = (mp_int*)XMALLOC(sizeof(mp_int), key->heap,
  6822. DYNAMIC_TYPE_ECC);
  6823. if (key->sign_k) {
  6824. ret = mp_init(key->sign_k);
  6825. }
  6826. else {
  6827. ret = MEMORY_E;
  6828. }
  6829. }
  6830. #endif
  6831. if (ret == 0) {
  6832. ret = mp_read_unsigned_bin(key->sign_k, k, klen);
  6833. }
  6834. if (ret == 0 && mp_cmp(key->sign_k, curve->order) != MP_LT) {
  6835. ret = MP_VAL;
  6836. }
  6837. #ifdef WOLFSSL_NO_MALLOC
  6838. if (ret == 0) {
  6839. key->sign_k_set = 1;
  6840. }
  6841. #endif
  6842. wc_ecc_curve_free(curve);
  6843. FREE_CURVE_SPECS();
  6844. return ret;
  6845. }
  6846. #endif /* WOLFSSL_ECDSA_SET_K || WOLFSSL_ECDSA_SET_K_ONE_LOOP */
  6847. #endif /* WOLFSSL_ATECC508A && WOLFSSL_CRYPTOCELL */
  6848. #endif /* !HAVE_ECC_SIGN */
  6849. #ifdef WOLFSSL_CUSTOM_CURVES
  6850. void wc_ecc_free_curve(const ecc_set_type* curve, void* heap)
  6851. {
  6852. #ifndef WOLFSSL_ECC_CURVE_STATIC
  6853. if (curve->prime != NULL)
  6854. XFREE((void*)curve->prime, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6855. if (curve->Af != NULL)
  6856. XFREE((void*)curve->Af, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6857. if (curve->Bf != NULL)
  6858. XFREE((void*)curve->Bf, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6859. if (curve->order != NULL)
  6860. XFREE((void*)curve->order, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6861. if (curve->Gx != NULL)
  6862. XFREE((void*)curve->Gx, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6863. if (curve->Gy != NULL)
  6864. XFREE((void*)curve->Gy, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6865. #endif
  6866. XFREE((void*)curve, heap, DYNAMIC_TYPE_ECC_BUFFER);
  6867. (void)heap;
  6868. }
  6869. #endif /* WOLFSSL_CUSTOM_CURVES */
  6870. /**
  6871. Free an ECC key from memory
  6872. key The key you wish to free
  6873. */
  6874. WOLFSSL_ABI
  6875. int wc_ecc_free(ecc_key* key)
  6876. {
  6877. if (key == NULL) {
  6878. return 0;
  6879. }
  6880. #if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP)
  6881. #ifndef WOLFSSL_NO_MALLOC
  6882. if (key->sign_k != NULL)
  6883. #endif
  6884. {
  6885. mp_forcezero(key->sign_k);
  6886. mp_free(key->sign_k);
  6887. #ifndef WOLFSSL_NO_MALLOC
  6888. XFREE(key->sign_k, key->heap, DYNAMIC_TYPE_ECC);
  6889. #endif
  6890. }
  6891. #endif
  6892. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  6893. #ifdef WC_ASYNC_ENABLE_ECC
  6894. wolfAsync_DevCtxFree(&key->asyncDev, WOLFSSL_ASYNC_MARKER_ECC);
  6895. #endif
  6896. wc_ecc_free_async(key);
  6897. #endif
  6898. #if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  6899. /* free secure memory */
  6900. if ((key->blackKey != CAAM_BLACK_KEY_CCM &&
  6901. key->blackKey != CAAM_BLACK_KEY_ECB) && key->blackKey > 0) {
  6902. caamFreePart(key->partNum);
  6903. }
  6904. #endif
  6905. #ifdef WOLFSSL_SE050
  6906. se050_ecc_free_key(key);
  6907. #endif
  6908. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  6909. atmel_ecc_free(key->slot);
  6910. key->slot = ATECC_INVALID_SLOT;
  6911. #endif /* WOLFSSL_ATECC508A */
  6912. #ifdef WOLFSSL_KCAPI_ECC
  6913. KcapiEcc_Free(key);
  6914. #endif
  6915. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  6916. key->privKey = NULL;
  6917. ForceZero(key->keyRaw, sizeof(key->keyRaw));
  6918. ForceZero(&key->xSec, sizeof(key->xSec));
  6919. #endif
  6920. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  6921. wc_MAXQ10XX_EccFree(key);
  6922. #endif
  6923. mp_clear(key->pubkey.x);
  6924. mp_clear(key->pubkey.y);
  6925. mp_clear(key->pubkey.z);
  6926. #ifdef ALT_ECC_SIZE
  6927. if (key->k)
  6928. #endif
  6929. mp_forcezero(key->k);
  6930. #ifdef WOLFSSL_CUSTOM_CURVES
  6931. if (key->deallocSet && key->dp != NULL)
  6932. wc_ecc_free_curve(key->dp, key->heap);
  6933. #endif
  6934. #ifdef WOLFSSL_CHECK_MEM_ZERO
  6935. wc_MemZero_Check(key, sizeof(ecc_key));
  6936. #endif
  6937. return 0;
  6938. }
  6939. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  6940. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SP_MATH) && \
  6941. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  6942. /* Handles add failure cases:
  6943. *
  6944. * Before add:
  6945. * Case 1: A is infinity
  6946. * -> Copy B into result.
  6947. * Case 2: B is infinity
  6948. * -> Copy A into result.
  6949. * Case 3: x and z are the same in A and B (same x value in affine)
  6950. * Case 3a: y values the same - same point
  6951. * -> Double instead of add.
  6952. * Case 3b: y values different - negative of the other when points on curve
  6953. * -> Need to set result to infinity.
  6954. *
  6955. * After add:
  6956. * Case 1: A and B are the same point (maybe different z)
  6957. * (Result was: x == y == z == 0)
  6958. * -> Need to double instead.
  6959. *
  6960. * Case 2: A + B = <infinity> = 0.
  6961. * (Result was: z == 0, x and/or y not 0)
  6962. * -> Need to set result to infinity.
  6963. */
  6964. int ecc_projective_add_point_safe(ecc_point* A, ecc_point* B, ecc_point* R,
  6965. mp_int* a, mp_int* modulus, mp_digit mp, int* infinity)
  6966. {
  6967. int err;
  6968. if (mp_iszero(A->x) && mp_iszero(A->y)) {
  6969. /* A is infinity. */
  6970. err = wc_ecc_copy_point(B, R);
  6971. }
  6972. else if (mp_iszero(B->x) && mp_iszero(B->y)) {
  6973. /* B is infinity. */
  6974. err = wc_ecc_copy_point(A, R);
  6975. }
  6976. else if ((mp_cmp(A->x, B->x) == MP_EQ) && (mp_cmp(A->z, B->z) == MP_EQ)) {
  6977. /* x ordinattes the same. */
  6978. if (mp_cmp(A->y, B->y) == MP_EQ) {
  6979. /* A = B */
  6980. err = _ecc_projective_dbl_point(B, R, a, modulus, mp);
  6981. }
  6982. else {
  6983. /* A = -B */
  6984. err = mp_set(R->x, 0);
  6985. if (err == MP_OKAY)
  6986. err = mp_set(R->y, 0);
  6987. if (err == MP_OKAY)
  6988. err = mp_set(R->z, 1);
  6989. if ((err == MP_OKAY) && (infinity != NULL))
  6990. *infinity = 1;
  6991. }
  6992. }
  6993. else {
  6994. err = _ecc_projective_add_point(A, B, R, a, modulus, mp);
  6995. if ((err == MP_OKAY) && mp_iszero(R->z)) {
  6996. /* When all zero then should have done a double */
  6997. if (mp_iszero(R->x) && mp_iszero(R->y)) {
  6998. if (mp_iszero(B->z)) {
  6999. err = wc_ecc_copy_point(B, R);
  7000. if (err == MP_OKAY) {
  7001. err = mp_montgomery_calc_normalization(R->z, modulus);
  7002. }
  7003. if (err == MP_OKAY) {
  7004. err = _ecc_projective_dbl_point(R, R, a, modulus, mp);
  7005. }
  7006. }
  7007. else {
  7008. err = _ecc_projective_dbl_point(B, R, a, modulus, mp);
  7009. }
  7010. }
  7011. /* When only Z zero then result is infinity */
  7012. else {
  7013. err = mp_set(R->x, 0);
  7014. if (err == MP_OKAY)
  7015. err = mp_set(R->y, 0);
  7016. if (err == MP_OKAY)
  7017. err = mp_set(R->z, 1);
  7018. if ((err == MP_OKAY) && (infinity != NULL))
  7019. *infinity = 1;
  7020. }
  7021. }
  7022. }
  7023. return err;
  7024. }
  7025. /* Handles when P is the infinity point.
  7026. *
  7027. * Double infinity -> infinity.
  7028. * Otherwise do normal double - which can't lead to infinity as odd order.
  7029. */
  7030. int ecc_projective_dbl_point_safe(ecc_point *P, ecc_point *R, mp_int* a,
  7031. mp_int* modulus, mp_digit mp)
  7032. {
  7033. int err;
  7034. if (mp_iszero(P->x) && mp_iszero(P->y)) {
  7035. /* P is infinity. */
  7036. err = wc_ecc_copy_point(P, R);
  7037. }
  7038. else {
  7039. err = _ecc_projective_dbl_point(P, R, a, modulus, mp);
  7040. if ((err == MP_OKAY) && mp_iszero(R->z)) {
  7041. err = mp_set(R->x, 0);
  7042. if (err == MP_OKAY)
  7043. err = mp_set(R->y, 0);
  7044. if (err == MP_OKAY)
  7045. err = mp_set(R->z, 1);
  7046. }
  7047. }
  7048. return err;
  7049. }
  7050. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A
  7051. && !WOLFSSL_CRYPTOCELL && !WOLFSSL_SP_MATH */
  7052. #if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_ATECC508A) && \
  7053. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_CRYPTOCELL) && \
  7054. !defined(WOLFSSL_KCAPI_ECC) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  7055. #ifdef ECC_SHAMIR
  7056. static int ecc_mont_norm_points(ecc_point* A, ecc_point* Am, ecc_point* B,
  7057. ecc_point* Bm, mp_int* modulus, void* heap)
  7058. {
  7059. int err = MP_OKAY;
  7060. DECL_MP_INT_SIZE_DYN(mu, mp_bitsused(modulus), MAX_ECC_BITS_USE);
  7061. (void)heap;
  7062. NEW_MP_INT_SIZE(mu, mp_bitsused(modulus), heap, DYNAMIC_TYPE_ECC);
  7063. #ifdef MP_INT_SIZE_CHECK_NULL
  7064. if (mu == NULL)
  7065. err = MEMORY_E;
  7066. #endif
  7067. if (err == MP_OKAY) {
  7068. err = INIT_MP_INT_SIZE(mu, mp_bitsused(modulus));
  7069. }
  7070. if (err == MP_OKAY) {
  7071. err = mp_montgomery_calc_normalization(mu, modulus);
  7072. if (err == MP_OKAY) {
  7073. /* copy ones ... */
  7074. err = mp_mulmod(A->x, mu, modulus, Am->x);
  7075. }
  7076. if (err == MP_OKAY)
  7077. err = mp_mulmod(A->y, mu, modulus, Am->y);
  7078. if (err == MP_OKAY)
  7079. err = mp_mulmod(A->z, mu, modulus, Am->z);
  7080. if (err == MP_OKAY)
  7081. err = mp_mulmod(B->x, mu, modulus, Bm->x);
  7082. if (err == MP_OKAY)
  7083. err = mp_mulmod(B->y, mu, modulus, Bm->y);
  7084. if (err == MP_OKAY)
  7085. err = mp_mulmod(B->z, mu, modulus, Bm->z);
  7086. /* done with mu */
  7087. mp_clear(mu);
  7088. }
  7089. FREE_MP_INT_SIZE(mu, heap, DYNAMIC_TYPE_ECC);
  7090. return err;
  7091. }
  7092. /** Computes kA*A + kB*B = C using Shamir's Trick
  7093. A First point to multiply
  7094. kA What to multiple A by
  7095. B Second point to multiply
  7096. kB What to multiple B by
  7097. C [out] Destination point (can overlap with A or B)
  7098. a ECC curve parameter a
  7099. modulus Modulus for curve
  7100. return MP_OKAY on success
  7101. */
  7102. #ifdef FP_ECC
  7103. static int normal_ecc_mul2add(ecc_point* A, mp_int* kA,
  7104. ecc_point* B, mp_int* kB,
  7105. ecc_point* C, mp_int* a, mp_int* modulus,
  7106. void* heap)
  7107. #else
  7108. int ecc_mul2add(ecc_point* A, mp_int* kA,
  7109. ecc_point* B, mp_int* kB,
  7110. ecc_point* C, mp_int* a, mp_int* modulus,
  7111. void* heap)
  7112. #endif
  7113. {
  7114. #ifdef WOLFSSL_SMALL_STACK_CACHE
  7115. ecc_key *key = NULL;
  7116. #endif
  7117. #ifdef WOLFSSL_SMALL_STACK
  7118. ecc_point** precomp = NULL;
  7119. #else
  7120. ecc_point* precomp[SHAMIR_PRECOMP_SZ];
  7121. #ifdef WOLFSSL_NO_MALLOC
  7122. ecc_point lcl_precomp[SHAMIR_PRECOMP_SZ];
  7123. #endif
  7124. #endif
  7125. unsigned int bitbufA, bitbufB, lenA, lenB, len, nA, nB, nibble;
  7126. #ifdef WOLFSSL_NO_MALLOC
  7127. unsigned char tA[ECC_BUFSIZE];
  7128. unsigned char tB[ECC_BUFSIZE];
  7129. #else
  7130. unsigned char* tA = NULL;
  7131. unsigned char* tB = NULL;
  7132. #endif
  7133. int err = MP_OKAY, first, x, y;
  7134. mp_digit mp = 0;
  7135. /* argchks */
  7136. if (A == NULL || kA == NULL || B == NULL || kB == NULL || C == NULL ||
  7137. modulus == NULL) {
  7138. return ECC_BAD_ARG_E;
  7139. }
  7140. #ifndef WOLFSSL_NO_MALLOC
  7141. /* allocate memory */
  7142. tA = (unsigned char*)XMALLOC(ECC_BUFSIZE, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7143. if (tA == NULL) {
  7144. return GEN_MEM_ERR;
  7145. }
  7146. tB = (unsigned char*)XMALLOC(ECC_BUFSIZE, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7147. if (tB == NULL) {
  7148. XFREE(tA, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7149. return GEN_MEM_ERR;
  7150. }
  7151. #endif
  7152. #ifdef WOLFSSL_SMALL_STACK_CACHE
  7153. key = (ecc_key *)XMALLOC(sizeof(*key), heap, DYNAMIC_TYPE_ECC_BUFFER);
  7154. if (key == NULL) {
  7155. XFREE(tB, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7156. XFREE(tA, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7157. return GEN_MEM_ERR;
  7158. }
  7159. #endif
  7160. #ifdef WOLFSSL_SMALL_STACK
  7161. precomp = (ecc_point**)XMALLOC(sizeof(ecc_point*) * SHAMIR_PRECOMP_SZ, heap,
  7162. DYNAMIC_TYPE_ECC_BUFFER);
  7163. if (precomp == NULL) {
  7164. XFREE(tB, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7165. XFREE(tA, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7166. #ifdef WOLFSSL_SMALL_STACK_CACHE
  7167. XFREE(key, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7168. #endif
  7169. return GEN_MEM_ERR;
  7170. }
  7171. #endif
  7172. #ifdef WOLFSSL_SMALL_STACK_CACHE
  7173. key->t1 = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  7174. key->t2 = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  7175. #ifdef ALT_ECC_SIZE
  7176. key->x = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  7177. key->y = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  7178. key->z = (mp_int*)XMALLOC(sizeof(mp_int), heap, DYNAMIC_TYPE_ECC);
  7179. #endif
  7180. if (key->t1 == NULL || key->t2 == NULL
  7181. #ifdef ALT_ECC_SIZE
  7182. || key->x == NULL || key->y == NULL || key->z == NULL
  7183. #endif
  7184. ) {
  7185. #ifdef ALT_ECC_SIZE
  7186. XFREE(key->z, heap, DYNAMIC_TYPE_ECC);
  7187. XFREE(key->y, heap, DYNAMIC_TYPE_ECC);
  7188. XFREE(key->x, heap, DYNAMIC_TYPE_ECC);
  7189. #endif
  7190. XFREE(key->t2, heap, DYNAMIC_TYPE_ECC);
  7191. XFREE(key->t1, heap, DYNAMIC_TYPE_ECC);
  7192. XFREE(precomp, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7193. XFREE(tB, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7194. XFREE(tA, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7195. XFREE(key, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7196. return MEMORY_E;
  7197. }
  7198. C->key = key;
  7199. #endif /* WOLFSSL_SMALL_STACK_CACHE */
  7200. /* init variables */
  7201. XMEMSET(tA, 0, ECC_BUFSIZE);
  7202. XMEMSET(tB, 0, ECC_BUFSIZE);
  7203. #ifndef WOLFSSL_SMALL_STACK
  7204. XMEMSET(precomp, 0, sizeof(precomp));
  7205. #else
  7206. XMEMSET(precomp, 0, sizeof(ecc_point*) * SHAMIR_PRECOMP_SZ);
  7207. #endif
  7208. #ifdef WOLFSSL_CHECK_MEM_ZERO
  7209. wc_MemZero_Add("ecc_mul2add tA", tA, ECC_BUFSIZE);
  7210. wc_MemZero_Add("ecc_mul2add tB", tB, ECC_BUFSIZE);
  7211. #endif
  7212. /* get sizes */
  7213. lenA = (unsigned int)mp_unsigned_bin_size(kA);
  7214. lenB = (unsigned int)mp_unsigned_bin_size(kB);
  7215. len = MAX(lenA, lenB);
  7216. /* sanity check */
  7217. if ((lenA > ECC_BUFSIZE) || (lenB > ECC_BUFSIZE)) {
  7218. err = BAD_FUNC_ARG;
  7219. }
  7220. if (err == MP_OKAY) {
  7221. /* extract and justify kA */
  7222. err = mp_to_unsigned_bin(kA, (len - lenA) + tA);
  7223. /* extract and justify kB */
  7224. if (err == MP_OKAY)
  7225. err = mp_to_unsigned_bin(kB, (len - lenB) + tB);
  7226. /* allocate the table */
  7227. if (err == MP_OKAY) {
  7228. for (x = 0; x < SHAMIR_PRECOMP_SZ; x++) {
  7229. #ifdef WOLFSSL_NO_MALLOC
  7230. precomp[x] = &lcl_precomp[x];
  7231. #endif
  7232. err = wc_ecc_new_point_ex(&precomp[x], heap);
  7233. if (err != MP_OKAY)
  7234. break;
  7235. #ifdef WOLFSSL_SMALL_STACK_CACHE
  7236. precomp[x]->key = key;
  7237. #endif
  7238. }
  7239. }
  7240. }
  7241. if (err == MP_OKAY)
  7242. /* init montgomery reduction */
  7243. err = mp_montgomery_setup(modulus, &mp);
  7244. if (err == MP_OKAY) {
  7245. err = ecc_mont_norm_points(A, precomp[1], B, precomp[1<<2], modulus, heap);
  7246. }
  7247. if (err == MP_OKAY) {
  7248. /* precomp [i,0](A + B) table */
  7249. err = ecc_projective_dbl_point_safe(precomp[1], precomp[2], a, modulus, mp);
  7250. }
  7251. if (err == MP_OKAY) {
  7252. err = ecc_projective_add_point_safe(precomp[1], precomp[2], precomp[3],
  7253. a, modulus, mp, NULL);
  7254. }
  7255. if (err == MP_OKAY) {
  7256. /* precomp [0,i](A + B) table */
  7257. err = ecc_projective_dbl_point_safe(precomp[4], precomp[8], a, modulus, mp);
  7258. }
  7259. if (err == MP_OKAY) {
  7260. err = ecc_projective_add_point_safe(precomp[4], precomp[8], precomp[12], a,
  7261. modulus, mp, NULL);
  7262. }
  7263. if (err == MP_OKAY) {
  7264. /* precomp [i,j](A + B) table (i != 0, j != 0) */
  7265. for (x = 1; x < 4; x++) {
  7266. for (y = 1; y < 4; y++) {
  7267. if (err == MP_OKAY) {
  7268. err = ecc_projective_add_point_safe(precomp[x], precomp[(y<<2)],
  7269. precomp[x+(y<<2)], a, modulus,
  7270. mp, NULL);
  7271. }
  7272. }
  7273. }
  7274. }
  7275. if (err == MP_OKAY) {
  7276. nibble = 3;
  7277. first = 1;
  7278. bitbufA = tA[0];
  7279. bitbufB = tB[0];
  7280. /* for every byte of the multiplicands */
  7281. for (x = 0; x < (int)len || nibble != 3; ) {
  7282. /* grab a nibble */
  7283. if (++nibble == 4) {
  7284. if (x == (int)len) break;
  7285. bitbufA = tA[x];
  7286. bitbufB = tB[x];
  7287. nibble = 0;
  7288. x++;
  7289. }
  7290. /* extract two bits from both, shift/update */
  7291. nA = (bitbufA >> 6) & 0x03;
  7292. nB = (bitbufB >> 6) & 0x03;
  7293. bitbufA = (bitbufA << 2) & 0xFF;
  7294. bitbufB = (bitbufB << 2) & 0xFF;
  7295. /* if both zero, if first, continue */
  7296. if ((nA == 0) && (nB == 0) && (first == 1)) {
  7297. continue;
  7298. }
  7299. /* double twice, only if this isn't the first */
  7300. if (first == 0) {
  7301. /* double twice */
  7302. if (err == MP_OKAY)
  7303. err = ecc_projective_dbl_point_safe(C, C, a, modulus, mp);
  7304. if (err == MP_OKAY)
  7305. err = ecc_projective_dbl_point_safe(C, C, a, modulus, mp);
  7306. else
  7307. break;
  7308. }
  7309. /* if not both zero */
  7310. if ((nA != 0) || (nB != 0)) {
  7311. unsigned int i = nA + (nB<<2);
  7312. if (first == 1) {
  7313. /* if first, copy from table */
  7314. first = 0;
  7315. if (err == MP_OKAY)
  7316. err = mp_copy(precomp[i]->x, C->x);
  7317. if (err == MP_OKAY)
  7318. err = mp_copy(precomp[i]->y, C->y);
  7319. if (err == MP_OKAY)
  7320. err = mp_copy(precomp[i]->z, C->z);
  7321. else
  7322. break;
  7323. } else {
  7324. /* if not first, add from table */
  7325. if (err == MP_OKAY)
  7326. err = ecc_projective_add_point_safe(C, precomp[i],
  7327. C, a, modulus, mp,
  7328. &first);
  7329. if (err != MP_OKAY)
  7330. break;
  7331. }
  7332. }
  7333. }
  7334. }
  7335. /* reduce to affine */
  7336. if (err == MP_OKAY)
  7337. err = ecc_map(C, modulus, mp);
  7338. /* clean up */
  7339. for (x = 0; x < SHAMIR_PRECOMP_SZ; x++) {
  7340. wc_ecc_del_point_ex(precomp[x], heap);
  7341. }
  7342. ForceZero(tA, ECC_BUFSIZE);
  7343. ForceZero(tB, ECC_BUFSIZE);
  7344. #ifdef WOLFSSL_SMALL_STACK_CACHE
  7345. #ifdef ALT_ECC_SIZE
  7346. XFREE(key->z, heap, DYNAMIC_TYPE_ECC);
  7347. XFREE(key->y, heap, DYNAMIC_TYPE_ECC);
  7348. XFREE(key->x, heap, DYNAMIC_TYPE_ECC);
  7349. #endif
  7350. XFREE(key->t2, heap, DYNAMIC_TYPE_ECC);
  7351. XFREE(key->t1, heap, DYNAMIC_TYPE_ECC);
  7352. XFREE(key, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7353. C->key = NULL;
  7354. #endif
  7355. #ifdef WOLFSSL_SMALL_STACK
  7356. XFREE(precomp, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7357. #endif
  7358. #ifndef WOLFSSL_NO_MALLOC
  7359. XFREE(tB, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7360. XFREE(tA, heap, DYNAMIC_TYPE_ECC_BUFFER);
  7361. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  7362. wc_MemZero_Check(tB, ECC_BUFSIZE);
  7363. wc_MemZero_Check(tA, ECC_BUFSIZE);
  7364. #endif
  7365. return err;
  7366. }
  7367. #endif /* ECC_SHAMIR */
  7368. #endif /* (!WOLFSSL_SP_MATH && !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A &&
  7369. * !WOLFSSL_CRYPTOCEL */
  7370. #ifdef HAVE_ECC_VERIFY
  7371. #ifndef NO_ASN
  7372. /* verify
  7373. *
  7374. * w = s^-1 mod n
  7375. * u1 = xw
  7376. * u2 = rw
  7377. * X = u1*G + u2*Q
  7378. * v = X_x1 mod n
  7379. * accept if v == r
  7380. */
  7381. /**
  7382. Verify an ECC signature
  7383. sig The signature to verify
  7384. siglen The length of the signature (octets)
  7385. hash The hash (message digest) that was signed
  7386. hashlen The length of the hash (octets)
  7387. res Result of signature, 1==valid, 0==invalid
  7388. key The corresponding public ECC key
  7389. return MP_OKAY if successful (even if the signature is not valid)
  7390. Caller should check the *res value to determine if the signature
  7391. is valid or invalid. Other negative values are returned on error.
  7392. */
  7393. WOLFSSL_ABI
  7394. int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
  7395. word32 hashlen, int* res, ecc_key* key)
  7396. {
  7397. int err;
  7398. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  7399. mp_int *r = NULL, *s = NULL;
  7400. #else
  7401. DECL_MP_INT_SIZE_DYN(r, ECC_KEY_MAX_BITS(key), MAX_ECC_BITS_USE);
  7402. DECL_MP_INT_SIZE_DYN(s, ECC_KEY_MAX_BITS(key), MAX_ECC_BITS_USE);
  7403. #endif
  7404. #ifdef WOLFSSL_ASYNC_CRYPT
  7405. int isPrivateKeyOnly = 0;
  7406. #endif
  7407. if (sig == NULL || hash == NULL || res == NULL || key == NULL) {
  7408. return ECC_BAD_ARG_E;
  7409. }
  7410. #ifdef WOLF_CRYPTO_CB
  7411. #ifndef WOLF_CRYPTO_CB_FIND
  7412. if (key->devId != INVALID_DEVID)
  7413. #endif
  7414. {
  7415. err = wc_CryptoCb_EccVerify(sig, siglen, hash, hashlen, res, key);
  7416. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  7417. if (err != CRYPTOCB_UNAVAILABLE)
  7418. return err;
  7419. /* fall-through when unavailable */
  7420. #endif
  7421. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  7422. if (err == CRYPTOCB_UNAVAILABLE) {
  7423. err = NO_VALID_DEVID;
  7424. }
  7425. #endif
  7426. }
  7427. #endif
  7428. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  7429. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  7430. err = wc_ecc_alloc_async(key);
  7431. if (err != 0)
  7432. return err;
  7433. r = key->r;
  7434. s = key->s;
  7435. #else
  7436. NEW_MP_INT_SIZE(r, ECC_KEY_MAX_BITS(key), key->heap, DYNAMIC_TYPE_ECC);
  7437. #ifdef MP_INT_SIZE_CHECK_NULL
  7438. if (r == NULL)
  7439. return MEMORY_E;
  7440. #endif
  7441. NEW_MP_INT_SIZE(s, ECC_KEY_MAX_BITS(key), key->heap, DYNAMIC_TYPE_ECC);
  7442. #ifdef MP_INT_SIZE_CHECK_NULL
  7443. if (s == NULL) {
  7444. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  7445. return MEMORY_E;
  7446. }
  7447. #endif
  7448. err = INIT_MP_INT_SIZE(r, ECC_KEY_MAX_BITS(key));
  7449. if (err != 0) {
  7450. FREE_MP_INT_SIZE(s, key->heap, DYNAMIC_TYPE_ECC);
  7451. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  7452. return err;
  7453. }
  7454. err = INIT_MP_INT_SIZE(s, ECC_KEY_MAX_BITS(key));
  7455. if (err != 0) {
  7456. FREE_MP_INT_SIZE(s, key->heap, DYNAMIC_TYPE_ECC);
  7457. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  7458. return err;
  7459. }
  7460. #endif /* WOLFSSL_ASYNC_CRYPT */
  7461. switch (key->state) {
  7462. case ECC_STATE_NONE:
  7463. case ECC_STATE_VERIFY_DECODE:
  7464. key->state = ECC_STATE_VERIFY_DECODE;
  7465. /* default to invalid signature */
  7466. *res = 0;
  7467. /* Decode ASN.1 ECDSA signature. */
  7468. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  7469. /* Note, DecodeECC_DSA_Sig() calls mp_init() on r and s.
  7470. * If either of those don't allocate correctly, none of
  7471. * the rest of this function will execute, and everything
  7472. * gets cleaned up at the end. */
  7473. err = DecodeECC_DSA_Sig(sig, siglen, r, s);
  7474. #else
  7475. /* r and s are initialized. */
  7476. err = DecodeECC_DSA_Sig_Ex(sig, siglen, r, s, 0);
  7477. #endif
  7478. if (err < 0) {
  7479. break;
  7480. }
  7481. FALL_THROUGH;
  7482. case ECC_STATE_VERIFY_DO:
  7483. key->state = ECC_STATE_VERIFY_DO;
  7484. #ifdef WOLFSSL_ASYNC_CRYPT
  7485. if (key->type == ECC_PRIVATEKEY_ONLY) {
  7486. isPrivateKeyOnly = 1;
  7487. }
  7488. #endif
  7489. err = wc_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);
  7490. #ifndef WOLFSSL_ASYNC_CRYPT
  7491. /* done with R/S */
  7492. mp_clear(r);
  7493. mp_clear(s);
  7494. FREE_MP_INT_SIZE(s, key->heap, DYNAMIC_TYPE_ECC);
  7495. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  7496. #ifdef MP_INT_SIZE_CHECK_NULL
  7497. r = NULL;
  7498. s = NULL;
  7499. #endif
  7500. #endif
  7501. if (err < 0) {
  7502. break;
  7503. }
  7504. FALL_THROUGH;
  7505. case ECC_STATE_VERIFY_RES:
  7506. key->state = ECC_STATE_VERIFY_RES;
  7507. err = 0;
  7508. break;
  7509. default:
  7510. err = BAD_STATE_E;
  7511. }
  7512. #ifdef WOLFSSL_ASYNC_CRYPT
  7513. /* if async pending then return and skip done cleanup below */
  7514. if (err == WC_PENDING_E) {
  7515. if (!isPrivateKeyOnly) /* do not advance state if doing make pub key */
  7516. key->state++;
  7517. return err;
  7518. }
  7519. #endif
  7520. /* cleanup */
  7521. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  7522. wc_ecc_free_async(key);
  7523. #else
  7524. FREE_MP_INT_SIZE(s, key->heap, DYNAMIC_TYPE_ECC);
  7525. FREE_MP_INT_SIZE(r, key->heap, DYNAMIC_TYPE_ECC);
  7526. #endif
  7527. /* make sure required variables are reset */
  7528. wc_ecc_reset(key);
  7529. #else
  7530. (void)siglen;
  7531. (void)hashlen;
  7532. (void)s;
  7533. (void)r;
  7534. (void)err;
  7535. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  7536. return err;
  7537. }
  7538. #endif /* !NO_ASN */
  7539. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  7540. #if !defined(WOLFSSL_STM32_PKA) && !defined(WOLFSSL_PSOC6_CRYPTO) && \
  7541. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  7542. static int wc_ecc_check_r_s_range(ecc_key* key, mp_int* r, mp_int* s)
  7543. {
  7544. int err = MP_OKAY;
  7545. DECLARE_CURVE_SPECS(1);
  7546. ALLOC_CURVE_SPECS(1, err);
  7547. if (err == MP_OKAY) {
  7548. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ORDER);
  7549. }
  7550. if (err != 0) {
  7551. FREE_CURVE_SPECS();
  7552. return err;
  7553. }
  7554. if (mp_iszero(r) || mp_iszero(s)) {
  7555. err = MP_ZERO_E;
  7556. }
  7557. if ((err == 0) && (mp_cmp(r, curve->order) != MP_LT)) {
  7558. err = MP_VAL;
  7559. }
  7560. if ((err == 0) && (mp_cmp(s, curve->order) != MP_LT)) {
  7561. err = MP_VAL;
  7562. }
  7563. wc_ecc_curve_free(curve);
  7564. FREE_CURVE_SPECS();
  7565. return err;
  7566. }
  7567. #endif /* !WOLFSSL_STM32_PKA && !WOLFSSL_PSOC6_CRYPTO */
  7568. #ifdef HAVE_ECC_VERIFY_HELPER
  7569. static int ecc_verify_hash_sp(mp_int *r, mp_int *s, const byte* hash,
  7570. word32 hashlen, int* res, ecc_key* key)
  7571. {
  7572. (void)r;
  7573. (void)s;
  7574. (void)hash;
  7575. (void)hashlen;
  7576. (void)res;
  7577. (void)key;
  7578. #if defined(WOLFSSL_DSP) && !defined(FREESCALE_LTC_ECC)
  7579. if (key->handle != -1) {
  7580. return sp_dsp_ecc_verify_256(key->handle, hash, hashlen, key->pubkey.x,
  7581. key->pubkey.y, key->pubkey.z, r, s, res, key->heap);
  7582. }
  7583. if (wolfSSL_GetHandleCbSet() == 1) {
  7584. return sp_dsp_ecc_verify_256(0, hash, hashlen, key->pubkey.x,
  7585. key->pubkey.y, key->pubkey.z, r, s, res, key->heap);
  7586. }
  7587. #endif
  7588. #if defined(WOLFSSL_SP_MATH) && !defined(FREESCALE_LTC_ECC)
  7589. if (key->idx == ECC_CUSTOM_IDX || (1
  7590. #ifndef WOLFSSL_SP_NO_256
  7591. && ecc_sets[key->idx].id != ECC_SECP256R1
  7592. #endif
  7593. #ifdef WOLFSSL_SP_SM2
  7594. && ecc_sets[key->idx].id != ECC_SM2P256V1
  7595. #endif
  7596. #ifdef WOLFSSL_SP_384
  7597. && ecc_sets[key->idx].id != ECC_SECP384R1
  7598. #endif
  7599. #ifdef WOLFSSL_SP_521
  7600. && ecc_sets[key->idx].id != ECC_SECP521R1
  7601. #endif
  7602. )) {
  7603. return WC_KEY_SIZE_E;
  7604. }
  7605. #endif
  7606. #if defined(WOLFSSL_HAVE_SP_ECC)
  7607. if (key->idx != ECC_CUSTOM_IDX) {
  7608. #if defined(WC_ECC_NONBLOCK) && defined(WC_ECC_NONBLOCK_ONLY)
  7609. /* perform blocking call to non-blocking function */
  7610. ecc_nb_ctx_t nb_ctx;
  7611. int err;
  7612. XMEMSET(&nb_ctx, 0, sizeof(nb_ctx));
  7613. err = NOT_COMPILED_IN; /* set default error */
  7614. #endif
  7615. #ifndef WOLFSSL_SP_NO_256
  7616. if (ecc_sets[key->idx].id == ECC_SECP256R1) {
  7617. #ifdef WC_ECC_NONBLOCK
  7618. if (key->nb_ctx) {
  7619. return sp_ecc_verify_256_nb(&key->nb_ctx->sp_ctx, hash, hashlen,
  7620. key->pubkey.x, key->pubkey.y, key->pubkey.z, r, s, res,
  7621. key->heap);
  7622. }
  7623. #ifdef WC_ECC_NONBLOCK_ONLY
  7624. do { /* perform blocking call to non-blocking function */
  7625. err = sp_ecc_verify_256_nb(&nb_ctx.sp_ctx, hash, hashlen,
  7626. key->pubkey.x, key->pubkey.y, key->pubkey.z, r, s, res,
  7627. key->heap);
  7628. } while (err == FP_WOULDBLOCK);
  7629. return err;
  7630. #endif
  7631. #endif /* WC_ECC_NONBLOCK */
  7632. #if !defined(WC_ECC_NONBLOCK) || (defined(WC_ECC_NONBLOCK) && !defined(WC_ECC_NONBLOCK_ONLY))
  7633. {
  7634. int ret;
  7635. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  7636. ret = sp_ecc_verify_256(hash, hashlen, key->pubkey.x,
  7637. key->pubkey.y, key->pubkey.z, r, s, res, key->heap);
  7638. RESTORE_VECTOR_REGISTERS();
  7639. return ret;
  7640. }
  7641. #endif
  7642. }
  7643. #endif
  7644. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  7645. if (ecc_sets[key->idx].id == ECC_SM2P256V1) {
  7646. #if defined(FP_ECC_CONTROL) && !defined(WOLFSSL_DSP_BUILD)
  7647. return sp_ecc_cache_verify_sm2_256(hash, hashlen, key->pubkey.x,
  7648. key->pubkey.y, key->pubkey.z, r, s, res,
  7649. sp_ecc_get_cache_entry_256(&(key->pubkey), ECC_SM2P256V1,
  7650. key->fpIdx, key->fpBuild, key->heap),
  7651. key->heap);
  7652. #endif
  7653. #if !defined(FP_ECC_CONTROL)
  7654. return sp_ecc_verify_sm2_256(hash, hashlen, key->pubkey.x,
  7655. key->pubkey.y, key->pubkey.z, r, s, res, key->heap);
  7656. #endif
  7657. }
  7658. #endif
  7659. #ifdef WOLFSSL_SP_384
  7660. if (ecc_sets[key->idx].id == ECC_SECP384R1) {
  7661. #ifdef WC_ECC_NONBLOCK
  7662. if (key->nb_ctx) {
  7663. return sp_ecc_verify_384_nb(&key->nb_ctx->sp_ctx, hash, hashlen,
  7664. key->pubkey.x, key->pubkey.y, key->pubkey.z, r, s, res,
  7665. key->heap);
  7666. }
  7667. #ifdef WC_ECC_NONBLOCK_ONLY
  7668. do { /* perform blocking call to non-blocking function */
  7669. err = sp_ecc_verify_384_nb(&nb_ctx.sp_ctx, hash, hashlen,
  7670. key->pubkey.x, key->pubkey.y, key->pubkey.z, r, s, res,
  7671. key->heap);
  7672. } while (err == FP_WOULDBLOCK);
  7673. return err;
  7674. #endif
  7675. #endif /* WC_ECC_NONBLOCK */
  7676. #if !defined(WC_ECC_NONBLOCK) || (defined(WC_ECC_NONBLOCK) && !defined(WC_ECC_NONBLOCK_ONLY))
  7677. {
  7678. int ret;
  7679. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  7680. ret = sp_ecc_verify_384(hash, hashlen, key->pubkey.x,
  7681. key->pubkey.y, key->pubkey.z, r, s, res, key->heap);
  7682. RESTORE_VECTOR_REGISTERS();
  7683. return ret;
  7684. }
  7685. #endif
  7686. }
  7687. #endif
  7688. #ifdef WOLFSSL_SP_521
  7689. if (ecc_sets[key->idx].id == ECC_SECP521R1) {
  7690. #ifdef WC_ECC_NONBLOCK
  7691. if (key->nb_ctx) {
  7692. return sp_ecc_verify_521_nb(&key->nb_ctx->sp_ctx, hash, hashlen,
  7693. key->pubkey.x, key->pubkey.y, key->pubkey.z, r, s, res,
  7694. key->heap);
  7695. }
  7696. #ifdef WC_ECC_NONBLOCK_ONLY
  7697. do { /* perform blocking call to non-blocking function */
  7698. err = sp_ecc_verify_521_nb(&nb_ctx.sp_ctx, hash, hashlen,
  7699. key->pubkey.x, key->pubkey.y, key->pubkey.z, r, s, res,
  7700. key->heap);
  7701. } while (err == FP_WOULDBLOCK);
  7702. return err;
  7703. #endif
  7704. #endif /* WC_ECC_NONBLOCK */
  7705. #if !defined(WC_ECC_NONBLOCK) || (defined(WC_ECC_NONBLOCK) && !defined(WC_ECC_NONBLOCK_ONLY))
  7706. {
  7707. int ret;
  7708. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  7709. ret = sp_ecc_verify_521(hash, hashlen, key->pubkey.x,
  7710. key->pubkey.y, key->pubkey.z, r, s, res, key->heap);
  7711. RESTORE_VECTOR_REGISTERS();
  7712. return ret;
  7713. }
  7714. #endif
  7715. }
  7716. #endif
  7717. }
  7718. #endif
  7719. return NOT_COMPILED_IN;
  7720. }
  7721. #if !defined(WOLFSSL_SP_MATH) || defined(FREESCALE_LTC_ECC)
  7722. static int ecc_verify_hash(mp_int *r, mp_int *s, const byte* hash,
  7723. word32 hashlen, int* res, ecc_key* key, ecc_curve_spec* curve)
  7724. {
  7725. int err;
  7726. ecc_point* mG = NULL;
  7727. ecc_point* mQ = NULL;
  7728. #ifdef WOLFSSL_NO_MALLOC
  7729. ecc_point lcl_mG;
  7730. ecc_point lcl_mQ;
  7731. #endif
  7732. DECL_MP_INT_SIZE_DYN(w, ECC_KEY_MAX_BITS(key), MAX_ECC_BITS_USE);
  7733. #if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(HAVE_CAVIUM_V)
  7734. DECL_MP_INT_SIZE_DYN(e_lcl, ECC_KEY_MAX_BITS(key), MAX_ECC_BITS_USE);
  7735. #endif
  7736. mp_int* e;
  7737. mp_int* v = NULL; /* Will be w. */
  7738. mp_int* u1 = NULL; /* Will be e. */
  7739. mp_int* u2 = NULL; /* Will be w. */
  7740. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_CAVIUM_V)
  7741. err = wc_ecc_alloc_mpint(key, &key->e);
  7742. if (err != 0) {
  7743. return err;
  7744. }
  7745. e = key->e;
  7746. err = mp_init(e);
  7747. #else
  7748. NEW_MP_INT_SIZE(e_lcl, ECC_KEY_MAX_BITS(key), key->heap, DYNAMIC_TYPE_ECC);
  7749. #ifdef MP_INT_SIZE_CHECK_NULL
  7750. if (e_lcl == NULL) {
  7751. return MEMORY_E;
  7752. }
  7753. #endif
  7754. e = e_lcl;
  7755. err = INIT_MP_INT_SIZE(e, ECC_KEY_MAX_BITS(key));
  7756. #endif /* WOLFSSL_ASYNC_CRYPT && HAVE_CAVIUM_V */
  7757. if (err != MP_OKAY) {
  7758. #ifdef WOLFSSL_SMALL_STACK
  7759. #if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(HAVE_CAVIUM_V)
  7760. XFREE(e_lcl, key->heap, DYNAMIC_TYPE_ECC);
  7761. #endif
  7762. #endif
  7763. return MEMORY_E;
  7764. }
  7765. /* read hash */
  7766. if (err == MP_OKAY) {
  7767. /* we may need to truncate if hash is longer than key size */
  7768. unsigned int orderBits = (unsigned int)mp_count_bits(curve->order);
  7769. /* truncate down to byte size, may be all that's needed */
  7770. if ( (WOLFSSL_BIT_SIZE * hashlen) > orderBits)
  7771. hashlen = (orderBits + WOLFSSL_BIT_SIZE - 1) / WOLFSSL_BIT_SIZE;
  7772. err = mp_read_unsigned_bin(e, hash, hashlen);
  7773. /* may still need bit truncation too */
  7774. if (err == MP_OKAY && (WOLFSSL_BIT_SIZE * hashlen) > orderBits)
  7775. mp_rshb(e, (int)(WOLFSSL_BIT_SIZE - (orderBits & 0x7)));
  7776. }
  7777. /* check for async hardware acceleration */
  7778. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  7779. if (err == MP_OKAY && key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  7780. #if defined(HAVE_CAVIUM_V) || defined(HAVE_INTEL_QA)
  7781. #ifdef HAVE_CAVIUM_V
  7782. if (NitroxEccIsCurveSupported(key))
  7783. #endif
  7784. {
  7785. word32 keySz = (word32)key->dp->size;
  7786. err = wc_mp_to_bigint_sz(e, &e->raw, keySz);
  7787. if (err == MP_OKAY)
  7788. err = wc_mp_to_bigint_sz(key->pubkey.x, &key->pubkey.x->raw, keySz);
  7789. if (err == MP_OKAY)
  7790. err = wc_mp_to_bigint_sz(key->pubkey.y, &key->pubkey.y->raw, keySz);
  7791. if (err == MP_OKAY)
  7792. #ifdef HAVE_CAVIUM_V
  7793. err = NitroxEcdsaVerify(key, &e->raw, &key->pubkey.x->raw,
  7794. &key->pubkey.y->raw, &r->raw, &s->raw,
  7795. &curve->prime->raw, &curve->order->raw, res);
  7796. #else
  7797. err = IntelQaEcdsaVerify(&key->asyncDev, &e->raw, &key->pubkey.x->raw,
  7798. &key->pubkey.y->raw, &r->raw, &s->raw, &curve->Af->raw,
  7799. &curve->Bf->raw, &curve->prime->raw, &curve->order->raw,
  7800. &curve->Gx->raw, &curve->Gy->raw, res);
  7801. #endif
  7802. #ifndef HAVE_CAVIUM_V
  7803. mp_clear(e);
  7804. #endif
  7805. return err;
  7806. }
  7807. #endif /* HAVE_CAVIUM_V || HAVE_INTEL_QA */
  7808. }
  7809. #endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
  7810. NEW_MP_INT_SIZE(w, ECC_KEY_MAX_BITS(key), key->heap, DYNAMIC_TYPE_ECC);
  7811. #ifdef MP_INT_SIZE_CHECK_NULL
  7812. if (w == NULL) {
  7813. err = MEMORY_E;
  7814. }
  7815. #endif
  7816. if (err == MP_OKAY) {
  7817. u1 = e;
  7818. u2 = w;
  7819. v = w;
  7820. }
  7821. if (err == MP_OKAY) {
  7822. err = INIT_MP_INT_SIZE(w, ECC_KEY_MAX_BITS(key));
  7823. }
  7824. /* allocate points */
  7825. if (err == MP_OKAY) {
  7826. #ifdef WOLFSSL_NO_MALLOC
  7827. mG = &lcl_mG;
  7828. #endif
  7829. err = wc_ecc_new_point_ex(&mG, key->heap);
  7830. }
  7831. if (err == MP_OKAY) {
  7832. #ifdef WOLFSSL_NO_MALLOC
  7833. mQ = &lcl_mQ;
  7834. #endif
  7835. err = wc_ecc_new_point_ex(&mQ, key->heap);
  7836. }
  7837. /* w = s^-1 mod n */
  7838. if (err == MP_OKAY)
  7839. err = mp_invmod(s, curve->order, w);
  7840. /* u1 = ew */
  7841. if (err == MP_OKAY)
  7842. err = mp_mulmod(e, w, curve->order, u1);
  7843. /* u2 = rw */
  7844. if (err == MP_OKAY)
  7845. err = mp_mulmod(r, w, curve->order, u2);
  7846. /* find mG and mQ */
  7847. if (err == MP_OKAY)
  7848. err = mp_copy(curve->Gx, mG->x);
  7849. if (err == MP_OKAY)
  7850. err = mp_copy(curve->Gy, mG->y);
  7851. if (err == MP_OKAY)
  7852. err = mp_set(mG->z, 1);
  7853. if (err == MP_OKAY)
  7854. err = mp_copy(key->pubkey.x, mQ->x);
  7855. if (err == MP_OKAY)
  7856. err = mp_copy(key->pubkey.y, mQ->y);
  7857. if (err == MP_OKAY)
  7858. err = mp_copy(key->pubkey.z, mQ->z);
  7859. #if defined(FREESCALE_LTC_ECC)
  7860. /* use PKHA to compute u1*mG + u2*mQ */
  7861. if (err == MP_OKAY)
  7862. err = wc_ecc_mulmod_ex(u1, mG, mG, curve->Af, curve->prime, 0, key->heap);
  7863. if (err == MP_OKAY)
  7864. err = wc_ecc_mulmod_ex(u2, mQ, mQ, curve->Af, curve->prime, 0, key->heap);
  7865. if (err == MP_OKAY)
  7866. err = wc_ecc_point_add(mG, mQ, mG, curve->prime);
  7867. #else
  7868. #ifndef ECC_SHAMIR
  7869. if (err == MP_OKAY)
  7870. {
  7871. mp_digit mp = 0;
  7872. if (!mp_iszero((MP_INT_SIZE*)u1)) {
  7873. /* compute u1*mG + u2*mQ = mG */
  7874. err = wc_ecc_mulmod_ex(u1, mG, mG, curve->Af, curve->prime, 0,
  7875. key->heap);
  7876. if (err == MP_OKAY) {
  7877. err = wc_ecc_mulmod_ex(u2, mQ, mQ, curve->Af, curve->prime, 0,
  7878. key->heap);
  7879. }
  7880. /* find the montgomery mp */
  7881. if (err == MP_OKAY)
  7882. err = mp_montgomery_setup(curve->prime, &mp);
  7883. /* add them */
  7884. if (err == MP_OKAY)
  7885. err = ecc_projective_add_point_safe(mQ, mG, mG, curve->Af,
  7886. curve->prime, mp, NULL);
  7887. }
  7888. else {
  7889. /* compute 0*mG + u2*mQ = mG */
  7890. err = wc_ecc_mulmod_ex(u2, mQ, mG, curve->Af, curve->prime, 0,
  7891. key->heap);
  7892. /* find the montgomery mp */
  7893. if (err == MP_OKAY)
  7894. err = mp_montgomery_setup(curve->prime, &mp);
  7895. }
  7896. /* reduce */
  7897. if (err == MP_OKAY)
  7898. err = ecc_map(mG, curve->prime, mp);
  7899. }
  7900. #else
  7901. /* use Shamir's trick to compute u1*mG + u2*mQ using half the doubles */
  7902. if (err == MP_OKAY) {
  7903. err = ecc_mul2add(mG, u1, mQ, u2, mG, curve->Af, curve->prime,
  7904. key->heap);
  7905. }
  7906. #endif /* ECC_SHAMIR */
  7907. #endif /* FREESCALE_LTC_ECC */
  7908. /* v = X_x1 mod n */
  7909. if (err == MP_OKAY)
  7910. err = mp_mod(mG->x, curve->order, v);
  7911. /* does v == r */
  7912. if (err == MP_OKAY) {
  7913. if (mp_cmp(v, r) == MP_EQ)
  7914. *res = 1;
  7915. }
  7916. /* cleanup */
  7917. wc_ecc_del_point_ex(mG, key->heap);
  7918. wc_ecc_del_point_ex(mQ, key->heap);
  7919. mp_clear(e);
  7920. mp_clear(w);
  7921. FREE_MP_INT_SIZE(w, key->heap, DYNAMIC_TYPE_ECC);
  7922. #if !defined(WOLFSSL_ASYNC_CRYPT) || !defined(HAVE_CAVIUM_V)
  7923. FREE_MP_INT_SIZE(e_lcl, key->heap, DYNAMIC_TYPE_ECC);
  7924. #endif
  7925. return err;
  7926. }
  7927. #endif /* !WOLFSSL_SP_MATH || FREESCALE_LTC_ECC */
  7928. #endif /* HAVE_ECC_VERIFY_HELPER */
  7929. /**
  7930. Verify an ECC signature
  7931. r The signature R component to verify
  7932. s The signature S component to verify
  7933. hash The hash (message digest) that was signed
  7934. hashlen The length of the hash (octets)
  7935. res Result of signature, 1==valid, 0==invalid
  7936. key The corresponding public ECC key
  7937. return MP_OKAY if successful (even if the signature is not valid)
  7938. Caller should check the *res value to determine if the signature
  7939. is valid or invalid. Other negative values are returned on error.
  7940. */
  7941. int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
  7942. word32 hashlen, int* res, ecc_key* key)
  7943. {
  7944. #if defined(WOLFSSL_STM32_PKA)
  7945. return stm32_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);
  7946. #elif defined(WOLFSSL_PSOC6_CRYPTO)
  7947. return psoc6_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);
  7948. #else
  7949. int err;
  7950. word32 keySz = 0;
  7951. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  7952. byte sigRS[ATECC_KEY_SIZE*2];
  7953. #elif defined(WOLFSSL_CRYPTOCELL)
  7954. byte sigRS[ECC_MAX_CRYPTO_HW_SIZE*2];
  7955. CRYS_ECDSA_VerifyUserContext_t sigCtxTemp;
  7956. word32 msgLenInBytes = hashlen;
  7957. CRYS_ECPKI_HASH_OpMode_t hash_mode;
  7958. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  7959. byte sigRS[ECC_MAX_CRYPTO_HW_SIZE * 2];
  7960. #elif defined(WOLFSSL_KCAPI_ECC)
  7961. byte sigRS[MAX_ECC_BYTES*2];
  7962. #elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  7963. byte sigRS[ECC_MAX_CRYPTO_HW_SIZE * 2];
  7964. byte hashcopy[ECC_MAX_CRYPTO_HW_SIZE] = {0};
  7965. #else
  7966. int curveLoaded = 0;
  7967. DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT);
  7968. #endif
  7969. if (r == NULL || s == NULL || hash == NULL || res == NULL || key == NULL)
  7970. return ECC_BAD_ARG_E;
  7971. /* default to invalid signature */
  7972. *res = 0;
  7973. /* is the IDX valid ? */
  7974. if (wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL) {
  7975. return ECC_BAD_ARG_E;
  7976. }
  7977. err = wc_ecc_check_r_s_range(key, r, s);
  7978. if (err != MP_OKAY) {
  7979. return err;
  7980. }
  7981. keySz = (word32)key->dp->size;
  7982. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  7983. defined(WOLFSSL_ASYNC_CRYPT_SW)
  7984. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  7985. if (wc_AsyncSwInit(&key->asyncDev, ASYNC_SW_ECC_VERIFY)) {
  7986. WC_ASYNC_SW* sw = &key->asyncDev.sw;
  7987. sw->eccVerify.r = r;
  7988. sw->eccVerify.s = s;
  7989. sw->eccVerify.hash = hash;
  7990. sw->eccVerify.hashlen = hashlen;
  7991. sw->eccVerify.stat = res;
  7992. sw->eccVerify.key = key;
  7993. return WC_PENDING_E;
  7994. }
  7995. }
  7996. #endif
  7997. #ifndef HAVE_ECC_VERIFY_HELPER
  7998. #ifndef WOLFSSL_SE050
  7999. /* Extract R and S with front zero padding (if required),
  8000. * SE050 does this in port layer */
  8001. XMEMSET(sigRS, 0, sizeof(sigRS));
  8002. err = mp_to_unsigned_bin(r, sigRS +
  8003. (keySz - mp_unsigned_bin_size(r)));
  8004. if (err != MP_OKAY) {
  8005. return err;
  8006. }
  8007. err = mp_to_unsigned_bin(s, sigRS + keySz +
  8008. (keySz - mp_unsigned_bin_size(s)));
  8009. if (err != MP_OKAY) {
  8010. return err;
  8011. }
  8012. #endif /* WOLFSSL_SE050 */
  8013. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  8014. err = atmel_ecc_verify(hash, sigRS, key->pubkey_raw, res);
  8015. if (err != 0) {
  8016. return err;
  8017. }
  8018. (void)hashlen;
  8019. #elif defined(WOLFSSL_CRYPTOCELL)
  8020. /* truncate if hash is longer than key size */
  8021. if (msgLenInBytes > keySz) {
  8022. msgLenInBytes = keySz;
  8023. }
  8024. hash_mode = cc310_hashModeECC(msgLenInBytes);
  8025. if (hash_mode == CRYS_ECPKI_HASH_OpModeLast) {
  8026. /* hash_mode = */ cc310_hashModeECC(keySz);
  8027. hash_mode = CRYS_ECPKI_HASH_SHA256_mode;
  8028. }
  8029. /* verify the signature using the public key */
  8030. err = CRYS_ECDSA_Verify(&sigCtxTemp,
  8031. &key->ctx.pubKey,
  8032. hash_mode,
  8033. &sigRS[0],
  8034. keySz*2,
  8035. (byte*)hash,
  8036. msgLenInBytes);
  8037. if (err == CRYS_ECDSA_VERIFY_INCONSISTENT_VERIFY_ERROR) {
  8038. /* signature verification reported invalid signature. */
  8039. *res = 0; /* Redundant, added for code clarity */
  8040. err = MP_OKAY;
  8041. }
  8042. else if (err != SA_SILIB_RET_OK) {
  8043. WOLFSSL_MSG("CRYS_ECDSA_Verify failed");
  8044. return err;
  8045. }
  8046. else {
  8047. /* valid signature. */
  8048. *res = 1;
  8049. err = MP_OKAY;
  8050. }
  8051. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  8052. err = silabs_ecc_verify_hash(&sigRS[0], keySz * 2,
  8053. hash, hashlen,
  8054. res, key);
  8055. #elif defined(WOLFSSL_KCAPI_ECC)
  8056. err = KcapiEcc_Verify(key, hash, hashlen, sigRS, keySz * 2);
  8057. if (err == 0) {
  8058. *res = 1;
  8059. }
  8060. #elif defined(WOLFSSL_SE050)
  8061. err = se050_ecc_verify_hash_ex(hash, hashlen, r, s, key, res);
  8062. #elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  8063. if (hashlen > sizeof(hashcopy))
  8064. return ECC_BAD_ARG_E;
  8065. buf_reverse(hashcopy, hash, (hashlen < keySz) ? hashlen : keySz);
  8066. mp_reverse(sigRS, keySz);
  8067. mp_reverse(sigRS + keySz, keySz);
  8068. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(hashcopy), keySz);
  8069. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(key->keyRaw), keySz * 2);
  8070. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(sigRS), keySz * 2);
  8071. err = XSecure_EllipticVerifySign(&(key->xSec.cinst),
  8072. xil_curve_type[key->dp->id],
  8073. XIL_CAST_U64(hashcopy), keySz,
  8074. XIL_CAST_U64(key->keyRaw),
  8075. XIL_CAST_U64(sigRS));
  8076. if (err != XST_SUCCESS) {
  8077. WOLFSSL_XIL_ERROR("Verify ECC signature failed", err);
  8078. err = WC_HW_E;
  8079. } else {
  8080. *res = 1;
  8081. }
  8082. #endif
  8083. #else
  8084. /* checking if private key with no public part */
  8085. if (key->type == ECC_PRIVATEKEY_ONLY) {
  8086. WOLFSSL_MSG("Verify called with private key, generating public part");
  8087. ALLOC_CURVE_SPECS(ECC_CURVE_FIELD_COUNT, err);
  8088. if (err != MP_OKAY) {
  8089. return err;
  8090. }
  8091. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  8092. if (err != MP_OKAY) {
  8093. FREE_CURVE_SPECS();
  8094. return err;
  8095. }
  8096. err = ecc_make_pub_ex(key, curve, NULL, NULL);
  8097. if (err != MP_OKAY) {
  8098. WOLFSSL_MSG("Unable to extract public key");
  8099. wc_ecc_curve_free(curve);
  8100. FREE_CURVE_SPECS();
  8101. return err;
  8102. }
  8103. curveLoaded = 1;
  8104. }
  8105. err = ecc_verify_hash_sp(r, s, hash, hashlen, res, key);
  8106. if (err != NOT_COMPILED_IN) {
  8107. if (curveLoaded) {
  8108. wc_ecc_curve_free(curve);
  8109. FREE_CURVE_SPECS();
  8110. }
  8111. return err;
  8112. }
  8113. #if !defined(WOLFSSL_SP_MATH) || defined(FREESCALE_LTC_ECC)
  8114. if (!curveLoaded) {
  8115. err = 0; /* potential for NOT_COMPILED_IN error from SP attempt */
  8116. ALLOC_CURVE_SPECS(ECC_CURVE_FIELD_COUNT, err);
  8117. if (err != 0) {
  8118. return err;
  8119. }
  8120. /* read in the specs for this curve */
  8121. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  8122. if (err != 0) {
  8123. FREE_CURVE_SPECS();
  8124. return err;
  8125. }
  8126. }
  8127. err = ecc_verify_hash(r, s, hash, hashlen, res, key, curve);
  8128. #endif /* !WOLFSSL_SP_MATH || FREESCALE_LTC_ECC */
  8129. (void)curveLoaded;
  8130. wc_ecc_curve_free(curve);
  8131. FREE_CURVE_SPECS();
  8132. #endif /* HAVE_ECC_VERIFY_HELPER */
  8133. (void)keySz;
  8134. (void)hashlen;
  8135. return err;
  8136. #endif /* WOLFSSL_STM32_PKA */
  8137. }
  8138. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  8139. #endif /* HAVE_ECC_VERIFY */
  8140. #ifdef HAVE_ECC_KEY_IMPORT
  8141. /* import point from der
  8142. * if shortKeySize != 0 then keysize is always (inLen-1)>>1 */
  8143. int wc_ecc_import_point_der_ex(const byte* in, word32 inLen,
  8144. const int curve_idx, ecc_point* point,
  8145. int shortKeySize)
  8146. {
  8147. int err = 0;
  8148. #ifdef HAVE_COMP_KEY
  8149. int compressed = 0;
  8150. #endif
  8151. int keysize;
  8152. byte pointType;
  8153. #ifndef HAVE_COMP_KEY
  8154. (void)shortKeySize;
  8155. #endif
  8156. if (in == NULL || point == NULL || (curve_idx < 0) ||
  8157. (wc_ecc_is_valid_idx(curve_idx) == 0))
  8158. return ECC_BAD_ARG_E;
  8159. /* must be odd */
  8160. if ((inLen & 1) == 0) {
  8161. return ECC_BAD_ARG_E;
  8162. }
  8163. /* clear if previously allocated */
  8164. mp_clear(point->x);
  8165. mp_clear(point->y);
  8166. mp_clear(point->z);
  8167. /* init point */
  8168. #ifdef ALT_ECC_SIZE
  8169. point->x = (mp_int*)&point->xyz[0];
  8170. point->y = (mp_int*)&point->xyz[1];
  8171. point->z = (mp_int*)&point->xyz[2];
  8172. alt_fp_init(point->x);
  8173. alt_fp_init(point->y);
  8174. alt_fp_init(point->z);
  8175. #else
  8176. err = mp_init_multi(point->x, point->y, point->z, NULL, NULL, NULL);
  8177. #endif
  8178. if (err != MP_OKAY)
  8179. return MEMORY_E;
  8180. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  8181. /* check for point type (4, 2, or 3) */
  8182. pointType = in[0];
  8183. if (pointType != ECC_POINT_UNCOMP && pointType != ECC_POINT_COMP_EVEN &&
  8184. pointType != ECC_POINT_COMP_ODD) {
  8185. err = ASN_PARSE_E;
  8186. }
  8187. if (pointType == ECC_POINT_COMP_EVEN || pointType == ECC_POINT_COMP_ODD) {
  8188. #ifdef HAVE_COMP_KEY
  8189. compressed = 1;
  8190. #else
  8191. err = NOT_COMPILED_IN;
  8192. #endif
  8193. }
  8194. /* adjust to skip first byte */
  8195. inLen -= 1;
  8196. in += 1;
  8197. /* calculate key size based on inLen / 2 if uncompressed or shortKeySize
  8198. * is true */
  8199. #ifdef HAVE_COMP_KEY
  8200. keysize = (int)((compressed && !shortKeySize) ? inLen : inLen>>1);
  8201. #else
  8202. keysize = (int)(inLen>>1);
  8203. #endif
  8204. /* read data */
  8205. if (err == MP_OKAY)
  8206. err = mp_read_unsigned_bin(point->x, in, (word32)keysize);
  8207. #ifdef HAVE_COMP_KEY
  8208. if (err == MP_OKAY && compressed == 1) { /* build y */
  8209. #if defined(WOLFSSL_HAVE_SP_ECC)
  8210. #ifndef WOLFSSL_SP_NO_256
  8211. if (curve_idx != ECC_CUSTOM_IDX &&
  8212. ecc_sets[curve_idx].id == ECC_SECP256R1) {
  8213. err = sp_ecc_uncompress_256(point->x, pointType, point->y);
  8214. }
  8215. else
  8216. #endif
  8217. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  8218. if (curve_idx != ECC_CUSTOM_IDX &&
  8219. ecc_sets[curve_idx].id == ECC_SM2P256V1) {
  8220. sp_ecc_uncompress_sm2_256(point->x, pointType, point->y);
  8221. }
  8222. else
  8223. #endif
  8224. #ifdef WOLFSSL_SP_384
  8225. if (curve_idx != ECC_CUSTOM_IDX &&
  8226. ecc_sets[curve_idx].id == ECC_SECP384R1) {
  8227. err = sp_ecc_uncompress_384(point->x, pointType, point->y);
  8228. }
  8229. else
  8230. #endif
  8231. #ifdef WOLFSSL_SP_521
  8232. if (curve_idx != ECC_CUSTOM_IDX &&
  8233. ecc_sets[curve_idx].id == ECC_SECP521R1) {
  8234. err = sp_ecc_uncompress_521(point->x, pointType, point->y);
  8235. }
  8236. else
  8237. #endif
  8238. #endif
  8239. #if !defined(WOLFSSL_SP_MATH)
  8240. {
  8241. int did_init = 0;
  8242. #ifdef WOLFSSL_SMALL_STACK
  8243. mp_int* t1 = NULL;
  8244. mp_int* t2 = NULL;
  8245. #else
  8246. mp_int t1[1], t2[1];
  8247. #endif
  8248. DECLARE_CURVE_SPECS(3);
  8249. ALLOC_CURVE_SPECS(3, err);
  8250. #ifdef WOLFSSL_SMALL_STACK
  8251. if (err == MP_OKAY) {
  8252. t1 = (mp_int*)XMALLOC(sizeof(mp_int), NULL,
  8253. DYNAMIC_TYPE_BIGINT);
  8254. if (t1 == NULL) {
  8255. err = MEMORY_E;
  8256. }
  8257. }
  8258. if (err == MP_OKAY) {
  8259. t2 = (mp_int*)XMALLOC(sizeof(mp_int), NULL,
  8260. DYNAMIC_TYPE_BIGINT);
  8261. if (t2 == NULL) {
  8262. err = MEMORY_E;
  8263. }
  8264. }
  8265. #endif
  8266. if (err == MP_OKAY) {
  8267. if (mp_init_multi(t1, t2, NULL, NULL, NULL, NULL) != MP_OKAY)
  8268. err = MEMORY_E;
  8269. else
  8270. did_init = 1;
  8271. }
  8272. /* load curve info */
  8273. if (err == MP_OKAY)
  8274. err = wc_ecc_curve_load(&ecc_sets[curve_idx], &curve,
  8275. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
  8276. ECC_CURVE_FIELD_BF));
  8277. #if defined(WOLFSSL_CUSTOM_CURVES) && \
  8278. defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  8279. /* validate prime is prime for custom curves */
  8280. if (err == MP_OKAY && curve_idx == ECC_CUSTOM_IDX) {
  8281. int isPrime = MP_NO;
  8282. err = mp_prime_is_prime(curve->prime, 8, &isPrime);
  8283. if (err == MP_OKAY && isPrime == MP_NO)
  8284. err = MP_VAL;
  8285. }
  8286. #endif
  8287. /* compute x^3 */
  8288. if (err == MP_OKAY)
  8289. err = mp_sqr(point->x, t1);
  8290. if (err == MP_OKAY)
  8291. err = mp_mulmod(t1, point->x, curve->prime, t1);
  8292. /* compute x^3 + a*x */
  8293. if (err == MP_OKAY)
  8294. err = mp_mulmod(curve->Af, point->x, curve->prime, t2);
  8295. if (err == MP_OKAY)
  8296. err = mp_add(t1, t2, t1);
  8297. /* compute x^3 + a*x + b */
  8298. if (err == MP_OKAY)
  8299. err = mp_add(t1, curve->Bf, t1);
  8300. /* compute sqrt(x^3 + a*x + b) */
  8301. if (err == MP_OKAY)
  8302. err = mp_sqrtmod_prime(t1, curve->prime, t2);
  8303. /* adjust y */
  8304. if (err == MP_OKAY) {
  8305. if ((mp_isodd(t2) == MP_YES &&
  8306. pointType == ECC_POINT_COMP_ODD) ||
  8307. (mp_isodd(t2) == MP_NO &&
  8308. pointType == ECC_POINT_COMP_EVEN)) {
  8309. err = mp_mod(t2, curve->prime, point->y);
  8310. }
  8311. else {
  8312. err = mp_submod(curve->prime, t2, curve->prime, point->y);
  8313. }
  8314. }
  8315. if (did_init) {
  8316. mp_clear(t2);
  8317. mp_clear(t1);
  8318. }
  8319. #ifdef WOLFSSL_SMALL_STACK
  8320. if (t1 != NULL) {
  8321. XFREE(t1, NULL, DYNAMIC_TYPE_BIGINT);
  8322. }
  8323. if (t2 != NULL) {
  8324. XFREE(t2, NULL, DYNAMIC_TYPE_BIGINT);
  8325. }
  8326. #endif
  8327. wc_ecc_curve_free(curve);
  8328. FREE_CURVE_SPECS();
  8329. }
  8330. #else
  8331. {
  8332. err = WC_KEY_SIZE_E;
  8333. }
  8334. #endif
  8335. }
  8336. #endif
  8337. if (err == MP_OKAY) {
  8338. #ifdef HAVE_COMP_KEY
  8339. if (compressed == 0)
  8340. #endif
  8341. err = mp_read_unsigned_bin(point->y, in + keysize, (word32)keysize);
  8342. }
  8343. if (err == MP_OKAY)
  8344. err = mp_set(point->z, 1);
  8345. if (err != MP_OKAY) {
  8346. mp_clear(point->x);
  8347. mp_clear(point->y);
  8348. mp_clear(point->z);
  8349. }
  8350. RESTORE_VECTOR_REGISTERS();
  8351. return err;
  8352. }
  8353. /* function for backwards compatibility with previous implementations */
  8354. int wc_ecc_import_point_der(const byte* in, word32 inLen, const int curve_idx,
  8355. ecc_point* point)
  8356. {
  8357. return wc_ecc_import_point_der_ex(in, inLen, curve_idx, point, 1);
  8358. }
  8359. #endif /* HAVE_ECC_KEY_IMPORT */
  8360. #ifdef HAVE_ECC_KEY_EXPORT
  8361. /* export point to der */
  8362. int wc_ecc_export_point_der_ex(const int curve_idx, ecc_point* point, byte* out,
  8363. word32* outLen, int compressed)
  8364. {
  8365. if (compressed == 0)
  8366. return wc_ecc_export_point_der(curve_idx, point, out, outLen);
  8367. #ifdef HAVE_COMP_KEY
  8368. else
  8369. return wc_ecc_export_point_der_compressed(curve_idx, point, out, outLen);
  8370. #else
  8371. return NOT_COMPILED_IN;
  8372. #endif
  8373. }
  8374. int wc_ecc_export_point_der(const int curve_idx, ecc_point* point, byte* out,
  8375. word32* outLen)
  8376. {
  8377. int ret = MP_OKAY;
  8378. word32 numlen;
  8379. #ifdef WOLFSSL_SMALL_STACK
  8380. byte* buf;
  8381. #else
  8382. byte buf[ECC_BUFSIZE];
  8383. #endif
  8384. if ((curve_idx < 0) || (wc_ecc_is_valid_idx(curve_idx) == 0))
  8385. return ECC_BAD_ARG_E;
  8386. numlen = (word32)ecc_sets[curve_idx].size;
  8387. /* return length needed only */
  8388. if (point != NULL && out == NULL && outLen != NULL) {
  8389. *outLen = 1 + 2*numlen;
  8390. return LENGTH_ONLY_E;
  8391. }
  8392. if (point == NULL || out == NULL || outLen == NULL)
  8393. return ECC_BAD_ARG_E;
  8394. if (*outLen < (1 + 2*numlen)) {
  8395. *outLen = 1 + 2*numlen;
  8396. return BUFFER_E;
  8397. }
  8398. /* Sanity check the ordinates' sizes. */
  8399. if (((word32)mp_unsigned_bin_size(point->x) > numlen) ||
  8400. ((word32)mp_unsigned_bin_size(point->y) > numlen)) {
  8401. return ECC_BAD_ARG_E;
  8402. }
  8403. /* store byte point type */
  8404. out[0] = ECC_POINT_UNCOMP;
  8405. #ifdef WOLFSSL_SMALL_STACK
  8406. buf = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  8407. if (buf == NULL)
  8408. return MEMORY_E;
  8409. #endif
  8410. /* pad and store x */
  8411. XMEMSET(buf, 0, ECC_BUFSIZE);
  8412. ret = mp_to_unsigned_bin(point->x, buf +
  8413. (numlen - (word32)mp_unsigned_bin_size(point->x)));
  8414. if (ret != MP_OKAY)
  8415. goto done;
  8416. XMEMCPY(out+1, buf, numlen);
  8417. /* pad and store y */
  8418. XMEMSET(buf, 0, ECC_BUFSIZE);
  8419. ret = mp_to_unsigned_bin(point->y, buf +
  8420. (numlen - (word32)mp_unsigned_bin_size(point->y)));
  8421. if (ret != MP_OKAY)
  8422. goto done;
  8423. XMEMCPY(out+1+numlen, buf, numlen);
  8424. *outLen = 1 + 2*numlen;
  8425. done:
  8426. #ifdef WOLFSSL_SMALL_STACK
  8427. XFREE(buf, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  8428. #endif
  8429. return ret;
  8430. }
  8431. /* export point to der */
  8432. #ifdef HAVE_COMP_KEY
  8433. int wc_ecc_export_point_der_compressed(const int curve_idx, ecc_point* point,
  8434. byte* out, word32* outLen)
  8435. {
  8436. int ret = MP_OKAY;
  8437. word32 numlen;
  8438. word32 output_len;
  8439. #ifdef WOLFSSL_SMALL_STACK
  8440. byte* buf;
  8441. #else
  8442. byte buf[ECC_BUFSIZE];
  8443. #endif
  8444. if ((curve_idx < 0) || (wc_ecc_is_valid_idx(curve_idx) == 0))
  8445. return ECC_BAD_ARG_E;
  8446. numlen = (word32)ecc_sets[curve_idx].size;
  8447. output_len = 1 + numlen; /* y point type + x */
  8448. /* return length needed only */
  8449. if (point != NULL && out == NULL && outLen != NULL) {
  8450. *outLen = output_len;
  8451. return LENGTH_ONLY_E;
  8452. }
  8453. if (point == NULL || out == NULL || outLen == NULL)
  8454. return ECC_BAD_ARG_E;
  8455. if (*outLen < output_len) {
  8456. *outLen = output_len;
  8457. return BUFFER_E;
  8458. }
  8459. /* Sanity check the ordinate's size. */
  8460. if ((word32)mp_unsigned_bin_size(point->x) > numlen) {
  8461. return ECC_BAD_ARG_E;
  8462. }
  8463. /* store byte point type */
  8464. out[0] = mp_isodd(point->y) == MP_YES ? ECC_POINT_COMP_ODD :
  8465. ECC_POINT_COMP_EVEN;
  8466. #ifdef WOLFSSL_SMALL_STACK
  8467. buf = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  8468. if (buf == NULL)
  8469. return MEMORY_E;
  8470. #endif
  8471. /* pad and store x */
  8472. XMEMSET(buf, 0, ECC_BUFSIZE);
  8473. ret = mp_to_unsigned_bin(point->x, buf +
  8474. (numlen - (word32)mp_unsigned_bin_size(point->x)));
  8475. if (ret != MP_OKAY)
  8476. goto done;
  8477. XMEMCPY(out+1, buf, numlen);
  8478. *outLen = output_len;
  8479. done:
  8480. #ifdef WOLFSSL_SMALL_STACK
  8481. XFREE(buf, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  8482. #endif
  8483. return ret;
  8484. }
  8485. #endif /* HAVE_COMP_KEY */
  8486. /* export public ECC key in ANSI X9.63 format */
  8487. WOLFSSL_ABI
  8488. int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
  8489. {
  8490. int ret = MP_OKAY;
  8491. word32 numlen;
  8492. #ifdef WOLFSSL_SMALL_STACK
  8493. byte* buf;
  8494. #else
  8495. byte buf[ECC_BUFSIZE];
  8496. #endif
  8497. word32 pubxlen, pubylen;
  8498. /* return length needed only */
  8499. if (key != NULL && out == NULL && outLen != NULL) {
  8500. /* if key hasn't been setup assume max bytes for size estimation */
  8501. numlen = key->dp ? (word32)key->dp->size : MAX_ECC_BYTES;
  8502. *outLen = 1 + 2 * numlen;
  8503. return LENGTH_ONLY_E;
  8504. }
  8505. if (key == NULL || out == NULL || outLen == NULL)
  8506. return ECC_BAD_ARG_E;
  8507. if (key->type == ECC_PRIVATEKEY_ONLY)
  8508. return ECC_PRIVATEONLY_E;
  8509. #if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  8510. /* check if public key in secure memory */
  8511. if (key->securePubKey > 0) {
  8512. int keySz = wc_ecc_size(key);
  8513. /* store byte point type */
  8514. out[0] = ECC_POINT_UNCOMP;
  8515. if (caamReadPartition((CAAM_ADDRESS)key->securePubKey, out+1, keySz*2) != 0)
  8516. return WC_HW_E;
  8517. *outLen = 1 + 2*keySz;
  8518. return MP_OKAY;
  8519. }
  8520. #endif
  8521. if (key->type == 0 || wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL){
  8522. return ECC_BAD_ARG_E;
  8523. }
  8524. numlen = (word32)key->dp->size;
  8525. /* verify room in out buffer */
  8526. if (*outLen < (1 + 2*numlen)) {
  8527. *outLen = 1 + 2*numlen;
  8528. return BUFFER_E;
  8529. }
  8530. /* verify public key length is less than key size */
  8531. pubxlen = (word32)mp_unsigned_bin_size(key->pubkey.x);
  8532. pubylen = (word32)mp_unsigned_bin_size(key->pubkey.y);
  8533. if ((pubxlen > numlen) || (pubylen > numlen)) {
  8534. WOLFSSL_MSG("Public key x/y invalid!");
  8535. return BUFFER_E;
  8536. }
  8537. /* store byte point type */
  8538. out[0] = ECC_POINT_UNCOMP;
  8539. #ifdef WOLFSSL_SMALL_STACK
  8540. buf = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  8541. if (buf == NULL)
  8542. return MEMORY_E;
  8543. #endif
  8544. /* pad and store x */
  8545. XMEMSET(buf, 0, ECC_BUFSIZE);
  8546. ret = mp_to_unsigned_bin(key->pubkey.x, buf + (numlen - pubxlen));
  8547. if (ret != MP_OKAY)
  8548. goto done;
  8549. XMEMCPY(out+1, buf, numlen);
  8550. /* pad and store y */
  8551. XMEMSET(buf, 0, ECC_BUFSIZE);
  8552. ret = mp_to_unsigned_bin(key->pubkey.y, buf + (numlen - pubylen));
  8553. if (ret != MP_OKAY)
  8554. goto done;
  8555. XMEMCPY(out+1+numlen, buf, numlen);
  8556. *outLen = 1 + 2*numlen;
  8557. done:
  8558. #ifdef WOLFSSL_SMALL_STACK
  8559. XFREE(buf, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  8560. #endif
  8561. return ret;
  8562. }
  8563. /* export public ECC key in ANSI X9.63 format, extended with
  8564. * compression option */
  8565. WOLFSSL_ABI
  8566. int wc_ecc_export_x963_ex(ecc_key* key, byte* out, word32* outLen,
  8567. int compressed)
  8568. {
  8569. if (compressed == 0)
  8570. return wc_ecc_export_x963(key, out, outLen);
  8571. #ifdef HAVE_COMP_KEY
  8572. else
  8573. return wc_ecc_export_x963_compressed(key, out, outLen);
  8574. #else
  8575. return NOT_COMPILED_IN;
  8576. #endif
  8577. }
  8578. #endif /* HAVE_ECC_KEY_EXPORT */
  8579. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  8580. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SE050) && \
  8581. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(WOLFSSL_STM32_PKA)
  8582. /* is ecc point on curve described by dp ? */
  8583. static int _ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime)
  8584. {
  8585. #if !defined(WOLFSSL_SP_MATH)
  8586. int err;
  8587. #ifdef WOLFSSL_SMALL_STACK
  8588. mp_int* t1;
  8589. mp_int* t2;
  8590. #else
  8591. mp_int t1[1], t2[1];
  8592. #endif
  8593. #ifdef WOLFSSL_SMALL_STACK
  8594. t1 = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  8595. if (t1 == NULL)
  8596. return MEMORY_E;
  8597. t2 = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  8598. if (t2 == NULL) {
  8599. XFREE(t1, NULL, DYNAMIC_TYPE_ECC);
  8600. return MEMORY_E;
  8601. }
  8602. #endif
  8603. if ((err = mp_init_multi(t1, t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  8604. #ifdef WOLFSSL_SMALL_STACK
  8605. XFREE(t2, NULL, DYNAMIC_TYPE_ECC);
  8606. XFREE(t1, NULL, DYNAMIC_TYPE_ECC);
  8607. #endif
  8608. return err;
  8609. }
  8610. SAVE_VECTOR_REGISTERS(err = _svr_ret;);
  8611. /* compute y^2 */
  8612. if (err == MP_OKAY)
  8613. err = mp_sqr(ecp->y, t1);
  8614. /* compute x^3 */
  8615. if (err == MP_OKAY)
  8616. err = mp_sqr(ecp->x, t2);
  8617. if (err == MP_OKAY)
  8618. err = mp_mod(t2, prime, t2);
  8619. if (err == MP_OKAY)
  8620. err = mp_mul(ecp->x, t2, t2);
  8621. /* compute y^2 - x^3 */
  8622. if (err == MP_OKAY)
  8623. err = mp_submod(t1, t2, prime, t1);
  8624. /* Determine if curve "a" should be used in calc */
  8625. #ifdef WOLFSSL_CUSTOM_CURVES
  8626. if (err == MP_OKAY) {
  8627. /* Use a and prime to determine if a == 3 */
  8628. err = mp_set(t2, 0);
  8629. if (err == MP_OKAY)
  8630. err = mp_submod(prime, a, prime, t2);
  8631. }
  8632. if (err == MP_OKAY && mp_cmp_d(t2, 3) != MP_EQ) {
  8633. /* compute y^2 - x^3 + a*x */
  8634. if (err == MP_OKAY)
  8635. err = mp_mulmod(t2, ecp->x, prime, t2);
  8636. if (err == MP_OKAY)
  8637. err = mp_addmod(t1, t2, prime, t1);
  8638. }
  8639. else
  8640. #endif /* WOLFSSL_CUSTOM_CURVES */
  8641. {
  8642. /* assumes "a" == 3 */
  8643. (void)a;
  8644. /* compute y^2 - x^3 + 3x */
  8645. if (err == MP_OKAY)
  8646. err = mp_add(t1, ecp->x, t1);
  8647. if (err == MP_OKAY)
  8648. err = mp_add(t1, ecp->x, t1);
  8649. if (err == MP_OKAY)
  8650. err = mp_add(t1, ecp->x, t1);
  8651. if (err == MP_OKAY)
  8652. err = mp_mod(t1, prime, t1);
  8653. }
  8654. /* adjust range (0, prime) */
  8655. while (err == MP_OKAY && mp_isneg(t1)) {
  8656. err = mp_add(t1, prime, t1);
  8657. }
  8658. while (err == MP_OKAY && mp_cmp(t1, prime) != MP_LT) {
  8659. err = mp_sub(t1, prime, t1);
  8660. }
  8661. /* compare to b */
  8662. if (err == MP_OKAY) {
  8663. if (mp_cmp(t1, b) != MP_EQ) {
  8664. err = IS_POINT_E;
  8665. } else {
  8666. err = MP_OKAY;
  8667. }
  8668. }
  8669. mp_clear(t1);
  8670. mp_clear(t2);
  8671. RESTORE_VECTOR_REGISTERS();
  8672. #ifdef WOLFSSL_SMALL_STACK
  8673. XFREE(t2, NULL, DYNAMIC_TYPE_ECC);
  8674. XFREE(t1, NULL, DYNAMIC_TYPE_ECC);
  8675. #endif
  8676. return err;
  8677. #else
  8678. (void)a;
  8679. (void)b;
  8680. #ifdef WOLFSSL_HAVE_SP_ECC
  8681. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  8682. if ((mp_count_bits(prime) == 256) && (!mp_is_bit_set(prime, 224))) {
  8683. return sp_ecc_is_point_sm2_256(ecp->x, ecp->y);
  8684. }
  8685. #endif
  8686. #ifndef WOLFSSL_SP_NO_256
  8687. if (mp_count_bits(prime) == 256) {
  8688. return sp_ecc_is_point_256(ecp->x, ecp->y);
  8689. }
  8690. #endif
  8691. #ifdef WOLFSSL_SP_384
  8692. if (mp_count_bits(prime) == 384) {
  8693. return sp_ecc_is_point_384(ecp->x, ecp->y);
  8694. }
  8695. #endif
  8696. #ifdef WOLFSSL_SP_521
  8697. if (mp_count_bits(prime) == 521) {
  8698. return sp_ecc_is_point_521(ecp->x, ecp->y);
  8699. }
  8700. #endif
  8701. #else
  8702. (void)ecp;
  8703. (void)prime;
  8704. #endif
  8705. return WC_KEY_SIZE_E;
  8706. #endif
  8707. }
  8708. int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime)
  8709. {
  8710. int err = MP_OKAY;
  8711. /* Validate parameters. */
  8712. if ((ecp == NULL) || (a == NULL) || (b == NULL) || (prime == NULL)) {
  8713. err = BAD_FUNC_ARG;
  8714. }
  8715. if (err == MP_OKAY) {
  8716. /* x must be in the range [0, p-1] */
  8717. if ((mp_cmp(ecp->x, prime) != MP_LT) || mp_isneg(ecp->x)) {
  8718. err = ECC_OUT_OF_RANGE_E;
  8719. }
  8720. }
  8721. if (err == MP_OKAY) {
  8722. /* y must be in the range [0, p-1] */
  8723. if ((mp_cmp(ecp->y, prime) != MP_LT) || mp_isneg(ecp->y)) {
  8724. err = ECC_OUT_OF_RANGE_E;
  8725. }
  8726. }
  8727. if (err == MP_OKAY) {
  8728. /* z must be one, that is point must be in affine form. */
  8729. if (!mp_isone(ecp->z)) {
  8730. err = ECC_BAD_ARG_E;
  8731. }
  8732. }
  8733. if (err == MP_OKAY) {
  8734. /* Check x and y are valid for curve equation. */
  8735. err = _ecc_is_point(ecp, a, b, prime);
  8736. }
  8737. return err;
  8738. }
  8739. #if (FIPS_VERSION_GE(5,0) || defined(WOLFSSL_VALIDATE_ECC_KEYGEN) || \
  8740. (defined(WOLFSSL_VALIDATE_ECC_IMPORT) && !defined(WOLFSSL_SP_MATH))) && \
  8741. !defined(WOLFSSL_KCAPI_ECC) || defined(WOLFSSL_CAAM)
  8742. /* validate privkey * generator == pubkey, 0 on success */
  8743. static int ecc_check_privkey_gen(ecc_key* key, mp_int* a, mp_int* prime)
  8744. {
  8745. int err;
  8746. ecc_point* base = NULL;
  8747. ecc_point* res = NULL;
  8748. #ifdef WOLFSSL_NO_MALLOC
  8749. ecc_point lcl_base;
  8750. ecc_point lcl_res;
  8751. #endif
  8752. DECLARE_CURVE_SPECS(3);
  8753. if (key == NULL)
  8754. return BAD_FUNC_ARG;
  8755. ALLOC_CURVE_SPECS(3, err);
  8756. #ifdef WOLFSSL_NO_MALLOC
  8757. res = &lcl_res;
  8758. #endif
  8759. err = wc_ecc_new_point_ex(&res, key->heap);
  8760. #ifdef WOLFSSL_HAVE_SP_ECC
  8761. #ifndef WOLFSSL_SP_NO_256
  8762. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1) {
  8763. if (err == MP_OKAY) {
  8764. err = sp_ecc_mulmod_base_256(key->k, res, 1, key->heap);
  8765. }
  8766. }
  8767. else
  8768. #endif
  8769. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  8770. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SM2P256V1) {
  8771. if (err == MP_OKAY) {
  8772. err = sp_ecc_mulmod_base_sm2_256(key->k, res, 1, key->heap);
  8773. }
  8774. }
  8775. else
  8776. #endif
  8777. #ifdef WOLFSSL_SP_384
  8778. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP384R1) {
  8779. if (err == MP_OKAY) {
  8780. err = sp_ecc_mulmod_base_384(key->k, res, 1, key->heap);
  8781. }
  8782. }
  8783. else
  8784. #endif
  8785. #ifdef WOLFSSL_SP_521
  8786. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP521R1) {
  8787. if (err == MP_OKAY) {
  8788. err = sp_ecc_mulmod_base_521(key->k, res, 1, key->heap);
  8789. }
  8790. }
  8791. else
  8792. #endif
  8793. #endif
  8794. {
  8795. if (err == MP_OKAY) {
  8796. #ifdef WOLFSSL_NO_MALLOC
  8797. base = &lcl_base;
  8798. #endif
  8799. err = wc_ecc_new_point_ex(&base, key->heap);
  8800. }
  8801. if (err == MP_OKAY) {
  8802. /* load curve info */
  8803. err = wc_ecc_curve_load(key->dp, &curve, (ECC_CURVE_FIELD_GX |
  8804. ECC_CURVE_FIELD_GY | ECC_CURVE_FIELD_ORDER));
  8805. }
  8806. /* set up base generator */
  8807. if (err == MP_OKAY)
  8808. err = mp_copy(curve->Gx, base->x);
  8809. if (err == MP_OKAY)
  8810. err = mp_copy(curve->Gy, base->y);
  8811. if (err == MP_OKAY)
  8812. err = mp_set(base->z, 1);
  8813. #ifdef WOLFSSL_KCAPI_ECC
  8814. if (err == MP_OKAY) {
  8815. word32 pubkey_sz = (word32)key->dp->size*2;
  8816. if (key->handle == NULL) {
  8817. /* if handle loaded, then pubkey_raw already populated */
  8818. err = KcapiEcc_LoadKey(key, key->pubkey_raw, &pubkey_sz, 1);
  8819. }
  8820. if (err == 0) {
  8821. err = mp_read_unsigned_bin(res->x, key->pubkey_raw,
  8822. pubkey_sz/2);
  8823. }
  8824. if (err == MP_OKAY) {
  8825. err = mp_read_unsigned_bin(res->y,
  8826. key->pubkey_raw + pubkey_sz/2,
  8827. pubkey_sz/2);
  8828. }
  8829. if (err == MP_OKAY) {
  8830. err = mp_set(res->z, 1);
  8831. }
  8832. }
  8833. (void)a;
  8834. (void)prime;
  8835. #else
  8836. #ifdef ECC_TIMING_RESISTANT
  8837. if (err == MP_OKAY)
  8838. err = wc_ecc_mulmod_ex2(key->k, base, res, a, prime, curve->order,
  8839. key->rng, 1, key->heap);
  8840. #else
  8841. if (err == MP_OKAY)
  8842. err = wc_ecc_mulmod_ex2(key->k, base, res, a, prime, curve->order,
  8843. NULL, 1, key->heap);
  8844. #endif
  8845. #endif /* WOLFSSL_KCAPI_ECC */
  8846. }
  8847. if (err == MP_OKAY) {
  8848. /* compare result to public key */
  8849. if (mp_cmp(res->x, key->pubkey.x) != MP_EQ ||
  8850. mp_cmp(res->y, key->pubkey.y) != MP_EQ ||
  8851. mp_cmp(res->z, key->pubkey.z) != MP_EQ) {
  8852. /* didn't match */
  8853. err = ECC_PRIV_KEY_E;
  8854. }
  8855. }
  8856. wc_ecc_curve_free(curve);
  8857. wc_ecc_del_point_ex(res, key->heap);
  8858. wc_ecc_del_point_ex(base, key->heap);
  8859. FREE_CURVE_SPECS();
  8860. return err;
  8861. }
  8862. #endif /* FIPS_VERSION_GE(5,0) || WOLFSSL_VALIDATE_ECC_KEYGEN ||
  8863. * (!WOLFSSL_SP_MATH && WOLFSSL_VALIDATE_ECC_IMPORT) */
  8864. #if (FIPS_VERSION_GE(5,0) || defined(WOLFSSL_VALIDATE_ECC_KEYGEN)) && \
  8865. !defined(WOLFSSL_KCAPI_ECC)
  8866. /* check privkey generator helper, creates prime needed */
  8867. static int ecc_check_privkey_gen_helper(ecc_key* key)
  8868. {
  8869. int err;
  8870. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  8871. DECLARE_CURVE_SPECS(2);
  8872. #endif
  8873. if (key == NULL)
  8874. return BAD_FUNC_ARG;
  8875. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  8876. /* Hardware based private key, so this operation is not supported */
  8877. err = MP_OKAY; /* just report success */
  8878. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  8879. /* Hardware based private key, so this operation is not supported */
  8880. err = MP_OKAY; /* just report success */
  8881. #elif defined(WOLFSSL_KCAPI_ECC)
  8882. /* Hardware based private key, so this operation is not supported */
  8883. err = MP_OKAY; /* just report success */
  8884. #else
  8885. err = MP_OKAY;
  8886. ALLOC_CURVE_SPECS(2, err);
  8887. /* load curve info */
  8888. if (err == MP_OKAY)
  8889. err = wc_ecc_curve_load(key->dp, &curve,
  8890. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF));
  8891. if (err == MP_OKAY)
  8892. err = ecc_check_privkey_gen(key, curve->Af, curve->prime);
  8893. wc_ecc_curve_free(curve);
  8894. FREE_CURVE_SPECS();
  8895. #endif /* WOLFSSL_ATECC508A */
  8896. return err;
  8897. }
  8898. /* Performs a Pairwise Consistency Test on an ECC key pair. */
  8899. static int _ecc_pairwise_consistency_test(ecc_key* key, WC_RNG* rng)
  8900. {
  8901. int err = 0;
  8902. word32 flags = key->flags;
  8903. /* If flags not set default to cofactor and dec/sign */
  8904. if ((flags & (WC_ECC_FLAG_COFACTOR | WC_ECC_FLAG_DEC_SIGN)) == 0) {
  8905. flags = (WC_ECC_FLAG_COFACTOR | WC_ECC_FLAG_DEC_SIGN);
  8906. }
  8907. if (flags & WC_ECC_FLAG_COFACTOR) {
  8908. err = ecc_check_privkey_gen_helper(key);
  8909. }
  8910. if (!err && (flags & WC_ECC_FLAG_DEC_SIGN)) {
  8911. byte* sig;
  8912. byte* digest;
  8913. word32 sigLen, digestLen;
  8914. int dynRng = 0, res = 0;
  8915. sigLen = (word32)wc_ecc_sig_size(key);
  8916. digestLen = WC_SHA256_DIGEST_SIZE;
  8917. sig = (byte*)XMALLOC(sigLen + digestLen, NULL, DYNAMIC_TYPE_ECC);
  8918. if (sig == NULL)
  8919. return MEMORY_E;
  8920. digest = sig + sigLen;
  8921. if (rng == NULL) {
  8922. dynRng = 1;
  8923. rng = wc_rng_new(NULL, 0, NULL);
  8924. if (rng == NULL) {
  8925. XFREE(sig, NULL, DYNAMIC_TYPE_ECC);
  8926. return MEMORY_E;
  8927. }
  8928. }
  8929. err = wc_RNG_GenerateBlock(rng, digest, digestLen);
  8930. if (!err)
  8931. err = wc_ecc_sign_hash(digest, WC_SHA256_DIGEST_SIZE, sig, &sigLen,
  8932. rng, key);
  8933. if (!err)
  8934. err = wc_ecc_verify_hash(sig, sigLen,
  8935. digest, WC_SHA256_DIGEST_SIZE, &res, key);
  8936. if (res == 0)
  8937. err = ECC_PCT_E;
  8938. if (dynRng) {
  8939. wc_rng_free(rng);
  8940. }
  8941. ForceZero(sig, sigLen + digestLen);
  8942. XFREE(sig, NULL, DYNAMIC_TYPE_ECC);
  8943. }
  8944. (void)rng;
  8945. if (err != 0)
  8946. err = ECC_PCT_E;
  8947. return err;
  8948. }
  8949. #endif /* (FIPS v5 or later || WOLFSSL_VALIDATE_ECC_KEYGEN) &&!WOLFSSL_KCAPI_ECC */
  8950. #ifdef HAVE_ECC_CHECK_PUBKEY_ORDER
  8951. /* validate order * pubkey = point at infinity, 0 on success */
  8952. static int ecc_check_pubkey_order(ecc_key* key, ecc_point* pubkey, mp_int* a,
  8953. mp_int* prime, mp_int* order)
  8954. {
  8955. ecc_point* inf = NULL;
  8956. #ifdef WOLFSSL_NO_MALLOC
  8957. ecc_point lcl_inf;
  8958. #endif
  8959. int err;
  8960. if (key == NULL)
  8961. return BAD_FUNC_ARG;
  8962. if (mp_count_bits(pubkey->x) > mp_count_bits(prime) ||
  8963. mp_count_bits(pubkey->y) > mp_count_bits(prime) ||
  8964. mp_count_bits(pubkey->z) > mp_count_bits(prime)) {
  8965. return IS_POINT_E;
  8966. }
  8967. #ifdef WOLFSSL_NO_MALLOC
  8968. inf = &lcl_inf;
  8969. #endif
  8970. err = wc_ecc_new_point_ex(&inf, key->heap);
  8971. if (err == MP_OKAY) {
  8972. #ifdef WOLFSSL_HAVE_SP_ECC
  8973. #ifndef WOLFSSL_SP_NO_256
  8974. if (key->idx != ECC_CUSTOM_IDX &&
  8975. ecc_sets[key->idx].id == ECC_SECP256R1) {
  8976. err = sp_ecc_mulmod_256(order, pubkey, inf, 1, key->heap);
  8977. }
  8978. else
  8979. #endif
  8980. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  8981. if (key->idx != ECC_CUSTOM_IDX &&
  8982. ecc_sets[key->idx].id == ECC_SM2P256V1) {
  8983. err = sp_ecc_mulmod_sm2_256(order, pubkey, inf, 1, key->heap);
  8984. }
  8985. else
  8986. #endif
  8987. #ifdef WOLFSSL_SP_384
  8988. if (key->idx != ECC_CUSTOM_IDX &&
  8989. ecc_sets[key->idx].id == ECC_SECP384R1) {
  8990. err = sp_ecc_mulmod_384(order, pubkey, inf, 1, key->heap);
  8991. }
  8992. else
  8993. #endif
  8994. #ifdef WOLFSSL_SP_521
  8995. if (key->idx != ECC_CUSTOM_IDX &&
  8996. ecc_sets[key->idx].id == ECC_SECP521R1) {
  8997. err = sp_ecc_mulmod_521(order, pubkey, inf, 1, key->heap);
  8998. }
  8999. else
  9000. #endif
  9001. #endif
  9002. #if !defined(WOLFSSL_SP_MATH)
  9003. err = wc_ecc_mulmod_ex(order, pubkey, inf, a, prime, 1, key->heap);
  9004. if (err == MP_OKAY && !wc_ecc_point_is_at_infinity(inf))
  9005. err = ECC_INF_E;
  9006. #else
  9007. {
  9008. (void)a;
  9009. (void)prime;
  9010. err = WC_KEY_SIZE_E;
  9011. }
  9012. #endif
  9013. }
  9014. wc_ecc_del_point_ex(inf, key->heap);
  9015. return err;
  9016. }
  9017. #endif /* !WOLFSSL_SP_MATH */
  9018. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_CRYPTOCELL*/
  9019. #ifdef OPENSSL_EXTRA
  9020. int wc_ecc_get_generator(ecc_point* ecp, int curve_idx)
  9021. {
  9022. int err = MP_OKAY;
  9023. DECLARE_CURVE_SPECS(2);
  9024. if (!ecp || curve_idx < 0 || curve_idx > (int)(ECC_SET_COUNT-1))
  9025. return BAD_FUNC_ARG;
  9026. ALLOC_CURVE_SPECS(2, err);
  9027. if (err == MP_OKAY)
  9028. err = wc_ecc_curve_load(&ecc_sets[curve_idx], &curve,
  9029. (ECC_CURVE_FIELD_GX | ECC_CURVE_FIELD_GY));
  9030. if (err == MP_OKAY)
  9031. err = mp_copy(curve->Gx, ecp->x);
  9032. if (err == MP_OKAY)
  9033. err = mp_copy(curve->Gy, ecp->y);
  9034. if (err == MP_OKAY)
  9035. err = mp_set(ecp->z, 1);
  9036. wc_ecc_curve_free(curve);
  9037. FREE_CURVE_SPECS();
  9038. return err;
  9039. }
  9040. #endif /* OPENSSLALL */
  9041. /* Validate the public key per SP 800-56Ar3 section 5.6.2.3.3,
  9042. * ECC Full Public Key Validation Routine. If the parameter
  9043. * partial is set, then it follows section 5.6.2.3.4, the ECC
  9044. * Partial Public Key Validation Routine.
  9045. * If the parameter priv is set, add in a few extra
  9046. * checks on the bounds of the private key. */
  9047. static int _ecc_validate_public_key(ecc_key* key, int partial, int priv)
  9048. {
  9049. int err = MP_OKAY;
  9050. #ifndef WOLFSSL_SP_MATH
  9051. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  9052. !defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_SILABS_SE_ACCEL) && \
  9053. !defined(WOLFSSL_SE050) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) && \
  9054. !defined(WOLF_CRYPTO_CB_ONLY_ECC) && !defined(WOLFSSL_STM32_PKA)
  9055. mp_int* b = NULL;
  9056. #ifdef USE_ECC_B_PARAM
  9057. DECLARE_CURVE_SPECS(4);
  9058. #else
  9059. #ifndef WOLFSSL_SMALL_STACK
  9060. mp_int b_lcl;
  9061. #endif
  9062. DECLARE_CURVE_SPECS(3);
  9063. #endif /* USE_ECC_B_PARAM */
  9064. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A &&
  9065. !WOLFSSL_CRYPTOCELL && !WOLFSSL_SILABS_SE_ACCEL && !WOLFSSL_SE050 */
  9066. #endif /* !WOLFSSL_SP_MATH */
  9067. ASSERT_SAVED_VECTOR_REGISTERS();
  9068. if (key == NULL)
  9069. return BAD_FUNC_ARG;
  9070. #ifdef WOLFSSL_HAVE_SP_ECC
  9071. #ifndef WOLFSSL_SP_NO_256
  9072. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1) {
  9073. return sp_ecc_check_key_256(key->pubkey.x, key->pubkey.y,
  9074. key->type == ECC_PRIVATEKEY ? key->k : NULL, key->heap);
  9075. }
  9076. #endif
  9077. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  9078. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SM2P256V1) {
  9079. return sp_ecc_check_key_sm2_256(key->pubkey.x, key->pubkey.y,
  9080. key->type == ECC_PRIVATEKEY ? key->k : NULL, key->heap);
  9081. }
  9082. #endif
  9083. #ifdef WOLFSSL_SP_384
  9084. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP384R1) {
  9085. return sp_ecc_check_key_384(key->pubkey.x, key->pubkey.y,
  9086. key->type == ECC_PRIVATEKEY ? key->k : NULL, key->heap);
  9087. }
  9088. #endif
  9089. #ifdef WOLFSSL_SP_521
  9090. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP521R1) {
  9091. return sp_ecc_check_key_521(key->pubkey.x, key->pubkey.y,
  9092. key->type == ECC_PRIVATEKEY ? key->k : NULL, key->heap);
  9093. }
  9094. #endif
  9095. #if defined(WOLFSSL_SP_1024) && defined(WOLFCRYPT_HAVE_SAKKE)
  9096. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SAKKE_1) {
  9097. return sp_ecc_check_key_1024(key->pubkey.x, key->pubkey.y,
  9098. key->type == ECC_PRIVATEKEY ? key->k : NULL, key->heap);
  9099. }
  9100. #endif
  9101. #endif
  9102. #ifndef WOLFSSL_SP_MATH
  9103. #ifndef HAVE_ECC_CHECK_PUBKEY_ORDER
  9104. /* consider key check success on HW crypto
  9105. * ex: ATECC508/608A, CryptoCell and Silabs
  9106. *
  9107. * consider key check success on Crypt Cb
  9108. */
  9109. err = MP_OKAY;
  9110. #else
  9111. #ifdef USE_ECC_B_PARAM
  9112. ALLOC_CURVE_SPECS(4, err);
  9113. #else
  9114. ALLOC_CURVE_SPECS(3, err);
  9115. #ifndef WOLFSSL_SMALL_STACK
  9116. b = &b_lcl;
  9117. #else
  9118. b = (mp_int*)XMALLOC(sizeof(mp_int), key->heap, DYNAMIC_TYPE_ECC);
  9119. if (b == NULL) {
  9120. FREE_CURVE_SPECS();
  9121. return MEMORY_E;
  9122. }
  9123. #endif
  9124. XMEMSET(b, 0, sizeof(mp_int));
  9125. #endif
  9126. #ifdef WOLFSSL_CAAM
  9127. /* keys can be black encrypted ones which can not be checked like plain text
  9128. * keys */
  9129. if (key->blackKey > 0) {
  9130. /* encrypted key was used */
  9131. #ifdef WOLFSSL_SMALL_STACK
  9132. XFREE(b, key->heap, DYNAMIC_TYPE_ECC);
  9133. #endif
  9134. FREE_CURVE_SPECS();
  9135. return 0;
  9136. }
  9137. #endif
  9138. /* SP 800-56Ar3, section 5.6.2.3.3, process step 1 */
  9139. /* SP 800-56Ar3, section 5.6.2.3.4, process step 1 */
  9140. /* pubkey point cannot be at infinity */
  9141. if (wc_ecc_point_is_at_infinity(&key->pubkey)) {
  9142. #ifdef WOLFSSL_SMALL_STACK
  9143. XFREE(b, key->heap, DYNAMIC_TYPE_ECC);
  9144. #endif
  9145. FREE_CURVE_SPECS();
  9146. return ECC_INF_E;
  9147. }
  9148. /* load curve info */
  9149. if (err == MP_OKAY)
  9150. err = wc_ecc_curve_load(key->dp, &curve, (ECC_CURVE_FIELD_PRIME |
  9151. ECC_CURVE_FIELD_AF | ECC_CURVE_FIELD_ORDER
  9152. #ifdef USE_ECC_B_PARAM
  9153. | ECC_CURVE_FIELD_BF
  9154. #endif
  9155. ));
  9156. #ifndef USE_ECC_B_PARAM
  9157. /* load curve b parameter */
  9158. if (err == MP_OKAY)
  9159. err = mp_init(b);
  9160. if (err == MP_OKAY)
  9161. err = mp_read_radix(b, key->dp->Bf, MP_RADIX_HEX);
  9162. #else
  9163. if (err == MP_OKAY)
  9164. b = curve->Bf;
  9165. #endif
  9166. /* SP 800-56Ar3, section 5.6.2.3.3, process step 2 */
  9167. /* SP 800-56Ar3, section 5.6.2.3.4, process step 2 */
  9168. /* Qx must be in the range [0, p-1] */
  9169. if (err == MP_OKAY) {
  9170. if ((mp_cmp(key->pubkey.x, curve->prime) != MP_LT) ||
  9171. mp_isneg(key->pubkey.x)) {
  9172. err = ECC_OUT_OF_RANGE_E;
  9173. }
  9174. }
  9175. /* Qy must be in the range [0, p-1] */
  9176. if (err == MP_OKAY) {
  9177. if ((mp_cmp(key->pubkey.y, curve->prime) != MP_LT) ||
  9178. mp_isneg(key->pubkey.y)) {
  9179. err = ECC_OUT_OF_RANGE_E;
  9180. }
  9181. }
  9182. /* SP 800-56Ar3, section 5.6.2.3.3, process step 3 */
  9183. /* SP 800-56Ar3, section 5.6.2.3.4, process step 3 */
  9184. /* make sure point is actually on curve */
  9185. if (err == MP_OKAY)
  9186. err = _ecc_is_point(&key->pubkey, curve->Af, b, curve->prime);
  9187. if (!partial) {
  9188. /* SP 800-56Ar3, section 5.6.2.3.3, process step 4 */
  9189. /* pubkey * order must be at infinity */
  9190. if (err == MP_OKAY)
  9191. err = ecc_check_pubkey_order(key, &key->pubkey, curve->Af,
  9192. curve->prime, curve->order);
  9193. }
  9194. if (priv) {
  9195. /* SP 800-56Ar3, section 5.6.2.1.2 */
  9196. /* private keys must be in the range [1, n-1] */
  9197. if ((err == MP_OKAY) && (key->type == ECC_PRIVATEKEY) &&
  9198. (mp_iszero(key->k) || mp_isneg(key->k) ||
  9199. (mp_cmp(key->k, curve->order) != MP_LT))
  9200. #ifdef WOLFSSL_KCAPI_ECC
  9201. && key->handle == NULL
  9202. #endif
  9203. ) {
  9204. err = ECC_PRIV_KEY_E;
  9205. }
  9206. #if defined(WOLFSSL_VALIDATE_ECC_IMPORT) || defined(WOLFSSL_CAAM)
  9207. /* SP 800-56Ar3, section 5.6.2.1.4, method (b) for ECC */
  9208. /* private * base generator must equal pubkey */
  9209. if (err == MP_OKAY && key->type == ECC_PRIVATEKEY)
  9210. err = ecc_check_privkey_gen(key, curve->Af, curve->prime);
  9211. #endif
  9212. }
  9213. wc_ecc_curve_free(curve);
  9214. #ifndef USE_ECC_B_PARAM
  9215. mp_clear(b);
  9216. #ifdef WOLFSSL_SMALL_STACK
  9217. XFREE(b, key->heap, DYNAMIC_TYPE_ECC);
  9218. #endif
  9219. #endif
  9220. FREE_CURVE_SPECS();
  9221. #endif /* HAVE_ECC_CHECK_PUBKEY_ORDER */
  9222. #else
  9223. err = WC_KEY_SIZE_E;
  9224. #endif /* !WOLFSSL_SP_MATH */
  9225. (void)partial;
  9226. (void)priv;
  9227. return err;
  9228. }
  9229. /* perform sanity checks on ecc key validity, 0 on success */
  9230. WOLFSSL_ABI
  9231. int wc_ecc_check_key(ecc_key* key)
  9232. {
  9233. int ret;
  9234. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  9235. ret = _ecc_validate_public_key(key, 0, 1);
  9236. RESTORE_VECTOR_REGISTERS();
  9237. return ret;
  9238. }
  9239. #ifdef HAVE_ECC_KEY_IMPORT
  9240. /* import public ECC key in ANSI X9.63 format */
  9241. int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
  9242. int curve_id)
  9243. {
  9244. int err = MP_OKAY;
  9245. #ifdef HAVE_COMP_KEY
  9246. int compressed = 0;
  9247. #endif
  9248. int keysize = 0;
  9249. byte pointType;
  9250. #ifdef WOLFSSL_CRYPTOCELL
  9251. const CRYS_ECPKI_Domain_t* pDomain;
  9252. CRYS_ECPKI_BUILD_TempData_t tempBuff;
  9253. #endif
  9254. if (in == NULL || key == NULL)
  9255. return BAD_FUNC_ARG;
  9256. /* must be odd */
  9257. if ((inLen & 1) == 0) {
  9258. return ECC_BAD_ARG_E;
  9259. }
  9260. /* make sure required variables are reset */
  9261. wc_ecc_reset(key);
  9262. /* init key */
  9263. #ifdef ALT_ECC_SIZE
  9264. key->pubkey.x = (mp_int*)&key->pubkey.xyz[0];
  9265. key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
  9266. key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
  9267. alt_fp_init(key->pubkey.x);
  9268. alt_fp_init(key->pubkey.y);
  9269. alt_fp_init(key->pubkey.z);
  9270. key->k = (mp_int*)key->ka;
  9271. alt_fp_init(key->k);
  9272. #else
  9273. err = mp_init_multi(key->k,
  9274. key->pubkey.x, key->pubkey.y, key->pubkey.z, NULL, NULL);
  9275. #endif
  9276. if (err != MP_OKAY)
  9277. return MEMORY_E;
  9278. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  9279. /* check for point type (4, 2, or 3) */
  9280. pointType = in[0];
  9281. if (pointType != ECC_POINT_UNCOMP && pointType != ECC_POINT_COMP_EVEN &&
  9282. pointType != ECC_POINT_COMP_ODD) {
  9283. err = ASN_PARSE_E;
  9284. }
  9285. if (pointType == ECC_POINT_COMP_EVEN || pointType == ECC_POINT_COMP_ODD) {
  9286. #ifdef HAVE_COMP_KEY
  9287. compressed = 1;
  9288. #else
  9289. err = NOT_COMPILED_IN;
  9290. #endif
  9291. }
  9292. /* adjust to skip first byte */
  9293. inLen -= 1;
  9294. in += 1;
  9295. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  9296. /* For SECP256R1 only save raw public key for hardware */
  9297. if (curve_id == ECC_SECP256R1 && inLen <= (word32)sizeof(key->pubkey_raw)) {
  9298. #ifdef HAVE_COMP_KEY
  9299. if (!compressed)
  9300. #endif
  9301. XMEMCPY(key->pubkey_raw, (byte*)in, inLen);
  9302. }
  9303. #elif defined(WOLFSSL_KCAPI_ECC)
  9304. XMEMCPY(key->pubkey_raw, (byte*)in, inLen);
  9305. #endif
  9306. if (err == MP_OKAY) {
  9307. #ifdef HAVE_COMP_KEY
  9308. /* adjust inLen if compressed */
  9309. if (compressed)
  9310. inLen = inLen*2 + 1; /* used uncompressed len */
  9311. #endif
  9312. /* determine key size */
  9313. keysize = (int)(inLen>>1);
  9314. err = wc_ecc_set_curve(key, keysize, curve_id);
  9315. key->type = ECC_PUBLICKEY;
  9316. }
  9317. /* read data */
  9318. if (err == MP_OKAY)
  9319. err = mp_read_unsigned_bin(key->pubkey.x, in, (word32)keysize);
  9320. #ifdef HAVE_COMP_KEY
  9321. if (err == MP_OKAY && compressed == 1) { /* build y */
  9322. #if !defined(WOLFSSL_SP_MATH)
  9323. #ifdef WOLFSSL_SMALL_STACK
  9324. mp_int* t1 = NULL;
  9325. mp_int* t2 = NULL;
  9326. #else
  9327. mp_int t1[1], t2[1];
  9328. #endif
  9329. int did_init = 0;
  9330. DECLARE_CURVE_SPECS(3);
  9331. ALLOC_CURVE_SPECS(3, err);
  9332. #ifdef WOLFSSL_SMALL_STACK
  9333. if (err == MP_OKAY) {
  9334. t1 = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9335. if (t1 == NULL) {
  9336. err = MEMORY_E;
  9337. }
  9338. }
  9339. if (err == MP_OKAY) {
  9340. t2 = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9341. if (t2 == NULL) {
  9342. err = MEMORY_E;
  9343. }
  9344. }
  9345. #endif
  9346. if (err == MP_OKAY) {
  9347. if (mp_init_multi(t1, t2, NULL, NULL, NULL, NULL) != MP_OKAY)
  9348. err = MEMORY_E;
  9349. else
  9350. did_init = 1;
  9351. }
  9352. /* load curve info */
  9353. if (err == MP_OKAY)
  9354. err = wc_ecc_curve_load(key->dp, &curve,
  9355. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
  9356. ECC_CURVE_FIELD_BF));
  9357. #if defined(WOLFSSL_CUSTOM_CURVES) && \
  9358. defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  9359. /* validate prime is prime for custom curves */
  9360. if (err == MP_OKAY && key->idx == ECC_CUSTOM_IDX) {
  9361. int isPrime = MP_NO;
  9362. err = mp_prime_is_prime(curve->prime, 8, &isPrime);
  9363. if (err == MP_OKAY && isPrime == MP_NO)
  9364. err = MP_VAL;
  9365. }
  9366. #endif
  9367. /* compute x^3 */
  9368. if (err == MP_OKAY)
  9369. err = mp_sqrmod(key->pubkey.x, curve->prime, t1);
  9370. if (err == MP_OKAY)
  9371. err = mp_mulmod(t1, key->pubkey.x, curve->prime, t1);
  9372. /* compute x^3 + a*x */
  9373. if (err == MP_OKAY)
  9374. err = mp_mulmod(curve->Af, key->pubkey.x, curve->prime, t2);
  9375. if (err == MP_OKAY)
  9376. err = mp_add(t1, t2, t1);
  9377. /* compute x^3 + a*x + b */
  9378. if (err == MP_OKAY)
  9379. err = mp_add(t1, curve->Bf, t1);
  9380. /* compute sqrt(x^3 + a*x + b) */
  9381. if (err == MP_OKAY)
  9382. err = mp_sqrtmod_prime(t1, curve->prime, t2);
  9383. /* adjust y */
  9384. if (err == MP_OKAY) {
  9385. if ((mp_isodd(t2) == MP_YES && pointType == ECC_POINT_COMP_ODD) ||
  9386. (mp_isodd(t2) == MP_NO && pointType == ECC_POINT_COMP_EVEN)) {
  9387. err = mp_mod(t2, curve->prime, t2);
  9388. }
  9389. else {
  9390. err = mp_submod(curve->prime, t2, curve->prime, t2);
  9391. }
  9392. if (err == MP_OKAY)
  9393. err = mp_copy(t2, key->pubkey.y);
  9394. }
  9395. if (did_init) {
  9396. mp_clear(t2);
  9397. mp_clear(t1);
  9398. }
  9399. #ifdef WOLFSSL_SMALL_STACK
  9400. if (t1 != NULL) {
  9401. XFREE(t1, NULL, DYNAMIC_TYPE_BIGINT);
  9402. }
  9403. if (t2 != NULL) {
  9404. XFREE(t2, NULL, DYNAMIC_TYPE_BIGINT);
  9405. }
  9406. #endif
  9407. wc_ecc_curve_free(curve);
  9408. FREE_CURVE_SPECS();
  9409. #else
  9410. #ifndef WOLFSSL_SP_NO_256
  9411. if (key->dp->id == ECC_SECP256R1) {
  9412. err = sp_ecc_uncompress_256(key->pubkey.x, pointType,
  9413. key->pubkey.y);
  9414. }
  9415. else
  9416. #endif
  9417. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  9418. if (key->dp->id == ECC_SM2P256V1) {
  9419. sp_ecc_uncompress_sm2_256(key->pubkey.x, pointType, key->pubkey.y);
  9420. }
  9421. else
  9422. #endif
  9423. #ifdef WOLFSSL_SP_384
  9424. if (key->dp->id == ECC_SECP384R1) {
  9425. err = sp_ecc_uncompress_384(key->pubkey.x, pointType,
  9426. key->pubkey.y);
  9427. }
  9428. else
  9429. #endif
  9430. #ifdef WOLFSSL_SP_521
  9431. if (key->dp->id == ECC_SECP521R1) {
  9432. err = sp_ecc_uncompress_521(key->pubkey.x, pointType,
  9433. key->pubkey.y);
  9434. }
  9435. else
  9436. #endif
  9437. {
  9438. err = WC_KEY_SIZE_E;
  9439. }
  9440. #endif
  9441. }
  9442. #endif /* HAVE_COMP_KEY */
  9443. if (err == MP_OKAY) {
  9444. #ifdef HAVE_COMP_KEY
  9445. if (compressed == 0)
  9446. #endif
  9447. {
  9448. err = mp_read_unsigned_bin(key->pubkey.y, in + keysize,
  9449. (word32)keysize);
  9450. }
  9451. }
  9452. if (err == MP_OKAY)
  9453. err = mp_set(key->pubkey.z, 1);
  9454. #ifdef WOLFSSL_CRYPTOCELL
  9455. if (err == MP_OKAY) {
  9456. pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id));
  9457. /* create public key from external key buffer */
  9458. err = CRYS_ECPKI_BuildPublKeyFullCheck(pDomain,
  9459. (byte*)in-1, /* re-adjust */
  9460. inLen+1, /* original input */
  9461. &key->ctx.pubKey,
  9462. &tempBuff);
  9463. if (err != SA_SILIB_RET_OK){
  9464. WOLFSSL_MSG("CRYS_ECPKI_BuildPublKeyFullCheck failed");
  9465. }
  9466. }
  9467. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  9468. if (err == MP_OKAY)
  9469. err = silabs_ecc_import(key, keysize, 1, 0);
  9470. #elif defined(WOLFSSL_SE050)
  9471. if (err == MP_OKAY) {
  9472. /* reset key ID, in case used before */
  9473. key->keyId = 0;
  9474. key->keyIdSet = 0;
  9475. }
  9476. #elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  9477. #ifndef HAVE_COMP_KEY
  9478. if (err == MP_OKAY) {
  9479. #else
  9480. if (err == MP_OKAY && !compressed) {
  9481. #endif
  9482. buf_reverse(&key->keyRaw[0], &in[0], keysize);
  9483. buf_reverse(&key->keyRaw[keysize], &in[keysize], keysize);
  9484. }
  9485. #endif
  9486. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  9487. if (err == MP_OKAY)
  9488. err = wc_ecc_check_key(key);
  9489. #endif
  9490. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  9491. if (err == MP_OKAY) {
  9492. err = wc_MAXQ10XX_EccSetKey(key, keysize);
  9493. }
  9494. #endif
  9495. if (err != MP_OKAY) {
  9496. mp_clear(key->pubkey.x);
  9497. mp_clear(key->pubkey.y);
  9498. mp_clear(key->pubkey.z);
  9499. mp_clear(key->k);
  9500. }
  9501. RESTORE_VECTOR_REGISTERS();
  9502. return err;
  9503. }
  9504. WOLFSSL_ABI
  9505. int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key)
  9506. {
  9507. return wc_ecc_import_x963_ex(in, inLen, key, ECC_CURVE_DEF);
  9508. }
  9509. #endif /* HAVE_ECC_KEY_IMPORT */
  9510. #ifdef HAVE_ECC_KEY_EXPORT
  9511. /* export ecc key to component form, d is optional if only exporting public
  9512. * encType is WC_TYPE_UNSIGNED_BIN or WC_TYPE_HEX_STR
  9513. * return MP_OKAY on success */
  9514. int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
  9515. byte* qy, word32* qyLen, byte* d, word32* dLen, int encType)
  9516. {
  9517. int err = 0;
  9518. word32 keySz;
  9519. if (key == NULL) {
  9520. return BAD_FUNC_ARG;
  9521. }
  9522. if (wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL) {
  9523. return ECC_BAD_ARG_E;
  9524. }
  9525. keySz = (word32)key->dp->size;
  9526. /* private key, d */
  9527. if (d != NULL) {
  9528. if (dLen == NULL ||
  9529. (key->type != ECC_PRIVATEKEY && key->type != ECC_PRIVATEKEY_ONLY))
  9530. return BAD_FUNC_ARG;
  9531. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  9532. /* Hardware cannot export private portion */
  9533. return NOT_COMPILED_IN;
  9534. #else
  9535. #if defined(WOLFSSL_SECO_CAAM)
  9536. if (key->blackKey > 0 && key->devId == WOLFSSL_SECO_DEVID) {
  9537. /* Hardware cannot export private portion */
  9538. WOLFSSL_MSG("Can not export private key from HSM");
  9539. return NOT_COMPILED_IN;
  9540. }
  9541. #endif
  9542. #if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  9543. if (key->blackKey == CAAM_BLACK_KEY_CCM) {
  9544. if (*dLen < keySz + WC_CAAM_MAC_SZ) {
  9545. *dLen = keySz + WC_CAAM_MAC_SZ;
  9546. return BUFFER_E;
  9547. }
  9548. err = wc_export_int(key->k, d, dLen, keySz + WC_CAAM_MAC_SZ,
  9549. encType);
  9550. *dLen = keySz + WC_CAAM_MAC_SZ;
  9551. }
  9552. else if (encType == WC_TYPE_BLACK_KEY &&
  9553. key->blackKey != CAAM_BLACK_KEY_ECB &&
  9554. key->blackKey > 0) {
  9555. if (*dLen < keySz + WC_CAAM_MAC_SZ) {
  9556. *dLen = keySz + WC_CAAM_MAC_SZ;
  9557. return BUFFER_E;
  9558. }
  9559. if (key->blackKey != CAAM_BLACK_KEY_CCM) {
  9560. if (caamReadPartition(key->blackKey, d, keySz + WC_CAAM_MAC_SZ) != 0)
  9561. return WC_HW_E;
  9562. }
  9563. *dLen = keySz + WC_CAAM_MAC_SZ;
  9564. }
  9565. else
  9566. #endif
  9567. {
  9568. err = wc_export_int(key->k, d, dLen, keySz, encType);
  9569. if (err != MP_OKAY)
  9570. return err;
  9571. }
  9572. #endif
  9573. }
  9574. /* public x component */
  9575. if (qx != NULL) {
  9576. if (qxLen == NULL || key->type == ECC_PRIVATEKEY_ONLY)
  9577. return BAD_FUNC_ARG;
  9578. err = wc_export_int(key->pubkey.x, qx, qxLen, keySz, encType);
  9579. if (err != MP_OKAY)
  9580. return err;
  9581. }
  9582. /* public y component */
  9583. if (qy != NULL) {
  9584. if (qyLen == NULL || key->type == ECC_PRIVATEKEY_ONLY)
  9585. return BAD_FUNC_ARG;
  9586. err = wc_export_int(key->pubkey.y, qy, qyLen, keySz, encType);
  9587. if (err != MP_OKAY)
  9588. return err;
  9589. }
  9590. return err;
  9591. }
  9592. /* export ecc private key only raw, outLen is in/out size as unsigned bin
  9593. return MP_OKAY on success */
  9594. WOLFSSL_ABI
  9595. int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen)
  9596. {
  9597. if (out == NULL || outLen == NULL) {
  9598. return BAD_FUNC_ARG;
  9599. }
  9600. #if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  9601. /* check if black key in secure memory */
  9602. if ((key->blackKey != CAAM_BLACK_KEY_CCM &&
  9603. key->blackKey != CAAM_BLACK_KEY_ECB) && key->blackKey > 0) {
  9604. return wc_ecc_export_ex(key, NULL, NULL, NULL, NULL, out, outLen,
  9605. WC_TYPE_BLACK_KEY);
  9606. }
  9607. #endif
  9608. return wc_ecc_export_ex(key, NULL, NULL, NULL, NULL, out, outLen,
  9609. WC_TYPE_UNSIGNED_BIN);
  9610. }
  9611. /* export public key to raw elements including public (Qx,Qy) as unsigned bin
  9612. * return MP_OKAY on success, negative on error */
  9613. int wc_ecc_export_public_raw(ecc_key* key, byte* qx, word32* qxLen,
  9614. byte* qy, word32* qyLen)
  9615. {
  9616. if (qx == NULL || qxLen == NULL || qy == NULL || qyLen == NULL) {
  9617. return BAD_FUNC_ARG;
  9618. }
  9619. return wc_ecc_export_ex(key, qx, qxLen, qy, qyLen, NULL, NULL,
  9620. WC_TYPE_UNSIGNED_BIN);
  9621. }
  9622. /* export ecc key to raw elements including public (Qx,Qy) and
  9623. * private (d) as unsigned bin
  9624. * return MP_OKAY on success, negative on error */
  9625. int wc_ecc_export_private_raw(ecc_key* key, byte* qx, word32* qxLen,
  9626. byte* qy, word32* qyLen, byte* d, word32* dLen)
  9627. {
  9628. return wc_ecc_export_ex(key, qx, qxLen, qy, qyLen, d, dLen,
  9629. WC_TYPE_UNSIGNED_BIN);
  9630. }
  9631. #endif /* HAVE_ECC_KEY_EXPORT */
  9632. #ifdef HAVE_ECC_KEY_IMPORT
  9633. /* import private key, public part optional if (pub) passed as NULL */
  9634. int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz,
  9635. const byte* pub, word32 pubSz, ecc_key* key,
  9636. int curve_id)
  9637. {
  9638. int ret;
  9639. #ifdef WOLFSSL_CRYPTOCELL
  9640. const CRYS_ECPKI_Domain_t* pDomain;
  9641. #endif
  9642. if (key == NULL || priv == NULL)
  9643. return BAD_FUNC_ARG;
  9644. /* public optional, NULL if only importing private */
  9645. if (pub != NULL) {
  9646. #ifndef NO_ASN
  9647. word32 idx = 0;
  9648. ret = wc_ecc_import_x963_ex(pub, pubSz, key, curve_id);
  9649. if (ret < 0)
  9650. ret = wc_EccPublicKeyDecode(pub, &idx, key, pubSz);
  9651. key->type = ECC_PRIVATEKEY;
  9652. #else
  9653. (void)pubSz;
  9654. ret = NOT_COMPILED_IN;
  9655. #endif
  9656. }
  9657. else {
  9658. /* make sure required variables are reset */
  9659. wc_ecc_reset(key);
  9660. /* set key size */
  9661. ret = wc_ecc_set_curve(key, (int)privSz, curve_id);
  9662. key->type = ECC_PRIVATEKEY_ONLY;
  9663. }
  9664. if (ret != 0)
  9665. return ret;
  9666. #ifdef WOLFSSL_CRYPTOCELL
  9667. pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id));
  9668. /* import private key - priv checked for NULL at top */
  9669. if (priv[0] != '\0') {
  9670. /* Create private key from external key buffer*/
  9671. ret = CRYS_ECPKI_BuildPrivKey(pDomain,
  9672. priv,
  9673. privSz,
  9674. &key->ctx.privKey);
  9675. if (ret != SA_SILIB_RET_OK) {
  9676. WOLFSSL_MSG("CRYS_ECPKI_BuildPrivKey failed");
  9677. return ret;
  9678. }
  9679. ret = mp_read_unsigned_bin(key->k, priv, privSz);
  9680. }
  9681. #elif defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  9682. if ((wc_ecc_size(key) + WC_CAAM_MAC_SZ) == (int)privSz) {
  9683. #ifdef WOLFSSL_CAAM_BLACK_KEY_SM
  9684. int part = caamFindUnusedPartition();
  9685. if (part >= 0) {
  9686. CAAM_ADDRESS vaddr = caamGetPartition(part, privSz*3);
  9687. if (vaddr == 0) {
  9688. WOLFSSL_MSG("Unable to get partition");
  9689. return MEMORY_E;
  9690. }
  9691. key->partNum = part;
  9692. key->blackKey = (word32)vaddr;
  9693. if (caamWriteToPartition(vaddr, priv, privSz) != 0)
  9694. return WC_HW_E;
  9695. if (pub != NULL) {
  9696. /* +1 to account for x963 compressed bit */
  9697. if (caamWriteToPartition(vaddr + privSz, pub + 1, pubSz - 1) != 0)
  9698. return WC_HW_E;
  9699. key->securePubKey = (word32)vaddr + privSz;
  9700. }
  9701. }
  9702. else {
  9703. WOLFSSL_MSG("Unable to find an unused partition");
  9704. return MEMORY_E;
  9705. }
  9706. #else
  9707. key->blackKey = CAAM_BLACK_KEY_CCM;
  9708. ret = mp_read_unsigned_bin(key->k, priv, privSz);
  9709. #endif
  9710. }
  9711. else {
  9712. key->blackKey = 0;
  9713. ret = mp_read_unsigned_bin(key->k, priv, privSz);
  9714. /* If using AES-ECB encrypted black keys check here if key is valid,
  9715. * if not valid than assume is an encrypted key. A public key is needed
  9716. * for testing validity. */
  9717. if (key->devId == WOLFSSL_CAAM_DEVID && (
  9718. wc_ecc_get_curve_id(key->idx) == ECC_SECP256R1 ||
  9719. wc_ecc_get_curve_id(key->idx) == ECC_SECP384R1)) {
  9720. if ((pub != NULL) && (ret == MP_OKAY) &&
  9721. (_ecc_validate_public_key(key, 1, 1) != MP_OKAY)) {
  9722. key->blackKey = CAAM_BLACK_KEY_ECB;
  9723. }
  9724. else if ((pub == NULL) && (ret == MP_OKAY)) {
  9725. WOLFSSL_MSG("Assuming encrypted key with no public key to check");
  9726. key->blackKey = CAAM_BLACK_KEY_ECB;
  9727. }
  9728. else {
  9729. WOLFSSL_MSG("Importing key that is not a black key!");
  9730. }
  9731. }
  9732. }
  9733. #else
  9734. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  9735. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  9736. #endif
  9737. ret = mp_read_unsigned_bin(key->k, priv, privSz);
  9738. #ifdef HAVE_WOLF_BIGINT
  9739. if (ret == 0 &&
  9740. wc_bigint_from_unsigned_bin(&key->k->raw, priv, privSz) != 0) {
  9741. mp_clear(key->k);
  9742. ret = ASN_GETINT_E;
  9743. }
  9744. #endif /* HAVE_WOLF_BIGINT */
  9745. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  9746. if (ret == 0) {
  9747. #ifdef WOLFSSL_SMALL_STACK
  9748. mp_int* order = NULL;
  9749. #else
  9750. mp_int order[1];
  9751. #endif
  9752. #ifdef WOLFSSL_SMALL_STACK
  9753. order = (mp_int*)XMALLOC(sizeof(mp_int), key->heap, DYNAMIC_TYPE_ECC);
  9754. if (order == NULL) {
  9755. ret = MEMORY_E;
  9756. }
  9757. #endif
  9758. if (ret == 0) {
  9759. ret = mp_init(order);
  9760. }
  9761. if (ret == 0) {
  9762. ret = mp_read_radix(order, key->dp->order, MP_RADIX_HEX);
  9763. }
  9764. #ifdef WOLFSSL_SM2
  9765. /* SM2 curve: private key must be less than order-1. */
  9766. if ((ret == 0) && (key->idx != ECC_CUSTOM_IDX) &&
  9767. (ecc_sets[key->idx].id == ECC_SM2P256V1)) {
  9768. ret = mp_sub_d(order, 1, order);
  9769. }
  9770. #endif
  9771. if ((ret == 0) && (mp_cmp(key->k, order) != MP_LT)) {
  9772. ret = ECC_PRIV_KEY_E;
  9773. }
  9774. #ifdef WOLFSSL_SMALL_STACK
  9775. XFREE(order, key->heap, DYNAMIC_TYPE_ECC);
  9776. #endif
  9777. }
  9778. #endif /* WOLFSSL_VALIDATE_ECC_IMPORT */
  9779. #endif /* WOLFSSL_CRYPTOCELL */
  9780. #if defined(WOLFSSL_VALIDATE_ECC_IMPORT) && !defined(WOLFSSL_KCAPI_ECC)
  9781. if ((pub != NULL) && (ret == MP_OKAY))
  9782. /* public key needed to perform key validation */
  9783. ret = _ecc_validate_public_key(key, 1, 1);
  9784. #endif
  9785. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  9786. RESTORE_VECTOR_REGISTERS();
  9787. #endif
  9788. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  9789. if (ret == 0) {
  9790. ret = wc_MAXQ10XX_EccSetKey(key, key->dp->size);
  9791. }
  9792. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  9793. if (ret == 0) {
  9794. ret = silabs_ecc_import(key, key->dp->size, (pub != NULL), 1);
  9795. }
  9796. #endif
  9797. return ret;
  9798. }
  9799. /* ecc private key import, public key in ANSI X9.63 format, private raw */
  9800. WOLFSSL_ABI
  9801. int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
  9802. word32 pubSz, ecc_key* key)
  9803. {
  9804. return wc_ecc_import_private_key_ex(priv, privSz, pub, pubSz, key,
  9805. ECC_CURVE_DEF);
  9806. }
  9807. #endif /* HAVE_ECC_KEY_IMPORT */
  9808. #ifndef NO_ASN
  9809. /**
  9810. Convert ECC R,S to signature
  9811. r R component of signature
  9812. s S component of signature
  9813. out DER-encoded ECDSA signature
  9814. outlen [in/out] output buffer size, output signature size
  9815. return MP_OKAY on success
  9816. */
  9817. WOLFSSL_ABI
  9818. int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen)
  9819. {
  9820. int err;
  9821. #ifdef WOLFSSL_SMALL_STACK
  9822. mp_int* rtmp = NULL;
  9823. mp_int* stmp = NULL;
  9824. #else
  9825. mp_int rtmp[1];
  9826. mp_int stmp[1];
  9827. #endif
  9828. if (r == NULL || s == NULL || out == NULL || outlen == NULL)
  9829. return ECC_BAD_ARG_E;
  9830. #ifdef WOLFSSL_SMALL_STACK
  9831. rtmp = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  9832. if (rtmp == NULL)
  9833. return MEMORY_E;
  9834. stmp = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  9835. if (stmp == NULL) {
  9836. XFREE(rtmp, NULL, DYNAMIC_TYPE_ECC);
  9837. return MEMORY_E;
  9838. }
  9839. #endif
  9840. err = mp_init_multi(rtmp, stmp, NULL, NULL, NULL, NULL);
  9841. if (err != MP_OKAY) {
  9842. #ifdef WOLFSSL_SMALL_STACK
  9843. XFREE(stmp, NULL, DYNAMIC_TYPE_ECC);
  9844. XFREE(rtmp, NULL, DYNAMIC_TYPE_ECC);
  9845. #endif
  9846. return err;
  9847. }
  9848. err = mp_read_radix(rtmp, r, MP_RADIX_HEX);
  9849. if (err == MP_OKAY)
  9850. err = mp_read_radix(stmp, s, MP_RADIX_HEX);
  9851. if (err == MP_OKAY) {
  9852. if (mp_iszero(rtmp) == MP_YES || mp_iszero(stmp) == MP_YES)
  9853. err = MP_ZERO_E;
  9854. }
  9855. if (err == MP_OKAY) {
  9856. if (mp_isneg(rtmp) == MP_YES || mp_isneg(stmp) == MP_YES) {
  9857. err = MP_READ_E;
  9858. }
  9859. }
  9860. /* convert mp_ints to ECDSA sig, initializes rtmp and stmp internally */
  9861. if (err == MP_OKAY)
  9862. err = StoreECC_DSA_Sig(out, outlen, rtmp, stmp);
  9863. mp_clear(rtmp);
  9864. mp_clear(stmp);
  9865. #ifdef WOLFSSL_SMALL_STACK
  9866. XFREE(stmp, NULL, DYNAMIC_TYPE_ECC);
  9867. XFREE(rtmp, NULL, DYNAMIC_TYPE_ECC);
  9868. #endif
  9869. return err;
  9870. }
  9871. /**
  9872. Convert ECC R,S raw unsigned bin to signature
  9873. r R component of signature
  9874. rSz R size
  9875. s S component of signature
  9876. sSz S size
  9877. out DER-encoded ECDSA signature
  9878. outlen [in/out] output buffer size, output signature size
  9879. return MP_OKAY on success
  9880. */
  9881. int wc_ecc_rs_raw_to_sig(const byte* r, word32 rSz, const byte* s, word32 sSz,
  9882. byte* out, word32* outlen)
  9883. {
  9884. if (r == NULL || s == NULL || out == NULL || outlen == NULL)
  9885. return ECC_BAD_ARG_E;
  9886. /* convert mp_ints to ECDSA sig, initializes rtmp and stmp internally */
  9887. return StoreECC_DSA_Sig_Bin(out, outlen, r, rSz, s, sSz);
  9888. }
  9889. /**
  9890. Convert ECC signature to R,S
  9891. sig DER-encoded ECDSA signature
  9892. sigLen length of signature in octets
  9893. r R component of signature
  9894. rLen [in/out] output "r" buffer size, output "r" size
  9895. s S component of signature
  9896. sLen [in/out] output "s" buffer size, output "s" size
  9897. return MP_OKAY on success, negative on error
  9898. */
  9899. int wc_ecc_sig_to_rs(const byte* sig, word32 sigLen, byte* r, word32* rLen,
  9900. byte* s, word32* sLen)
  9901. {
  9902. if (sig == NULL || r == NULL || rLen == NULL || s == NULL || sLen == NULL)
  9903. return ECC_BAD_ARG_E;
  9904. return DecodeECC_DSA_Sig_Bin(sig, sigLen, r, rLen, s, sLen);
  9905. }
  9906. #endif /* !NO_ASN */
  9907. #ifdef HAVE_ECC_KEY_IMPORT
  9908. static int wc_ecc_import_raw_private(ecc_key* key, const char* qx,
  9909. const char* qy, const char* d, int curve_id, int encType)
  9910. {
  9911. int err = MP_OKAY;
  9912. #if defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_ATECC508A) && \
  9913. !defined(WOLFSSL_ATECC608A)
  9914. const CRYS_ECPKI_Domain_t* pDomain;
  9915. CRYS_ECPKI_BUILD_TempData_t tempBuff;
  9916. byte keyRaw[ECC_MAX_CRYPTO_HW_SIZE*2 + 1];
  9917. #endif
  9918. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  9919. defined(WOLFSSL_CRYPTOCELL)
  9920. word32 keySz = 0;
  9921. #endif
  9922. /* if d is NULL, only import as public key using Qx,Qy */
  9923. if (key == NULL || qx == NULL || qy == NULL) {
  9924. return BAD_FUNC_ARG;
  9925. }
  9926. /* make sure required variables are reset */
  9927. wc_ecc_reset(key);
  9928. /* set curve type and index */
  9929. err = wc_ecc_set_curve(key, 0, curve_id);
  9930. if (err != 0) {
  9931. return err;
  9932. }
  9933. /* init key */
  9934. #ifdef ALT_ECC_SIZE
  9935. key->pubkey.x = (mp_int*)&key->pubkey.xyz[0];
  9936. key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
  9937. key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
  9938. alt_fp_init(key->pubkey.x);
  9939. alt_fp_init(key->pubkey.y);
  9940. alt_fp_init(key->pubkey.z);
  9941. key->k = (mp_int*)key->ka;
  9942. alt_fp_init(key->k);
  9943. #else
  9944. err = mp_init_multi(key->k, key->pubkey.x, key->pubkey.y, key->pubkey.z,
  9945. NULL, NULL);
  9946. #endif
  9947. if (err != MP_OKAY)
  9948. return MEMORY_E;
  9949. /* read Qx */
  9950. if (err == MP_OKAY) {
  9951. if (encType == WC_TYPE_HEX_STR)
  9952. err = mp_read_radix(key->pubkey.x, qx, MP_RADIX_HEX);
  9953. else
  9954. err = mp_read_unsigned_bin(key->pubkey.x, (const byte*)qx,
  9955. (word32)key->dp->size);
  9956. if (mp_isneg(key->pubkey.x)) {
  9957. WOLFSSL_MSG("Invalid Qx");
  9958. err = BAD_FUNC_ARG;
  9959. }
  9960. if (mp_unsigned_bin_size(key->pubkey.x) > key->dp->size) {
  9961. err = BAD_FUNC_ARG;
  9962. }
  9963. }
  9964. /* read Qy */
  9965. if (err == MP_OKAY) {
  9966. if (encType == WC_TYPE_HEX_STR)
  9967. err = mp_read_radix(key->pubkey.y, qy, MP_RADIX_HEX);
  9968. else
  9969. err = mp_read_unsigned_bin(key->pubkey.y, (const byte*)qy,
  9970. (word32)key->dp->size);
  9971. if (mp_isneg(key->pubkey.y)) {
  9972. WOLFSSL_MSG("Invalid Qy");
  9973. err = BAD_FUNC_ARG;
  9974. }
  9975. if (mp_unsigned_bin_size(key->pubkey.y) > key->dp->size) {
  9976. err = BAD_FUNC_ARG;
  9977. }
  9978. }
  9979. if (err == MP_OKAY) {
  9980. if (mp_iszero(key->pubkey.x) && mp_iszero(key->pubkey.y)) {
  9981. WOLFSSL_MSG("Invalid Qx and Qy");
  9982. err = ECC_INF_E;
  9983. }
  9984. }
  9985. if (err == MP_OKAY)
  9986. err = mp_set(key->pubkey.z, 1);
  9987. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  9988. /* For SECP256R1 only save raw public key for hardware */
  9989. if (err == MP_OKAY && curve_id == ECC_SECP256R1) {
  9990. keySz = key->dp->size;
  9991. err = wc_export_int(key->pubkey.x, key->pubkey_raw,
  9992. &keySz, keySz, WC_TYPE_UNSIGNED_BIN);
  9993. if (err == MP_OKAY)
  9994. err = wc_export_int(key->pubkey.y, &key->pubkey_raw[keySz],
  9995. &keySz, keySz, WC_TYPE_UNSIGNED_BIN);
  9996. }
  9997. #elif defined(WOLFSSL_CRYPTOCELL)
  9998. if (err == MP_OKAY) {
  9999. keyRaw[0] = ECC_POINT_UNCOMP;
  10000. keySz = (word32)key->dp->size;
  10001. err = wc_export_int(key->pubkey.x, &keyRaw[1], &keySz, keySz,
  10002. WC_TYPE_UNSIGNED_BIN);
  10003. if (err == MP_OKAY) {
  10004. err = wc_export_int(key->pubkey.y, &keyRaw[1+keySz],
  10005. &keySz, keySz, WC_TYPE_UNSIGNED_BIN);
  10006. }
  10007. if (err == MP_OKAY) {
  10008. pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id));
  10009. /* create public key from external key buffer */
  10010. err = CRYS_ECPKI_BuildPublKeyFullCheck(pDomain,
  10011. keyRaw,
  10012. keySz*2 + 1,
  10013. &key->ctx.pubKey,
  10014. &tempBuff);
  10015. }
  10016. if (err != SA_SILIB_RET_OK){
  10017. WOLFSSL_MSG("CRYS_ECPKI_BuildPublKeyFullCheck failed");
  10018. return err;
  10019. }
  10020. }
  10021. #elif defined(WOLFSSL_KCAPI_ECC)
  10022. if (err == MP_OKAY) {
  10023. word32 keySz = key->dp->size;
  10024. err = wc_export_int(key->pubkey.x, key->pubkey_raw,
  10025. &keySz, keySz, WC_TYPE_UNSIGNED_BIN);
  10026. if (err == MP_OKAY) {
  10027. err = wc_export_int(key->pubkey.y,
  10028. &key->pubkey_raw[keySz], &keySz, keySz,
  10029. WC_TYPE_UNSIGNED_BIN);
  10030. }
  10031. }
  10032. #elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  10033. if (err == MP_OKAY) {
  10034. const word32 keySize = key->dp->size;
  10035. word32 bufSize = sizeof(key->keyRaw);
  10036. err = wc_export_int(key->pubkey.x, key->keyRaw, &bufSize, keySize,
  10037. WC_TYPE_UNSIGNED_BIN);
  10038. if (err == MP_OKAY) {
  10039. const word32 offset = bufSize;
  10040. bufSize = sizeof(key->keyRaw) - offset;
  10041. err = wc_export_int(key->pubkey.y, &key->keyRaw[offset], &bufSize,
  10042. keySize, WC_TYPE_UNSIGNED_BIN);
  10043. }
  10044. if (err == MP_OKAY) {
  10045. mp_reverse(key->keyRaw, keySize);
  10046. mp_reverse(&key->keyRaw[keySize], keySize);
  10047. WOLFSSL_XIL_DCACHE_FLUSH_RANGE(XIL_CAST_U64(key->keyRaw),
  10048. keySize * 2);
  10049. #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN
  10050. err = XSecure_EllipticValidateKey(&(key->xSec.cinst),
  10051. xil_curve_type[key->dp->id],
  10052. XIL_CAST_U64(key->keyRaw));
  10053. if (err) {
  10054. WOLFSSL_XIL_ERROR("Validation of ECC key failed", err);
  10055. err = WC_HW_E;
  10056. }
  10057. #endif
  10058. }
  10059. }
  10060. #endif
  10061. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  10062. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  10063. #endif
  10064. /* import private key */
  10065. if (err == MP_OKAY) {
  10066. if (d != NULL) {
  10067. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  10068. /* Hardware doesn't support loading private key */
  10069. err = NOT_COMPILED_IN;
  10070. #elif defined(WOLFSSL_CRYPTOCELL)
  10071. key->type = ECC_PRIVATEKEY;
  10072. if (encType == WC_TYPE_HEX_STR)
  10073. err = mp_read_radix(key->k, d, MP_RADIX_HEX);
  10074. else
  10075. err = mp_read_unsigned_bin(key->k, (const byte*)d,
  10076. key->dp->size);
  10077. if (err == MP_OKAY) {
  10078. err = wc_export_int(key->k, &keyRaw[0], &keySz, keySz,
  10079. WC_TYPE_UNSIGNED_BIN);
  10080. }
  10081. if (err == MP_OKAY) {
  10082. /* Create private key from external key buffer*/
  10083. err = CRYS_ECPKI_BuildPrivKey(pDomain,
  10084. keyRaw,
  10085. keySz,
  10086. &key->ctx.privKey);
  10087. if (err != SA_SILIB_RET_OK){
  10088. WOLFSSL_MSG("CRYS_ECPKI_BuildPrivKey failed");
  10089. return err;
  10090. }
  10091. }
  10092. #else
  10093. key->type = ECC_PRIVATEKEY;
  10094. if (encType == WC_TYPE_HEX_STR)
  10095. err = mp_read_radix(key->k, d, MP_RADIX_HEX);
  10096. else {
  10097. #if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
  10098. if (key->blackKey == CAAM_BLACK_KEY_CCM) {
  10099. err = mp_read_unsigned_bin(key->k, (const byte*)d,
  10100. key->dp->size + WC_CAAM_MAC_SZ);
  10101. }
  10102. else
  10103. #endif /* WOLFSSL_QNX_CAAM */
  10104. {
  10105. err = mp_read_unsigned_bin(key->k, (const byte*)d,
  10106. (word32)key->dp->size);
  10107. }
  10108. }
  10109. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  10110. if (err == MP_OKAY) {
  10111. const word32 key_size = key->dp->size;
  10112. word32 buf_size = key_size;
  10113. err = wc_export_int(key->k, key->privKey,
  10114. &buf_size, key_size, WC_TYPE_UNSIGNED_BIN);
  10115. mp_reverse(key->privKey, key_size);
  10116. }
  10117. #endif
  10118. #endif /* #else-case of custom HW-specific implementations */
  10119. if (mp_iszero(key->k) || mp_isneg(key->k)) {
  10120. WOLFSSL_MSG("Invalid private key");
  10121. err = BAD_FUNC_ARG;
  10122. }
  10123. } else {
  10124. key->type = ECC_PUBLICKEY;
  10125. }
  10126. }
  10127. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  10128. if (err == MP_OKAY) {
  10129. err = wc_ecc_check_key(key);
  10130. if (err == IS_POINT_E && (mp_iszero(key->pubkey.x) ||
  10131. mp_iszero(key->pubkey.y))) {
  10132. err = BAD_FUNC_ARG;
  10133. }
  10134. }
  10135. #endif
  10136. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  10137. RESTORE_VECTOR_REGISTERS();
  10138. #endif
  10139. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  10140. if (err == MP_OKAY) {
  10141. err = wc_MAXQ10XX_EccSetKey(key, key->dp->size);
  10142. }
  10143. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  10144. if (err == MP_OKAY) {
  10145. err = silabs_ecc_import(key, key->dp->size, 1, (d != NULL));
  10146. }
  10147. #endif
  10148. if (err != MP_OKAY) {
  10149. mp_clear(key->pubkey.x);
  10150. mp_clear(key->pubkey.y);
  10151. mp_clear(key->pubkey.z);
  10152. mp_clear(key->k);
  10153. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  10154. ForceZero(key->keyRaw, sizeof(key->keyRaw));
  10155. #endif
  10156. }
  10157. return err;
  10158. }
  10159. /**
  10160. Import raw ECC key
  10161. key The destination ecc_key structure
  10162. qx x component of the public key, as ASCII hex string
  10163. qy y component of the public key, as ASCII hex string
  10164. d private key, as ASCII hex string, optional if importing public
  10165. key only
  10166. dp Custom ecc_set_type
  10167. return MP_OKAY on success
  10168. */
  10169. int wc_ecc_import_raw_ex(ecc_key* key, const char* qx, const char* qy,
  10170. const char* d, int curve_id)
  10171. {
  10172. return wc_ecc_import_raw_private(key, qx, qy, d, curve_id,
  10173. WC_TYPE_HEX_STR);
  10174. }
  10175. /* Import x, y and optional private (d) as unsigned binary */
  10176. int wc_ecc_import_unsigned(ecc_key* key, const byte* qx, const byte* qy,
  10177. const byte* d, int curve_id)
  10178. {
  10179. return wc_ecc_import_raw_private(key, (const char*)qx, (const char*)qy,
  10180. (const char*)d, curve_id, WC_TYPE_UNSIGNED_BIN);
  10181. }
  10182. /**
  10183. Import raw ECC key
  10184. key The destination ecc_key structure
  10185. qx x component of the public key, as ASCII hex string
  10186. qy y component of the public key, as ASCII hex string
  10187. d private key, as ASCII hex string, optional if importing public
  10188. key only
  10189. curveName ECC curve name, from ecc_sets[]
  10190. return MP_OKAY on success
  10191. */
  10192. WOLFSSL_ABI
  10193. int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
  10194. const char* d, const char* curveName)
  10195. {
  10196. int err, x;
  10197. /* if d is NULL, only import as public key using Qx,Qy */
  10198. if (key == NULL || qx == NULL || qy == NULL || curveName == NULL) {
  10199. return BAD_FUNC_ARG;
  10200. }
  10201. /* set curve type and index */
  10202. for (x = 0; ecc_sets[x].size != 0; x++) {
  10203. if (XSTRNCMP(ecc_sets[x].name, curveName,
  10204. XSTRLEN(curveName)) == 0) {
  10205. break;
  10206. }
  10207. }
  10208. if (ecc_sets[x].size == 0) {
  10209. WOLFSSL_MSG("ecc_set curve name not found");
  10210. err = ASN_PARSE_E;
  10211. } else {
  10212. return wc_ecc_import_raw_private(key, qx, qy, d, ecc_sets[x].id,
  10213. WC_TYPE_HEX_STR);
  10214. }
  10215. return err;
  10216. }
  10217. #endif /* HAVE_ECC_KEY_IMPORT */
  10218. #if defined(HAVE_ECC_ENCRYPT) && !defined(WOLFSSL_ECIES_OLD)
  10219. /* public key size in octets */
  10220. static int ecc_public_key_size(ecc_key* key, word32* sz)
  10221. {
  10222. if (key == NULL || key->dp == NULL)
  10223. return BAD_FUNC_ARG;
  10224. /* 'Uncompressed' | x | y */
  10225. *sz = 1 + 2 * (word32)key->dp->size;
  10226. return 0;
  10227. }
  10228. #endif
  10229. /* key size in octets */
  10230. WOLFSSL_ABI
  10231. int wc_ecc_size(ecc_key* key)
  10232. {
  10233. if (key == NULL || key->dp == NULL)
  10234. return 0;
  10235. return key->dp->size;
  10236. }
  10237. /* maximum signature size based on key size */
  10238. WOLFSSL_ABI
  10239. int wc_ecc_sig_size_calc(int sz)
  10240. {
  10241. int maxSigSz = 0;
  10242. /* calculate based on key bits */
  10243. /* maximum possible signature header size is 7 bytes plus 2 bytes padding */
  10244. maxSigSz = (sz * 2) + SIG_HEADER_SZ + ECC_MAX_PAD_SZ;
  10245. /* if total length is less than 128 + SEQ(1)+LEN(1) then subtract 1 */
  10246. if (maxSigSz < (128 + 2)) {
  10247. maxSigSz -= 1;
  10248. }
  10249. return maxSigSz;
  10250. }
  10251. /* maximum signature size based on actual key curve */
  10252. WOLFSSL_ABI
  10253. int wc_ecc_sig_size(const ecc_key* key)
  10254. {
  10255. int maxSigSz;
  10256. int orderBits, keySz;
  10257. if (key == NULL || key->dp == NULL)
  10258. return 0;
  10259. /* the signature r and s will always be less than order */
  10260. /* if the order MSB (top bit of byte) is set then ASN encoding needs
  10261. extra byte for r and s, so add 2 */
  10262. keySz = key->dp->size;
  10263. orderBits = wc_ecc_get_curve_order_bit_count(key->dp);
  10264. if (orderBits > keySz * 8) {
  10265. keySz = (orderBits + 7) / 8;
  10266. }
  10267. /* maximum possible signature header size is 7 bytes */
  10268. maxSigSz = (keySz * 2) + SIG_HEADER_SZ;
  10269. if ((orderBits % 8) == 0) {
  10270. /* MSB can be set, so add 2 */
  10271. maxSigSz += ECC_MAX_PAD_SZ;
  10272. }
  10273. /* if total length is less than 128 + SEQ(1)+LEN(1) then subtract 1 */
  10274. if (maxSigSz < (128 + 2)) {
  10275. maxSigSz -= 1;
  10276. }
  10277. return maxSigSz;
  10278. }
  10279. #ifdef FP_ECC
  10280. /* fixed point ECC cache */
  10281. /* number of entries in the cache */
  10282. #ifndef FP_ENTRIES
  10283. #define FP_ENTRIES 15
  10284. #endif
  10285. /* number of bits in LUT */
  10286. #ifndef FP_LUT
  10287. #define FP_LUT 8U
  10288. #endif
  10289. #ifdef ECC_SHAMIR
  10290. /* Sharmir requires a bigger LUT, TAO */
  10291. #if (FP_LUT > 12) || (FP_LUT < 4)
  10292. #error FP_LUT must be between 4 and 12 inclusively
  10293. #endif
  10294. #else
  10295. #if (FP_LUT > 12) || (FP_LUT < 2)
  10296. #error FP_LUT must be between 2 and 12 inclusively
  10297. #endif
  10298. #endif
  10299. #if !defined(WOLFSSL_SP_MATH)
  10300. /** Our FP cache */
  10301. typedef struct {
  10302. ecc_point* g; /* cached COPY of base point */
  10303. ecc_point* LUT[1U<<FP_LUT]; /* fixed point lookup */
  10304. int LUT_set; /* flag to determine if the LUT has been computed */
  10305. mp_int mu; /* copy of the montgomery constant */
  10306. int lru_count; /* amount of times this entry has been used */
  10307. int lock; /* flag to indicate cache eviction */
  10308. /* permitted (0) or not (1) */
  10309. } fp_cache_t;
  10310. /* if HAVE_THREAD_LS this cache is per thread, no locking needed */
  10311. static THREAD_LS_T fp_cache_t fp_cache[FP_ENTRIES];
  10312. #ifndef HAVE_THREAD_LS
  10313. static volatile int initMutex = 0; /* prevent multiple mutex inits */
  10314. static wolfSSL_Mutex ecc_fp_lock;
  10315. #endif /* HAVE_THREAD_LS */
  10316. /* simple table to help direct the generation of the LUT */
  10317. static const struct {
  10318. int ham, terma, termb;
  10319. } lut_orders[] = {
  10320. { 0, 0, 0 }, { 1, 0, 0 }, { 1, 0, 0 }, { 2, 1, 2 }, { 1, 0, 0 }, { 2, 1, 4 }, { 2, 2, 4 }, { 3, 3, 4 },
  10321. { 1, 0, 0 }, { 2, 1, 8 }, { 2, 2, 8 }, { 3, 3, 8 }, { 2, 4, 8 }, { 3, 5, 8 }, { 3, 6, 8 }, { 4, 7, 8 },
  10322. { 1, 0, 0 }, { 2, 1, 16 }, { 2, 2, 16 }, { 3, 3, 16 }, { 2, 4, 16 }, { 3, 5, 16 }, { 3, 6, 16 }, { 4, 7, 16 },
  10323. { 2, 8, 16 }, { 3, 9, 16 }, { 3, 10, 16 }, { 4, 11, 16 }, { 3, 12, 16 }, { 4, 13, 16 }, { 4, 14, 16 }, { 5, 15, 16 },
  10324. { 1, 0, 0 }, { 2, 1, 32 }, { 2, 2, 32 }, { 3, 3, 32 }, { 2, 4, 32 }, { 3, 5, 32 }, { 3, 6, 32 }, { 4, 7, 32 },
  10325. { 2, 8, 32 }, { 3, 9, 32 }, { 3, 10, 32 }, { 4, 11, 32 }, { 3, 12, 32 }, { 4, 13, 32 }, { 4, 14, 32 }, { 5, 15, 32 },
  10326. { 2, 16, 32 }, { 3, 17, 32 }, { 3, 18, 32 }, { 4, 19, 32 }, { 3, 20, 32 }, { 4, 21, 32 }, { 4, 22, 32 }, { 5, 23, 32 },
  10327. { 3, 24, 32 }, { 4, 25, 32 }, { 4, 26, 32 }, { 5, 27, 32 }, { 4, 28, 32 }, { 5, 29, 32 }, { 5, 30, 32 }, { 6, 31, 32 },
  10328. #if FP_LUT > 6
  10329. { 1, 0, 0 }, { 2, 1, 64 }, { 2, 2, 64 }, { 3, 3, 64 }, { 2, 4, 64 }, { 3, 5, 64 }, { 3, 6, 64 }, { 4, 7, 64 },
  10330. { 2, 8, 64 }, { 3, 9, 64 }, { 3, 10, 64 }, { 4, 11, 64 }, { 3, 12, 64 }, { 4, 13, 64 }, { 4, 14, 64 }, { 5, 15, 64 },
  10331. { 2, 16, 64 }, { 3, 17, 64 }, { 3, 18, 64 }, { 4, 19, 64 }, { 3, 20, 64 }, { 4, 21, 64 }, { 4, 22, 64 }, { 5, 23, 64 },
  10332. { 3, 24, 64 }, { 4, 25, 64 }, { 4, 26, 64 }, { 5, 27, 64 }, { 4, 28, 64 }, { 5, 29, 64 }, { 5, 30, 64 }, { 6, 31, 64 },
  10333. { 2, 32, 64 }, { 3, 33, 64 }, { 3, 34, 64 }, { 4, 35, 64 }, { 3, 36, 64 }, { 4, 37, 64 }, { 4, 38, 64 }, { 5, 39, 64 },
  10334. { 3, 40, 64 }, { 4, 41, 64 }, { 4, 42, 64 }, { 5, 43, 64 }, { 4, 44, 64 }, { 5, 45, 64 }, { 5, 46, 64 }, { 6, 47, 64 },
  10335. { 3, 48, 64 }, { 4, 49, 64 }, { 4, 50, 64 }, { 5, 51, 64 }, { 4, 52, 64 }, { 5, 53, 64 }, { 5, 54, 64 }, { 6, 55, 64 },
  10336. { 4, 56, 64 }, { 5, 57, 64 }, { 5, 58, 64 }, { 6, 59, 64 }, { 5, 60, 64 }, { 6, 61, 64 }, { 6, 62, 64 }, { 7, 63, 64 },
  10337. #if FP_LUT > 7
  10338. { 1, 0, 0 }, { 2, 1, 128 }, { 2, 2, 128 }, { 3, 3, 128 }, { 2, 4, 128 }, { 3, 5, 128 }, { 3, 6, 128 }, { 4, 7, 128 },
  10339. { 2, 8, 128 }, { 3, 9, 128 }, { 3, 10, 128 }, { 4, 11, 128 }, { 3, 12, 128 }, { 4, 13, 128 }, { 4, 14, 128 }, { 5, 15, 128 },
  10340. { 2, 16, 128 }, { 3, 17, 128 }, { 3, 18, 128 }, { 4, 19, 128 }, { 3, 20, 128 }, { 4, 21, 128 }, { 4, 22, 128 }, { 5, 23, 128 },
  10341. { 3, 24, 128 }, { 4, 25, 128 }, { 4, 26, 128 }, { 5, 27, 128 }, { 4, 28, 128 }, { 5, 29, 128 }, { 5, 30, 128 }, { 6, 31, 128 },
  10342. { 2, 32, 128 }, { 3, 33, 128 }, { 3, 34, 128 }, { 4, 35, 128 }, { 3, 36, 128 }, { 4, 37, 128 }, { 4, 38, 128 }, { 5, 39, 128 },
  10343. { 3, 40, 128 }, { 4, 41, 128 }, { 4, 42, 128 }, { 5, 43, 128 }, { 4, 44, 128 }, { 5, 45, 128 }, { 5, 46, 128 }, { 6, 47, 128 },
  10344. { 3, 48, 128 }, { 4, 49, 128 }, { 4, 50, 128 }, { 5, 51, 128 }, { 4, 52, 128 }, { 5, 53, 128 }, { 5, 54, 128 }, { 6, 55, 128 },
  10345. { 4, 56, 128 }, { 5, 57, 128 }, { 5, 58, 128 }, { 6, 59, 128 }, { 5, 60, 128 }, { 6, 61, 128 }, { 6, 62, 128 }, { 7, 63, 128 },
  10346. { 2, 64, 128 }, { 3, 65, 128 }, { 3, 66, 128 }, { 4, 67, 128 }, { 3, 68, 128 }, { 4, 69, 128 }, { 4, 70, 128 }, { 5, 71, 128 },
  10347. { 3, 72, 128 }, { 4, 73, 128 }, { 4, 74, 128 }, { 5, 75, 128 }, { 4, 76, 128 }, { 5, 77, 128 }, { 5, 78, 128 }, { 6, 79, 128 },
  10348. { 3, 80, 128 }, { 4, 81, 128 }, { 4, 82, 128 }, { 5, 83, 128 }, { 4, 84, 128 }, { 5, 85, 128 }, { 5, 86, 128 }, { 6, 87, 128 },
  10349. { 4, 88, 128 }, { 5, 89, 128 }, { 5, 90, 128 }, { 6, 91, 128 }, { 5, 92, 128 }, { 6, 93, 128 }, { 6, 94, 128 }, { 7, 95, 128 },
  10350. { 3, 96, 128 }, { 4, 97, 128 }, { 4, 98, 128 }, { 5, 99, 128 }, { 4, 100, 128 }, { 5, 101, 128 }, { 5, 102, 128 }, { 6, 103, 128 },
  10351. { 4, 104, 128 }, { 5, 105, 128 }, { 5, 106, 128 }, { 6, 107, 128 }, { 5, 108, 128 }, { 6, 109, 128 }, { 6, 110, 128 }, { 7, 111, 128 },
  10352. { 4, 112, 128 }, { 5, 113, 128 }, { 5, 114, 128 }, { 6, 115, 128 }, { 5, 116, 128 }, { 6, 117, 128 }, { 6, 118, 128 }, { 7, 119, 128 },
  10353. { 5, 120, 128 }, { 6, 121, 128 }, { 6, 122, 128 }, { 7, 123, 128 }, { 6, 124, 128 }, { 7, 125, 128 }, { 7, 126, 128 }, { 8, 127, 128 },
  10354. #if FP_LUT > 8
  10355. { 1, 0, 0 }, { 2, 1, 256 }, { 2, 2, 256 }, { 3, 3, 256 }, { 2, 4, 256 }, { 3, 5, 256 }, { 3, 6, 256 }, { 4, 7, 256 },
  10356. { 2, 8, 256 }, { 3, 9, 256 }, { 3, 10, 256 }, { 4, 11, 256 }, { 3, 12, 256 }, { 4, 13, 256 }, { 4, 14, 256 }, { 5, 15, 256 },
  10357. { 2, 16, 256 }, { 3, 17, 256 }, { 3, 18, 256 }, { 4, 19, 256 }, { 3, 20, 256 }, { 4, 21, 256 }, { 4, 22, 256 }, { 5, 23, 256 },
  10358. { 3, 24, 256 }, { 4, 25, 256 }, { 4, 26, 256 }, { 5, 27, 256 }, { 4, 28, 256 }, { 5, 29, 256 }, { 5, 30, 256 }, { 6, 31, 256 },
  10359. { 2, 32, 256 }, { 3, 33, 256 }, { 3, 34, 256 }, { 4, 35, 256 }, { 3, 36, 256 }, { 4, 37, 256 }, { 4, 38, 256 }, { 5, 39, 256 },
  10360. { 3, 40, 256 }, { 4, 41, 256 }, { 4, 42, 256 }, { 5, 43, 256 }, { 4, 44, 256 }, { 5, 45, 256 }, { 5, 46, 256 }, { 6, 47, 256 },
  10361. { 3, 48, 256 }, { 4, 49, 256 }, { 4, 50, 256 }, { 5, 51, 256 }, { 4, 52, 256 }, { 5, 53, 256 }, { 5, 54, 256 }, { 6, 55, 256 },
  10362. { 4, 56, 256 }, { 5, 57, 256 }, { 5, 58, 256 }, { 6, 59, 256 }, { 5, 60, 256 }, { 6, 61, 256 }, { 6, 62, 256 }, { 7, 63, 256 },
  10363. { 2, 64, 256 }, { 3, 65, 256 }, { 3, 66, 256 }, { 4, 67, 256 }, { 3, 68, 256 }, { 4, 69, 256 }, { 4, 70, 256 }, { 5, 71, 256 },
  10364. { 3, 72, 256 }, { 4, 73, 256 }, { 4, 74, 256 }, { 5, 75, 256 }, { 4, 76, 256 }, { 5, 77, 256 }, { 5, 78, 256 }, { 6, 79, 256 },
  10365. { 3, 80, 256 }, { 4, 81, 256 }, { 4, 82, 256 }, { 5, 83, 256 }, { 4, 84, 256 }, { 5, 85, 256 }, { 5, 86, 256 }, { 6, 87, 256 },
  10366. { 4, 88, 256 }, { 5, 89, 256 }, { 5, 90, 256 }, { 6, 91, 256 }, { 5, 92, 256 }, { 6, 93, 256 }, { 6, 94, 256 }, { 7, 95, 256 },
  10367. { 3, 96, 256 }, { 4, 97, 256 }, { 4, 98, 256 }, { 5, 99, 256 }, { 4, 100, 256 }, { 5, 101, 256 }, { 5, 102, 256 }, { 6, 103, 256 },
  10368. { 4, 104, 256 }, { 5, 105, 256 }, { 5, 106, 256 }, { 6, 107, 256 }, { 5, 108, 256 }, { 6, 109, 256 }, { 6, 110, 256 }, { 7, 111, 256 },
  10369. { 4, 112, 256 }, { 5, 113, 256 }, { 5, 114, 256 }, { 6, 115, 256 }, { 5, 116, 256 }, { 6, 117, 256 }, { 6, 118, 256 }, { 7, 119, 256 },
  10370. { 5, 120, 256 }, { 6, 121, 256 }, { 6, 122, 256 }, { 7, 123, 256 }, { 6, 124, 256 }, { 7, 125, 256 }, { 7, 126, 256 }, { 8, 127, 256 },
  10371. { 2, 128, 256 }, { 3, 129, 256 }, { 3, 130, 256 }, { 4, 131, 256 }, { 3, 132, 256 }, { 4, 133, 256 }, { 4, 134, 256 }, { 5, 135, 256 },
  10372. { 3, 136, 256 }, { 4, 137, 256 }, { 4, 138, 256 }, { 5, 139, 256 }, { 4, 140, 256 }, { 5, 141, 256 }, { 5, 142, 256 }, { 6, 143, 256 },
  10373. { 3, 144, 256 }, { 4, 145, 256 }, { 4, 146, 256 }, { 5, 147, 256 }, { 4, 148, 256 }, { 5, 149, 256 }, { 5, 150, 256 }, { 6, 151, 256 },
  10374. { 4, 152, 256 }, { 5, 153, 256 }, { 5, 154, 256 }, { 6, 155, 256 }, { 5, 156, 256 }, { 6, 157, 256 }, { 6, 158, 256 }, { 7, 159, 256 },
  10375. { 3, 160, 256 }, { 4, 161, 256 }, { 4, 162, 256 }, { 5, 163, 256 }, { 4, 164, 256 }, { 5, 165, 256 }, { 5, 166, 256 }, { 6, 167, 256 },
  10376. { 4, 168, 256 }, { 5, 169, 256 }, { 5, 170, 256 }, { 6, 171, 256 }, { 5, 172, 256 }, { 6, 173, 256 }, { 6, 174, 256 }, { 7, 175, 256 },
  10377. { 4, 176, 256 }, { 5, 177, 256 }, { 5, 178, 256 }, { 6, 179, 256 }, { 5, 180, 256 }, { 6, 181, 256 }, { 6, 182, 256 }, { 7, 183, 256 },
  10378. { 5, 184, 256 }, { 6, 185, 256 }, { 6, 186, 256 }, { 7, 187, 256 }, { 6, 188, 256 }, { 7, 189, 256 }, { 7, 190, 256 }, { 8, 191, 256 },
  10379. { 3, 192, 256 }, { 4, 193, 256 }, { 4, 194, 256 }, { 5, 195, 256 }, { 4, 196, 256 }, { 5, 197, 256 }, { 5, 198, 256 }, { 6, 199, 256 },
  10380. { 4, 200, 256 }, { 5, 201, 256 }, { 5, 202, 256 }, { 6, 203, 256 }, { 5, 204, 256 }, { 6, 205, 256 }, { 6, 206, 256 }, { 7, 207, 256 },
  10381. { 4, 208, 256 }, { 5, 209, 256 }, { 5, 210, 256 }, { 6, 211, 256 }, { 5, 212, 256 }, { 6, 213, 256 }, { 6, 214, 256 }, { 7, 215, 256 },
  10382. { 5, 216, 256 }, { 6, 217, 256 }, { 6, 218, 256 }, { 7, 219, 256 }, { 6, 220, 256 }, { 7, 221, 256 }, { 7, 222, 256 }, { 8, 223, 256 },
  10383. { 4, 224, 256 }, { 5, 225, 256 }, { 5, 226, 256 }, { 6, 227, 256 }, { 5, 228, 256 }, { 6, 229, 256 }, { 6, 230, 256 }, { 7, 231, 256 },
  10384. { 5, 232, 256 }, { 6, 233, 256 }, { 6, 234, 256 }, { 7, 235, 256 }, { 6, 236, 256 }, { 7, 237, 256 }, { 7, 238, 256 }, { 8, 239, 256 },
  10385. { 5, 240, 256 }, { 6, 241, 256 }, { 6, 242, 256 }, { 7, 243, 256 }, { 6, 244, 256 }, { 7, 245, 256 }, { 7, 246, 256 }, { 8, 247, 256 },
  10386. { 6, 248, 256 }, { 7, 249, 256 }, { 7, 250, 256 }, { 8, 251, 256 }, { 7, 252, 256 }, { 8, 253, 256 }, { 8, 254, 256 }, { 9, 255, 256 },
  10387. #if FP_LUT > 9
  10388. { 1, 0, 0 }, { 2, 1, 512 }, { 2, 2, 512 }, { 3, 3, 512 }, { 2, 4, 512 }, { 3, 5, 512 }, { 3, 6, 512 }, { 4, 7, 512 },
  10389. { 2, 8, 512 }, { 3, 9, 512 }, { 3, 10, 512 }, { 4, 11, 512 }, { 3, 12, 512 }, { 4, 13, 512 }, { 4, 14, 512 }, { 5, 15, 512 },
  10390. { 2, 16, 512 }, { 3, 17, 512 }, { 3, 18, 512 }, { 4, 19, 512 }, { 3, 20, 512 }, { 4, 21, 512 }, { 4, 22, 512 }, { 5, 23, 512 },
  10391. { 3, 24, 512 }, { 4, 25, 512 }, { 4, 26, 512 }, { 5, 27, 512 }, { 4, 28, 512 }, { 5, 29, 512 }, { 5, 30, 512 }, { 6, 31, 512 },
  10392. { 2, 32, 512 }, { 3, 33, 512 }, { 3, 34, 512 }, { 4, 35, 512 }, { 3, 36, 512 }, { 4, 37, 512 }, { 4, 38, 512 }, { 5, 39, 512 },
  10393. { 3, 40, 512 }, { 4, 41, 512 }, { 4, 42, 512 }, { 5, 43, 512 }, { 4, 44, 512 }, { 5, 45, 512 }, { 5, 46, 512 }, { 6, 47, 512 },
  10394. { 3, 48, 512 }, { 4, 49, 512 }, { 4, 50, 512 }, { 5, 51, 512 }, { 4, 52, 512 }, { 5, 53, 512 }, { 5, 54, 512 }, { 6, 55, 512 },
  10395. { 4, 56, 512 }, { 5, 57, 512 }, { 5, 58, 512 }, { 6, 59, 512 }, { 5, 60, 512 }, { 6, 61, 512 }, { 6, 62, 512 }, { 7, 63, 512 },
  10396. { 2, 64, 512 }, { 3, 65, 512 }, { 3, 66, 512 }, { 4, 67, 512 }, { 3, 68, 512 }, { 4, 69, 512 }, { 4, 70, 512 }, { 5, 71, 512 },
  10397. { 3, 72, 512 }, { 4, 73, 512 }, { 4, 74, 512 }, { 5, 75, 512 }, { 4, 76, 512 }, { 5, 77, 512 }, { 5, 78, 512 }, { 6, 79, 512 },
  10398. { 3, 80, 512 }, { 4, 81, 512 }, { 4, 82, 512 }, { 5, 83, 512 }, { 4, 84, 512 }, { 5, 85, 512 }, { 5, 86, 512 }, { 6, 87, 512 },
  10399. { 4, 88, 512 }, { 5, 89, 512 }, { 5, 90, 512 }, { 6, 91, 512 }, { 5, 92, 512 }, { 6, 93, 512 }, { 6, 94, 512 }, { 7, 95, 512 },
  10400. { 3, 96, 512 }, { 4, 97, 512 }, { 4, 98, 512 }, { 5, 99, 512 }, { 4, 100, 512 }, { 5, 101, 512 }, { 5, 102, 512 }, { 6, 103, 512 },
  10401. { 4, 104, 512 }, { 5, 105, 512 }, { 5, 106, 512 }, { 6, 107, 512 }, { 5, 108, 512 }, { 6, 109, 512 }, { 6, 110, 512 }, { 7, 111, 512 },
  10402. { 4, 112, 512 }, { 5, 113, 512 }, { 5, 114, 512 }, { 6, 115, 512 }, { 5, 116, 512 }, { 6, 117, 512 }, { 6, 118, 512 }, { 7, 119, 512 },
  10403. { 5, 120, 512 }, { 6, 121, 512 }, { 6, 122, 512 }, { 7, 123, 512 }, { 6, 124, 512 }, { 7, 125, 512 }, { 7, 126, 512 }, { 8, 127, 512 },
  10404. { 2, 128, 512 }, { 3, 129, 512 }, { 3, 130, 512 }, { 4, 131, 512 }, { 3, 132, 512 }, { 4, 133, 512 }, { 4, 134, 512 }, { 5, 135, 512 },
  10405. { 3, 136, 512 }, { 4, 137, 512 }, { 4, 138, 512 }, { 5, 139, 512 }, { 4, 140, 512 }, { 5, 141, 512 }, { 5, 142, 512 }, { 6, 143, 512 },
  10406. { 3, 144, 512 }, { 4, 145, 512 }, { 4, 146, 512 }, { 5, 147, 512 }, { 4, 148, 512 }, { 5, 149, 512 }, { 5, 150, 512 }, { 6, 151, 512 },
  10407. { 4, 152, 512 }, { 5, 153, 512 }, { 5, 154, 512 }, { 6, 155, 512 }, { 5, 156, 512 }, { 6, 157, 512 }, { 6, 158, 512 }, { 7, 159, 512 },
  10408. { 3, 160, 512 }, { 4, 161, 512 }, { 4, 162, 512 }, { 5, 163, 512 }, { 4, 164, 512 }, { 5, 165, 512 }, { 5, 166, 512 }, { 6, 167, 512 },
  10409. { 4, 168, 512 }, { 5, 169, 512 }, { 5, 170, 512 }, { 6, 171, 512 }, { 5, 172, 512 }, { 6, 173, 512 }, { 6, 174, 512 }, { 7, 175, 512 },
  10410. { 4, 176, 512 }, { 5, 177, 512 }, { 5, 178, 512 }, { 6, 179, 512 }, { 5, 180, 512 }, { 6, 181, 512 }, { 6, 182, 512 }, { 7, 183, 512 },
  10411. { 5, 184, 512 }, { 6, 185, 512 }, { 6, 186, 512 }, { 7, 187, 512 }, { 6, 188, 512 }, { 7, 189, 512 }, { 7, 190, 512 }, { 8, 191, 512 },
  10412. { 3, 192, 512 }, { 4, 193, 512 }, { 4, 194, 512 }, { 5, 195, 512 }, { 4, 196, 512 }, { 5, 197, 512 }, { 5, 198, 512 }, { 6, 199, 512 },
  10413. { 4, 200, 512 }, { 5, 201, 512 }, { 5, 202, 512 }, { 6, 203, 512 }, { 5, 204, 512 }, { 6, 205, 512 }, { 6, 206, 512 }, { 7, 207, 512 },
  10414. { 4, 208, 512 }, { 5, 209, 512 }, { 5, 210, 512 }, { 6, 211, 512 }, { 5, 212, 512 }, { 6, 213, 512 }, { 6, 214, 512 }, { 7, 215, 512 },
  10415. { 5, 216, 512 }, { 6, 217, 512 }, { 6, 218, 512 }, { 7, 219, 512 }, { 6, 220, 512 }, { 7, 221, 512 }, { 7, 222, 512 }, { 8, 223, 512 },
  10416. { 4, 224, 512 }, { 5, 225, 512 }, { 5, 226, 512 }, { 6, 227, 512 }, { 5, 228, 512 }, { 6, 229, 512 }, { 6, 230, 512 }, { 7, 231, 512 },
  10417. { 5, 232, 512 }, { 6, 233, 512 }, { 6, 234, 512 }, { 7, 235, 512 }, { 6, 236, 512 }, { 7, 237, 512 }, { 7, 238, 512 }, { 8, 239, 512 },
  10418. { 5, 240, 512 }, { 6, 241, 512 }, { 6, 242, 512 }, { 7, 243, 512 }, { 6, 244, 512 }, { 7, 245, 512 }, { 7, 246, 512 }, { 8, 247, 512 },
  10419. { 6, 248, 512 }, { 7, 249, 512 }, { 7, 250, 512 }, { 8, 251, 512 }, { 7, 252, 512 }, { 8, 253, 512 }, { 8, 254, 512 }, { 9, 255, 512 },
  10420. { 2, 256, 512 }, { 3, 257, 512 }, { 3, 258, 512 }, { 4, 259, 512 }, { 3, 260, 512 }, { 4, 261, 512 }, { 4, 262, 512 }, { 5, 263, 512 },
  10421. { 3, 264, 512 }, { 4, 265, 512 }, { 4, 266, 512 }, { 5, 267, 512 }, { 4, 268, 512 }, { 5, 269, 512 }, { 5, 270, 512 }, { 6, 271, 512 },
  10422. { 3, 272, 512 }, { 4, 273, 512 }, { 4, 274, 512 }, { 5, 275, 512 }, { 4, 276, 512 }, { 5, 277, 512 }, { 5, 278, 512 }, { 6, 279, 512 },
  10423. { 4, 280, 512 }, { 5, 281, 512 }, { 5, 282, 512 }, { 6, 283, 512 }, { 5, 284, 512 }, { 6, 285, 512 }, { 6, 286, 512 }, { 7, 287, 512 },
  10424. { 3, 288, 512 }, { 4, 289, 512 }, { 4, 290, 512 }, { 5, 291, 512 }, { 4, 292, 512 }, { 5, 293, 512 }, { 5, 294, 512 }, { 6, 295, 512 },
  10425. { 4, 296, 512 }, { 5, 297, 512 }, { 5, 298, 512 }, { 6, 299, 512 }, { 5, 300, 512 }, { 6, 301, 512 }, { 6, 302, 512 }, { 7, 303, 512 },
  10426. { 4, 304, 512 }, { 5, 305, 512 }, { 5, 306, 512 }, { 6, 307, 512 }, { 5, 308, 512 }, { 6, 309, 512 }, { 6, 310, 512 }, { 7, 311, 512 },
  10427. { 5, 312, 512 }, { 6, 313, 512 }, { 6, 314, 512 }, { 7, 315, 512 }, { 6, 316, 512 }, { 7, 317, 512 }, { 7, 318, 512 }, { 8, 319, 512 },
  10428. { 3, 320, 512 }, { 4, 321, 512 }, { 4, 322, 512 }, { 5, 323, 512 }, { 4, 324, 512 }, { 5, 325, 512 }, { 5, 326, 512 }, { 6, 327, 512 },
  10429. { 4, 328, 512 }, { 5, 329, 512 }, { 5, 330, 512 }, { 6, 331, 512 }, { 5, 332, 512 }, { 6, 333, 512 }, { 6, 334, 512 }, { 7, 335, 512 },
  10430. { 4, 336, 512 }, { 5, 337, 512 }, { 5, 338, 512 }, { 6, 339, 512 }, { 5, 340, 512 }, { 6, 341, 512 }, { 6, 342, 512 }, { 7, 343, 512 },
  10431. { 5, 344, 512 }, { 6, 345, 512 }, { 6, 346, 512 }, { 7, 347, 512 }, { 6, 348, 512 }, { 7, 349, 512 }, { 7, 350, 512 }, { 8, 351, 512 },
  10432. { 4, 352, 512 }, { 5, 353, 512 }, { 5, 354, 512 }, { 6, 355, 512 }, { 5, 356, 512 }, { 6, 357, 512 }, { 6, 358, 512 }, { 7, 359, 512 },
  10433. { 5, 360, 512 }, { 6, 361, 512 }, { 6, 362, 512 }, { 7, 363, 512 }, { 6, 364, 512 }, { 7, 365, 512 }, { 7, 366, 512 }, { 8, 367, 512 },
  10434. { 5, 368, 512 }, { 6, 369, 512 }, { 6, 370, 512 }, { 7, 371, 512 }, { 6, 372, 512 }, { 7, 373, 512 }, { 7, 374, 512 }, { 8, 375, 512 },
  10435. { 6, 376, 512 }, { 7, 377, 512 }, { 7, 378, 512 }, { 8, 379, 512 }, { 7, 380, 512 }, { 8, 381, 512 }, { 8, 382, 512 }, { 9, 383, 512 },
  10436. { 3, 384, 512 }, { 4, 385, 512 }, { 4, 386, 512 }, { 5, 387, 512 }, { 4, 388, 512 }, { 5, 389, 512 }, { 5, 390, 512 }, { 6, 391, 512 },
  10437. { 4, 392, 512 }, { 5, 393, 512 }, { 5, 394, 512 }, { 6, 395, 512 }, { 5, 396, 512 }, { 6, 397, 512 }, { 6, 398, 512 }, { 7, 399, 512 },
  10438. { 4, 400, 512 }, { 5, 401, 512 }, { 5, 402, 512 }, { 6, 403, 512 }, { 5, 404, 512 }, { 6, 405, 512 }, { 6, 406, 512 }, { 7, 407, 512 },
  10439. { 5, 408, 512 }, { 6, 409, 512 }, { 6, 410, 512 }, { 7, 411, 512 }, { 6, 412, 512 }, { 7, 413, 512 }, { 7, 414, 512 }, { 8, 415, 512 },
  10440. { 4, 416, 512 }, { 5, 417, 512 }, { 5, 418, 512 }, { 6, 419, 512 }, { 5, 420, 512 }, { 6, 421, 512 }, { 6, 422, 512 }, { 7, 423, 512 },
  10441. { 5, 424, 512 }, { 6, 425, 512 }, { 6, 426, 512 }, { 7, 427, 512 }, { 6, 428, 512 }, { 7, 429, 512 }, { 7, 430, 512 }, { 8, 431, 512 },
  10442. { 5, 432, 512 }, { 6, 433, 512 }, { 6, 434, 512 }, { 7, 435, 512 }, { 6, 436, 512 }, { 7, 437, 512 }, { 7, 438, 512 }, { 8, 439, 512 },
  10443. { 6, 440, 512 }, { 7, 441, 512 }, { 7, 442, 512 }, { 8, 443, 512 }, { 7, 444, 512 }, { 8, 445, 512 }, { 8, 446, 512 }, { 9, 447, 512 },
  10444. { 4, 448, 512 }, { 5, 449, 512 }, { 5, 450, 512 }, { 6, 451, 512 }, { 5, 452, 512 }, { 6, 453, 512 }, { 6, 454, 512 }, { 7, 455, 512 },
  10445. { 5, 456, 512 }, { 6, 457, 512 }, { 6, 458, 512 }, { 7, 459, 512 }, { 6, 460, 512 }, { 7, 461, 512 }, { 7, 462, 512 }, { 8, 463, 512 },
  10446. { 5, 464, 512 }, { 6, 465, 512 }, { 6, 466, 512 }, { 7, 467, 512 }, { 6, 468, 512 }, { 7, 469, 512 }, { 7, 470, 512 }, { 8, 471, 512 },
  10447. { 6, 472, 512 }, { 7, 473, 512 }, { 7, 474, 512 }, { 8, 475, 512 }, { 7, 476, 512 }, { 8, 477, 512 }, { 8, 478, 512 }, { 9, 479, 512 },
  10448. { 5, 480, 512 }, { 6, 481, 512 }, { 6, 482, 512 }, { 7, 483, 512 }, { 6, 484, 512 }, { 7, 485, 512 }, { 7, 486, 512 }, { 8, 487, 512 },
  10449. { 6, 488, 512 }, { 7, 489, 512 }, { 7, 490, 512 }, { 8, 491, 512 }, { 7, 492, 512 }, { 8, 493, 512 }, { 8, 494, 512 }, { 9, 495, 512 },
  10450. { 6, 496, 512 }, { 7, 497, 512 }, { 7, 498, 512 }, { 8, 499, 512 }, { 7, 500, 512 }, { 8, 501, 512 }, { 8, 502, 512 }, { 9, 503, 512 },
  10451. { 7, 504, 512 }, { 8, 505, 512 }, { 8, 506, 512 }, { 9, 507, 512 }, { 8, 508, 512 }, { 9, 509, 512 }, { 9, 510, 512 }, { 10, 511, 512 },
  10452. #if FP_LUT > 10
  10453. { 1, 0, 0 }, { 2, 1, 1024 }, { 2, 2, 1024 }, { 3, 3, 1024 }, { 2, 4, 1024 }, { 3, 5, 1024 }, { 3, 6, 1024 }, { 4, 7, 1024 },
  10454. { 2, 8, 1024 }, { 3, 9, 1024 }, { 3, 10, 1024 }, { 4, 11, 1024 }, { 3, 12, 1024 }, { 4, 13, 1024 }, { 4, 14, 1024 }, { 5, 15, 1024 },
  10455. { 2, 16, 1024 }, { 3, 17, 1024 }, { 3, 18, 1024 }, { 4, 19, 1024 }, { 3, 20, 1024 }, { 4, 21, 1024 }, { 4, 22, 1024 }, { 5, 23, 1024 },
  10456. { 3, 24, 1024 }, { 4, 25, 1024 }, { 4, 26, 1024 }, { 5, 27, 1024 }, { 4, 28, 1024 }, { 5, 29, 1024 }, { 5, 30, 1024 }, { 6, 31, 1024 },
  10457. { 2, 32, 1024 }, { 3, 33, 1024 }, { 3, 34, 1024 }, { 4, 35, 1024 }, { 3, 36, 1024 }, { 4, 37, 1024 }, { 4, 38, 1024 }, { 5, 39, 1024 },
  10458. { 3, 40, 1024 }, { 4, 41, 1024 }, { 4, 42, 1024 }, { 5, 43, 1024 }, { 4, 44, 1024 }, { 5, 45, 1024 }, { 5, 46, 1024 }, { 6, 47, 1024 },
  10459. { 3, 48, 1024 }, { 4, 49, 1024 }, { 4, 50, 1024 }, { 5, 51, 1024 }, { 4, 52, 1024 }, { 5, 53, 1024 }, { 5, 54, 1024 }, { 6, 55, 1024 },
  10460. { 4, 56, 1024 }, { 5, 57, 1024 }, { 5, 58, 1024 }, { 6, 59, 1024 }, { 5, 60, 1024 }, { 6, 61, 1024 }, { 6, 62, 1024 }, { 7, 63, 1024 },
  10461. { 2, 64, 1024 }, { 3, 65, 1024 }, { 3, 66, 1024 }, { 4, 67, 1024 }, { 3, 68, 1024 }, { 4, 69, 1024 }, { 4, 70, 1024 }, { 5, 71, 1024 },
  10462. { 3, 72, 1024 }, { 4, 73, 1024 }, { 4, 74, 1024 }, { 5, 75, 1024 }, { 4, 76, 1024 }, { 5, 77, 1024 }, { 5, 78, 1024 }, { 6, 79, 1024 },
  10463. { 3, 80, 1024 }, { 4, 81, 1024 }, { 4, 82, 1024 }, { 5, 83, 1024 }, { 4, 84, 1024 }, { 5, 85, 1024 }, { 5, 86, 1024 }, { 6, 87, 1024 },
  10464. { 4, 88, 1024 }, { 5, 89, 1024 }, { 5, 90, 1024 }, { 6, 91, 1024 }, { 5, 92, 1024 }, { 6, 93, 1024 }, { 6, 94, 1024 }, { 7, 95, 1024 },
  10465. { 3, 96, 1024 }, { 4, 97, 1024 }, { 4, 98, 1024 }, { 5, 99, 1024 }, { 4, 100, 1024 }, { 5, 101, 1024 }, { 5, 102, 1024 }, { 6, 103, 1024 },
  10466. { 4, 104, 1024 }, { 5, 105, 1024 }, { 5, 106, 1024 }, { 6, 107, 1024 }, { 5, 108, 1024 }, { 6, 109, 1024 }, { 6, 110, 1024 }, { 7, 111, 1024 },
  10467. { 4, 112, 1024 }, { 5, 113, 1024 }, { 5, 114, 1024 }, { 6, 115, 1024 }, { 5, 116, 1024 }, { 6, 117, 1024 }, { 6, 118, 1024 }, { 7, 119, 1024 },
  10468. { 5, 120, 1024 }, { 6, 121, 1024 }, { 6, 122, 1024 }, { 7, 123, 1024 }, { 6, 124, 1024 }, { 7, 125, 1024 }, { 7, 126, 1024 }, { 8, 127, 1024 },
  10469. { 2, 128, 1024 }, { 3, 129, 1024 }, { 3, 130, 1024 }, { 4, 131, 1024 }, { 3, 132, 1024 }, { 4, 133, 1024 }, { 4, 134, 1024 }, { 5, 135, 1024 },
  10470. { 3, 136, 1024 }, { 4, 137, 1024 }, { 4, 138, 1024 }, { 5, 139, 1024 }, { 4, 140, 1024 }, { 5, 141, 1024 }, { 5, 142, 1024 }, { 6, 143, 1024 },
  10471. { 3, 144, 1024 }, { 4, 145, 1024 }, { 4, 146, 1024 }, { 5, 147, 1024 }, { 4, 148, 1024 }, { 5, 149, 1024 }, { 5, 150, 1024 }, { 6, 151, 1024 },
  10472. { 4, 152, 1024 }, { 5, 153, 1024 }, { 5, 154, 1024 }, { 6, 155, 1024 }, { 5, 156, 1024 }, { 6, 157, 1024 }, { 6, 158, 1024 }, { 7, 159, 1024 },
  10473. { 3, 160, 1024 }, { 4, 161, 1024 }, { 4, 162, 1024 }, { 5, 163, 1024 }, { 4, 164, 1024 }, { 5, 165, 1024 }, { 5, 166, 1024 }, { 6, 167, 1024 },
  10474. { 4, 168, 1024 }, { 5, 169, 1024 }, { 5, 170, 1024 }, { 6, 171, 1024 }, { 5, 172, 1024 }, { 6, 173, 1024 }, { 6, 174, 1024 }, { 7, 175, 1024 },
  10475. { 4, 176, 1024 }, { 5, 177, 1024 }, { 5, 178, 1024 }, { 6, 179, 1024 }, { 5, 180, 1024 }, { 6, 181, 1024 }, { 6, 182, 1024 }, { 7, 183, 1024 },
  10476. { 5, 184, 1024 }, { 6, 185, 1024 }, { 6, 186, 1024 }, { 7, 187, 1024 }, { 6, 188, 1024 }, { 7, 189, 1024 }, { 7, 190, 1024 }, { 8, 191, 1024 },
  10477. { 3, 192, 1024 }, { 4, 193, 1024 }, { 4, 194, 1024 }, { 5, 195, 1024 }, { 4, 196, 1024 }, { 5, 197, 1024 }, { 5, 198, 1024 }, { 6, 199, 1024 },
  10478. { 4, 200, 1024 }, { 5, 201, 1024 }, { 5, 202, 1024 }, { 6, 203, 1024 }, { 5, 204, 1024 }, { 6, 205, 1024 }, { 6, 206, 1024 }, { 7, 207, 1024 },
  10479. { 4, 208, 1024 }, { 5, 209, 1024 }, { 5, 210, 1024 }, { 6, 211, 1024 }, { 5, 212, 1024 }, { 6, 213, 1024 }, { 6, 214, 1024 }, { 7, 215, 1024 },
  10480. { 5, 216, 1024 }, { 6, 217, 1024 }, { 6, 218, 1024 }, { 7, 219, 1024 }, { 6, 220, 1024 }, { 7, 221, 1024 }, { 7, 222, 1024 }, { 8, 223, 1024 },
  10481. { 4, 224, 1024 }, { 5, 225, 1024 }, { 5, 226, 1024 }, { 6, 227, 1024 }, { 5, 228, 1024 }, { 6, 229, 1024 }, { 6, 230, 1024 }, { 7, 231, 1024 },
  10482. { 5, 232, 1024 }, { 6, 233, 1024 }, { 6, 234, 1024 }, { 7, 235, 1024 }, { 6, 236, 1024 }, { 7, 237, 1024 }, { 7, 238, 1024 }, { 8, 239, 1024 },
  10483. { 5, 240, 1024 }, { 6, 241, 1024 }, { 6, 242, 1024 }, { 7, 243, 1024 }, { 6, 244, 1024 }, { 7, 245, 1024 }, { 7, 246, 1024 }, { 8, 247, 1024 },
  10484. { 6, 248, 1024 }, { 7, 249, 1024 }, { 7, 250, 1024 }, { 8, 251, 1024 }, { 7, 252, 1024 }, { 8, 253, 1024 }, { 8, 254, 1024 }, { 9, 255, 1024 },
  10485. { 2, 256, 1024 }, { 3, 257, 1024 }, { 3, 258, 1024 }, { 4, 259, 1024 }, { 3, 260, 1024 }, { 4, 261, 1024 }, { 4, 262, 1024 }, { 5, 263, 1024 },
  10486. { 3, 264, 1024 }, { 4, 265, 1024 }, { 4, 266, 1024 }, { 5, 267, 1024 }, { 4, 268, 1024 }, { 5, 269, 1024 }, { 5, 270, 1024 }, { 6, 271, 1024 },
  10487. { 3, 272, 1024 }, { 4, 273, 1024 }, { 4, 274, 1024 }, { 5, 275, 1024 }, { 4, 276, 1024 }, { 5, 277, 1024 }, { 5, 278, 1024 }, { 6, 279, 1024 },
  10488. { 4, 280, 1024 }, { 5, 281, 1024 }, { 5, 282, 1024 }, { 6, 283, 1024 }, { 5, 284, 1024 }, { 6, 285, 1024 }, { 6, 286, 1024 }, { 7, 287, 1024 },
  10489. { 3, 288, 1024 }, { 4, 289, 1024 }, { 4, 290, 1024 }, { 5, 291, 1024 }, { 4, 292, 1024 }, { 5, 293, 1024 }, { 5, 294, 1024 }, { 6, 295, 1024 },
  10490. { 4, 296, 1024 }, { 5, 297, 1024 }, { 5, 298, 1024 }, { 6, 299, 1024 }, { 5, 300, 1024 }, { 6, 301, 1024 }, { 6, 302, 1024 }, { 7, 303, 1024 },
  10491. { 4, 304, 1024 }, { 5, 305, 1024 }, { 5, 306, 1024 }, { 6, 307, 1024 }, { 5, 308, 1024 }, { 6, 309, 1024 }, { 6, 310, 1024 }, { 7, 311, 1024 },
  10492. { 5, 312, 1024 }, { 6, 313, 1024 }, { 6, 314, 1024 }, { 7, 315, 1024 }, { 6, 316, 1024 }, { 7, 317, 1024 }, { 7, 318, 1024 }, { 8, 319, 1024 },
  10493. { 3, 320, 1024 }, { 4, 321, 1024 }, { 4, 322, 1024 }, { 5, 323, 1024 }, { 4, 324, 1024 }, { 5, 325, 1024 }, { 5, 326, 1024 }, { 6, 327, 1024 },
  10494. { 4, 328, 1024 }, { 5, 329, 1024 }, { 5, 330, 1024 }, { 6, 331, 1024 }, { 5, 332, 1024 }, { 6, 333, 1024 }, { 6, 334, 1024 }, { 7, 335, 1024 },
  10495. { 4, 336, 1024 }, { 5, 337, 1024 }, { 5, 338, 1024 }, { 6, 339, 1024 }, { 5, 340, 1024 }, { 6, 341, 1024 }, { 6, 342, 1024 }, { 7, 343, 1024 },
  10496. { 5, 344, 1024 }, { 6, 345, 1024 }, { 6, 346, 1024 }, { 7, 347, 1024 }, { 6, 348, 1024 }, { 7, 349, 1024 }, { 7, 350, 1024 }, { 8, 351, 1024 },
  10497. { 4, 352, 1024 }, { 5, 353, 1024 }, { 5, 354, 1024 }, { 6, 355, 1024 }, { 5, 356, 1024 }, { 6, 357, 1024 }, { 6, 358, 1024 }, { 7, 359, 1024 },
  10498. { 5, 360, 1024 }, { 6, 361, 1024 }, { 6, 362, 1024 }, { 7, 363, 1024 }, { 6, 364, 1024 }, { 7, 365, 1024 }, { 7, 366, 1024 }, { 8, 367, 1024 },
  10499. { 5, 368, 1024 }, { 6, 369, 1024 }, { 6, 370, 1024 }, { 7, 371, 1024 }, { 6, 372, 1024 }, { 7, 373, 1024 }, { 7, 374, 1024 }, { 8, 375, 1024 },
  10500. { 6, 376, 1024 }, { 7, 377, 1024 }, { 7, 378, 1024 }, { 8, 379, 1024 }, { 7, 380, 1024 }, { 8, 381, 1024 }, { 8, 382, 1024 }, { 9, 383, 1024 },
  10501. { 3, 384, 1024 }, { 4, 385, 1024 }, { 4, 386, 1024 }, { 5, 387, 1024 }, { 4, 388, 1024 }, { 5, 389, 1024 }, { 5, 390, 1024 }, { 6, 391, 1024 },
  10502. { 4, 392, 1024 }, { 5, 393, 1024 }, { 5, 394, 1024 }, { 6, 395, 1024 }, { 5, 396, 1024 }, { 6, 397, 1024 }, { 6, 398, 1024 }, { 7, 399, 1024 },
  10503. { 4, 400, 1024 }, { 5, 401, 1024 }, { 5, 402, 1024 }, { 6, 403, 1024 }, { 5, 404, 1024 }, { 6, 405, 1024 }, { 6, 406, 1024 }, { 7, 407, 1024 },
  10504. { 5, 408, 1024 }, { 6, 409, 1024 }, { 6, 410, 1024 }, { 7, 411, 1024 }, { 6, 412, 1024 }, { 7, 413, 1024 }, { 7, 414, 1024 }, { 8, 415, 1024 },
  10505. { 4, 416, 1024 }, { 5, 417, 1024 }, { 5, 418, 1024 }, { 6, 419, 1024 }, { 5, 420, 1024 }, { 6, 421, 1024 }, { 6, 422, 1024 }, { 7, 423, 1024 },
  10506. { 5, 424, 1024 }, { 6, 425, 1024 }, { 6, 426, 1024 }, { 7, 427, 1024 }, { 6, 428, 1024 }, { 7, 429, 1024 }, { 7, 430, 1024 }, { 8, 431, 1024 },
  10507. { 5, 432, 1024 }, { 6, 433, 1024 }, { 6, 434, 1024 }, { 7, 435, 1024 }, { 6, 436, 1024 }, { 7, 437, 1024 }, { 7, 438, 1024 }, { 8, 439, 1024 },
  10508. { 6, 440, 1024 }, { 7, 441, 1024 }, { 7, 442, 1024 }, { 8, 443, 1024 }, { 7, 444, 1024 }, { 8, 445, 1024 }, { 8, 446, 1024 }, { 9, 447, 1024 },
  10509. { 4, 448, 1024 }, { 5, 449, 1024 }, { 5, 450, 1024 }, { 6, 451, 1024 }, { 5, 452, 1024 }, { 6, 453, 1024 }, { 6, 454, 1024 }, { 7, 455, 1024 },
  10510. { 5, 456, 1024 }, { 6, 457, 1024 }, { 6, 458, 1024 }, { 7, 459, 1024 }, { 6, 460, 1024 }, { 7, 461, 1024 }, { 7, 462, 1024 }, { 8, 463, 1024 },
  10511. { 5, 464, 1024 }, { 6, 465, 1024 }, { 6, 466, 1024 }, { 7, 467, 1024 }, { 6, 468, 1024 }, { 7, 469, 1024 }, { 7, 470, 1024 }, { 8, 471, 1024 },
  10512. { 6, 472, 1024 }, { 7, 473, 1024 }, { 7, 474, 1024 }, { 8, 475, 1024 }, { 7, 476, 1024 }, { 8, 477, 1024 }, { 8, 478, 1024 }, { 9, 479, 1024 },
  10513. { 5, 480, 1024 }, { 6, 481, 1024 }, { 6, 482, 1024 }, { 7, 483, 1024 }, { 6, 484, 1024 }, { 7, 485, 1024 }, { 7, 486, 1024 }, { 8, 487, 1024 },
  10514. { 6, 488, 1024 }, { 7, 489, 1024 }, { 7, 490, 1024 }, { 8, 491, 1024 }, { 7, 492, 1024 }, { 8, 493, 1024 }, { 8, 494, 1024 }, { 9, 495, 1024 },
  10515. { 6, 496, 1024 }, { 7, 497, 1024 }, { 7, 498, 1024 }, { 8, 499, 1024 }, { 7, 500, 1024 }, { 8, 501, 1024 }, { 8, 502, 1024 }, { 9, 503, 1024 },
  10516. { 7, 504, 1024 }, { 8, 505, 1024 }, { 8, 506, 1024 }, { 9, 507, 1024 }, { 8, 508, 1024 }, { 9, 509, 1024 }, { 9, 510, 1024 }, { 10, 511, 1024 },
  10517. { 2, 512, 1024 }, { 3, 513, 1024 }, { 3, 514, 1024 }, { 4, 515, 1024 }, { 3, 516, 1024 }, { 4, 517, 1024 }, { 4, 518, 1024 }, { 5, 519, 1024 },
  10518. { 3, 520, 1024 }, { 4, 521, 1024 }, { 4, 522, 1024 }, { 5, 523, 1024 }, { 4, 524, 1024 }, { 5, 525, 1024 }, { 5, 526, 1024 }, { 6, 527, 1024 },
  10519. { 3, 528, 1024 }, { 4, 529, 1024 }, { 4, 530, 1024 }, { 5, 531, 1024 }, { 4, 532, 1024 }, { 5, 533, 1024 }, { 5, 534, 1024 }, { 6, 535, 1024 },
  10520. { 4, 536, 1024 }, { 5, 537, 1024 }, { 5, 538, 1024 }, { 6, 539, 1024 }, { 5, 540, 1024 }, { 6, 541, 1024 }, { 6, 542, 1024 }, { 7, 543, 1024 },
  10521. { 3, 544, 1024 }, { 4, 545, 1024 }, { 4, 546, 1024 }, { 5, 547, 1024 }, { 4, 548, 1024 }, { 5, 549, 1024 }, { 5, 550, 1024 }, { 6, 551, 1024 },
  10522. { 4, 552, 1024 }, { 5, 553, 1024 }, { 5, 554, 1024 }, { 6, 555, 1024 }, { 5, 556, 1024 }, { 6, 557, 1024 }, { 6, 558, 1024 }, { 7, 559, 1024 },
  10523. { 4, 560, 1024 }, { 5, 561, 1024 }, { 5, 562, 1024 }, { 6, 563, 1024 }, { 5, 564, 1024 }, { 6, 565, 1024 }, { 6, 566, 1024 }, { 7, 567, 1024 },
  10524. { 5, 568, 1024 }, { 6, 569, 1024 }, { 6, 570, 1024 }, { 7, 571, 1024 }, { 6, 572, 1024 }, { 7, 573, 1024 }, { 7, 574, 1024 }, { 8, 575, 1024 },
  10525. { 3, 576, 1024 }, { 4, 577, 1024 }, { 4, 578, 1024 }, { 5, 579, 1024 }, { 4, 580, 1024 }, { 5, 581, 1024 }, { 5, 582, 1024 }, { 6, 583, 1024 },
  10526. { 4, 584, 1024 }, { 5, 585, 1024 }, { 5, 586, 1024 }, { 6, 587, 1024 }, { 5, 588, 1024 }, { 6, 589, 1024 }, { 6, 590, 1024 }, { 7, 591, 1024 },
  10527. { 4, 592, 1024 }, { 5, 593, 1024 }, { 5, 594, 1024 }, { 6, 595, 1024 }, { 5, 596, 1024 }, { 6, 597, 1024 }, { 6, 598, 1024 }, { 7, 599, 1024 },
  10528. { 5, 600, 1024 }, { 6, 601, 1024 }, { 6, 602, 1024 }, { 7, 603, 1024 }, { 6, 604, 1024 }, { 7, 605, 1024 }, { 7, 606, 1024 }, { 8, 607, 1024 },
  10529. { 4, 608, 1024 }, { 5, 609, 1024 }, { 5, 610, 1024 }, { 6, 611, 1024 }, { 5, 612, 1024 }, { 6, 613, 1024 }, { 6, 614, 1024 }, { 7, 615, 1024 },
  10530. { 5, 616, 1024 }, { 6, 617, 1024 }, { 6, 618, 1024 }, { 7, 619, 1024 }, { 6, 620, 1024 }, { 7, 621, 1024 }, { 7, 622, 1024 }, { 8, 623, 1024 },
  10531. { 5, 624, 1024 }, { 6, 625, 1024 }, { 6, 626, 1024 }, { 7, 627, 1024 }, { 6, 628, 1024 }, { 7, 629, 1024 }, { 7, 630, 1024 }, { 8, 631, 1024 },
  10532. { 6, 632, 1024 }, { 7, 633, 1024 }, { 7, 634, 1024 }, { 8, 635, 1024 }, { 7, 636, 1024 }, { 8, 637, 1024 }, { 8, 638, 1024 }, { 9, 639, 1024 },
  10533. { 3, 640, 1024 }, { 4, 641, 1024 }, { 4, 642, 1024 }, { 5, 643, 1024 }, { 4, 644, 1024 }, { 5, 645, 1024 }, { 5, 646, 1024 }, { 6, 647, 1024 },
  10534. { 4, 648, 1024 }, { 5, 649, 1024 }, { 5, 650, 1024 }, { 6, 651, 1024 }, { 5, 652, 1024 }, { 6, 653, 1024 }, { 6, 654, 1024 }, { 7, 655, 1024 },
  10535. { 4, 656, 1024 }, { 5, 657, 1024 }, { 5, 658, 1024 }, { 6, 659, 1024 }, { 5, 660, 1024 }, { 6, 661, 1024 }, { 6, 662, 1024 }, { 7, 663, 1024 },
  10536. { 5, 664, 1024 }, { 6, 665, 1024 }, { 6, 666, 1024 }, { 7, 667, 1024 }, { 6, 668, 1024 }, { 7, 669, 1024 }, { 7, 670, 1024 }, { 8, 671, 1024 },
  10537. { 4, 672, 1024 }, { 5, 673, 1024 }, { 5, 674, 1024 }, { 6, 675, 1024 }, { 5, 676, 1024 }, { 6, 677, 1024 }, { 6, 678, 1024 }, { 7, 679, 1024 },
  10538. { 5, 680, 1024 }, { 6, 681, 1024 }, { 6, 682, 1024 }, { 7, 683, 1024 }, { 6, 684, 1024 }, { 7, 685, 1024 }, { 7, 686, 1024 }, { 8, 687, 1024 },
  10539. { 5, 688, 1024 }, { 6, 689, 1024 }, { 6, 690, 1024 }, { 7, 691, 1024 }, { 6, 692, 1024 }, { 7, 693, 1024 }, { 7, 694, 1024 }, { 8, 695, 1024 },
  10540. { 6, 696, 1024 }, { 7, 697, 1024 }, { 7, 698, 1024 }, { 8, 699, 1024 }, { 7, 700, 1024 }, { 8, 701, 1024 }, { 8, 702, 1024 }, { 9, 703, 1024 },
  10541. { 4, 704, 1024 }, { 5, 705, 1024 }, { 5, 706, 1024 }, { 6, 707, 1024 }, { 5, 708, 1024 }, { 6, 709, 1024 }, { 6, 710, 1024 }, { 7, 711, 1024 },
  10542. { 5, 712, 1024 }, { 6, 713, 1024 }, { 6, 714, 1024 }, { 7, 715, 1024 }, { 6, 716, 1024 }, { 7, 717, 1024 }, { 7, 718, 1024 }, { 8, 719, 1024 },
  10543. { 5, 720, 1024 }, { 6, 721, 1024 }, { 6, 722, 1024 }, { 7, 723, 1024 }, { 6, 724, 1024 }, { 7, 725, 1024 }, { 7, 726, 1024 }, { 8, 727, 1024 },
  10544. { 6, 728, 1024 }, { 7, 729, 1024 }, { 7, 730, 1024 }, { 8, 731, 1024 }, { 7, 732, 1024 }, { 8, 733, 1024 }, { 8, 734, 1024 }, { 9, 735, 1024 },
  10545. { 5, 736, 1024 }, { 6, 737, 1024 }, { 6, 738, 1024 }, { 7, 739, 1024 }, { 6, 740, 1024 }, { 7, 741, 1024 }, { 7, 742, 1024 }, { 8, 743, 1024 },
  10546. { 6, 744, 1024 }, { 7, 745, 1024 }, { 7, 746, 1024 }, { 8, 747, 1024 }, { 7, 748, 1024 }, { 8, 749, 1024 }, { 8, 750, 1024 }, { 9, 751, 1024 },
  10547. { 6, 752, 1024 }, { 7, 753, 1024 }, { 7, 754, 1024 }, { 8, 755, 1024 }, { 7, 756, 1024 }, { 8, 757, 1024 }, { 8, 758, 1024 }, { 9, 759, 1024 },
  10548. { 7, 760, 1024 }, { 8, 761, 1024 }, { 8, 762, 1024 }, { 9, 763, 1024 }, { 8, 764, 1024 }, { 9, 765, 1024 }, { 9, 766, 1024 }, { 10, 767, 1024 },
  10549. { 3, 768, 1024 }, { 4, 769, 1024 }, { 4, 770, 1024 }, { 5, 771, 1024 }, { 4, 772, 1024 }, { 5, 773, 1024 }, { 5, 774, 1024 }, { 6, 775, 1024 },
  10550. { 4, 776, 1024 }, { 5, 777, 1024 }, { 5, 778, 1024 }, { 6, 779, 1024 }, { 5, 780, 1024 }, { 6, 781, 1024 }, { 6, 782, 1024 }, { 7, 783, 1024 },
  10551. { 4, 784, 1024 }, { 5, 785, 1024 }, { 5, 786, 1024 }, { 6, 787, 1024 }, { 5, 788, 1024 }, { 6, 789, 1024 }, { 6, 790, 1024 }, { 7, 791, 1024 },
  10552. { 5, 792, 1024 }, { 6, 793, 1024 }, { 6, 794, 1024 }, { 7, 795, 1024 }, { 6, 796, 1024 }, { 7, 797, 1024 }, { 7, 798, 1024 }, { 8, 799, 1024 },
  10553. { 4, 800, 1024 }, { 5, 801, 1024 }, { 5, 802, 1024 }, { 6, 803, 1024 }, { 5, 804, 1024 }, { 6, 805, 1024 }, { 6, 806, 1024 }, { 7, 807, 1024 },
  10554. { 5, 808, 1024 }, { 6, 809, 1024 }, { 6, 810, 1024 }, { 7, 811, 1024 }, { 6, 812, 1024 }, { 7, 813, 1024 }, { 7, 814, 1024 }, { 8, 815, 1024 },
  10555. { 5, 816, 1024 }, { 6, 817, 1024 }, { 6, 818, 1024 }, { 7, 819, 1024 }, { 6, 820, 1024 }, { 7, 821, 1024 }, { 7, 822, 1024 }, { 8, 823, 1024 },
  10556. { 6, 824, 1024 }, { 7, 825, 1024 }, { 7, 826, 1024 }, { 8, 827, 1024 }, { 7, 828, 1024 }, { 8, 829, 1024 }, { 8, 830, 1024 }, { 9, 831, 1024 },
  10557. { 4, 832, 1024 }, { 5, 833, 1024 }, { 5, 834, 1024 }, { 6, 835, 1024 }, { 5, 836, 1024 }, { 6, 837, 1024 }, { 6, 838, 1024 }, { 7, 839, 1024 },
  10558. { 5, 840, 1024 }, { 6, 841, 1024 }, { 6, 842, 1024 }, { 7, 843, 1024 }, { 6, 844, 1024 }, { 7, 845, 1024 }, { 7, 846, 1024 }, { 8, 847, 1024 },
  10559. { 5, 848, 1024 }, { 6, 849, 1024 }, { 6, 850, 1024 }, { 7, 851, 1024 }, { 6, 852, 1024 }, { 7, 853, 1024 }, { 7, 854, 1024 }, { 8, 855, 1024 },
  10560. { 6, 856, 1024 }, { 7, 857, 1024 }, { 7, 858, 1024 }, { 8, 859, 1024 }, { 7, 860, 1024 }, { 8, 861, 1024 }, { 8, 862, 1024 }, { 9, 863, 1024 },
  10561. { 5, 864, 1024 }, { 6, 865, 1024 }, { 6, 866, 1024 }, { 7, 867, 1024 }, { 6, 868, 1024 }, { 7, 869, 1024 }, { 7, 870, 1024 }, { 8, 871, 1024 },
  10562. { 6, 872, 1024 }, { 7, 873, 1024 }, { 7, 874, 1024 }, { 8, 875, 1024 }, { 7, 876, 1024 }, { 8, 877, 1024 }, { 8, 878, 1024 }, { 9, 879, 1024 },
  10563. { 6, 880, 1024 }, { 7, 881, 1024 }, { 7, 882, 1024 }, { 8, 883, 1024 }, { 7, 884, 1024 }, { 8, 885, 1024 }, { 8, 886, 1024 }, { 9, 887, 1024 },
  10564. { 7, 888, 1024 }, { 8, 889, 1024 }, { 8, 890, 1024 }, { 9, 891, 1024 }, { 8, 892, 1024 }, { 9, 893, 1024 }, { 9, 894, 1024 }, { 10, 895, 1024 },
  10565. { 4, 896, 1024 }, { 5, 897, 1024 }, { 5, 898, 1024 }, { 6, 899, 1024 }, { 5, 900, 1024 }, { 6, 901, 1024 }, { 6, 902, 1024 }, { 7, 903, 1024 },
  10566. { 5, 904, 1024 }, { 6, 905, 1024 }, { 6, 906, 1024 }, { 7, 907, 1024 }, { 6, 908, 1024 }, { 7, 909, 1024 }, { 7, 910, 1024 }, { 8, 911, 1024 },
  10567. { 5, 912, 1024 }, { 6, 913, 1024 }, { 6, 914, 1024 }, { 7, 915, 1024 }, { 6, 916, 1024 }, { 7, 917, 1024 }, { 7, 918, 1024 }, { 8, 919, 1024 },
  10568. { 6, 920, 1024 }, { 7, 921, 1024 }, { 7, 922, 1024 }, { 8, 923, 1024 }, { 7, 924, 1024 }, { 8, 925, 1024 }, { 8, 926, 1024 }, { 9, 927, 1024 },
  10569. { 5, 928, 1024 }, { 6, 929, 1024 }, { 6, 930, 1024 }, { 7, 931, 1024 }, { 6, 932, 1024 }, { 7, 933, 1024 }, { 7, 934, 1024 }, { 8, 935, 1024 },
  10570. { 6, 936, 1024 }, { 7, 937, 1024 }, { 7, 938, 1024 }, { 8, 939, 1024 }, { 7, 940, 1024 }, { 8, 941, 1024 }, { 8, 942, 1024 }, { 9, 943, 1024 },
  10571. { 6, 944, 1024 }, { 7, 945, 1024 }, { 7, 946, 1024 }, { 8, 947, 1024 }, { 7, 948, 1024 }, { 8, 949, 1024 }, { 8, 950, 1024 }, { 9, 951, 1024 },
  10572. { 7, 952, 1024 }, { 8, 953, 1024 }, { 8, 954, 1024 }, { 9, 955, 1024 }, { 8, 956, 1024 }, { 9, 957, 1024 }, { 9, 958, 1024 }, { 10, 959, 1024 },
  10573. { 5, 960, 1024 }, { 6, 961, 1024 }, { 6, 962, 1024 }, { 7, 963, 1024 }, { 6, 964, 1024 }, { 7, 965, 1024 }, { 7, 966, 1024 }, { 8, 967, 1024 },
  10574. { 6, 968, 1024 }, { 7, 969, 1024 }, { 7, 970, 1024 }, { 8, 971, 1024 }, { 7, 972, 1024 }, { 8, 973, 1024 }, { 8, 974, 1024 }, { 9, 975, 1024 },
  10575. { 6, 976, 1024 }, { 7, 977, 1024 }, { 7, 978, 1024 }, { 8, 979, 1024 }, { 7, 980, 1024 }, { 8, 981, 1024 }, { 8, 982, 1024 }, { 9, 983, 1024 },
  10576. { 7, 984, 1024 }, { 8, 985, 1024 }, { 8, 986, 1024 }, { 9, 987, 1024 }, { 8, 988, 1024 }, { 9, 989, 1024 }, { 9, 990, 1024 }, { 10, 991, 1024 },
  10577. { 6, 992, 1024 }, { 7, 993, 1024 }, { 7, 994, 1024 }, { 8, 995, 1024 }, { 7, 996, 1024 }, { 8, 997, 1024 }, { 8, 998, 1024 }, { 9, 999, 1024 },
  10578. { 7, 1000, 1024 }, { 8, 1001, 1024 }, { 8, 1002, 1024 }, { 9, 1003, 1024 }, { 8, 1004, 1024 }, { 9, 1005, 1024 }, { 9, 1006, 1024 }, { 10, 1007, 1024 },
  10579. { 7, 1008, 1024 }, { 8, 1009, 1024 }, { 8, 1010, 1024 }, { 9, 1011, 1024 }, { 8, 1012, 1024 }, { 9, 1013, 1024 }, { 9, 1014, 1024 }, { 10, 1015, 1024 },
  10580. { 8, 1016, 1024 }, { 9, 1017, 1024 }, { 9, 1018, 1024 }, { 10, 1019, 1024 }, { 9, 1020, 1024 }, { 10, 1021, 1024 }, { 10, 1022, 1024 }, { 11, 1023, 1024 },
  10581. #if FP_LUT > 11
  10582. { 1, 0, 0 }, { 2, 1, 2048 }, { 2, 2, 2048 }, { 3, 3, 2048 }, { 2, 4, 2048 }, { 3, 5, 2048 }, { 3, 6, 2048 }, { 4, 7, 2048 },
  10583. { 2, 8, 2048 }, { 3, 9, 2048 }, { 3, 10, 2048 }, { 4, 11, 2048 }, { 3, 12, 2048 }, { 4, 13, 2048 }, { 4, 14, 2048 }, { 5, 15, 2048 },
  10584. { 2, 16, 2048 }, { 3, 17, 2048 }, { 3, 18, 2048 }, { 4, 19, 2048 }, { 3, 20, 2048 }, { 4, 21, 2048 }, { 4, 22, 2048 }, { 5, 23, 2048 },
  10585. { 3, 24, 2048 }, { 4, 25, 2048 }, { 4, 26, 2048 }, { 5, 27, 2048 }, { 4, 28, 2048 }, { 5, 29, 2048 }, { 5, 30, 2048 }, { 6, 31, 2048 },
  10586. { 2, 32, 2048 }, { 3, 33, 2048 }, { 3, 34, 2048 }, { 4, 35, 2048 }, { 3, 36, 2048 }, { 4, 37, 2048 }, { 4, 38, 2048 }, { 5, 39, 2048 },
  10587. { 3, 40, 2048 }, { 4, 41, 2048 }, { 4, 42, 2048 }, { 5, 43, 2048 }, { 4, 44, 2048 }, { 5, 45, 2048 }, { 5, 46, 2048 }, { 6, 47, 2048 },
  10588. { 3, 48, 2048 }, { 4, 49, 2048 }, { 4, 50, 2048 }, { 5, 51, 2048 }, { 4, 52, 2048 }, { 5, 53, 2048 }, { 5, 54, 2048 }, { 6, 55, 2048 },
  10589. { 4, 56, 2048 }, { 5, 57, 2048 }, { 5, 58, 2048 }, { 6, 59, 2048 }, { 5, 60, 2048 }, { 6, 61, 2048 }, { 6, 62, 2048 }, { 7, 63, 2048 },
  10590. { 2, 64, 2048 }, { 3, 65, 2048 }, { 3, 66, 2048 }, { 4, 67, 2048 }, { 3, 68, 2048 }, { 4, 69, 2048 }, { 4, 70, 2048 }, { 5, 71, 2048 },
  10591. { 3, 72, 2048 }, { 4, 73, 2048 }, { 4, 74, 2048 }, { 5, 75, 2048 }, { 4, 76, 2048 }, { 5, 77, 2048 }, { 5, 78, 2048 }, { 6, 79, 2048 },
  10592. { 3, 80, 2048 }, { 4, 81, 2048 }, { 4, 82, 2048 }, { 5, 83, 2048 }, { 4, 84, 2048 }, { 5, 85, 2048 }, { 5, 86, 2048 }, { 6, 87, 2048 },
  10593. { 4, 88, 2048 }, { 5, 89, 2048 }, { 5, 90, 2048 }, { 6, 91, 2048 }, { 5, 92, 2048 }, { 6, 93, 2048 }, { 6, 94, 2048 }, { 7, 95, 2048 },
  10594. { 3, 96, 2048 }, { 4, 97, 2048 }, { 4, 98, 2048 }, { 5, 99, 2048 }, { 4, 100, 2048 }, { 5, 101, 2048 }, { 5, 102, 2048 }, { 6, 103, 2048 },
  10595. { 4, 104, 2048 }, { 5, 105, 2048 }, { 5, 106, 2048 }, { 6, 107, 2048 }, { 5, 108, 2048 }, { 6, 109, 2048 }, { 6, 110, 2048 }, { 7, 111, 2048 },
  10596. { 4, 112, 2048 }, { 5, 113, 2048 }, { 5, 114, 2048 }, { 6, 115, 2048 }, { 5, 116, 2048 }, { 6, 117, 2048 }, { 6, 118, 2048 }, { 7, 119, 2048 },
  10597. { 5, 120, 2048 }, { 6, 121, 2048 }, { 6, 122, 2048 }, { 7, 123, 2048 }, { 6, 124, 2048 }, { 7, 125, 2048 }, { 7, 126, 2048 }, { 8, 127, 2048 },
  10598. { 2, 128, 2048 }, { 3, 129, 2048 }, { 3, 130, 2048 }, { 4, 131, 2048 }, { 3, 132, 2048 }, { 4, 133, 2048 }, { 4, 134, 2048 }, { 5, 135, 2048 },
  10599. { 3, 136, 2048 }, { 4, 137, 2048 }, { 4, 138, 2048 }, { 5, 139, 2048 }, { 4, 140, 2048 }, { 5, 141, 2048 }, { 5, 142, 2048 }, { 6, 143, 2048 },
  10600. { 3, 144, 2048 }, { 4, 145, 2048 }, { 4, 146, 2048 }, { 5, 147, 2048 }, { 4, 148, 2048 }, { 5, 149, 2048 }, { 5, 150, 2048 }, { 6, 151, 2048 },
  10601. { 4, 152, 2048 }, { 5, 153, 2048 }, { 5, 154, 2048 }, { 6, 155, 2048 }, { 5, 156, 2048 }, { 6, 157, 2048 }, { 6, 158, 2048 }, { 7, 159, 2048 },
  10602. { 3, 160, 2048 }, { 4, 161, 2048 }, { 4, 162, 2048 }, { 5, 163, 2048 }, { 4, 164, 2048 }, { 5, 165, 2048 }, { 5, 166, 2048 }, { 6, 167, 2048 },
  10603. { 4, 168, 2048 }, { 5, 169, 2048 }, { 5, 170, 2048 }, { 6, 171, 2048 }, { 5, 172, 2048 }, { 6, 173, 2048 }, { 6, 174, 2048 }, { 7, 175, 2048 },
  10604. { 4, 176, 2048 }, { 5, 177, 2048 }, { 5, 178, 2048 }, { 6, 179, 2048 }, { 5, 180, 2048 }, { 6, 181, 2048 }, { 6, 182, 2048 }, { 7, 183, 2048 },
  10605. { 5, 184, 2048 }, { 6, 185, 2048 }, { 6, 186, 2048 }, { 7, 187, 2048 }, { 6, 188, 2048 }, { 7, 189, 2048 }, { 7, 190, 2048 }, { 8, 191, 2048 },
  10606. { 3, 192, 2048 }, { 4, 193, 2048 }, { 4, 194, 2048 }, { 5, 195, 2048 }, { 4, 196, 2048 }, { 5, 197, 2048 }, { 5, 198, 2048 }, { 6, 199, 2048 },
  10607. { 4, 200, 2048 }, { 5, 201, 2048 }, { 5, 202, 2048 }, { 6, 203, 2048 }, { 5, 204, 2048 }, { 6, 205, 2048 }, { 6, 206, 2048 }, { 7, 207, 2048 },
  10608. { 4, 208, 2048 }, { 5, 209, 2048 }, { 5, 210, 2048 }, { 6, 211, 2048 }, { 5, 212, 2048 }, { 6, 213, 2048 }, { 6, 214, 2048 }, { 7, 215, 2048 },
  10609. { 5, 216, 2048 }, { 6, 217, 2048 }, { 6, 218, 2048 }, { 7, 219, 2048 }, { 6, 220, 2048 }, { 7, 221, 2048 }, { 7, 222, 2048 }, { 8, 223, 2048 },
  10610. { 4, 224, 2048 }, { 5, 225, 2048 }, { 5, 226, 2048 }, { 6, 227, 2048 }, { 5, 228, 2048 }, { 6, 229, 2048 }, { 6, 230, 2048 }, { 7, 231, 2048 },
  10611. { 5, 232, 2048 }, { 6, 233, 2048 }, { 6, 234, 2048 }, { 7, 235, 2048 }, { 6, 236, 2048 }, { 7, 237, 2048 }, { 7, 238, 2048 }, { 8, 239, 2048 },
  10612. { 5, 240, 2048 }, { 6, 241, 2048 }, { 6, 242, 2048 }, { 7, 243, 2048 }, { 6, 244, 2048 }, { 7, 245, 2048 }, { 7, 246, 2048 }, { 8, 247, 2048 },
  10613. { 6, 248, 2048 }, { 7, 249, 2048 }, { 7, 250, 2048 }, { 8, 251, 2048 }, { 7, 252, 2048 }, { 8, 253, 2048 }, { 8, 254, 2048 }, { 9, 255, 2048 },
  10614. { 2, 256, 2048 }, { 3, 257, 2048 }, { 3, 258, 2048 }, { 4, 259, 2048 }, { 3, 260, 2048 }, { 4, 261, 2048 }, { 4, 262, 2048 }, { 5, 263, 2048 },
  10615. { 3, 264, 2048 }, { 4, 265, 2048 }, { 4, 266, 2048 }, { 5, 267, 2048 }, { 4, 268, 2048 }, { 5, 269, 2048 }, { 5, 270, 2048 }, { 6, 271, 2048 },
  10616. { 3, 272, 2048 }, { 4, 273, 2048 }, { 4, 274, 2048 }, { 5, 275, 2048 }, { 4, 276, 2048 }, { 5, 277, 2048 }, { 5, 278, 2048 }, { 6, 279, 2048 },
  10617. { 4, 280, 2048 }, { 5, 281, 2048 }, { 5, 282, 2048 }, { 6, 283, 2048 }, { 5, 284, 2048 }, { 6, 285, 2048 }, { 6, 286, 2048 }, { 7, 287, 2048 },
  10618. { 3, 288, 2048 }, { 4, 289, 2048 }, { 4, 290, 2048 }, { 5, 291, 2048 }, { 4, 292, 2048 }, { 5, 293, 2048 }, { 5, 294, 2048 }, { 6, 295, 2048 },
  10619. { 4, 296, 2048 }, { 5, 297, 2048 }, { 5, 298, 2048 }, { 6, 299, 2048 }, { 5, 300, 2048 }, { 6, 301, 2048 }, { 6, 302, 2048 }, { 7, 303, 2048 },
  10620. { 4, 304, 2048 }, { 5, 305, 2048 }, { 5, 306, 2048 }, { 6, 307, 2048 }, { 5, 308, 2048 }, { 6, 309, 2048 }, { 6, 310, 2048 }, { 7, 311, 2048 },
  10621. { 5, 312, 2048 }, { 6, 313, 2048 }, { 6, 314, 2048 }, { 7, 315, 2048 }, { 6, 316, 2048 }, { 7, 317, 2048 }, { 7, 318, 2048 }, { 8, 319, 2048 },
  10622. { 3, 320, 2048 }, { 4, 321, 2048 }, { 4, 322, 2048 }, { 5, 323, 2048 }, { 4, 324, 2048 }, { 5, 325, 2048 }, { 5, 326, 2048 }, { 6, 327, 2048 },
  10623. { 4, 328, 2048 }, { 5, 329, 2048 }, { 5, 330, 2048 }, { 6, 331, 2048 }, { 5, 332, 2048 }, { 6, 333, 2048 }, { 6, 334, 2048 }, { 7, 335, 2048 },
  10624. { 4, 336, 2048 }, { 5, 337, 2048 }, { 5, 338, 2048 }, { 6, 339, 2048 }, { 5, 340, 2048 }, { 6, 341, 2048 }, { 6, 342, 2048 }, { 7, 343, 2048 },
  10625. { 5, 344, 2048 }, { 6, 345, 2048 }, { 6, 346, 2048 }, { 7, 347, 2048 }, { 6, 348, 2048 }, { 7, 349, 2048 }, { 7, 350, 2048 }, { 8, 351, 2048 },
  10626. { 4, 352, 2048 }, { 5, 353, 2048 }, { 5, 354, 2048 }, { 6, 355, 2048 }, { 5, 356, 2048 }, { 6, 357, 2048 }, { 6, 358, 2048 }, { 7, 359, 2048 },
  10627. { 5, 360, 2048 }, { 6, 361, 2048 }, { 6, 362, 2048 }, { 7, 363, 2048 }, { 6, 364, 2048 }, { 7, 365, 2048 }, { 7, 366, 2048 }, { 8, 367, 2048 },
  10628. { 5, 368, 2048 }, { 6, 369, 2048 }, { 6, 370, 2048 }, { 7, 371, 2048 }, { 6, 372, 2048 }, { 7, 373, 2048 }, { 7, 374, 2048 }, { 8, 375, 2048 },
  10629. { 6, 376, 2048 }, { 7, 377, 2048 }, { 7, 378, 2048 }, { 8, 379, 2048 }, { 7, 380, 2048 }, { 8, 381, 2048 }, { 8, 382, 2048 }, { 9, 383, 2048 },
  10630. { 3, 384, 2048 }, { 4, 385, 2048 }, { 4, 386, 2048 }, { 5, 387, 2048 }, { 4, 388, 2048 }, { 5, 389, 2048 }, { 5, 390, 2048 }, { 6, 391, 2048 },
  10631. { 4, 392, 2048 }, { 5, 393, 2048 }, { 5, 394, 2048 }, { 6, 395, 2048 }, { 5, 396, 2048 }, { 6, 397, 2048 }, { 6, 398, 2048 }, { 7, 399, 2048 },
  10632. { 4, 400, 2048 }, { 5, 401, 2048 }, { 5, 402, 2048 }, { 6, 403, 2048 }, { 5, 404, 2048 }, { 6, 405, 2048 }, { 6, 406, 2048 }, { 7, 407, 2048 },
  10633. { 5, 408, 2048 }, { 6, 409, 2048 }, { 6, 410, 2048 }, { 7, 411, 2048 }, { 6, 412, 2048 }, { 7, 413, 2048 }, { 7, 414, 2048 }, { 8, 415, 2048 },
  10634. { 4, 416, 2048 }, { 5, 417, 2048 }, { 5, 418, 2048 }, { 6, 419, 2048 }, { 5, 420, 2048 }, { 6, 421, 2048 }, { 6, 422, 2048 }, { 7, 423, 2048 },
  10635. { 5, 424, 2048 }, { 6, 425, 2048 }, { 6, 426, 2048 }, { 7, 427, 2048 }, { 6, 428, 2048 }, { 7, 429, 2048 }, { 7, 430, 2048 }, { 8, 431, 2048 },
  10636. { 5, 432, 2048 }, { 6, 433, 2048 }, { 6, 434, 2048 }, { 7, 435, 2048 }, { 6, 436, 2048 }, { 7, 437, 2048 }, { 7, 438, 2048 }, { 8, 439, 2048 },
  10637. { 6, 440, 2048 }, { 7, 441, 2048 }, { 7, 442, 2048 }, { 8, 443, 2048 }, { 7, 444, 2048 }, { 8, 445, 2048 }, { 8, 446, 2048 }, { 9, 447, 2048 },
  10638. { 4, 448, 2048 }, { 5, 449, 2048 }, { 5, 450, 2048 }, { 6, 451, 2048 }, { 5, 452, 2048 }, { 6, 453, 2048 }, { 6, 454, 2048 }, { 7, 455, 2048 },
  10639. { 5, 456, 2048 }, { 6, 457, 2048 }, { 6, 458, 2048 }, { 7, 459, 2048 }, { 6, 460, 2048 }, { 7, 461, 2048 }, { 7, 462, 2048 }, { 8, 463, 2048 },
  10640. { 5, 464, 2048 }, { 6, 465, 2048 }, { 6, 466, 2048 }, { 7, 467, 2048 }, { 6, 468, 2048 }, { 7, 469, 2048 }, { 7, 470, 2048 }, { 8, 471, 2048 },
  10641. { 6, 472, 2048 }, { 7, 473, 2048 }, { 7, 474, 2048 }, { 8, 475, 2048 }, { 7, 476, 2048 }, { 8, 477, 2048 }, { 8, 478, 2048 }, { 9, 479, 2048 },
  10642. { 5, 480, 2048 }, { 6, 481, 2048 }, { 6, 482, 2048 }, { 7, 483, 2048 }, { 6, 484, 2048 }, { 7, 485, 2048 }, { 7, 486, 2048 }, { 8, 487, 2048 },
  10643. { 6, 488, 2048 }, { 7, 489, 2048 }, { 7, 490, 2048 }, { 8, 491, 2048 }, { 7, 492, 2048 }, { 8, 493, 2048 }, { 8, 494, 2048 }, { 9, 495, 2048 },
  10644. { 6, 496, 2048 }, { 7, 497, 2048 }, { 7, 498, 2048 }, { 8, 499, 2048 }, { 7, 500, 2048 }, { 8, 501, 2048 }, { 8, 502, 2048 }, { 9, 503, 2048 },
  10645. { 7, 504, 2048 }, { 8, 505, 2048 }, { 8, 506, 2048 }, { 9, 507, 2048 }, { 8, 508, 2048 }, { 9, 509, 2048 }, { 9, 510, 2048 }, { 10, 511, 2048 },
  10646. { 2, 512, 2048 }, { 3, 513, 2048 }, { 3, 514, 2048 }, { 4, 515, 2048 }, { 3, 516, 2048 }, { 4, 517, 2048 }, { 4, 518, 2048 }, { 5, 519, 2048 },
  10647. { 3, 520, 2048 }, { 4, 521, 2048 }, { 4, 522, 2048 }, { 5, 523, 2048 }, { 4, 524, 2048 }, { 5, 525, 2048 }, { 5, 526, 2048 }, { 6, 527, 2048 },
  10648. { 3, 528, 2048 }, { 4, 529, 2048 }, { 4, 530, 2048 }, { 5, 531, 2048 }, { 4, 532, 2048 }, { 5, 533, 2048 }, { 5, 534, 2048 }, { 6, 535, 2048 },
  10649. { 4, 536, 2048 }, { 5, 537, 2048 }, { 5, 538, 2048 }, { 6, 539, 2048 }, { 5, 540, 2048 }, { 6, 541, 2048 }, { 6, 542, 2048 }, { 7, 543, 2048 },
  10650. { 3, 544, 2048 }, { 4, 545, 2048 }, { 4, 546, 2048 }, { 5, 547, 2048 }, { 4, 548, 2048 }, { 5, 549, 2048 }, { 5, 550, 2048 }, { 6, 551, 2048 },
  10651. { 4, 552, 2048 }, { 5, 553, 2048 }, { 5, 554, 2048 }, { 6, 555, 2048 }, { 5, 556, 2048 }, { 6, 557, 2048 }, { 6, 558, 2048 }, { 7, 559, 2048 },
  10652. { 4, 560, 2048 }, { 5, 561, 2048 }, { 5, 562, 2048 }, { 6, 563, 2048 }, { 5, 564, 2048 }, { 6, 565, 2048 }, { 6, 566, 2048 }, { 7, 567, 2048 },
  10653. { 5, 568, 2048 }, { 6, 569, 2048 }, { 6, 570, 2048 }, { 7, 571, 2048 }, { 6, 572, 2048 }, { 7, 573, 2048 }, { 7, 574, 2048 }, { 8, 575, 2048 },
  10654. { 3, 576, 2048 }, { 4, 577, 2048 }, { 4, 578, 2048 }, { 5, 579, 2048 }, { 4, 580, 2048 }, { 5, 581, 2048 }, { 5, 582, 2048 }, { 6, 583, 2048 },
  10655. { 4, 584, 2048 }, { 5, 585, 2048 }, { 5, 586, 2048 }, { 6, 587, 2048 }, { 5, 588, 2048 }, { 6, 589, 2048 }, { 6, 590, 2048 }, { 7, 591, 2048 },
  10656. { 4, 592, 2048 }, { 5, 593, 2048 }, { 5, 594, 2048 }, { 6, 595, 2048 }, { 5, 596, 2048 }, { 6, 597, 2048 }, { 6, 598, 2048 }, { 7, 599, 2048 },
  10657. { 5, 600, 2048 }, { 6, 601, 2048 }, { 6, 602, 2048 }, { 7, 603, 2048 }, { 6, 604, 2048 }, { 7, 605, 2048 }, { 7, 606, 2048 }, { 8, 607, 2048 },
  10658. { 4, 608, 2048 }, { 5, 609, 2048 }, { 5, 610, 2048 }, { 6, 611, 2048 }, { 5, 612, 2048 }, { 6, 613, 2048 }, { 6, 614, 2048 }, { 7, 615, 2048 },
  10659. { 5, 616, 2048 }, { 6, 617, 2048 }, { 6, 618, 2048 }, { 7, 619, 2048 }, { 6, 620, 2048 }, { 7, 621, 2048 }, { 7, 622, 2048 }, { 8, 623, 2048 },
  10660. { 5, 624, 2048 }, { 6, 625, 2048 }, { 6, 626, 2048 }, { 7, 627, 2048 }, { 6, 628, 2048 }, { 7, 629, 2048 }, { 7, 630, 2048 }, { 8, 631, 2048 },
  10661. { 6, 632, 2048 }, { 7, 633, 2048 }, { 7, 634, 2048 }, { 8, 635, 2048 }, { 7, 636, 2048 }, { 8, 637, 2048 }, { 8, 638, 2048 }, { 9, 639, 2048 },
  10662. { 3, 640, 2048 }, { 4, 641, 2048 }, { 4, 642, 2048 }, { 5, 643, 2048 }, { 4, 644, 2048 }, { 5, 645, 2048 }, { 5, 646, 2048 }, { 6, 647, 2048 },
  10663. { 4, 648, 2048 }, { 5, 649, 2048 }, { 5, 650, 2048 }, { 6, 651, 2048 }, { 5, 652, 2048 }, { 6, 653, 2048 }, { 6, 654, 2048 }, { 7, 655, 2048 },
  10664. { 4, 656, 2048 }, { 5, 657, 2048 }, { 5, 658, 2048 }, { 6, 659, 2048 }, { 5, 660, 2048 }, { 6, 661, 2048 }, { 6, 662, 2048 }, { 7, 663, 2048 },
  10665. { 5, 664, 2048 }, { 6, 665, 2048 }, { 6, 666, 2048 }, { 7, 667, 2048 }, { 6, 668, 2048 }, { 7, 669, 2048 }, { 7, 670, 2048 }, { 8, 671, 2048 },
  10666. { 4, 672, 2048 }, { 5, 673, 2048 }, { 5, 674, 2048 }, { 6, 675, 2048 }, { 5, 676, 2048 }, { 6, 677, 2048 }, { 6, 678, 2048 }, { 7, 679, 2048 },
  10667. { 5, 680, 2048 }, { 6, 681, 2048 }, { 6, 682, 2048 }, { 7, 683, 2048 }, { 6, 684, 2048 }, { 7, 685, 2048 }, { 7, 686, 2048 }, { 8, 687, 2048 },
  10668. { 5, 688, 2048 }, { 6, 689, 2048 }, { 6, 690, 2048 }, { 7, 691, 2048 }, { 6, 692, 2048 }, { 7, 693, 2048 }, { 7, 694, 2048 }, { 8, 695, 2048 },
  10669. { 6, 696, 2048 }, { 7, 697, 2048 }, { 7, 698, 2048 }, { 8, 699, 2048 }, { 7, 700, 2048 }, { 8, 701, 2048 }, { 8, 702, 2048 }, { 9, 703, 2048 },
  10670. { 4, 704, 2048 }, { 5, 705, 2048 }, { 5, 706, 2048 }, { 6, 707, 2048 }, { 5, 708, 2048 }, { 6, 709, 2048 }, { 6, 710, 2048 }, { 7, 711, 2048 },
  10671. { 5, 712, 2048 }, { 6, 713, 2048 }, { 6, 714, 2048 }, { 7, 715, 2048 }, { 6, 716, 2048 }, { 7, 717, 2048 }, { 7, 718, 2048 }, { 8, 719, 2048 },
  10672. { 5, 720, 2048 }, { 6, 721, 2048 }, { 6, 722, 2048 }, { 7, 723, 2048 }, { 6, 724, 2048 }, { 7, 725, 2048 }, { 7, 726, 2048 }, { 8, 727, 2048 },
  10673. { 6, 728, 2048 }, { 7, 729, 2048 }, { 7, 730, 2048 }, { 8, 731, 2048 }, { 7, 732, 2048 }, { 8, 733, 2048 }, { 8, 734, 2048 }, { 9, 735, 2048 },
  10674. { 5, 736, 2048 }, { 6, 737, 2048 }, { 6, 738, 2048 }, { 7, 739, 2048 }, { 6, 740, 2048 }, { 7, 741, 2048 }, { 7, 742, 2048 }, { 8, 743, 2048 },
  10675. { 6, 744, 2048 }, { 7, 745, 2048 }, { 7, 746, 2048 }, { 8, 747, 2048 }, { 7, 748, 2048 }, { 8, 749, 2048 }, { 8, 750, 2048 }, { 9, 751, 2048 },
  10676. { 6, 752, 2048 }, { 7, 753, 2048 }, { 7, 754, 2048 }, { 8, 755, 2048 }, { 7, 756, 2048 }, { 8, 757, 2048 }, { 8, 758, 2048 }, { 9, 759, 2048 },
  10677. { 7, 760, 2048 }, { 8, 761, 2048 }, { 8, 762, 2048 }, { 9, 763, 2048 }, { 8, 764, 2048 }, { 9, 765, 2048 }, { 9, 766, 2048 }, { 10, 767, 2048 },
  10678. { 3, 768, 2048 }, { 4, 769, 2048 }, { 4, 770, 2048 }, { 5, 771, 2048 }, { 4, 772, 2048 }, { 5, 773, 2048 }, { 5, 774, 2048 }, { 6, 775, 2048 },
  10679. { 4, 776, 2048 }, { 5, 777, 2048 }, { 5, 778, 2048 }, { 6, 779, 2048 }, { 5, 780, 2048 }, { 6, 781, 2048 }, { 6, 782, 2048 }, { 7, 783, 2048 },
  10680. { 4, 784, 2048 }, { 5, 785, 2048 }, { 5, 786, 2048 }, { 6, 787, 2048 }, { 5, 788, 2048 }, { 6, 789, 2048 }, { 6, 790, 2048 }, { 7, 791, 2048 },
  10681. { 5, 792, 2048 }, { 6, 793, 2048 }, { 6, 794, 2048 }, { 7, 795, 2048 }, { 6, 796, 2048 }, { 7, 797, 2048 }, { 7, 798, 2048 }, { 8, 799, 2048 },
  10682. { 4, 800, 2048 }, { 5, 801, 2048 }, { 5, 802, 2048 }, { 6, 803, 2048 }, { 5, 804, 2048 }, { 6, 805, 2048 }, { 6, 806, 2048 }, { 7, 807, 2048 },
  10683. { 5, 808, 2048 }, { 6, 809, 2048 }, { 6, 810, 2048 }, { 7, 811, 2048 }, { 6, 812, 2048 }, { 7, 813, 2048 }, { 7, 814, 2048 }, { 8, 815, 2048 },
  10684. { 5, 816, 2048 }, { 6, 817, 2048 }, { 6, 818, 2048 }, { 7, 819, 2048 }, { 6, 820, 2048 }, { 7, 821, 2048 }, { 7, 822, 2048 }, { 8, 823, 2048 },
  10685. { 6, 824, 2048 }, { 7, 825, 2048 }, { 7, 826, 2048 }, { 8, 827, 2048 }, { 7, 828, 2048 }, { 8, 829, 2048 }, { 8, 830, 2048 }, { 9, 831, 2048 },
  10686. { 4, 832, 2048 }, { 5, 833, 2048 }, { 5, 834, 2048 }, { 6, 835, 2048 }, { 5, 836, 2048 }, { 6, 837, 2048 }, { 6, 838, 2048 }, { 7, 839, 2048 },
  10687. { 5, 840, 2048 }, { 6, 841, 2048 }, { 6, 842, 2048 }, { 7, 843, 2048 }, { 6, 844, 2048 }, { 7, 845, 2048 }, { 7, 846, 2048 }, { 8, 847, 2048 },
  10688. { 5, 848, 2048 }, { 6, 849, 2048 }, { 6, 850, 2048 }, { 7, 851, 2048 }, { 6, 852, 2048 }, { 7, 853, 2048 }, { 7, 854, 2048 }, { 8, 855, 2048 },
  10689. { 6, 856, 2048 }, { 7, 857, 2048 }, { 7, 858, 2048 }, { 8, 859, 2048 }, { 7, 860, 2048 }, { 8, 861, 2048 }, { 8, 862, 2048 }, { 9, 863, 2048 },
  10690. { 5, 864, 2048 }, { 6, 865, 2048 }, { 6, 866, 2048 }, { 7, 867, 2048 }, { 6, 868, 2048 }, { 7, 869, 2048 }, { 7, 870, 2048 }, { 8, 871, 2048 },
  10691. { 6, 872, 2048 }, { 7, 873, 2048 }, { 7, 874, 2048 }, { 8, 875, 2048 }, { 7, 876, 2048 }, { 8, 877, 2048 }, { 8, 878, 2048 }, { 9, 879, 2048 },
  10692. { 6, 880, 2048 }, { 7, 881, 2048 }, { 7, 882, 2048 }, { 8, 883, 2048 }, { 7, 884, 2048 }, { 8, 885, 2048 }, { 8, 886, 2048 }, { 9, 887, 2048 },
  10693. { 7, 888, 2048 }, { 8, 889, 2048 }, { 8, 890, 2048 }, { 9, 891, 2048 }, { 8, 892, 2048 }, { 9, 893, 2048 }, { 9, 894, 2048 }, { 10, 895, 2048 },
  10694. { 4, 896, 2048 }, { 5, 897, 2048 }, { 5, 898, 2048 }, { 6, 899, 2048 }, { 5, 900, 2048 }, { 6, 901, 2048 }, { 6, 902, 2048 }, { 7, 903, 2048 },
  10695. { 5, 904, 2048 }, { 6, 905, 2048 }, { 6, 906, 2048 }, { 7, 907, 2048 }, { 6, 908, 2048 }, { 7, 909, 2048 }, { 7, 910, 2048 }, { 8, 911, 2048 },
  10696. { 5, 912, 2048 }, { 6, 913, 2048 }, { 6, 914, 2048 }, { 7, 915, 2048 }, { 6, 916, 2048 }, { 7, 917, 2048 }, { 7, 918, 2048 }, { 8, 919, 2048 },
  10697. { 6, 920, 2048 }, { 7, 921, 2048 }, { 7, 922, 2048 }, { 8, 923, 2048 }, { 7, 924, 2048 }, { 8, 925, 2048 }, { 8, 926, 2048 }, { 9, 927, 2048 },
  10698. { 5, 928, 2048 }, { 6, 929, 2048 }, { 6, 930, 2048 }, { 7, 931, 2048 }, { 6, 932, 2048 }, { 7, 933, 2048 }, { 7, 934, 2048 }, { 8, 935, 2048 },
  10699. { 6, 936, 2048 }, { 7, 937, 2048 }, { 7, 938, 2048 }, { 8, 939, 2048 }, { 7, 940, 2048 }, { 8, 941, 2048 }, { 8, 942, 2048 }, { 9, 943, 2048 },
  10700. { 6, 944, 2048 }, { 7, 945, 2048 }, { 7, 946, 2048 }, { 8, 947, 2048 }, { 7, 948, 2048 }, { 8, 949, 2048 }, { 8, 950, 2048 }, { 9, 951, 2048 },
  10701. { 7, 952, 2048 }, { 8, 953, 2048 }, { 8, 954, 2048 }, { 9, 955, 2048 }, { 8, 956, 2048 }, { 9, 957, 2048 }, { 9, 958, 2048 }, { 10, 959, 2048 },
  10702. { 5, 960, 2048 }, { 6, 961, 2048 }, { 6, 962, 2048 }, { 7, 963, 2048 }, { 6, 964, 2048 }, { 7, 965, 2048 }, { 7, 966, 2048 }, { 8, 967, 2048 },
  10703. { 6, 968, 2048 }, { 7, 969, 2048 }, { 7, 970, 2048 }, { 8, 971, 2048 }, { 7, 972, 2048 }, { 8, 973, 2048 }, { 8, 974, 2048 }, { 9, 975, 2048 },
  10704. { 6, 976, 2048 }, { 7, 977, 2048 }, { 7, 978, 2048 }, { 8, 979, 2048 }, { 7, 980, 2048 }, { 8, 981, 2048 }, { 8, 982, 2048 }, { 9, 983, 2048 },
  10705. { 7, 984, 2048 }, { 8, 985, 2048 }, { 8, 986, 2048 }, { 9, 987, 2048 }, { 8, 988, 2048 }, { 9, 989, 2048 }, { 9, 990, 2048 }, { 10, 991, 2048 },
  10706. { 6, 992, 2048 }, { 7, 993, 2048 }, { 7, 994, 2048 }, { 8, 995, 2048 }, { 7, 996, 2048 }, { 8, 997, 2048 }, { 8, 998, 2048 }, { 9, 999, 2048 },
  10707. { 7, 1000, 2048 }, { 8, 1001, 2048 }, { 8, 1002, 2048 }, { 9, 1003, 2048 }, { 8, 1004, 2048 }, { 9, 1005, 2048 }, { 9, 1006, 2048 }, { 10, 1007, 2048 },
  10708. { 7, 1008, 2048 }, { 8, 1009, 2048 }, { 8, 1010, 2048 }, { 9, 1011, 2048 }, { 8, 1012, 2048 }, { 9, 1013, 2048 }, { 9, 1014, 2048 }, { 10, 1015, 2048 },
  10709. { 8, 1016, 2048 }, { 9, 1017, 2048 }, { 9, 1018, 2048 }, { 10, 1019, 2048 }, { 9, 1020, 2048 }, { 10, 1021, 2048 }, { 10, 1022, 2048 }, { 11, 1023, 2048 },
  10710. { 2, 1024, 2048 }, { 3, 1025, 2048 }, { 3, 1026, 2048 }, { 4, 1027, 2048 }, { 3, 1028, 2048 }, { 4, 1029, 2048 }, { 4, 1030, 2048 }, { 5, 1031, 2048 },
  10711. { 3, 1032, 2048 }, { 4, 1033, 2048 }, { 4, 1034, 2048 }, { 5, 1035, 2048 }, { 4, 1036, 2048 }, { 5, 1037, 2048 }, { 5, 1038, 2048 }, { 6, 1039, 2048 },
  10712. { 3, 1040, 2048 }, { 4, 1041, 2048 }, { 4, 1042, 2048 }, { 5, 1043, 2048 }, { 4, 1044, 2048 }, { 5, 1045, 2048 }, { 5, 1046, 2048 }, { 6, 1047, 2048 },
  10713. { 4, 1048, 2048 }, { 5, 1049, 2048 }, { 5, 1050, 2048 }, { 6, 1051, 2048 }, { 5, 1052, 2048 }, { 6, 1053, 2048 }, { 6, 1054, 2048 }, { 7, 1055, 2048 },
  10714. { 3, 1056, 2048 }, { 4, 1057, 2048 }, { 4, 1058, 2048 }, { 5, 1059, 2048 }, { 4, 1060, 2048 }, { 5, 1061, 2048 }, { 5, 1062, 2048 }, { 6, 1063, 2048 },
  10715. { 4, 1064, 2048 }, { 5, 1065, 2048 }, { 5, 1066, 2048 }, { 6, 1067, 2048 }, { 5, 1068, 2048 }, { 6, 1069, 2048 }, { 6, 1070, 2048 }, { 7, 1071, 2048 },
  10716. { 4, 1072, 2048 }, { 5, 1073, 2048 }, { 5, 1074, 2048 }, { 6, 1075, 2048 }, { 5, 1076, 2048 }, { 6, 1077, 2048 }, { 6, 1078, 2048 }, { 7, 1079, 2048 },
  10717. { 5, 1080, 2048 }, { 6, 1081, 2048 }, { 6, 1082, 2048 }, { 7, 1083, 2048 }, { 6, 1084, 2048 }, { 7, 1085, 2048 }, { 7, 1086, 2048 }, { 8, 1087, 2048 },
  10718. { 3, 1088, 2048 }, { 4, 1089, 2048 }, { 4, 1090, 2048 }, { 5, 1091, 2048 }, { 4, 1092, 2048 }, { 5, 1093, 2048 }, { 5, 1094, 2048 }, { 6, 1095, 2048 },
  10719. { 4, 1096, 2048 }, { 5, 1097, 2048 }, { 5, 1098, 2048 }, { 6, 1099, 2048 }, { 5, 1100, 2048 }, { 6, 1101, 2048 }, { 6, 1102, 2048 }, { 7, 1103, 2048 },
  10720. { 4, 1104, 2048 }, { 5, 1105, 2048 }, { 5, 1106, 2048 }, { 6, 1107, 2048 }, { 5, 1108, 2048 }, { 6, 1109, 2048 }, { 6, 1110, 2048 }, { 7, 1111, 2048 },
  10721. { 5, 1112, 2048 }, { 6, 1113, 2048 }, { 6, 1114, 2048 }, { 7, 1115, 2048 }, { 6, 1116, 2048 }, { 7, 1117, 2048 }, { 7, 1118, 2048 }, { 8, 1119, 2048 },
  10722. { 4, 1120, 2048 }, { 5, 1121, 2048 }, { 5, 1122, 2048 }, { 6, 1123, 2048 }, { 5, 1124, 2048 }, { 6, 1125, 2048 }, { 6, 1126, 2048 }, { 7, 1127, 2048 },
  10723. { 5, 1128, 2048 }, { 6, 1129, 2048 }, { 6, 1130, 2048 }, { 7, 1131, 2048 }, { 6, 1132, 2048 }, { 7, 1133, 2048 }, { 7, 1134, 2048 }, { 8, 1135, 2048 },
  10724. { 5, 1136, 2048 }, { 6, 1137, 2048 }, { 6, 1138, 2048 }, { 7, 1139, 2048 }, { 6, 1140, 2048 }, { 7, 1141, 2048 }, { 7, 1142, 2048 }, { 8, 1143, 2048 },
  10725. { 6, 1144, 2048 }, { 7, 1145, 2048 }, { 7, 1146, 2048 }, { 8, 1147, 2048 }, { 7, 1148, 2048 }, { 8, 1149, 2048 }, { 8, 1150, 2048 }, { 9, 1151, 2048 },
  10726. { 3, 1152, 2048 }, { 4, 1153, 2048 }, { 4, 1154, 2048 }, { 5, 1155, 2048 }, { 4, 1156, 2048 }, { 5, 1157, 2048 }, { 5, 1158, 2048 }, { 6, 1159, 2048 },
  10727. { 4, 1160, 2048 }, { 5, 1161, 2048 }, { 5, 1162, 2048 }, { 6, 1163, 2048 }, { 5, 1164, 2048 }, { 6, 1165, 2048 }, { 6, 1166, 2048 }, { 7, 1167, 2048 },
  10728. { 4, 1168, 2048 }, { 5, 1169, 2048 }, { 5, 1170, 2048 }, { 6, 1171, 2048 }, { 5, 1172, 2048 }, { 6, 1173, 2048 }, { 6, 1174, 2048 }, { 7, 1175, 2048 },
  10729. { 5, 1176, 2048 }, { 6, 1177, 2048 }, { 6, 1178, 2048 }, { 7, 1179, 2048 }, { 6, 1180, 2048 }, { 7, 1181, 2048 }, { 7, 1182, 2048 }, { 8, 1183, 2048 },
  10730. { 4, 1184, 2048 }, { 5, 1185, 2048 }, { 5, 1186, 2048 }, { 6, 1187, 2048 }, { 5, 1188, 2048 }, { 6, 1189, 2048 }, { 6, 1190, 2048 }, { 7, 1191, 2048 },
  10731. { 5, 1192, 2048 }, { 6, 1193, 2048 }, { 6, 1194, 2048 }, { 7, 1195, 2048 }, { 6, 1196, 2048 }, { 7, 1197, 2048 }, { 7, 1198, 2048 }, { 8, 1199, 2048 },
  10732. { 5, 1200, 2048 }, { 6, 1201, 2048 }, { 6, 1202, 2048 }, { 7, 1203, 2048 }, { 6, 1204, 2048 }, { 7, 1205, 2048 }, { 7, 1206, 2048 }, { 8, 1207, 2048 },
  10733. { 6, 1208, 2048 }, { 7, 1209, 2048 }, { 7, 1210, 2048 }, { 8, 1211, 2048 }, { 7, 1212, 2048 }, { 8, 1213, 2048 }, { 8, 1214, 2048 }, { 9, 1215, 2048 },
  10734. { 4, 1216, 2048 }, { 5, 1217, 2048 }, { 5, 1218, 2048 }, { 6, 1219, 2048 }, { 5, 1220, 2048 }, { 6, 1221, 2048 }, { 6, 1222, 2048 }, { 7, 1223, 2048 },
  10735. { 5, 1224, 2048 }, { 6, 1225, 2048 }, { 6, 1226, 2048 }, { 7, 1227, 2048 }, { 6, 1228, 2048 }, { 7, 1229, 2048 }, { 7, 1230, 2048 }, { 8, 1231, 2048 },
  10736. { 5, 1232, 2048 }, { 6, 1233, 2048 }, { 6, 1234, 2048 }, { 7, 1235, 2048 }, { 6, 1236, 2048 }, { 7, 1237, 2048 }, { 7, 1238, 2048 }, { 8, 1239, 2048 },
  10737. { 6, 1240, 2048 }, { 7, 1241, 2048 }, { 7, 1242, 2048 }, { 8, 1243, 2048 }, { 7, 1244, 2048 }, { 8, 1245, 2048 }, { 8, 1246, 2048 }, { 9, 1247, 2048 },
  10738. { 5, 1248, 2048 }, { 6, 1249, 2048 }, { 6, 1250, 2048 }, { 7, 1251, 2048 }, { 6, 1252, 2048 }, { 7, 1253, 2048 }, { 7, 1254, 2048 }, { 8, 1255, 2048 },
  10739. { 6, 1256, 2048 }, { 7, 1257, 2048 }, { 7, 1258, 2048 }, { 8, 1259, 2048 }, { 7, 1260, 2048 }, { 8, 1261, 2048 }, { 8, 1262, 2048 }, { 9, 1263, 2048 },
  10740. { 6, 1264, 2048 }, { 7, 1265, 2048 }, { 7, 1266, 2048 }, { 8, 1267, 2048 }, { 7, 1268, 2048 }, { 8, 1269, 2048 }, { 8, 1270, 2048 }, { 9, 1271, 2048 },
  10741. { 7, 1272, 2048 }, { 8, 1273, 2048 }, { 8, 1274, 2048 }, { 9, 1275, 2048 }, { 8, 1276, 2048 }, { 9, 1277, 2048 }, { 9, 1278, 2048 }, { 10, 1279, 2048 },
  10742. { 3, 1280, 2048 }, { 4, 1281, 2048 }, { 4, 1282, 2048 }, { 5, 1283, 2048 }, { 4, 1284, 2048 }, { 5, 1285, 2048 }, { 5, 1286, 2048 }, { 6, 1287, 2048 },
  10743. { 4, 1288, 2048 }, { 5, 1289, 2048 }, { 5, 1290, 2048 }, { 6, 1291, 2048 }, { 5, 1292, 2048 }, { 6, 1293, 2048 }, { 6, 1294, 2048 }, { 7, 1295, 2048 },
  10744. { 4, 1296, 2048 }, { 5, 1297, 2048 }, { 5, 1298, 2048 }, { 6, 1299, 2048 }, { 5, 1300, 2048 }, { 6, 1301, 2048 }, { 6, 1302, 2048 }, { 7, 1303, 2048 },
  10745. { 5, 1304, 2048 }, { 6, 1305, 2048 }, { 6, 1306, 2048 }, { 7, 1307, 2048 }, { 6, 1308, 2048 }, { 7, 1309, 2048 }, { 7, 1310, 2048 }, { 8, 1311, 2048 },
  10746. { 4, 1312, 2048 }, { 5, 1313, 2048 }, { 5, 1314, 2048 }, { 6, 1315, 2048 }, { 5, 1316, 2048 }, { 6, 1317, 2048 }, { 6, 1318, 2048 }, { 7, 1319, 2048 },
  10747. { 5, 1320, 2048 }, { 6, 1321, 2048 }, { 6, 1322, 2048 }, { 7, 1323, 2048 }, { 6, 1324, 2048 }, { 7, 1325, 2048 }, { 7, 1326, 2048 }, { 8, 1327, 2048 },
  10748. { 5, 1328, 2048 }, { 6, 1329, 2048 }, { 6, 1330, 2048 }, { 7, 1331, 2048 }, { 6, 1332, 2048 }, { 7, 1333, 2048 }, { 7, 1334, 2048 }, { 8, 1335, 2048 },
  10749. { 6, 1336, 2048 }, { 7, 1337, 2048 }, { 7, 1338, 2048 }, { 8, 1339, 2048 }, { 7, 1340, 2048 }, { 8, 1341, 2048 }, { 8, 1342, 2048 }, { 9, 1343, 2048 },
  10750. { 4, 1344, 2048 }, { 5, 1345, 2048 }, { 5, 1346, 2048 }, { 6, 1347, 2048 }, { 5, 1348, 2048 }, { 6, 1349, 2048 }, { 6, 1350, 2048 }, { 7, 1351, 2048 },
  10751. { 5, 1352, 2048 }, { 6, 1353, 2048 }, { 6, 1354, 2048 }, { 7, 1355, 2048 }, { 6, 1356, 2048 }, { 7, 1357, 2048 }, { 7, 1358, 2048 }, { 8, 1359, 2048 },
  10752. { 5, 1360, 2048 }, { 6, 1361, 2048 }, { 6, 1362, 2048 }, { 7, 1363, 2048 }, { 6, 1364, 2048 }, { 7, 1365, 2048 }, { 7, 1366, 2048 }, { 8, 1367, 2048 },
  10753. { 6, 1368, 2048 }, { 7, 1369, 2048 }, { 7, 1370, 2048 }, { 8, 1371, 2048 }, { 7, 1372, 2048 }, { 8, 1373, 2048 }, { 8, 1374, 2048 }, { 9, 1375, 2048 },
  10754. { 5, 1376, 2048 }, { 6, 1377, 2048 }, { 6, 1378, 2048 }, { 7, 1379, 2048 }, { 6, 1380, 2048 }, { 7, 1381, 2048 }, { 7, 1382, 2048 }, { 8, 1383, 2048 },
  10755. { 6, 1384, 2048 }, { 7, 1385, 2048 }, { 7, 1386, 2048 }, { 8, 1387, 2048 }, { 7, 1388, 2048 }, { 8, 1389, 2048 }, { 8, 1390, 2048 }, { 9, 1391, 2048 },
  10756. { 6, 1392, 2048 }, { 7, 1393, 2048 }, { 7, 1394, 2048 }, { 8, 1395, 2048 }, { 7, 1396, 2048 }, { 8, 1397, 2048 }, { 8, 1398, 2048 }, { 9, 1399, 2048 },
  10757. { 7, 1400, 2048 }, { 8, 1401, 2048 }, { 8, 1402, 2048 }, { 9, 1403, 2048 }, { 8, 1404, 2048 }, { 9, 1405, 2048 }, { 9, 1406, 2048 }, { 10, 1407, 2048 },
  10758. { 4, 1408, 2048 }, { 5, 1409, 2048 }, { 5, 1410, 2048 }, { 6, 1411, 2048 }, { 5, 1412, 2048 }, { 6, 1413, 2048 }, { 6, 1414, 2048 }, { 7, 1415, 2048 },
  10759. { 5, 1416, 2048 }, { 6, 1417, 2048 }, { 6, 1418, 2048 }, { 7, 1419, 2048 }, { 6, 1420, 2048 }, { 7, 1421, 2048 }, { 7, 1422, 2048 }, { 8, 1423, 2048 },
  10760. { 5, 1424, 2048 }, { 6, 1425, 2048 }, { 6, 1426, 2048 }, { 7, 1427, 2048 }, { 6, 1428, 2048 }, { 7, 1429, 2048 }, { 7, 1430, 2048 }, { 8, 1431, 2048 },
  10761. { 6, 1432, 2048 }, { 7, 1433, 2048 }, { 7, 1434, 2048 }, { 8, 1435, 2048 }, { 7, 1436, 2048 }, { 8, 1437, 2048 }, { 8, 1438, 2048 }, { 9, 1439, 2048 },
  10762. { 5, 1440, 2048 }, { 6, 1441, 2048 }, { 6, 1442, 2048 }, { 7, 1443, 2048 }, { 6, 1444, 2048 }, { 7, 1445, 2048 }, { 7, 1446, 2048 }, { 8, 1447, 2048 },
  10763. { 6, 1448, 2048 }, { 7, 1449, 2048 }, { 7, 1450, 2048 }, { 8, 1451, 2048 }, { 7, 1452, 2048 }, { 8, 1453, 2048 }, { 8, 1454, 2048 }, { 9, 1455, 2048 },
  10764. { 6, 1456, 2048 }, { 7, 1457, 2048 }, { 7, 1458, 2048 }, { 8, 1459, 2048 }, { 7, 1460, 2048 }, { 8, 1461, 2048 }, { 8, 1462, 2048 }, { 9, 1463, 2048 },
  10765. { 7, 1464, 2048 }, { 8, 1465, 2048 }, { 8, 1466, 2048 }, { 9, 1467, 2048 }, { 8, 1468, 2048 }, { 9, 1469, 2048 }, { 9, 1470, 2048 }, { 10, 1471, 2048 },
  10766. { 5, 1472, 2048 }, { 6, 1473, 2048 }, { 6, 1474, 2048 }, { 7, 1475, 2048 }, { 6, 1476, 2048 }, { 7, 1477, 2048 }, { 7, 1478, 2048 }, { 8, 1479, 2048 },
  10767. { 6, 1480, 2048 }, { 7, 1481, 2048 }, { 7, 1482, 2048 }, { 8, 1483, 2048 }, { 7, 1484, 2048 }, { 8, 1485, 2048 }, { 8, 1486, 2048 }, { 9, 1487, 2048 },
  10768. { 6, 1488, 2048 }, { 7, 1489, 2048 }, { 7, 1490, 2048 }, { 8, 1491, 2048 }, { 7, 1492, 2048 }, { 8, 1493, 2048 }, { 8, 1494, 2048 }, { 9, 1495, 2048 },
  10769. { 7, 1496, 2048 }, { 8, 1497, 2048 }, { 8, 1498, 2048 }, { 9, 1499, 2048 }, { 8, 1500, 2048 }, { 9, 1501, 2048 }, { 9, 1502, 2048 }, { 10, 1503, 2048 },
  10770. { 6, 1504, 2048 }, { 7, 1505, 2048 }, { 7, 1506, 2048 }, { 8, 1507, 2048 }, { 7, 1508, 2048 }, { 8, 1509, 2048 }, { 8, 1510, 2048 }, { 9, 1511, 2048 },
  10771. { 7, 1512, 2048 }, { 8, 1513, 2048 }, { 8, 1514, 2048 }, { 9, 1515, 2048 }, { 8, 1516, 2048 }, { 9, 1517, 2048 }, { 9, 1518, 2048 }, { 10, 1519, 2048 },
  10772. { 7, 1520, 2048 }, { 8, 1521, 2048 }, { 8, 1522, 2048 }, { 9, 1523, 2048 }, { 8, 1524, 2048 }, { 9, 1525, 2048 }, { 9, 1526, 2048 }, { 10, 1527, 2048 },
  10773. { 8, 1528, 2048 }, { 9, 1529, 2048 }, { 9, 1530, 2048 }, { 10, 1531, 2048 }, { 9, 1532, 2048 }, { 10, 1533, 2048 }, { 10, 1534, 2048 }, { 11, 1535, 2048 },
  10774. { 3, 1536, 2048 }, { 4, 1537, 2048 }, { 4, 1538, 2048 }, { 5, 1539, 2048 }, { 4, 1540, 2048 }, { 5, 1541, 2048 }, { 5, 1542, 2048 }, { 6, 1543, 2048 },
  10775. { 4, 1544, 2048 }, { 5, 1545, 2048 }, { 5, 1546, 2048 }, { 6, 1547, 2048 }, { 5, 1548, 2048 }, { 6, 1549, 2048 }, { 6, 1550, 2048 }, { 7, 1551, 2048 },
  10776. { 4, 1552, 2048 }, { 5, 1553, 2048 }, { 5, 1554, 2048 }, { 6, 1555, 2048 }, { 5, 1556, 2048 }, { 6, 1557, 2048 }, { 6, 1558, 2048 }, { 7, 1559, 2048 },
  10777. { 5, 1560, 2048 }, { 6, 1561, 2048 }, { 6, 1562, 2048 }, { 7, 1563, 2048 }, { 6, 1564, 2048 }, { 7, 1565, 2048 }, { 7, 1566, 2048 }, { 8, 1567, 2048 },
  10778. { 4, 1568, 2048 }, { 5, 1569, 2048 }, { 5, 1570, 2048 }, { 6, 1571, 2048 }, { 5, 1572, 2048 }, { 6, 1573, 2048 }, { 6, 1574, 2048 }, { 7, 1575, 2048 },
  10779. { 5, 1576, 2048 }, { 6, 1577, 2048 }, { 6, 1578, 2048 }, { 7, 1579, 2048 }, { 6, 1580, 2048 }, { 7, 1581, 2048 }, { 7, 1582, 2048 }, { 8, 1583, 2048 },
  10780. { 5, 1584, 2048 }, { 6, 1585, 2048 }, { 6, 1586, 2048 }, { 7, 1587, 2048 }, { 6, 1588, 2048 }, { 7, 1589, 2048 }, { 7, 1590, 2048 }, { 8, 1591, 2048 },
  10781. { 6, 1592, 2048 }, { 7, 1593, 2048 }, { 7, 1594, 2048 }, { 8, 1595, 2048 }, { 7, 1596, 2048 }, { 8, 1597, 2048 }, { 8, 1598, 2048 }, { 9, 1599, 2048 },
  10782. { 4, 1600, 2048 }, { 5, 1601, 2048 }, { 5, 1602, 2048 }, { 6, 1603, 2048 }, { 5, 1604, 2048 }, { 6, 1605, 2048 }, { 6, 1606, 2048 }, { 7, 1607, 2048 },
  10783. { 5, 1608, 2048 }, { 6, 1609, 2048 }, { 6, 1610, 2048 }, { 7, 1611, 2048 }, { 6, 1612, 2048 }, { 7, 1613, 2048 }, { 7, 1614, 2048 }, { 8, 1615, 2048 },
  10784. { 5, 1616, 2048 }, { 6, 1617, 2048 }, { 6, 1618, 2048 }, { 7, 1619, 2048 }, { 6, 1620, 2048 }, { 7, 1621, 2048 }, { 7, 1622, 2048 }, { 8, 1623, 2048 },
  10785. { 6, 1624, 2048 }, { 7, 1625, 2048 }, { 7, 1626, 2048 }, { 8, 1627, 2048 }, { 7, 1628, 2048 }, { 8, 1629, 2048 }, { 8, 1630, 2048 }, { 9, 1631, 2048 },
  10786. { 5, 1632, 2048 }, { 6, 1633, 2048 }, { 6, 1634, 2048 }, { 7, 1635, 2048 }, { 6, 1636, 2048 }, { 7, 1637, 2048 }, { 7, 1638, 2048 }, { 8, 1639, 2048 },
  10787. { 6, 1640, 2048 }, { 7, 1641, 2048 }, { 7, 1642, 2048 }, { 8, 1643, 2048 }, { 7, 1644, 2048 }, { 8, 1645, 2048 }, { 8, 1646, 2048 }, { 9, 1647, 2048 },
  10788. { 6, 1648, 2048 }, { 7, 1649, 2048 }, { 7, 1650, 2048 }, { 8, 1651, 2048 }, { 7, 1652, 2048 }, { 8, 1653, 2048 }, { 8, 1654, 2048 }, { 9, 1655, 2048 },
  10789. { 7, 1656, 2048 }, { 8, 1657, 2048 }, { 8, 1658, 2048 }, { 9, 1659, 2048 }, { 8, 1660, 2048 }, { 9, 1661, 2048 }, { 9, 1662, 2048 }, { 10, 1663, 2048 },
  10790. { 4, 1664, 2048 }, { 5, 1665, 2048 }, { 5, 1666, 2048 }, { 6, 1667, 2048 }, { 5, 1668, 2048 }, { 6, 1669, 2048 }, { 6, 1670, 2048 }, { 7, 1671, 2048 },
  10791. { 5, 1672, 2048 }, { 6, 1673, 2048 }, { 6, 1674, 2048 }, { 7, 1675, 2048 }, { 6, 1676, 2048 }, { 7, 1677, 2048 }, { 7, 1678, 2048 }, { 8, 1679, 2048 },
  10792. { 5, 1680, 2048 }, { 6, 1681, 2048 }, { 6, 1682, 2048 }, { 7, 1683, 2048 }, { 6, 1684, 2048 }, { 7, 1685, 2048 }, { 7, 1686, 2048 }, { 8, 1687, 2048 },
  10793. { 6, 1688, 2048 }, { 7, 1689, 2048 }, { 7, 1690, 2048 }, { 8, 1691, 2048 }, { 7, 1692, 2048 }, { 8, 1693, 2048 }, { 8, 1694, 2048 }, { 9, 1695, 2048 },
  10794. { 5, 1696, 2048 }, { 6, 1697, 2048 }, { 6, 1698, 2048 }, { 7, 1699, 2048 }, { 6, 1700, 2048 }, { 7, 1701, 2048 }, { 7, 1702, 2048 }, { 8, 1703, 2048 },
  10795. { 6, 1704, 2048 }, { 7, 1705, 2048 }, { 7, 1706, 2048 }, { 8, 1707, 2048 }, { 7, 1708, 2048 }, { 8, 1709, 2048 }, { 8, 1710, 2048 }, { 9, 1711, 2048 },
  10796. { 6, 1712, 2048 }, { 7, 1713, 2048 }, { 7, 1714, 2048 }, { 8, 1715, 2048 }, { 7, 1716, 2048 }, { 8, 1717, 2048 }, { 8, 1718, 2048 }, { 9, 1719, 2048 },
  10797. { 7, 1720, 2048 }, { 8, 1721, 2048 }, { 8, 1722, 2048 }, { 9, 1723, 2048 }, { 8, 1724, 2048 }, { 9, 1725, 2048 }, { 9, 1726, 2048 }, { 10, 1727, 2048 },
  10798. { 5, 1728, 2048 }, { 6, 1729, 2048 }, { 6, 1730, 2048 }, { 7, 1731, 2048 }, { 6, 1732, 2048 }, { 7, 1733, 2048 }, { 7, 1734, 2048 }, { 8, 1735, 2048 },
  10799. { 6, 1736, 2048 }, { 7, 1737, 2048 }, { 7, 1738, 2048 }, { 8, 1739, 2048 }, { 7, 1740, 2048 }, { 8, 1741, 2048 }, { 8, 1742, 2048 }, { 9, 1743, 2048 },
  10800. { 6, 1744, 2048 }, { 7, 1745, 2048 }, { 7, 1746, 2048 }, { 8, 1747, 2048 }, { 7, 1748, 2048 }, { 8, 1749, 2048 }, { 8, 1750, 2048 }, { 9, 1751, 2048 },
  10801. { 7, 1752, 2048 }, { 8, 1753, 2048 }, { 8, 1754, 2048 }, { 9, 1755, 2048 }, { 8, 1756, 2048 }, { 9, 1757, 2048 }, { 9, 1758, 2048 }, { 10, 1759, 2048 },
  10802. { 6, 1760, 2048 }, { 7, 1761, 2048 }, { 7, 1762, 2048 }, { 8, 1763, 2048 }, { 7, 1764, 2048 }, { 8, 1765, 2048 }, { 8, 1766, 2048 }, { 9, 1767, 2048 },
  10803. { 7, 1768, 2048 }, { 8, 1769, 2048 }, { 8, 1770, 2048 }, { 9, 1771, 2048 }, { 8, 1772, 2048 }, { 9, 1773, 2048 }, { 9, 1774, 2048 }, { 10, 1775, 2048 },
  10804. { 7, 1776, 2048 }, { 8, 1777, 2048 }, { 8, 1778, 2048 }, { 9, 1779, 2048 }, { 8, 1780, 2048 }, { 9, 1781, 2048 }, { 9, 1782, 2048 }, { 10, 1783, 2048 },
  10805. { 8, 1784, 2048 }, { 9, 1785, 2048 }, { 9, 1786, 2048 }, { 10, 1787, 2048 }, { 9, 1788, 2048 }, { 10, 1789, 2048 }, { 10, 1790, 2048 }, { 11, 1791, 2048 },
  10806. { 4, 1792, 2048 }, { 5, 1793, 2048 }, { 5, 1794, 2048 }, { 6, 1795, 2048 }, { 5, 1796, 2048 }, { 6, 1797, 2048 }, { 6, 1798, 2048 }, { 7, 1799, 2048 },
  10807. { 5, 1800, 2048 }, { 6, 1801, 2048 }, { 6, 1802, 2048 }, { 7, 1803, 2048 }, { 6, 1804, 2048 }, { 7, 1805, 2048 }, { 7, 1806, 2048 }, { 8, 1807, 2048 },
  10808. { 5, 1808, 2048 }, { 6, 1809, 2048 }, { 6, 1810, 2048 }, { 7, 1811, 2048 }, { 6, 1812, 2048 }, { 7, 1813, 2048 }, { 7, 1814, 2048 }, { 8, 1815, 2048 },
  10809. { 6, 1816, 2048 }, { 7, 1817, 2048 }, { 7, 1818, 2048 }, { 8, 1819, 2048 }, { 7, 1820, 2048 }, { 8, 1821, 2048 }, { 8, 1822, 2048 }, { 9, 1823, 2048 },
  10810. { 5, 1824, 2048 }, { 6, 1825, 2048 }, { 6, 1826, 2048 }, { 7, 1827, 2048 }, { 6, 1828, 2048 }, { 7, 1829, 2048 }, { 7, 1830, 2048 }, { 8, 1831, 2048 },
  10811. { 6, 1832, 2048 }, { 7, 1833, 2048 }, { 7, 1834, 2048 }, { 8, 1835, 2048 }, { 7, 1836, 2048 }, { 8, 1837, 2048 }, { 8, 1838, 2048 }, { 9, 1839, 2048 },
  10812. { 6, 1840, 2048 }, { 7, 1841, 2048 }, { 7, 1842, 2048 }, { 8, 1843, 2048 }, { 7, 1844, 2048 }, { 8, 1845, 2048 }, { 8, 1846, 2048 }, { 9, 1847, 2048 },
  10813. { 7, 1848, 2048 }, { 8, 1849, 2048 }, { 8, 1850, 2048 }, { 9, 1851, 2048 }, { 8, 1852, 2048 }, { 9, 1853, 2048 }, { 9, 1854, 2048 }, { 10, 1855, 2048 },
  10814. { 5, 1856, 2048 }, { 6, 1857, 2048 }, { 6, 1858, 2048 }, { 7, 1859, 2048 }, { 6, 1860, 2048 }, { 7, 1861, 2048 }, { 7, 1862, 2048 }, { 8, 1863, 2048 },
  10815. { 6, 1864, 2048 }, { 7, 1865, 2048 }, { 7, 1866, 2048 }, { 8, 1867, 2048 }, { 7, 1868, 2048 }, { 8, 1869, 2048 }, { 8, 1870, 2048 }, { 9, 1871, 2048 },
  10816. { 6, 1872, 2048 }, { 7, 1873, 2048 }, { 7, 1874, 2048 }, { 8, 1875, 2048 }, { 7, 1876, 2048 }, { 8, 1877, 2048 }, { 8, 1878, 2048 }, { 9, 1879, 2048 },
  10817. { 7, 1880, 2048 }, { 8, 1881, 2048 }, { 8, 1882, 2048 }, { 9, 1883, 2048 }, { 8, 1884, 2048 }, { 9, 1885, 2048 }, { 9, 1886, 2048 }, { 10, 1887, 2048 },
  10818. { 6, 1888, 2048 }, { 7, 1889, 2048 }, { 7, 1890, 2048 }, { 8, 1891, 2048 }, { 7, 1892, 2048 }, { 8, 1893, 2048 }, { 8, 1894, 2048 }, { 9, 1895, 2048 },
  10819. { 7, 1896, 2048 }, { 8, 1897, 2048 }, { 8, 1898, 2048 }, { 9, 1899, 2048 }, { 8, 1900, 2048 }, { 9, 1901, 2048 }, { 9, 1902, 2048 }, { 10, 1903, 2048 },
  10820. { 7, 1904, 2048 }, { 8, 1905, 2048 }, { 8, 1906, 2048 }, { 9, 1907, 2048 }, { 8, 1908, 2048 }, { 9, 1909, 2048 }, { 9, 1910, 2048 }, { 10, 1911, 2048 },
  10821. { 8, 1912, 2048 }, { 9, 1913, 2048 }, { 9, 1914, 2048 }, { 10, 1915, 2048 }, { 9, 1916, 2048 }, { 10, 1917, 2048 }, { 10, 1918, 2048 }, { 11, 1919, 2048 },
  10822. { 5, 1920, 2048 }, { 6, 1921, 2048 }, { 6, 1922, 2048 }, { 7, 1923, 2048 }, { 6, 1924, 2048 }, { 7, 1925, 2048 }, { 7, 1926, 2048 }, { 8, 1927, 2048 },
  10823. { 6, 1928, 2048 }, { 7, 1929, 2048 }, { 7, 1930, 2048 }, { 8, 1931, 2048 }, { 7, 1932, 2048 }, { 8, 1933, 2048 }, { 8, 1934, 2048 }, { 9, 1935, 2048 },
  10824. { 6, 1936, 2048 }, { 7, 1937, 2048 }, { 7, 1938, 2048 }, { 8, 1939, 2048 }, { 7, 1940, 2048 }, { 8, 1941, 2048 }, { 8, 1942, 2048 }, { 9, 1943, 2048 },
  10825. { 7, 1944, 2048 }, { 8, 1945, 2048 }, { 8, 1946, 2048 }, { 9, 1947, 2048 }, { 8, 1948, 2048 }, { 9, 1949, 2048 }, { 9, 1950, 2048 }, { 10, 1951, 2048 },
  10826. { 6, 1952, 2048 }, { 7, 1953, 2048 }, { 7, 1954, 2048 }, { 8, 1955, 2048 }, { 7, 1956, 2048 }, { 8, 1957, 2048 }, { 8, 1958, 2048 }, { 9, 1959, 2048 },
  10827. { 7, 1960, 2048 }, { 8, 1961, 2048 }, { 8, 1962, 2048 }, { 9, 1963, 2048 }, { 8, 1964, 2048 }, { 9, 1965, 2048 }, { 9, 1966, 2048 }, { 10, 1967, 2048 },
  10828. { 7, 1968, 2048 }, { 8, 1969, 2048 }, { 8, 1970, 2048 }, { 9, 1971, 2048 }, { 8, 1972, 2048 }, { 9, 1973, 2048 }, { 9, 1974, 2048 }, { 10, 1975, 2048 },
  10829. { 8, 1976, 2048 }, { 9, 1977, 2048 }, { 9, 1978, 2048 }, { 10, 1979, 2048 }, { 9, 1980, 2048 }, { 10, 1981, 2048 }, { 10, 1982, 2048 }, { 11, 1983, 2048 },
  10830. { 6, 1984, 2048 }, { 7, 1985, 2048 }, { 7, 1986, 2048 }, { 8, 1987, 2048 }, { 7, 1988, 2048 }, { 8, 1989, 2048 }, { 8, 1990, 2048 }, { 9, 1991, 2048 },
  10831. { 7, 1992, 2048 }, { 8, 1993, 2048 }, { 8, 1994, 2048 }, { 9, 1995, 2048 }, { 8, 1996, 2048 }, { 9, 1997, 2048 }, { 9, 1998, 2048 }, { 10, 1999, 2048 },
  10832. { 7, 2000, 2048 }, { 8, 2001, 2048 }, { 8, 2002, 2048 }, { 9, 2003, 2048 }, { 8, 2004, 2048 }, { 9, 2005, 2048 }, { 9, 2006, 2048 }, { 10, 2007, 2048 },
  10833. { 8, 2008, 2048 }, { 9, 2009, 2048 }, { 9, 2010, 2048 }, { 10, 2011, 2048 }, { 9, 2012, 2048 }, { 10, 2013, 2048 }, { 10, 2014, 2048 }, { 11, 2015, 2048 },
  10834. { 7, 2016, 2048 }, { 8, 2017, 2048 }, { 8, 2018, 2048 }, { 9, 2019, 2048 }, { 8, 2020, 2048 }, { 9, 2021, 2048 }, { 9, 2022, 2048 }, { 10, 2023, 2048 },
  10835. { 8, 2024, 2048 }, { 9, 2025, 2048 }, { 9, 2026, 2048 }, { 10, 2027, 2048 }, { 9, 2028, 2048 }, { 10, 2029, 2048 }, { 10, 2030, 2048 }, { 11, 2031, 2048 },
  10836. { 8, 2032, 2048 }, { 9, 2033, 2048 }, { 9, 2034, 2048 }, { 10, 2035, 2048 }, { 9, 2036, 2048 }, { 10, 2037, 2048 }, { 10, 2038, 2048 }, { 11, 2039, 2048 },
  10837. { 9, 2040, 2048 }, { 10, 2041, 2048 }, { 10, 2042, 2048 }, { 11, 2043, 2048 }, { 10, 2044, 2048 }, { 11, 2045, 2048 }, { 11, 2046, 2048 }, { 12, 2047, 2048 },
  10838. #endif
  10839. #endif
  10840. #endif
  10841. #endif
  10842. #endif
  10843. #endif
  10844. };
  10845. /* find a hole and free as required, return -1 if no hole found */
  10846. static int find_hole(void)
  10847. {
  10848. int x, y, z;
  10849. for (z = -1, y = INT_MAX, x = 0; x < FP_ENTRIES; x++) {
  10850. if (fp_cache[x].lru_count < y && fp_cache[x].lock == 0) {
  10851. z = x;
  10852. y = fp_cache[x].lru_count;
  10853. }
  10854. }
  10855. /* decrease all */
  10856. for (x = 0; x < FP_ENTRIES; x++) {
  10857. if (fp_cache[x].lru_count > 3) {
  10858. --(fp_cache[x].lru_count);
  10859. }
  10860. }
  10861. /* free entry z */
  10862. if (z >= 0 && fp_cache[z].g) {
  10863. mp_clear(&fp_cache[z].mu);
  10864. wc_ecc_del_point(fp_cache[z].g);
  10865. fp_cache[z].g = NULL;
  10866. for (x = 0; x < (1<<FP_LUT); x++) {
  10867. wc_ecc_del_point(fp_cache[z].LUT[x]);
  10868. fp_cache[z].LUT[x] = NULL;
  10869. }
  10870. fp_cache[z].LUT_set = 0;
  10871. fp_cache[z].lru_count = 0;
  10872. }
  10873. return z;
  10874. }
  10875. /* determine if a base is already in the cache and if so, where */
  10876. static int find_base(ecc_point* g)
  10877. {
  10878. int x;
  10879. for (x = 0; x < FP_ENTRIES; x++) {
  10880. if (fp_cache[x].g != NULL &&
  10881. mp_cmp(fp_cache[x].g->x, g->x) == MP_EQ &&
  10882. mp_cmp(fp_cache[x].g->y, g->y) == MP_EQ &&
  10883. mp_cmp(fp_cache[x].g->z, g->z) == MP_EQ) {
  10884. break;
  10885. }
  10886. }
  10887. if (x == FP_ENTRIES) {
  10888. x = -1;
  10889. }
  10890. return x;
  10891. }
  10892. /* add a new base to the cache */
  10893. static int add_entry(int idx, ecc_point *g)
  10894. {
  10895. unsigned x, y;
  10896. /* allocate base and LUT */
  10897. fp_cache[idx].g = wc_ecc_new_point();
  10898. if (fp_cache[idx].g == NULL) {
  10899. return GEN_MEM_ERR;
  10900. }
  10901. /* copy x and y */
  10902. if ((mp_copy(g->x, fp_cache[idx].g->x) != MP_OKAY) ||
  10903. (mp_copy(g->y, fp_cache[idx].g->y) != MP_OKAY) ||
  10904. (mp_copy(g->z, fp_cache[idx].g->z) != MP_OKAY)) {
  10905. wc_ecc_del_point(fp_cache[idx].g);
  10906. fp_cache[idx].g = NULL;
  10907. return GEN_MEM_ERR;
  10908. }
  10909. for (x = 0; x < (1U<<FP_LUT); x++) {
  10910. fp_cache[idx].LUT[x] = wc_ecc_new_point();
  10911. if (fp_cache[idx].LUT[x] == NULL) {
  10912. for (y = 0; y < x; y++) {
  10913. wc_ecc_del_point(fp_cache[idx].LUT[y]);
  10914. fp_cache[idx].LUT[y] = NULL;
  10915. }
  10916. wc_ecc_del_point(fp_cache[idx].g);
  10917. fp_cache[idx].g = NULL;
  10918. fp_cache[idx].lru_count = 0;
  10919. return GEN_MEM_ERR;
  10920. }
  10921. }
  10922. fp_cache[idx].LUT_set = 0;
  10923. fp_cache[idx].lru_count = 0;
  10924. return MP_OKAY;
  10925. }
  10926. #endif
  10927. #if !defined(WOLFSSL_SP_MATH)
  10928. /* build the LUT by spacing the bits of the input by #modulus/FP_LUT bits apart
  10929. *
  10930. * The algorithm builds patterns in increasing bit order by first making all
  10931. * single bit input patterns, then all two bit input patterns and so on
  10932. */
  10933. static int build_lut(int idx, mp_int* a, mp_int* modulus, mp_digit mp,
  10934. mp_int* mu)
  10935. {
  10936. int err;
  10937. unsigned x, y, bitlen, lut_gap;
  10938. #ifdef WOLFSSL_SMALL_STACK
  10939. mp_int *tmp = NULL;
  10940. #else
  10941. mp_int tmp[1];
  10942. #endif
  10943. int infinity;
  10944. #ifdef WOLFSSL_SMALL_STACK
  10945. if ((tmp = (mp_int *)XMALLOC(sizeof(*tmp), NULL, DYNAMIC_TYPE_ECC_BUFFER)) == NULL)
  10946. return MEMORY_E;
  10947. #endif
  10948. err = mp_init(tmp);
  10949. if (err != MP_OKAY) {
  10950. err = GEN_MEM_ERR;
  10951. goto errout;
  10952. }
  10953. /* sanity check to make sure lut_order table is of correct size,
  10954. should compile out to a NOP if true */
  10955. if ((sizeof(lut_orders) / sizeof(lut_orders[0])) < (1U<<FP_LUT)) {
  10956. err = BAD_FUNC_ARG;
  10957. goto errout;
  10958. }
  10959. /* get bitlen and round up to next multiple of FP_LUT */
  10960. bitlen = (unsigned)mp_unsigned_bin_size(modulus) << 3;
  10961. x = bitlen % FP_LUT;
  10962. if (x) {
  10963. bitlen += FP_LUT - x;
  10964. }
  10965. lut_gap = bitlen / FP_LUT;
  10966. /* init the mu */
  10967. err = mp_init_copy(&fp_cache[idx].mu, mu);
  10968. if (err != MP_OKAY)
  10969. goto errout;
  10970. /* copy base */
  10971. if ((mp_mulmod(fp_cache[idx].g->x, mu, modulus,
  10972. fp_cache[idx].LUT[1]->x) != MP_OKAY) ||
  10973. (mp_mulmod(fp_cache[idx].g->y, mu, modulus,
  10974. fp_cache[idx].LUT[1]->y) != MP_OKAY) ||
  10975. (mp_mulmod(fp_cache[idx].g->z, mu, modulus,
  10976. fp_cache[idx].LUT[1]->z) != MP_OKAY)) {
  10977. err = MP_MULMOD_E;
  10978. goto errout;
  10979. }
  10980. /* make all single bit entries */
  10981. for (x = 1; x < FP_LUT; x++) {
  10982. if ((mp_copy(fp_cache[idx].LUT[1<<(x-1)]->x,
  10983. fp_cache[idx].LUT[1<<x]->x) != MP_OKAY) ||
  10984. (mp_copy(fp_cache[idx].LUT[1<<(x-1)]->y,
  10985. fp_cache[idx].LUT[1<<x]->y) != MP_OKAY) ||
  10986. (mp_copy(fp_cache[idx].LUT[1<<(x-1)]->z,
  10987. fp_cache[idx].LUT[1<<x]->z) != MP_OKAY)){
  10988. err = MP_INIT_E;
  10989. goto errout;
  10990. } else {
  10991. /* now double it bitlen/FP_LUT times */
  10992. for (y = 0; y < lut_gap; y++) {
  10993. if ((err = ecc_projective_dbl_point_safe(fp_cache[idx].LUT[1<<x],
  10994. fp_cache[idx].LUT[1<<x], a, modulus, mp)) != MP_OKAY) {
  10995. goto errout;
  10996. }
  10997. }
  10998. }
  10999. }
  11000. /* now make all entries in increase order of hamming weight */
  11001. for (x = 2; x <= FP_LUT; x++) {
  11002. if (err != MP_OKAY)
  11003. goto errout;
  11004. for (y = 0; y < (1UL<<FP_LUT); y++) {
  11005. if (lut_orders[y].ham != (int)x) continue;
  11006. /* perform the add */
  11007. if ((err = ecc_projective_add_point_safe(
  11008. fp_cache[idx].LUT[lut_orders[y].terma],
  11009. fp_cache[idx].LUT[lut_orders[y].termb],
  11010. fp_cache[idx].LUT[y], a, modulus, mp,
  11011. &infinity)) != MP_OKAY) {
  11012. goto errout;
  11013. }
  11014. }
  11015. }
  11016. /* now map all entries back to affine space to make point addition faster */
  11017. for (x = 1; x < (1UL<<FP_LUT); x++) {
  11018. if (err != MP_OKAY)
  11019. break;
  11020. /* convert z to normal from montgomery */
  11021. err = mp_montgomery_reduce(fp_cache[idx].LUT[x]->z, modulus, mp);
  11022. /* invert it */
  11023. if (err == MP_OKAY)
  11024. err = mp_invmod(fp_cache[idx].LUT[x]->z, modulus,
  11025. fp_cache[idx].LUT[x]->z);
  11026. if (err == MP_OKAY)
  11027. /* now square it */
  11028. err = mp_sqrmod(fp_cache[idx].LUT[x]->z, modulus, tmp);
  11029. if (err == MP_OKAY)
  11030. /* fix x */
  11031. err = mp_mulmod(fp_cache[idx].LUT[x]->x, tmp, modulus,
  11032. fp_cache[idx].LUT[x]->x);
  11033. if (err == MP_OKAY)
  11034. /* get 1/z^3 */
  11035. err = mp_mulmod(tmp, fp_cache[idx].LUT[x]->z, modulus, tmp);
  11036. if (err == MP_OKAY)
  11037. /* fix y */
  11038. err = mp_mulmod(fp_cache[idx].LUT[x]->y, tmp, modulus,
  11039. fp_cache[idx].LUT[x]->y);
  11040. if (err == MP_OKAY)
  11041. /* free z */
  11042. mp_clear(fp_cache[idx].LUT[x]->z);
  11043. }
  11044. errout:
  11045. mp_clear(tmp);
  11046. #ifdef WOLFSSL_SMALL_STACK
  11047. XFREE(tmp, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11048. #endif
  11049. if (err == MP_OKAY) {
  11050. fp_cache[idx].LUT_set = 1;
  11051. return MP_OKAY;
  11052. }
  11053. /* err cleanup */
  11054. for (y = 0; y < (1U<<FP_LUT); y++) {
  11055. wc_ecc_del_point(fp_cache[idx].LUT[y]);
  11056. fp_cache[idx].LUT[y] = NULL;
  11057. }
  11058. wc_ecc_del_point(fp_cache[idx].g);
  11059. fp_cache[idx].g = NULL;
  11060. fp_cache[idx].LUT_set = 0;
  11061. fp_cache[idx].lru_count = 0;
  11062. mp_clear(&fp_cache[idx].mu);
  11063. return err;
  11064. }
  11065. /* perform a fixed point ECC mulmod */
  11066. static int accel_fp_mul(int idx, const mp_int* k, ecc_point *R, mp_int* a,
  11067. mp_int* modulus, mp_digit mp, int map)
  11068. {
  11069. #ifdef WOLFCRYPT_HAVE_SAKKE
  11070. #define KB_SIZE 256
  11071. #else
  11072. #define KB_SIZE 128
  11073. #endif
  11074. #ifdef WOLFSSL_SMALL_STACK
  11075. unsigned char* kb = NULL;
  11076. mp_int* tk = NULL;
  11077. mp_int* order = NULL;
  11078. #else
  11079. unsigned char kb[KB_SIZE];
  11080. mp_int tk[1];
  11081. mp_int order[1];
  11082. #endif
  11083. int x, err;
  11084. unsigned y, z = 0, bitlen, bitpos, lut_gap;
  11085. int first;
  11086. int tk_zeroize = 0;
  11087. #ifdef WOLFSSL_SMALL_STACK
  11088. tk = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  11089. if (tk == NULL) {
  11090. err = MEMORY_E; goto done;
  11091. }
  11092. order = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  11093. if (order == NULL) {
  11094. err = MEMORY_E; goto done;
  11095. }
  11096. #endif
  11097. if (mp_init_multi(tk, order, NULL, NULL, NULL, NULL) != MP_OKAY) {
  11098. err = MP_INIT_E; goto done;
  11099. }
  11100. if ((err = mp_copy(k, tk)) != MP_OKAY)
  11101. goto done;
  11102. tk_zeroize = 1;
  11103. #ifdef WOLFSSL_CHECK_MEM_ZERO
  11104. mp_memzero_add("accel_fp_mul tk", tk);
  11105. #endif
  11106. /* if it's smaller than modulus we fine */
  11107. if (mp_unsigned_bin_size(k) > mp_unsigned_bin_size(modulus)) {
  11108. /* find order */
  11109. y = (unsigned)mp_unsigned_bin_size(modulus);
  11110. for (x = 0; ecc_sets[x].size; x++) {
  11111. if (y <= (unsigned)ecc_sets[x].size) break;
  11112. }
  11113. /* back off if we are on the 521 bit curve */
  11114. if (y == 66) --x;
  11115. if ((err = mp_read_radix(order, ecc_sets[x].order,
  11116. MP_RADIX_HEX)) != MP_OKAY) {
  11117. goto done;
  11118. }
  11119. /* k must be less than modulus */
  11120. if (mp_cmp(tk, order) != MP_LT) {
  11121. if ((err = mp_mod(tk, order, tk)) != MP_OKAY) {
  11122. goto done;
  11123. }
  11124. }
  11125. }
  11126. /* get bitlen and round up to next multiple of FP_LUT */
  11127. bitlen = (unsigned)mp_unsigned_bin_size(modulus) << 3;
  11128. x = bitlen % FP_LUT;
  11129. if (x) {
  11130. bitlen += FP_LUT - (unsigned)x;
  11131. }
  11132. lut_gap = bitlen / FP_LUT;
  11133. /* get the k value */
  11134. if (mp_unsigned_bin_size(tk) > (int)(KB_SIZE - 2)) {
  11135. err = BUFFER_E; goto done;
  11136. }
  11137. /* store k */
  11138. #ifdef WOLFSSL_SMALL_STACK
  11139. kb = (unsigned char*)XMALLOC(KB_SIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11140. if (kb == NULL) {
  11141. err = MEMORY_E; goto done;
  11142. }
  11143. #endif
  11144. XMEMSET(kb, 0, KB_SIZE);
  11145. if ((err = mp_to_unsigned_bin(tk, kb)) == MP_OKAY) {
  11146. #ifdef WOLFSSL_CHECK_MEM_ZERO
  11147. wc_MemZero_Add("accel_fp_mul kb", kb, KB_SIZE);
  11148. #endif
  11149. /* let's reverse kb so it's little endian */
  11150. x = 0;
  11151. y = (unsigned)mp_unsigned_bin_size(tk);
  11152. if (y > 0) {
  11153. y -= 1;
  11154. }
  11155. while ((unsigned)x < y) {
  11156. z = kb[x]; kb[x] = kb[y]; kb[y] = (byte)z;
  11157. ++x; --y;
  11158. }
  11159. /* at this point we can start, yipee */
  11160. first = 1;
  11161. for (x = (int)lut_gap-1; x >= 0; x--) {
  11162. /* extract FP_LUT bits from kb spread out by lut_gap bits and offset
  11163. by x bits from the start */
  11164. bitpos = (unsigned)x;
  11165. for (y = z = 0; y < FP_LUT; y++) {
  11166. z |= ((kb[bitpos>>3] >> (bitpos&7)) & 1) << y;
  11167. bitpos += lut_gap; /* it's y*lut_gap + x, but here we can avoid
  11168. the mult in each loop */
  11169. }
  11170. /* double if not first */
  11171. if (!first) {
  11172. if ((err = ecc_projective_dbl_point_safe(R, R, a, modulus,
  11173. mp)) != MP_OKAY) {
  11174. break;
  11175. }
  11176. }
  11177. /* add if not first, otherwise copy */
  11178. if (!first && z) {
  11179. if ((err = ecc_projective_add_point_safe(R, fp_cache[idx].LUT[z],
  11180. R, a, modulus, mp, &first)) != MP_OKAY) {
  11181. break;
  11182. }
  11183. } else if (z) {
  11184. if ((mp_copy(fp_cache[idx].LUT[z]->x, R->x) != MP_OKAY) ||
  11185. (mp_copy(fp_cache[idx].LUT[z]->y, R->y) != MP_OKAY) ||
  11186. (mp_copy(&fp_cache[idx].mu, R->z) != MP_OKAY)) {
  11187. err = GEN_MEM_ERR;
  11188. break;
  11189. }
  11190. first = 0;
  11191. }
  11192. }
  11193. }
  11194. if (err == MP_OKAY) {
  11195. (void) z; /* Acknowledge the unused assignment */
  11196. ForceZero(kb, KB_SIZE);
  11197. /* map R back from projective space */
  11198. if (map) {
  11199. err = ecc_map(R, modulus, mp);
  11200. } else {
  11201. err = MP_OKAY;
  11202. }
  11203. }
  11204. done:
  11205. /* cleanup */
  11206. mp_clear(order);
  11207. /* Ensure it was initialized. */
  11208. if (tk_zeroize) {
  11209. mp_forcezero(tk);
  11210. }
  11211. #ifdef WOLFSSL_SMALL_STACK
  11212. XFREE(kb, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11213. XFREE(order, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11214. XFREE(tk, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11215. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  11216. wc_MemZero_Check(kb, KB_SIZE);
  11217. mp_memzero_check(tk);
  11218. #endif
  11219. #undef KB_SIZE
  11220. return err;
  11221. }
  11222. #endif
  11223. #ifdef ECC_SHAMIR
  11224. #if !defined(WOLFSSL_SP_MATH)
  11225. /* perform a fixed point ECC mulmod */
  11226. static int accel_fp_mul2add(int idx1, int idx2,
  11227. mp_int* kA, mp_int* kB,
  11228. ecc_point *R, mp_int* a,
  11229. mp_int* modulus, mp_digit mp)
  11230. {
  11231. #define KB_SIZE 128
  11232. #ifdef WOLFSSL_SMALL_STACK
  11233. unsigned char* kb[2] = {NULL, NULL};
  11234. mp_int* tka = NULL;
  11235. mp_int* tkb = NULL;
  11236. mp_int* order = NULL;
  11237. #else
  11238. unsigned char kb[2][KB_SIZE];
  11239. mp_int tka[1];
  11240. mp_int tkb[1];
  11241. mp_int order[1];
  11242. #endif
  11243. int x, err;
  11244. unsigned y, z, bitlen, bitpos, lut_gap, zA, zB;
  11245. int first;
  11246. #ifdef WOLFSSL_SMALL_STACK
  11247. tka = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  11248. if (tka == NULL) {
  11249. err = MEMORY_E; goto done;
  11250. }
  11251. tkb = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  11252. if (tkb == NULL) {
  11253. err = MEMORY_E; goto done;
  11254. }
  11255. order = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC);
  11256. if (order == NULL) {
  11257. err = MEMORY_E; goto done;
  11258. }
  11259. #endif
  11260. if (mp_init_multi(tka, tkb, order, NULL, NULL, NULL) != MP_OKAY) {
  11261. err = MP_INIT_E; goto done;
  11262. }
  11263. /* if it's smaller than modulus we fine */
  11264. if (mp_unsigned_bin_size(kA) > mp_unsigned_bin_size(modulus)) {
  11265. /* find order */
  11266. y = (unsigned)mp_unsigned_bin_size(modulus);
  11267. for (x = 0; ecc_sets[x].size; x++) {
  11268. if (y <= (unsigned)ecc_sets[x].size) break;
  11269. }
  11270. /* back off if we are on the 521 bit curve */
  11271. if (y == 66) --x;
  11272. if ((err = mp_read_radix(order, ecc_sets[x].order,
  11273. MP_RADIX_HEX)) != MP_OKAY) {
  11274. goto done;
  11275. }
  11276. /* kA must be less than modulus */
  11277. if (mp_cmp(kA, order) != MP_LT) {
  11278. if ((err = mp_mod(kA, order, tka)) != MP_OKAY) {
  11279. goto done;
  11280. }
  11281. } else {
  11282. if ((err = mp_copy(kA, tka)) != MP_OKAY) {
  11283. goto done;
  11284. }
  11285. }
  11286. } else {
  11287. if ((err = mp_copy(kA, tka)) != MP_OKAY) {
  11288. goto done;
  11289. }
  11290. }
  11291. #ifdef WOLFSSL_CHECK_MEM_ZERO
  11292. mp_memzero_add("accel_fp_mul2add tka", tka);
  11293. #endif
  11294. /* if it's smaller than modulus we fine */
  11295. if (mp_unsigned_bin_size(kB) > mp_unsigned_bin_size(modulus)) {
  11296. /* find order */
  11297. y = (unsigned)mp_unsigned_bin_size(modulus);
  11298. for (x = 0; ecc_sets[x].size; x++) {
  11299. if (y <= (unsigned)ecc_sets[x].size) break;
  11300. }
  11301. /* back off if we are on the 521 bit curve */
  11302. if (y == 66) --x;
  11303. if ((err = mp_read_radix(order, ecc_sets[x].order,
  11304. MP_RADIX_HEX)) != MP_OKAY) {
  11305. goto done;
  11306. }
  11307. /* kB must be less than modulus */
  11308. if (mp_cmp(kB, order) != MP_LT) {
  11309. if ((err = mp_mod(kB, order, tkb)) != MP_OKAY) {
  11310. goto done;
  11311. }
  11312. } else {
  11313. if ((err = mp_copy(kB, tkb)) != MP_OKAY) {
  11314. goto done;
  11315. }
  11316. }
  11317. } else {
  11318. if ((err = mp_copy(kB, tkb)) != MP_OKAY) {
  11319. goto done;
  11320. }
  11321. }
  11322. #ifdef WOLFSSL_CHECK_MEM_ZERO
  11323. mp_memzero_add("accel_fp_mul2add tkb", tkb);
  11324. #endif
  11325. /* get bitlen and round up to next multiple of FP_LUT */
  11326. bitlen = (unsigned)mp_unsigned_bin_size(modulus) << 3;
  11327. x = bitlen % FP_LUT;
  11328. if (x) {
  11329. bitlen += FP_LUT - (unsigned)x;
  11330. }
  11331. lut_gap = bitlen / FP_LUT;
  11332. /* get the k value */
  11333. if ((mp_unsigned_bin_size(tka) > (int)(KB_SIZE - 2)) ||
  11334. (mp_unsigned_bin_size(tkb) > (int)(KB_SIZE - 2)) ) {
  11335. err = BUFFER_E; goto done;
  11336. }
  11337. /* store k */
  11338. #ifdef WOLFSSL_SMALL_STACK
  11339. kb[0] = (unsigned char*)XMALLOC(KB_SIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11340. if (kb[0] == NULL) {
  11341. err = MEMORY_E; goto done;
  11342. }
  11343. #endif
  11344. XMEMSET(kb[0], 0, KB_SIZE);
  11345. if ((err = mp_to_unsigned_bin(tka, kb[0])) != MP_OKAY) {
  11346. goto done;
  11347. }
  11348. #ifdef WOLFSSL_CHECK_MEM_ZERO
  11349. wc_MemZero_Add("accel_fp_mul2add kb[0]", kb[0], KB_SIZE);
  11350. #endif
  11351. /* let's reverse kb so it's little endian */
  11352. x = 0;
  11353. y = (unsigned)mp_unsigned_bin_size(tka);
  11354. if (y > 0) {
  11355. y -= 1;
  11356. }
  11357. mp_clear(tka);
  11358. while ((unsigned)x < y) {
  11359. z = kb[0][x]; kb[0][x] = kb[0][y]; kb[0][y] = (byte)z;
  11360. ++x; --y;
  11361. }
  11362. /* store b */
  11363. #ifdef WOLFSSL_SMALL_STACK
  11364. kb[1] = (unsigned char*)XMALLOC(KB_SIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11365. if (kb[1] == NULL) {
  11366. err = MEMORY_E; goto done;
  11367. }
  11368. #endif
  11369. XMEMSET(kb[1], 0, KB_SIZE);
  11370. #ifdef WOLFSSL_CHECK_MEM_ZERO
  11371. wc_MemZero_Add("accel_fp_mul2add kb[1]", kb[1], KB_SIZE);
  11372. #endif
  11373. if ((err = mp_to_unsigned_bin(tkb, kb[1])) == MP_OKAY) {
  11374. x = 0;
  11375. y = (unsigned)mp_unsigned_bin_size(tkb);
  11376. if (y > 0) {
  11377. y -= 1;
  11378. }
  11379. while ((unsigned)x < y) {
  11380. z = kb[1][x]; kb[1][x] = kb[1][y]; kb[1][y] = (byte)z;
  11381. ++x; --y;
  11382. }
  11383. /* at this point we can start, yipee */
  11384. first = 1;
  11385. for (x = (int)lut_gap-1; x >= 0; x--) {
  11386. /* extract FP_LUT bits from kb spread out by lut_gap bits and
  11387. offset by x bits from the start */
  11388. bitpos = (unsigned)x;
  11389. for (y = zA = zB = 0; y < FP_LUT; y++) {
  11390. zA |= ((kb[0][bitpos>>3] >> (bitpos&7)) & 1) << y;
  11391. zB |= ((kb[1][bitpos>>3] >> (bitpos&7)) & 1) << y;
  11392. bitpos += lut_gap; /* it's y*lut_gap + x, but here we can avoid
  11393. the mult in each loop */
  11394. }
  11395. /* double if not first */
  11396. if (!first) {
  11397. if ((err = ecc_projective_dbl_point_safe(R, R, a, modulus,
  11398. mp)) != MP_OKAY) {
  11399. break;
  11400. }
  11401. /* add if not first, otherwise copy */
  11402. if (zA) {
  11403. if ((err = ecc_projective_add_point_safe(R,
  11404. fp_cache[idx1].LUT[zA], R, a,
  11405. modulus, mp, &first)) != MP_OKAY) {
  11406. break;
  11407. }
  11408. }
  11409. if (zB) {
  11410. if ((err = ecc_projective_add_point_safe(R,
  11411. fp_cache[idx2].LUT[zB], R, a,
  11412. modulus, mp, &first)) != MP_OKAY) {
  11413. break;
  11414. }
  11415. }
  11416. } else {
  11417. if (zA) {
  11418. if ((mp_copy(fp_cache[idx1].LUT[zA]->x, R->x) != MP_OKAY) ||
  11419. (mp_copy(fp_cache[idx1].LUT[zA]->y, R->y) != MP_OKAY) ||
  11420. (mp_copy(&fp_cache[idx1].mu, R->z) != MP_OKAY)) {
  11421. err = GEN_MEM_ERR;
  11422. break;
  11423. }
  11424. first = 0;
  11425. }
  11426. if (zB && first == 0) {
  11427. if ((err = ecc_projective_add_point_safe(R,
  11428. fp_cache[idx2].LUT[zB], R, a,
  11429. modulus, mp, &first)) != MP_OKAY){
  11430. break;
  11431. }
  11432. } else if (zB && first == 1) {
  11433. if ((mp_copy(fp_cache[idx2].LUT[zB]->x, R->x) != MP_OKAY) ||
  11434. (mp_copy(fp_cache[idx2].LUT[zB]->y, R->y) != MP_OKAY) ||
  11435. (mp_copy(&fp_cache[idx2].mu, R->z) != MP_OKAY)) {
  11436. err = GEN_MEM_ERR;
  11437. break;
  11438. }
  11439. first = 0;
  11440. }
  11441. }
  11442. }
  11443. }
  11444. done:
  11445. /* cleanup */
  11446. mp_forcezero(tkb);
  11447. mp_forcezero(tka);
  11448. mp_clear(order);
  11449. #ifdef WOLFSSL_SMALL_STACK
  11450. if (kb[0])
  11451. #endif
  11452. ForceZero(kb[0], KB_SIZE);
  11453. #ifdef WOLFSSL_SMALL_STACK
  11454. if (kb[1])
  11455. #endif
  11456. ForceZero(kb[1], KB_SIZE);
  11457. #ifdef WOLFSSL_SMALL_STACK
  11458. XFREE(kb[1], NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11459. XFREE(kb[0], NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11460. XFREE(order, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11461. XFREE(tkb, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11462. XFREE(tka, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11463. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  11464. wc_MemZero_Check(kb[1], KB_SIZE);
  11465. wc_MemZero_Check(kb[0], KB_SIZE);
  11466. mp_memzero_check(tkb);
  11467. mp_memzero_check(tka);
  11468. #endif
  11469. #undef KB_SIZE
  11470. if (err != MP_OKAY)
  11471. return err;
  11472. return ecc_map(R, modulus, mp);
  11473. }
  11474. /** ECC Fixed Point mulmod global with heap hint used
  11475. Computes kA*A + kB*B = C using Shamir's Trick
  11476. A First point to multiply
  11477. kA What to multiple A by
  11478. B Second point to multiply
  11479. kB What to multiple B by
  11480. C [out] Destination point (can overlap with A or B)
  11481. a ECC curve parameter a
  11482. modulus Modulus for curve
  11483. return MP_OKAY on success
  11484. */
  11485. int ecc_mul2add(ecc_point* A, mp_int* kA,
  11486. ecc_point* B, mp_int* kB,
  11487. ecc_point* C, mp_int* a, mp_int* modulus, void* heap)
  11488. {
  11489. int idx1 = -1, idx2 = -1, err, mpInit = 0;
  11490. mp_digit mp;
  11491. #ifdef WOLFSSL_SMALL_STACK
  11492. mp_int *mu = (mp_int *)XMALLOC(sizeof *mu, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11493. if (mu == NULL)
  11494. return MP_MEM;
  11495. #else
  11496. mp_int mu[1];
  11497. #endif
  11498. err = mp_init(mu);
  11499. if (err != MP_OKAY) {
  11500. #ifdef WOLFSSL_SMALL_STACK
  11501. XFREE(mu, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11502. #endif
  11503. return err;
  11504. }
  11505. #ifndef HAVE_THREAD_LS
  11506. if (initMutex == 0) { /* extra sanity check if wolfCrypt_Init not called */
  11507. wc_InitMutex(&ecc_fp_lock);
  11508. initMutex = 1;
  11509. }
  11510. if (wc_LockMutex(&ecc_fp_lock) != 0) {
  11511. #ifdef WOLFSSL_SMALL_STACK
  11512. XFREE(mu, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11513. #endif
  11514. return BAD_MUTEX_E;
  11515. }
  11516. #endif /* HAVE_THREAD_LS */
  11517. SAVE_VECTOR_REGISTERS(err = _svr_ret;);
  11518. /* find point */
  11519. idx1 = find_base(A);
  11520. /* no entry? */
  11521. if (idx1 == -1) {
  11522. /* find hole and add it */
  11523. if ((idx1 = find_hole()) >= 0) {
  11524. err = add_entry(idx1, A);
  11525. }
  11526. }
  11527. if (err == MP_OKAY && idx1 != -1) {
  11528. /* increment LRU */
  11529. ++(fp_cache[idx1].lru_count);
  11530. }
  11531. if (err == MP_OKAY) {
  11532. /* find point */
  11533. idx2 = find_base(B);
  11534. /* no entry? */
  11535. if (idx2 == -1) {
  11536. /* find hole and add it */
  11537. if ((idx2 = find_hole()) >= 0)
  11538. err = add_entry(idx2, B);
  11539. }
  11540. }
  11541. if (err == MP_OKAY && idx2 != -1) {
  11542. /* increment LRU */
  11543. ++(fp_cache[idx2].lru_count);
  11544. }
  11545. if (err == MP_OKAY) {
  11546. /* if it's >= 2 AND the LUT is not set build the LUT */
  11547. if (idx1 >= 0 && fp_cache[idx1].lru_count >= 2 && !fp_cache[idx1].LUT_set) {
  11548. /* compute mp */
  11549. err = mp_montgomery_setup(modulus, &mp);
  11550. if (err == MP_OKAY) {
  11551. mpInit = 1;
  11552. err = mp_montgomery_calc_normalization(mu, modulus);
  11553. }
  11554. if (err == MP_OKAY)
  11555. /* build the LUT */
  11556. err = build_lut(idx1, a, modulus, mp, mu);
  11557. }
  11558. }
  11559. if (err == MP_OKAY) {
  11560. /* if it's >= 2 AND the LUT is not set build the LUT */
  11561. if (idx2 >= 0 && fp_cache[idx2].lru_count >= 2 && !fp_cache[idx2].LUT_set) {
  11562. if (mpInit == 0) {
  11563. /* compute mp */
  11564. err = mp_montgomery_setup(modulus, &mp);
  11565. if (err == MP_OKAY) {
  11566. mpInit = 1;
  11567. err = mp_montgomery_calc_normalization(mu, modulus);
  11568. }
  11569. }
  11570. if (err == MP_OKAY)
  11571. /* build the LUT */
  11572. err = build_lut(idx2, a, modulus, mp, mu);
  11573. }
  11574. }
  11575. if (err == MP_OKAY) {
  11576. if (idx1 >=0 && idx2 >= 0 && fp_cache[idx1].LUT_set &&
  11577. fp_cache[idx2].LUT_set) {
  11578. if (mpInit == 0) {
  11579. /* compute mp */
  11580. err = mp_montgomery_setup(modulus, &mp);
  11581. }
  11582. if (err == MP_OKAY)
  11583. err = accel_fp_mul2add(idx1, idx2, kA, kB, C, a, modulus, mp);
  11584. } else {
  11585. err = normal_ecc_mul2add(A, kA, B, kB, C, a, modulus, heap);
  11586. }
  11587. }
  11588. RESTORE_VECTOR_REGISTERS();
  11589. #ifndef HAVE_THREAD_LS
  11590. wc_UnLockMutex(&ecc_fp_lock);
  11591. #endif /* HAVE_THREAD_LS */
  11592. mp_clear(mu);
  11593. #ifdef WOLFSSL_SMALL_STACK
  11594. XFREE(mu, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11595. #endif
  11596. return err;
  11597. }
  11598. #endif
  11599. #endif /* ECC_SHAMIR */
  11600. /** ECC Fixed Point mulmod global
  11601. k The multiplicand
  11602. G Base point to multiply
  11603. R [out] Destination of product
  11604. a ECC curve parameter a
  11605. modulus The modulus for the curve
  11606. map [boolean] If non-zero maps the point back to affine coordinates,
  11607. otherwise it's left in jacobian-montgomery form
  11608. return MP_OKAY if successful
  11609. */
  11610. int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
  11611. mp_int* modulus, int map, void* heap)
  11612. {
  11613. #if !defined(WOLFSSL_SP_MATH)
  11614. int idx, err = MP_OKAY;
  11615. mp_digit mp;
  11616. #ifdef WOLFSSL_SMALL_STACK
  11617. mp_int *mu = NULL;
  11618. #else
  11619. mp_int mu[1];
  11620. #endif
  11621. int mpSetup = 0;
  11622. #ifndef HAVE_THREAD_LS
  11623. int got_ecc_fp_lock = 0;
  11624. #endif
  11625. if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL) {
  11626. return ECC_BAD_ARG_E;
  11627. }
  11628. /* k can't have more bits than modulus count plus 1 */
  11629. if (mp_count_bits(k) > mp_count_bits(modulus) + 1) {
  11630. return ECC_OUT_OF_RANGE_E;
  11631. }
  11632. #ifdef WOLFSSL_SMALL_STACK
  11633. if ((mu = (mp_int *)XMALLOC(sizeof(*mu), NULL, DYNAMIC_TYPE_ECC_BUFFER)) == NULL)
  11634. return MP_MEM;
  11635. #endif
  11636. if (mp_init(mu) != MP_OKAY) {
  11637. err = MP_INIT_E;
  11638. goto out;
  11639. }
  11640. #ifndef HAVE_THREAD_LS
  11641. if (initMutex == 0) { /* extra sanity check if wolfCrypt_Init not called */
  11642. wc_InitMutex(&ecc_fp_lock);
  11643. initMutex = 1;
  11644. }
  11645. if (wc_LockMutex(&ecc_fp_lock) != 0) {
  11646. err = BAD_MUTEX_E;
  11647. goto out;
  11648. }
  11649. got_ecc_fp_lock = 1;
  11650. #endif /* HAVE_THREAD_LS */
  11651. SAVE_VECTOR_REGISTERS(err = _svr_ret; goto out;);
  11652. /* find point */
  11653. idx = find_base(G);
  11654. /* no entry? */
  11655. if (idx == -1) {
  11656. /* find hole and add it */
  11657. idx = find_hole();
  11658. if (idx >= 0)
  11659. err = add_entry(idx, G);
  11660. }
  11661. if (err == MP_OKAY && idx >= 0) {
  11662. /* increment LRU */
  11663. ++(fp_cache[idx].lru_count);
  11664. }
  11665. if (err == MP_OKAY) {
  11666. /* if it's 2 build the LUT, if it's higher just use the LUT */
  11667. if (idx >= 0 && fp_cache[idx].lru_count >= 2 && !fp_cache[idx].LUT_set) {
  11668. /* compute mp */
  11669. err = mp_montgomery_setup(modulus, &mp);
  11670. if (err == MP_OKAY) {
  11671. /* compute mu */
  11672. mpSetup = 1;
  11673. err = mp_montgomery_calc_normalization(mu, modulus);
  11674. }
  11675. if (err == MP_OKAY)
  11676. /* build the LUT */
  11677. err = build_lut(idx, a, modulus, mp, mu);
  11678. }
  11679. }
  11680. if (err == MP_OKAY) {
  11681. if (idx >= 0 && fp_cache[idx].LUT_set) {
  11682. if (mpSetup == 0) {
  11683. /* compute mp */
  11684. err = mp_montgomery_setup(modulus, &mp);
  11685. }
  11686. if (err == MP_OKAY)
  11687. err = accel_fp_mul(idx, k, R, a, modulus, mp, map);
  11688. } else {
  11689. err = normal_ecc_mulmod(k, G, R, a, modulus, NULL, map, heap);
  11690. }
  11691. }
  11692. RESTORE_VECTOR_REGISTERS();
  11693. out:
  11694. #ifndef HAVE_THREAD_LS
  11695. if (got_ecc_fp_lock)
  11696. wc_UnLockMutex(&ecc_fp_lock);
  11697. #endif /* HAVE_THREAD_LS */
  11698. mp_clear(mu);
  11699. #ifdef WOLFSSL_SMALL_STACK
  11700. XFREE(mu, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11701. #endif
  11702. return err;
  11703. #else /* WOLFSSL_SP_MATH */
  11704. if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL) {
  11705. return ECC_BAD_ARG_E;
  11706. }
  11707. if (mp_count_bits(G->x) > mp_count_bits(modulus) ||
  11708. mp_count_bits(G->y) > mp_count_bits(modulus) ||
  11709. mp_count_bits(G->z) > mp_count_bits(modulus)) {
  11710. return IS_POINT_E;
  11711. }
  11712. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  11713. if ((mp_count_bits(modulus) == 256) && (!mp_is_bit_set(modulus, 224))) {
  11714. int ret;
  11715. SAVE_VECTOR_REGISTERS(return _svr_ret);
  11716. ret = sp_ecc_mulmod_sm2_256(k, G, R, map, heap);
  11717. RESTORE_VECTOR_REGISTERS();
  11718. return ret;
  11719. }
  11720. #endif
  11721. #ifndef WOLFSSL_SP_NO_256
  11722. if (mp_count_bits(modulus) == 256) {
  11723. int ret;
  11724. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  11725. ret = sp_ecc_mulmod_256(k, G, R, map, heap);
  11726. RESTORE_VECTOR_REGISTERS();
  11727. return ret;
  11728. }
  11729. #endif
  11730. #ifdef WOLFSSL_SP_384
  11731. if (mp_count_bits(modulus) == 384) {
  11732. int ret;
  11733. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  11734. ret = sp_ecc_mulmod_384(k, G, R, map, heap);
  11735. RESTORE_VECTOR_REGISTERS();
  11736. return ret;
  11737. }
  11738. #endif
  11739. #ifdef WOLFSSL_SP_521
  11740. if (mp_count_bits(modulus) == 521) {
  11741. int ret;
  11742. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  11743. ret = sp_ecc_mulmod_521(k, G, R, map, heap);
  11744. RESTORE_VECTOR_REGISTERS();
  11745. return ret;
  11746. }
  11747. #endif
  11748. return WC_KEY_SIZE_E;
  11749. #endif /* WOLFSSL_SP_MATH */
  11750. }
  11751. /** ECC Fixed Point mulmod global
  11752. k The multiplicand
  11753. G Base point to multiply
  11754. R [out] Destination of product
  11755. a ECC curve parameter a
  11756. modulus The modulus for the curve
  11757. map [boolean] If non-zero maps the point back to affine coordinates,
  11758. otherwise it's left in jacobian-montgomery form
  11759. return MP_OKAY if successful
  11760. */
  11761. int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
  11762. mp_int* modulus, mp_int* order, WC_RNG* rng, int map, void* heap)
  11763. {
  11764. #if !defined(WOLFSSL_SP_MATH)
  11765. int idx, err = MP_OKAY;
  11766. mp_digit mp;
  11767. #ifdef WOLFSSL_SMALL_STACK
  11768. mp_int *mu = NULL;
  11769. #else
  11770. mp_int mu[1];
  11771. #endif
  11772. int mpSetup = 0;
  11773. #ifndef HAVE_THREAD_LS
  11774. int got_ecc_fp_lock = 0;
  11775. #endif
  11776. if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL ||
  11777. order == NULL) {
  11778. return ECC_BAD_ARG_E;
  11779. }
  11780. /* k can't have more bits than order */
  11781. if (mp_count_bits(k) > mp_count_bits(order)) {
  11782. return ECC_OUT_OF_RANGE_E;
  11783. }
  11784. #ifdef WOLFSSL_SMALL_STACK
  11785. if ((mu = (mp_int *)XMALLOC(sizeof(*mu), NULL, DYNAMIC_TYPE_ECC_BUFFER)) == NULL)
  11786. return MP_MEM;
  11787. #endif
  11788. if (mp_init(mu) != MP_OKAY) {
  11789. err = MP_INIT_E;
  11790. goto out;
  11791. }
  11792. #ifndef HAVE_THREAD_LS
  11793. if (initMutex == 0) { /* extra sanity check if wolfCrypt_Init not called */
  11794. wc_InitMutex(&ecc_fp_lock);
  11795. initMutex = 1;
  11796. }
  11797. if (wc_LockMutex(&ecc_fp_lock) != 0) {
  11798. err = BAD_MUTEX_E;
  11799. goto out;
  11800. }
  11801. got_ecc_fp_lock = 1;
  11802. #endif /* HAVE_THREAD_LS */
  11803. SAVE_VECTOR_REGISTERS(err = _svr_ret; goto out;);
  11804. /* find point */
  11805. idx = find_base(G);
  11806. /* no entry? */
  11807. if (idx == -1) {
  11808. /* find hole and add it */
  11809. idx = find_hole();
  11810. if (idx >= 0)
  11811. err = add_entry(idx, G);
  11812. }
  11813. if (err == MP_OKAY && idx >= 0) {
  11814. /* increment LRU */
  11815. ++(fp_cache[idx].lru_count);
  11816. }
  11817. if (err == MP_OKAY) {
  11818. /* if it's 2 build the LUT, if it's higher just use the LUT */
  11819. if (idx >= 0 && fp_cache[idx].lru_count >= 2 && !fp_cache[idx].LUT_set) {
  11820. /* compute mp */
  11821. err = mp_montgomery_setup(modulus, &mp);
  11822. if (err == MP_OKAY) {
  11823. /* compute mu */
  11824. mpSetup = 1;
  11825. err = mp_montgomery_calc_normalization(mu, modulus);
  11826. }
  11827. if (err == MP_OKAY)
  11828. /* build the LUT */
  11829. err = build_lut(idx, a, modulus, mp, mu);
  11830. }
  11831. }
  11832. if (err == MP_OKAY) {
  11833. if (idx >= 0 && fp_cache[idx].LUT_set) {
  11834. if (mpSetup == 0) {
  11835. /* compute mp */
  11836. err = mp_montgomery_setup(modulus, &mp);
  11837. }
  11838. if (err == MP_OKAY)
  11839. err = accel_fp_mul(idx, k, R, a, modulus, mp, map);
  11840. } else {
  11841. err = normal_ecc_mulmod(k, G, R, a, modulus, rng, map, heap);
  11842. }
  11843. }
  11844. RESTORE_VECTOR_REGISTERS();
  11845. out:
  11846. #ifndef HAVE_THREAD_LS
  11847. if (got_ecc_fp_lock)
  11848. wc_UnLockMutex(&ecc_fp_lock);
  11849. #endif /* HAVE_THREAD_LS */
  11850. mp_clear(mu);
  11851. #ifdef WOLFSSL_SMALL_STACK
  11852. XFREE(mu, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  11853. #endif
  11854. return err;
  11855. #else /* WOLFSSL_SP_MATH */
  11856. (void)rng;
  11857. if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL ||
  11858. order == NULL) {
  11859. return ECC_BAD_ARG_E;
  11860. }
  11861. if (mp_count_bits(G->x) > mp_count_bits(modulus) ||
  11862. mp_count_bits(G->y) > mp_count_bits(modulus) ||
  11863. mp_count_bits(G->z) > mp_count_bits(modulus)) {
  11864. return IS_POINT_E;
  11865. }
  11866. #if defined(WOLFSSL_SM2) && defined(WOLFSSL_SP_SM2)
  11867. if ((mp_count_bits(modulus) == 256) && (!mp_is_bit_set(modulus, 224))) {
  11868. int ret;
  11869. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  11870. ret = sp_ecc_mulmod_sm2_256(k, G, R, map, heap);
  11871. RESTORE_VECTOR_REGISTERS();
  11872. return ret;
  11873. }
  11874. #endif
  11875. #ifndef WOLFSSL_SP_NO_256
  11876. if (mp_count_bits(modulus) == 256) {
  11877. int ret;
  11878. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  11879. ret = sp_ecc_mulmod_256(k, G, R, map, heap);
  11880. RESTORE_VECTOR_REGISTERS();
  11881. return ret;
  11882. }
  11883. #endif
  11884. #ifdef WOLFSSL_SP_384
  11885. if (mp_count_bits(modulus) == 384) {
  11886. int ret;
  11887. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  11888. ret = sp_ecc_mulmod_384(k, G, R, map, heap);
  11889. RESTORE_VECTOR_REGISTERS();
  11890. return ret;
  11891. }
  11892. #endif
  11893. #ifdef WOLFSSL_SP_521
  11894. if (mp_count_bits(modulus) == 521) {
  11895. int ret;
  11896. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  11897. ret = sp_ecc_mulmod_521(k, G, R, map, heap);
  11898. RESTORE_VECTOR_REGISTERS();
  11899. return ret;
  11900. }
  11901. #endif
  11902. return WC_KEY_SIZE_E;
  11903. #endif /* WOLFSSL_SP_MATH */
  11904. }
  11905. #if !defined(WOLFSSL_SP_MATH)
  11906. /* helper function for freeing the cache ...
  11907. must be called with the cache mutex locked */
  11908. static void wc_ecc_fp_free_cache(void)
  11909. {
  11910. unsigned x, y;
  11911. for (x = 0; x < FP_ENTRIES; x++) {
  11912. if (fp_cache[x].g != NULL) {
  11913. for (y = 0; y < (1U<<FP_LUT); y++) {
  11914. wc_ecc_del_point(fp_cache[x].LUT[y]);
  11915. fp_cache[x].LUT[y] = NULL;
  11916. }
  11917. wc_ecc_del_point(fp_cache[x].g);
  11918. fp_cache[x].g = NULL;
  11919. mp_clear(&fp_cache[x].mu);
  11920. fp_cache[x].LUT_set = 0;
  11921. fp_cache[x].lru_count = 0;
  11922. fp_cache[x].lock = 0;
  11923. }
  11924. }
  11925. }
  11926. #endif
  11927. /** Init the Fixed Point cache */
  11928. void wc_ecc_fp_init(void)
  11929. {
  11930. #ifndef WOLFSSL_SP_MATH
  11931. #ifndef HAVE_THREAD_LS
  11932. if (initMutex == 0) {
  11933. wc_InitMutex(&ecc_fp_lock);
  11934. initMutex = 1;
  11935. }
  11936. #endif
  11937. #endif
  11938. }
  11939. /** Free the Fixed Point cache */
  11940. WOLFSSL_ABI
  11941. void wc_ecc_fp_free(void)
  11942. {
  11943. #if !defined(WOLFSSL_SP_MATH)
  11944. #ifndef HAVE_THREAD_LS
  11945. if (initMutex == 0) { /* extra sanity check if wolfCrypt_Init not called */
  11946. wc_InitMutex(&ecc_fp_lock);
  11947. initMutex = 1;
  11948. }
  11949. if (wc_LockMutex(&ecc_fp_lock) == 0) {
  11950. #endif /* HAVE_THREAD_LS */
  11951. wc_ecc_fp_free_cache();
  11952. #ifndef HAVE_THREAD_LS
  11953. wc_UnLockMutex(&ecc_fp_lock);
  11954. wc_FreeMutex(&ecc_fp_lock);
  11955. initMutex = 0;
  11956. }
  11957. #endif /* HAVE_THREAD_LS */
  11958. #endif
  11959. }
  11960. #endif /* FP_ECC */
  11961. int wc_ecc_set_rng(ecc_key* key, WC_RNG* rng)
  11962. {
  11963. int err = 0;
  11964. #ifdef ECC_TIMING_RESISTANT
  11965. if (key == NULL) {
  11966. err = BAD_FUNC_ARG;
  11967. }
  11968. else {
  11969. key->rng = rng;
  11970. }
  11971. #else
  11972. (void)key;
  11973. (void)rng;
  11974. /* report success, not an error if ECC_TIMING_RESISTANT is not defined */
  11975. #endif
  11976. return err;
  11977. }
  11978. #ifdef HAVE_ECC_ENCRYPT
  11979. enum ecCliState {
  11980. ecCLI_INIT = 1,
  11981. ecCLI_SALT_GET = 2,
  11982. ecCLI_SALT_SET = 3,
  11983. ecCLI_SENT_REQ = 4,
  11984. ecCLI_RECV_RESP = 5,
  11985. ecCLI_BAD_STATE = 99
  11986. };
  11987. enum ecSrvState {
  11988. ecSRV_INIT = 1,
  11989. ecSRV_SALT_GET = 2,
  11990. ecSRV_SALT_SET = 3,
  11991. ecSRV_RECV_REQ = 4,
  11992. ecSRV_SENT_RESP = 5,
  11993. ecSRV_BAD_STATE = 99
  11994. };
  11995. struct ecEncCtx {
  11996. const byte* kdfSalt; /* optional salt for kdf */
  11997. const byte* kdfInfo; /* optional info for kdf */
  11998. const byte* macSalt; /* optional salt for mac */
  11999. word32 kdfSaltSz; /* size of kdfSalt */
  12000. word32 kdfInfoSz; /* size of kdfInfo */
  12001. word32 macSaltSz; /* size of macSalt */
  12002. void* heap; /* heap hint for memory used */
  12003. byte clientSalt[EXCHANGE_SALT_SZ]; /* for msg exchange */
  12004. byte serverSalt[EXCHANGE_SALT_SZ]; /* for msg exchange */
  12005. byte encAlgo; /* which encryption type */
  12006. byte kdfAlgo; /* which key derivation function type */
  12007. byte macAlgo; /* which mac function type */
  12008. byte protocol; /* are we REQ_RESP client or server ? */
  12009. byte cliSt; /* protocol state, for sanity checks */
  12010. byte srvSt; /* protocol state, for sanity checks */
  12011. WC_RNG* rng;
  12012. };
  12013. /* optional set info, can be called before or after set_peer_salt */
  12014. int wc_ecc_ctx_set_algo(ecEncCtx* ctx, byte encAlgo, byte kdfAlgo, byte macAlgo)
  12015. {
  12016. if (ctx == NULL)
  12017. return BAD_FUNC_ARG;
  12018. ctx->encAlgo = encAlgo;
  12019. ctx->kdfAlgo = kdfAlgo;
  12020. ctx->macAlgo = macAlgo;
  12021. return 0;
  12022. }
  12023. const byte* wc_ecc_ctx_get_own_salt(ecEncCtx* ctx)
  12024. {
  12025. if (ctx == NULL || ctx->protocol == 0)
  12026. return NULL;
  12027. if (ctx->protocol == REQ_RESP_CLIENT) {
  12028. if (ctx->cliSt == ecCLI_INIT) {
  12029. ctx->cliSt = ecCLI_SALT_GET;
  12030. return ctx->clientSalt;
  12031. }
  12032. else {
  12033. ctx->cliSt = ecCLI_BAD_STATE;
  12034. return NULL;
  12035. }
  12036. }
  12037. else if (ctx->protocol == REQ_RESP_SERVER) {
  12038. if (ctx->srvSt == ecSRV_INIT) {
  12039. ctx->srvSt = ecSRV_SALT_GET;
  12040. return ctx->serverSalt;
  12041. }
  12042. else {
  12043. ctx->srvSt = ecSRV_BAD_STATE;
  12044. return NULL;
  12045. }
  12046. }
  12047. return NULL;
  12048. }
  12049. /* optional set info, can be called before or after set_peer_salt */
  12050. int wc_ecc_ctx_set_info(ecEncCtx* ctx, const byte* info, int sz)
  12051. {
  12052. if (ctx == NULL || info == 0 || sz < 0)
  12053. return BAD_FUNC_ARG;
  12054. ctx->kdfInfo = info;
  12055. ctx->kdfInfoSz = (word32)sz;
  12056. return 0;
  12057. }
  12058. static const char* exchange_info = "Secure Message Exchange";
  12059. int wc_ecc_ctx_set_peer_salt(ecEncCtx* ctx, const byte* salt)
  12060. {
  12061. byte tmp[EXCHANGE_SALT_SZ/2];
  12062. int halfSz = EXCHANGE_SALT_SZ/2;
  12063. if (ctx == NULL || ctx->protocol == 0 || salt == NULL)
  12064. return BAD_FUNC_ARG;
  12065. if (ctx->protocol == REQ_RESP_CLIENT) {
  12066. XMEMCPY(ctx->serverSalt, salt, EXCHANGE_SALT_SZ);
  12067. if (ctx->cliSt == ecCLI_SALT_GET)
  12068. ctx->cliSt = ecCLI_SALT_SET;
  12069. else {
  12070. ctx->cliSt = ecCLI_BAD_STATE;
  12071. return BAD_STATE_E;
  12072. }
  12073. }
  12074. else {
  12075. XMEMCPY(ctx->clientSalt, salt, EXCHANGE_SALT_SZ);
  12076. if (ctx->srvSt == ecSRV_SALT_GET)
  12077. ctx->srvSt = ecSRV_SALT_SET;
  12078. else {
  12079. ctx->srvSt = ecSRV_BAD_STATE;
  12080. return BAD_STATE_E;
  12081. }
  12082. }
  12083. /* mix half and half */
  12084. /* tmp stores 2nd half of client before overwrite */
  12085. XMEMCPY(tmp, ctx->clientSalt + halfSz, (size_t)halfSz);
  12086. XMEMCPY(ctx->clientSalt + halfSz, ctx->serverSalt, (size_t)halfSz);
  12087. XMEMCPY(ctx->serverSalt, tmp, (size_t)halfSz);
  12088. ctx->kdfSalt = ctx->clientSalt;
  12089. ctx->kdfSaltSz = EXCHANGE_SALT_SZ;
  12090. ctx->macSalt = ctx->serverSalt;
  12091. ctx->macSaltSz = EXCHANGE_SALT_SZ;
  12092. if (ctx->kdfInfo == NULL) {
  12093. /* default info */
  12094. ctx->kdfInfo = (const byte*)exchange_info;
  12095. ctx->kdfInfoSz = EXCHANGE_INFO_SZ;
  12096. }
  12097. return 0;
  12098. }
  12099. /* Set the salt pointer into context.
  12100. *
  12101. * @param [in, out] ctx ECIES context object.
  12102. * @param [in] salt Salt to use with KDF.
  12103. * @param [in] len Length of salt in bytes.
  12104. * @return 0 on success.
  12105. * @return BAD_FUNC_ARG when ctx is NULL or salt is NULL and len is not 0.
  12106. */
  12107. int wc_ecc_ctx_set_kdf_salt(ecEncCtx* ctx, const byte* salt, word32 len)
  12108. {
  12109. if (ctx == NULL || (salt == NULL && len != 0))
  12110. return BAD_FUNC_ARG;
  12111. ctx->kdfSalt = salt;
  12112. ctx->kdfSaltSz = len;
  12113. if (ctx->protocol == REQ_RESP_CLIENT) {
  12114. ctx->cliSt = ecCLI_SALT_SET;
  12115. }
  12116. else if (ctx->protocol == REQ_RESP_SERVER) {
  12117. ctx->srvSt = ecSRV_SALT_SET;
  12118. }
  12119. return 0;
  12120. }
  12121. static int ecc_ctx_set_salt(ecEncCtx* ctx, int flags)
  12122. {
  12123. byte* saltBuffer = NULL;
  12124. if (ctx == NULL || flags == 0)
  12125. return BAD_FUNC_ARG;
  12126. saltBuffer = (flags == REQ_RESP_CLIENT) ? ctx->clientSalt : ctx->serverSalt;
  12127. return wc_RNG_GenerateBlock(ctx->rng, saltBuffer, EXCHANGE_SALT_SZ);
  12128. }
  12129. static void ecc_ctx_init(ecEncCtx* ctx, int flags, WC_RNG* rng)
  12130. {
  12131. if (ctx) {
  12132. XMEMSET(ctx, 0, sizeof(ecEncCtx));
  12133. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  12134. #ifdef WOLFSSL_AES_128
  12135. ctx->encAlgo = ecAES_128_CBC;
  12136. #else
  12137. ctx->encAlgo = ecAES_256_CBC;
  12138. #endif
  12139. #elif !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  12140. #ifdef WOLFSSL_AES_256
  12141. ctx->encAlgo = ecAES_256_CTR;
  12142. #else
  12143. ctx->encAlgo = ecAES_128_CTR;
  12144. #endif
  12145. #else
  12146. #error "No valid encryption algorithm for ECIES configured."
  12147. #endif
  12148. ctx->kdfAlgo = ecHKDF_SHA256;
  12149. ctx->macAlgo = ecHMAC_SHA256;
  12150. ctx->protocol = (byte)flags;
  12151. ctx->rng = rng;
  12152. if (flags == REQ_RESP_CLIENT)
  12153. ctx->cliSt = ecCLI_INIT;
  12154. if (flags == REQ_RESP_SERVER)
  12155. ctx->srvSt = ecSRV_INIT;
  12156. }
  12157. }
  12158. /* allow ecc context reset so user doesn't have to init/free for reuse */
  12159. WOLFSSL_ABI
  12160. int wc_ecc_ctx_reset(ecEncCtx* ctx, WC_RNG* rng)
  12161. {
  12162. if (ctx == NULL || rng == NULL)
  12163. return BAD_FUNC_ARG;
  12164. ecc_ctx_init(ctx, ctx->protocol, rng);
  12165. return ecc_ctx_set_salt(ctx, ctx->protocol);
  12166. }
  12167. ecEncCtx* wc_ecc_ctx_new_ex(int flags, WC_RNG* rng, void* heap)
  12168. {
  12169. int ret = 0;
  12170. ecEncCtx* ctx = (ecEncCtx*)XMALLOC(sizeof(ecEncCtx), heap,
  12171. DYNAMIC_TYPE_ECC);
  12172. if (ctx) {
  12173. ctx->protocol = (byte)flags;
  12174. ctx->heap = heap;
  12175. }
  12176. ret = wc_ecc_ctx_reset(ctx, rng);
  12177. if (ret != 0) {
  12178. wc_ecc_ctx_free(ctx);
  12179. ctx = NULL;
  12180. }
  12181. return ctx;
  12182. }
  12183. /* alloc/init and set defaults, return new Context */
  12184. WOLFSSL_ABI
  12185. ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng)
  12186. {
  12187. return wc_ecc_ctx_new_ex(flags, rng, NULL);
  12188. }
  12189. /* free any resources, clear any keys */
  12190. WOLFSSL_ABI
  12191. void wc_ecc_ctx_free(ecEncCtx* ctx)
  12192. {
  12193. if (ctx) {
  12194. void* heap = ctx->heap;
  12195. ForceZero(ctx, sizeof(ecEncCtx));
  12196. XFREE(ctx, heap, DYNAMIC_TYPE_ECC);
  12197. (void)heap;
  12198. }
  12199. }
  12200. static int ecc_get_key_sizes(ecEncCtx* ctx, int* encKeySz, int* ivSz,
  12201. int* keysLen, word32* digestSz, word32* blockSz)
  12202. {
  12203. if (ctx) {
  12204. switch (ctx->encAlgo) {
  12205. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  12206. case ecAES_128_CBC:
  12207. *encKeySz = KEY_SIZE_128;
  12208. *ivSz = IV_SIZE_128;
  12209. *blockSz = AES_BLOCK_SIZE;
  12210. break;
  12211. case ecAES_256_CBC:
  12212. *encKeySz = KEY_SIZE_256;
  12213. *ivSz = IV_SIZE_128;
  12214. *blockSz = AES_BLOCK_SIZE;
  12215. break;
  12216. #endif
  12217. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  12218. case ecAES_128_CTR:
  12219. *encKeySz = KEY_SIZE_128;
  12220. *ivSz = 12;
  12221. *blockSz = 1;
  12222. break;
  12223. case ecAES_256_CTR:
  12224. *encKeySz = KEY_SIZE_256;
  12225. *ivSz = 12;
  12226. *blockSz = 1;
  12227. break;
  12228. #endif
  12229. default:
  12230. return BAD_FUNC_ARG;
  12231. }
  12232. switch (ctx->macAlgo) {
  12233. case ecHMAC_SHA256:
  12234. *digestSz = WC_SHA256_DIGEST_SIZE;
  12235. break;
  12236. default:
  12237. return BAD_FUNC_ARG;
  12238. }
  12239. } else
  12240. return BAD_FUNC_ARG;
  12241. #ifdef WOLFSSL_ECIES_OLD
  12242. *keysLen = *encKeySz + *ivSz + (int)*digestSz;
  12243. #else
  12244. *keysLen = *encKeySz + (int)*digestSz;
  12245. #endif
  12246. return 0;
  12247. }
  12248. /* ecc encrypt with shared secret run through kdf
  12249. ctx holds non default algos and inputs
  12250. msgSz should be the right size for encAlgo, i.e., already padded
  12251. return 0 on success */
  12252. int wc_ecc_encrypt_ex(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  12253. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx, int compressed)
  12254. {
  12255. int ret = 0;
  12256. word32 blockSz = 0;
  12257. #ifndef WOLFSSL_ECIES_OLD
  12258. #ifndef WOLFSSL_ECIES_GEN_IV
  12259. byte iv[ECC_MAX_IV_SIZE];
  12260. #endif
  12261. word32 pubKeySz = 0;
  12262. #endif
  12263. word32 digestSz = 0;
  12264. ecEncCtx localCtx;
  12265. #ifdef WOLFSSL_SMALL_STACK
  12266. byte* sharedSecret;
  12267. byte* keys;
  12268. #else
  12269. #if defined(WOLFSSL_ECIES_OLD) || !defined(WOLFSSL_ECIES_ISO18033)
  12270. byte sharedSecret[ECC_MAXSIZE]; /* 521 max size */
  12271. #else
  12272. byte sharedSecret[ECC_MAXSIZE * 3 + 1]; /* Public key too */
  12273. #endif
  12274. byte keys[ECC_BUFSIZE]; /* max size */
  12275. #endif
  12276. #if defined(WOLFSSL_ECIES_OLD) || !defined(WOLFSSL_ECIES_ISO18033)
  12277. word32 sharedSz = ECC_MAXSIZE;
  12278. #else
  12279. /* 'Uncompressed' byte | public key x | public key y | secret */
  12280. word32 sharedSz = 1 + ECC_MAXSIZE * 3;
  12281. #endif
  12282. int keysLen = 0;
  12283. int encKeySz = 0;
  12284. int ivSz = 0;
  12285. int offset = 0; /* keys offset if doing msg exchange */
  12286. byte* encKey = NULL;
  12287. byte* encIv = NULL;
  12288. byte* macKey = NULL;
  12289. if (privKey == NULL || pubKey == NULL || msg == NULL || out == NULL ||
  12290. outSz == NULL)
  12291. return BAD_FUNC_ARG;
  12292. if (ctx == NULL) { /* use defaults */
  12293. ecc_ctx_init(&localCtx, 0, NULL);
  12294. ctx = &localCtx;
  12295. }
  12296. ret = ecc_get_key_sizes(ctx, &encKeySz, &ivSz, &keysLen, &digestSz,
  12297. &blockSz);
  12298. if (ret != 0)
  12299. return ret;
  12300. #ifndef WOLFSSL_ECIES_OLD
  12301. if (!compressed) {
  12302. pubKeySz = 1 + (word32)wc_ecc_size(privKey) * 2;
  12303. }
  12304. else {
  12305. pubKeySz = 1 + (word32)wc_ecc_size(privKey);
  12306. }
  12307. #else
  12308. (void) compressed; /* avoid unused parameter if WOLFSSL_ECIES_OLD is defined */
  12309. #endif
  12310. if (ctx->protocol == REQ_RESP_SERVER) {
  12311. offset = keysLen;
  12312. keysLen *= 2;
  12313. if (ctx->srvSt != ecSRV_RECV_REQ)
  12314. return BAD_STATE_E;
  12315. ctx->srvSt = ecSRV_BAD_STATE; /* we're done no more ops allowed */
  12316. }
  12317. else if (ctx->protocol == REQ_RESP_CLIENT) {
  12318. if (ctx->cliSt != ecCLI_SALT_SET)
  12319. return BAD_STATE_E;
  12320. ctx->cliSt = ecCLI_SENT_REQ; /* only do this once */
  12321. }
  12322. if (keysLen > ECC_BUFSIZE) /* keys size */
  12323. return BUFFER_E;
  12324. if ((msgSz % blockSz) != 0)
  12325. return BAD_PADDING_E;
  12326. #ifdef WOLFSSL_ECIES_OLD
  12327. if (*outSz < (msgSz + digestSz))
  12328. return BUFFER_E;
  12329. #elif defined(WOLFSSL_ECIES_GEN_IV)
  12330. if (*outSz < (pubKeySz + ivSz + msgSz + digestSz))
  12331. return BUFFER_E;
  12332. #else
  12333. if (*outSz < (pubKeySz + msgSz + digestSz))
  12334. return BUFFER_E;
  12335. #endif
  12336. #ifdef ECC_TIMING_RESISTANT
  12337. if (ctx->rng != NULL && privKey->rng == NULL)
  12338. privKey->rng = ctx->rng;
  12339. #endif
  12340. #ifndef WOLFSSL_ECIES_OLD
  12341. if (privKey->type == ECC_PRIVATEKEY_ONLY) {
  12342. #ifdef ECC_TIMING_RESISTANT
  12343. ret = wc_ecc_make_pub_ex(privKey, NULL, privKey->rng);
  12344. #else
  12345. ret = wc_ecc_make_pub_ex(privKey, NULL, NULL);
  12346. #endif
  12347. if (ret != 0)
  12348. return ret;
  12349. }
  12350. ret = wc_ecc_export_x963_ex(privKey, out, &pubKeySz, compressed);
  12351. if (ret != 0)
  12352. return ret;
  12353. out += pubKeySz;
  12354. #endif
  12355. #ifdef WOLFSSL_SMALL_STACK
  12356. sharedSecret = (byte*)XMALLOC(sharedSz, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12357. if (sharedSecret == NULL)
  12358. return MEMORY_E;
  12359. keys = (byte*)XMALLOC(ECC_BUFSIZE, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12360. if (keys == NULL) {
  12361. XFREE(sharedSecret, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12362. return MEMORY_E;
  12363. }
  12364. #endif
  12365. SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
  12366. #ifdef WOLFSSL_ECIES_ISO18033
  12367. XMEMCPY(sharedSecret, out - pubKeySz, pubKeySz);
  12368. sharedSz -= pubKeySz;
  12369. #endif
  12370. do {
  12371. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  12372. ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  12373. if (ret != 0)
  12374. break;
  12375. #endif
  12376. #ifndef WOLFSSL_ECIES_ISO18033
  12377. ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret, &sharedSz);
  12378. #else
  12379. ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret + pubKeySz,
  12380. &sharedSz);
  12381. #endif
  12382. }
  12383. while (ret == WC_PENDING_E);
  12384. if (ret == 0) {
  12385. #ifdef WOLFSSL_ECIES_ISO18033
  12386. /* KDF data is encoded public key and secret. */
  12387. sharedSz += pubKeySz;
  12388. #endif
  12389. switch (ctx->kdfAlgo) {
  12390. case ecHKDF_SHA256 :
  12391. ret = wc_HKDF(WC_SHA256, sharedSecret, sharedSz, ctx->kdfSalt,
  12392. ctx->kdfSaltSz, ctx->kdfInfo, ctx->kdfInfoSz,
  12393. keys, (word32)keysLen);
  12394. break;
  12395. case ecHKDF_SHA1 :
  12396. ret = wc_HKDF(WC_SHA, sharedSecret, sharedSz, ctx->kdfSalt,
  12397. ctx->kdfSaltSz, ctx->kdfInfo, ctx->kdfInfoSz,
  12398. keys, (word32)keysLen);
  12399. break;
  12400. #if defined(HAVE_X963_KDF) && !defined(NO_HASH_WRAPPER)
  12401. case ecKDF_X963_SHA1 :
  12402. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, sharedSecret, sharedSz,
  12403. ctx->kdfInfo, ctx->kdfInfoSz, keys, (word32)keysLen);
  12404. break;
  12405. case ecKDF_X963_SHA256 :
  12406. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, sharedSecret, sharedSz,
  12407. ctx->kdfInfo, ctx->kdfInfoSz, keys, (word32)keysLen);
  12408. break;
  12409. case ecKDF_SHA1 :
  12410. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, sharedSecret, sharedSz,
  12411. NULL, 0, keys, (word32)keysLen);
  12412. break;
  12413. case ecKDF_SHA256 :
  12414. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, sharedSecret, sharedSz,
  12415. NULL, 0, keys, (word32)keysLen);
  12416. break;
  12417. #endif
  12418. default:
  12419. ret = BAD_FUNC_ARG;
  12420. break;
  12421. }
  12422. }
  12423. if (ret == 0) {
  12424. #ifdef WOLFSSL_ECIES_OLD
  12425. encKey = keys + offset;
  12426. encIv = encKey + encKeySz;
  12427. macKey = encKey + encKeySz + ivSz;
  12428. #elif defined(WOLFSSL_ECIES_GEN_IV)
  12429. encKey = keys + offset;
  12430. encIv = out;
  12431. out += ivSz;
  12432. macKey = encKey + encKeySz;
  12433. ret = wc_RNG_GenerateBlock(privKey->rng, encIv, ivSz);
  12434. #else
  12435. XMEMSET(iv, 0, (size_t)ivSz);
  12436. encKey = keys + offset;
  12437. encIv = iv;
  12438. macKey = encKey + encKeySz;
  12439. #endif
  12440. }
  12441. if (ret == 0) {
  12442. switch (ctx->encAlgo) {
  12443. case ecAES_128_CBC:
  12444. case ecAES_256_CBC:
  12445. {
  12446. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  12447. #ifdef WOLFSSL_SMALL_STACK
  12448. Aes *aes = (Aes *)XMALLOC(sizeof *aes, ctx->heap,
  12449. DYNAMIC_TYPE_AES);
  12450. if (aes == NULL) {
  12451. ret = MEMORY_E;
  12452. break;
  12453. }
  12454. #else
  12455. Aes aes[1];
  12456. #endif
  12457. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  12458. if (ret == 0) {
  12459. ret = wc_AesSetKey(aes, encKey, (word32)encKeySz, encIv,
  12460. AES_ENCRYPTION);
  12461. if (ret == 0) {
  12462. ret = wc_AesCbcEncrypt(aes, out, msg, msgSz);
  12463. #if defined(WOLFSSL_ASYNC_CRYPT) && \
  12464. defined(WC_ASYNC_ENABLE_AES)
  12465. ret = wc_AsyncWait(ret, &aes->asyncDev,
  12466. WC_ASYNC_FLAG_NONE);
  12467. #endif
  12468. }
  12469. wc_AesFree(aes);
  12470. }
  12471. #ifdef WOLFSSL_SMALL_STACK
  12472. XFREE(aes, ctx->heap, DYNAMIC_TYPE_AES);
  12473. #endif
  12474. #else
  12475. ret = NOT_COMPILED_IN;
  12476. #endif
  12477. break;
  12478. }
  12479. case ecAES_128_CTR:
  12480. case ecAES_256_CTR:
  12481. {
  12482. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  12483. byte ctr_iv[AES_BLOCK_SIZE];
  12484. #ifndef WOLFSSL_SMALL_STACK
  12485. Aes aes[1];
  12486. #else
  12487. Aes *aes = (Aes *)XMALLOC(sizeof *aes, ctx->heap,
  12488. DYNAMIC_TYPE_AES);
  12489. if (aes == NULL) {
  12490. ret = MEMORY_E;
  12491. break;
  12492. }
  12493. #endif
  12494. /* Include 4 byte counter starting at all zeros. */
  12495. XMEMCPY(ctr_iv, encIv, WOLFSSL_ECIES_GEN_IV_SIZE);
  12496. XMEMSET(ctr_iv + WOLFSSL_ECIES_GEN_IV_SIZE, 0,
  12497. AES_BLOCK_SIZE - WOLFSSL_ECIES_GEN_IV_SIZE);
  12498. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  12499. if (ret == 0) {
  12500. ret = wc_AesSetKey(aes, encKey, (word32)encKeySz, ctr_iv,
  12501. AES_ENCRYPTION);
  12502. if (ret == 0) {
  12503. ret = wc_AesCtrEncrypt(aes, out, msg, msgSz);
  12504. #if defined(WOLFSSL_ASYNC_CRYPT) && \
  12505. defined(WC_ASYNC_ENABLE_AES)
  12506. ret = wc_AsyncWait(ret, &aes->asyncDev,
  12507. WC_ASYNC_FLAG_NONE);
  12508. #endif
  12509. }
  12510. wc_AesFree(aes);
  12511. }
  12512. #ifdef WOLFSSL_SMALL_STACK
  12513. XFREE(aes, ctx->heap, DYNAMIC_TYPE_AES);
  12514. #endif
  12515. #else
  12516. ret = NOT_COMPILED_IN;
  12517. #endif
  12518. break;
  12519. }
  12520. default:
  12521. ret = BAD_FUNC_ARG;
  12522. break;
  12523. }
  12524. }
  12525. if (ret == 0) {
  12526. switch (ctx->macAlgo) {
  12527. case ecHMAC_SHA256:
  12528. {
  12529. #ifdef WOLFSSL_SMALL_STACK
  12530. Hmac *hmac = (Hmac *)XMALLOC(sizeof *hmac, ctx->heap,
  12531. DYNAMIC_TYPE_HMAC);
  12532. if (hmac == NULL) {
  12533. ret = MEMORY_E;
  12534. break;
  12535. }
  12536. #else
  12537. Hmac hmac[1];
  12538. #endif
  12539. ret = wc_HmacInit(hmac, NULL, INVALID_DEVID);
  12540. if (ret == 0) {
  12541. ret = wc_HmacSetKey(hmac, WC_SHA256, macKey,
  12542. WC_SHA256_DIGEST_SIZE);
  12543. if (ret == 0) {
  12544. #if !defined(WOLFSSL_ECIES_GEN_IV)
  12545. ret = wc_HmacUpdate(hmac, out, msgSz);
  12546. #else
  12547. /* IV is before encrypted message. */
  12548. ret = wc_HmacUpdate(hmac, encIv, ivSz + msgSz);
  12549. #endif
  12550. }
  12551. if (ret == 0)
  12552. ret = wc_HmacUpdate(hmac, ctx->macSalt, ctx->macSaltSz);
  12553. if (ret == 0)
  12554. ret = wc_HmacFinal(hmac, out+msgSz);
  12555. wc_HmacFree(hmac);
  12556. }
  12557. #ifdef WOLFSSL_SMALL_STACK
  12558. XFREE(hmac, ctx->heap, DYNAMIC_TYPE_HMAC);
  12559. #endif
  12560. break;
  12561. }
  12562. default:
  12563. ret = BAD_FUNC_ARG;
  12564. break;
  12565. }
  12566. }
  12567. if (ret == 0) {
  12568. #ifdef WOLFSSL_ECIES_OLD
  12569. *outSz = msgSz + digestSz;
  12570. #elif defined(WOLFSSL_ECIES_GEN_IV)
  12571. *outSz = pubKeySz + ivSz + msgSz + digestSz;
  12572. #else
  12573. *outSz = pubKeySz + msgSz + digestSz;
  12574. #endif
  12575. }
  12576. RESTORE_VECTOR_REGISTERS();
  12577. #ifdef WOLFSSL_SMALL_STACK
  12578. XFREE(sharedSecret, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12579. XFREE(keys, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12580. #endif
  12581. return ret;
  12582. }
  12583. /* ecc encrypt with shared secret run through kdf
  12584. ctx holds non default algos and inputs
  12585. msgSz should be the right size for encAlgo, i.e., already padded
  12586. return 0 on success */
  12587. WOLFSSL_ABI
  12588. int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  12589. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx)
  12590. {
  12591. return wc_ecc_encrypt_ex(privKey, pubKey, msg, msgSz, out, outSz, ctx, 0);
  12592. }
  12593. /* ecc decrypt with shared secret run through kdf
  12594. ctx holds non default algos and inputs
  12595. return 0 on success */
  12596. WOLFSSL_ABI
  12597. int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  12598. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx)
  12599. {
  12600. int ret = 0;
  12601. word32 blockSz = 0;
  12602. #ifndef WOLFSSL_ECIES_OLD
  12603. #ifndef WOLFSSL_ECIES_GEN_IV
  12604. byte iv[ECC_MAX_IV_SIZE];
  12605. #endif
  12606. word32 pubKeySz = 0;
  12607. #ifdef WOLFSSL_SMALL_STACK
  12608. ecc_key* peerKey = NULL;
  12609. #else
  12610. ecc_key peerKey[1];
  12611. #endif
  12612. #endif
  12613. word32 digestSz = 0;
  12614. ecEncCtx localCtx;
  12615. #ifdef WOLFSSL_SMALL_STACK
  12616. byte* sharedSecret;
  12617. byte* keys;
  12618. #else
  12619. #if defined(WOLFSSL_ECIES_OLD) || !defined(WOLFSSL_ECIES_ISO18033)
  12620. byte sharedSecret[ECC_MAXSIZE]; /* 521 max size */
  12621. #else
  12622. byte sharedSecret[ECC_MAXSIZE * 3 + 1]; /* Public key too */
  12623. #endif
  12624. byte keys[ECC_BUFSIZE]; /* max size */
  12625. #endif
  12626. #if defined(WOLFSSL_ECIES_OLD) || !defined(WOLFSSL_ECIES_ISO18033)
  12627. word32 sharedSz = ECC_MAXSIZE;
  12628. #else
  12629. word32 sharedSz = ECC_MAXSIZE * 3 + 1;
  12630. #endif
  12631. int keysLen = 0;
  12632. int encKeySz = 0;
  12633. int ivSz = 0;
  12634. int offset = 0; /* in case using msg exchange */
  12635. byte* encKey = NULL;
  12636. const byte* encIv = NULL;
  12637. byte* macKey = NULL;
  12638. if (privKey == NULL || msg == NULL || out == NULL || outSz == NULL)
  12639. return BAD_FUNC_ARG;
  12640. #ifdef WOLFSSL_ECIES_OLD
  12641. if (pubKey == NULL)
  12642. return BAD_FUNC_ARG;
  12643. #endif
  12644. if (ctx == NULL) { /* use defaults */
  12645. ecc_ctx_init(&localCtx, 0, NULL);
  12646. ctx = &localCtx;
  12647. }
  12648. ret = ecc_get_key_sizes(ctx, &encKeySz, &ivSz, &keysLen, &digestSz,
  12649. &blockSz);
  12650. if (ret != 0)
  12651. return ret;
  12652. #ifndef WOLFSSL_ECIES_OLD
  12653. ret = ecc_public_key_size(privKey, &pubKeySz);
  12654. if (ret != 0)
  12655. return ret;
  12656. #ifdef HAVE_COMP_KEY
  12657. if ((msgSz > 1) && ((msg[0] == 0x02) || (msg[0] == 0x03))) {
  12658. pubKeySz = (pubKeySz / 2) + 1;
  12659. }
  12660. #endif /* HAVE_COMP_KEY */
  12661. #endif /* WOLFSSL_ECIES_OLD */
  12662. if (ctx->protocol == REQ_RESP_CLIENT) {
  12663. offset = keysLen;
  12664. keysLen *= 2;
  12665. if (ctx->cliSt != ecCLI_SENT_REQ)
  12666. return BAD_STATE_E;
  12667. ctx->cliSt = ecSRV_BAD_STATE; /* we're done no more ops allowed */
  12668. }
  12669. else if (ctx->protocol == REQ_RESP_SERVER) {
  12670. if (ctx->srvSt != ecSRV_SALT_SET)
  12671. return BAD_STATE_E;
  12672. ctx->srvSt = ecSRV_RECV_REQ; /* only do this once */
  12673. }
  12674. if (keysLen > ECC_BUFSIZE) /* keys size */
  12675. return BUFFER_E;
  12676. #ifdef WOLFSSL_ECIES_OLD
  12677. if (((msgSz - digestSz) % blockSz) != 0)
  12678. return BAD_PADDING_E;
  12679. if (*outSz < (msgSz - digestSz))
  12680. return BUFFER_E;
  12681. #elif defined(WOLFSSL_ECIES_GEN_IV)
  12682. if (((msgSz - ivSz - digestSz - pubKeySz) % blockSz) != 0)
  12683. return BAD_PADDING_E;
  12684. if (msgSz < pubKeySz + ivSz + blockSz + digestSz)
  12685. return BAD_FUNC_ARG;
  12686. if (*outSz < (msgSz - ivSz - digestSz - pubKeySz))
  12687. return BUFFER_E;
  12688. #else
  12689. if (((msgSz - digestSz - pubKeySz) % blockSz) != 0)
  12690. return BAD_PADDING_E;
  12691. if (msgSz < pubKeySz + blockSz + digestSz)
  12692. return BAD_FUNC_ARG;
  12693. if (*outSz < (msgSz - digestSz - pubKeySz))
  12694. return BUFFER_E;
  12695. #endif
  12696. #ifdef ECC_TIMING_RESISTANT
  12697. if (ctx->rng != NULL && privKey->rng == NULL)
  12698. privKey->rng = ctx->rng;
  12699. #endif
  12700. #ifdef WOLFSSL_SMALL_STACK
  12701. sharedSecret = (byte*)XMALLOC(sharedSz, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12702. if (sharedSecret == NULL) {
  12703. #ifndef WOLFSSL_ECIES_OLD
  12704. if (pubKey == peerKey)
  12705. wc_ecc_free(peerKey);
  12706. #endif
  12707. return MEMORY_E;
  12708. }
  12709. keys = (byte*)XMALLOC(ECC_BUFSIZE, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12710. if (keys == NULL) {
  12711. XFREE(sharedSecret, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12712. #ifndef WOLFSSL_ECIES_OLD
  12713. if (pubKey == peerKey)
  12714. wc_ecc_free(peerKey);
  12715. #endif
  12716. return MEMORY_E;
  12717. }
  12718. #endif
  12719. SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
  12720. #ifndef WOLFSSL_ECIES_OLD
  12721. if (pubKey == NULL) {
  12722. #ifdef WOLFSSL_SMALL_STACK
  12723. peerKey = (ecc_key*)XMALLOC(sizeof(*peerKey), ctx->heap,
  12724. DYNAMIC_TYPE_ECC_BUFFER);
  12725. if (peerKey == NULL)
  12726. ret = MEMORY_E;
  12727. #endif
  12728. pubKey = peerKey;
  12729. }
  12730. else {
  12731. /* if a public key was passed in we should free it here before init
  12732. * and import */
  12733. wc_ecc_free(pubKey);
  12734. }
  12735. if (ret == 0) {
  12736. ret = wc_ecc_init_ex(pubKey, privKey->heap, INVALID_DEVID);
  12737. }
  12738. if (ret == 0) {
  12739. ret = wc_ecc_import_x963_ex(msg, pubKeySz, pubKey, privKey->dp->id);
  12740. }
  12741. if (ret == 0) {
  12742. /* Point is not MACed. */
  12743. msg += pubKeySz;
  12744. msgSz -= pubKeySz;
  12745. }
  12746. #endif
  12747. if (ret == 0) {
  12748. #ifdef WOLFSSL_ECIES_ISO18033
  12749. XMEMCPY(sharedSecret, msg - pubKeySz, pubKeySz);
  12750. sharedSz -= pubKeySz;
  12751. #endif
  12752. do {
  12753. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  12754. ret = wc_AsyncWait(ret, &privKey->asyncDev,
  12755. WC_ASYNC_FLAG_CALL_AGAIN);
  12756. if (ret != 0)
  12757. break;
  12758. #endif
  12759. #ifndef WOLFSSL_ECIES_ISO18033
  12760. ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret,
  12761. &sharedSz);
  12762. #else
  12763. ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret +
  12764. pubKeySz, &sharedSz);
  12765. #endif
  12766. } while (ret == WC_PENDING_E);
  12767. }
  12768. if (ret == 0) {
  12769. #ifdef WOLFSSL_ECIES_ISO18033
  12770. /* KDF data is encoded public key and secret. */
  12771. sharedSz += pubKeySz;
  12772. #endif
  12773. switch (ctx->kdfAlgo) {
  12774. case ecHKDF_SHA256 :
  12775. ret = wc_HKDF(WC_SHA256, sharedSecret, sharedSz, ctx->kdfSalt,
  12776. ctx->kdfSaltSz, ctx->kdfInfo, ctx->kdfInfoSz,
  12777. keys, (word32)keysLen);
  12778. break;
  12779. case ecHKDF_SHA1 :
  12780. ret = wc_HKDF(WC_SHA, sharedSecret, sharedSz, ctx->kdfSalt,
  12781. ctx->kdfSaltSz, ctx->kdfInfo, ctx->kdfInfoSz,
  12782. keys, (word32)keysLen);
  12783. break;
  12784. #if defined(HAVE_X963_KDF) && !defined(NO_HASH_WRAPPER)
  12785. case ecKDF_X963_SHA1 :
  12786. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, sharedSecret, sharedSz,
  12787. ctx->kdfInfo, ctx->kdfInfoSz, keys, (word32)keysLen);
  12788. break;
  12789. case ecKDF_X963_SHA256 :
  12790. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, sharedSecret, sharedSz,
  12791. ctx->kdfInfo, ctx->kdfInfoSz, keys, (word32)keysLen);
  12792. break;
  12793. case ecKDF_SHA1 :
  12794. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, sharedSecret, sharedSz,
  12795. NULL, 0, keys, (word32)keysLen);
  12796. break;
  12797. case ecKDF_SHA256 :
  12798. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, sharedSecret, sharedSz,
  12799. NULL, 0, keys, (word32)keysLen);
  12800. break;
  12801. #endif
  12802. default:
  12803. ret = BAD_FUNC_ARG;
  12804. break;
  12805. }
  12806. }
  12807. if (ret == 0) {
  12808. #ifdef WOLFSSL_ECIES_OLD
  12809. encKey = keys + offset;
  12810. encIv = encKey + encKeySz;
  12811. macKey = encKey + encKeySz + ivSz;
  12812. #elif defined(WOLFSSL_ECIES_GEN_IV)
  12813. encKey = keys + offset;
  12814. encIv = msg;
  12815. msg += ivSz;
  12816. msgSz -= ivSz;
  12817. macKey = encKey + encKeySz;
  12818. #else
  12819. XMEMSET(iv, 0, (size_t)ivSz);
  12820. encKey = keys + offset;
  12821. encIv = iv;
  12822. macKey = encKey + encKeySz;
  12823. #endif
  12824. switch (ctx->macAlgo) {
  12825. case ecHMAC_SHA256:
  12826. {
  12827. byte verify[WC_SHA256_DIGEST_SIZE];
  12828. #ifdef WOLFSSL_SMALL_STACK
  12829. Hmac *hmac = (Hmac *)XMALLOC(sizeof *hmac, ctx->heap,
  12830. DYNAMIC_TYPE_HMAC);
  12831. if (hmac == NULL) {
  12832. ret = MEMORY_E;
  12833. break;
  12834. }
  12835. #else
  12836. Hmac hmac[1];
  12837. #endif
  12838. ret = wc_HmacInit(hmac, NULL, INVALID_DEVID);
  12839. if (ret == 0) {
  12840. ret = wc_HmacSetKey(hmac, WC_SHA256, macKey,
  12841. WC_SHA256_DIGEST_SIZE);
  12842. if (ret == 0)
  12843. #if !defined(WOLFSSL_ECIES_GEN_IV)
  12844. ret = wc_HmacUpdate(hmac, msg, msgSz-digestSz);
  12845. #else
  12846. /* IV is before encrypted message. */
  12847. ret = wc_HmacUpdate(hmac, encIv, ivSz+msgSz-digestSz);
  12848. #endif
  12849. if (ret == 0)
  12850. ret = wc_HmacUpdate(hmac, ctx->macSalt, ctx->macSaltSz);
  12851. if (ret == 0)
  12852. ret = wc_HmacFinal(hmac, verify);
  12853. if ((ret == 0) && (XMEMCMP(verify, msg + msgSz - digestSz,
  12854. digestSz) != 0)) {
  12855. ret = -1;
  12856. }
  12857. wc_HmacFree(hmac);
  12858. }
  12859. #ifdef WOLFSSL_SMALL_STACK
  12860. XFREE(hmac, ctx->heap, DYNAMIC_TYPE_HMAC);
  12861. #endif
  12862. break;
  12863. }
  12864. default:
  12865. ret = BAD_FUNC_ARG;
  12866. break;
  12867. }
  12868. }
  12869. if (ret == 0) {
  12870. switch (ctx->encAlgo) {
  12871. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  12872. case ecAES_128_CBC:
  12873. case ecAES_256_CBC:
  12874. {
  12875. #ifdef WOLFSSL_SMALL_STACK
  12876. Aes *aes = (Aes *)XMALLOC(sizeof *aes, ctx->heap,
  12877. DYNAMIC_TYPE_AES);
  12878. if (aes == NULL) {
  12879. ret = MEMORY_E;
  12880. break;
  12881. }
  12882. #else
  12883. Aes aes[1];
  12884. #endif
  12885. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  12886. if (ret == 0) {
  12887. ret = wc_AesSetKey(aes, encKey, (word32)encKeySz, encIv,
  12888. AES_DECRYPTION);
  12889. if (ret == 0) {
  12890. ret = wc_AesCbcDecrypt(aes, out, msg, msgSz-digestSz);
  12891. #if defined(WOLFSSL_ASYNC_CRYPT) && \
  12892. defined(WC_ASYNC_ENABLE_AES)
  12893. ret = wc_AsyncWait(ret, &aes->asyncDev,
  12894. WC_ASYNC_FLAG_NONE);
  12895. #endif
  12896. }
  12897. wc_AesFree(aes);
  12898. }
  12899. #ifdef WOLFSSL_SMALL_STACK
  12900. XFREE(aes, ctx->heap, DYNAMIC_TYPE_AES);
  12901. #endif
  12902. break;
  12903. }
  12904. #endif
  12905. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER)
  12906. case ecAES_128_CTR:
  12907. case ecAES_256_CTR:
  12908. {
  12909. #ifdef WOLFSSL_SMALL_STACK
  12910. Aes *aes = (Aes *)XMALLOC(sizeof *aes, ctx->heap,
  12911. DYNAMIC_TYPE_AES);
  12912. if (aes == NULL) {
  12913. ret = MEMORY_E;
  12914. break;
  12915. }
  12916. #else
  12917. Aes aes[1];
  12918. #endif
  12919. ret = wc_AesInit(aes, NULL, INVALID_DEVID);
  12920. if (ret == 0) {
  12921. byte ctr_iv[AES_BLOCK_SIZE];
  12922. /* Make a 16 byte IV from the bytes passed in. */
  12923. XMEMCPY(ctr_iv, encIv, WOLFSSL_ECIES_GEN_IV_SIZE);
  12924. XMEMSET(ctr_iv + WOLFSSL_ECIES_GEN_IV_SIZE, 0,
  12925. AES_BLOCK_SIZE - WOLFSSL_ECIES_GEN_IV_SIZE);
  12926. ret = wc_AesSetKey(aes, encKey, (word32)encKeySz, ctr_iv,
  12927. AES_ENCRYPTION);
  12928. if (ret == 0) {
  12929. ret = wc_AesCtrEncrypt(aes, out, msg, msgSz-digestSz);
  12930. #if defined(WOLFSSL_ASYNC_CRYPT) && \
  12931. defined(WC_ASYNC_ENABLE_AES)
  12932. ret = wc_AsyncWait(ret, &aes->asyncDev,
  12933. WC_ASYNC_FLAG_NONE);
  12934. #endif
  12935. }
  12936. wc_AesFree(aes);
  12937. }
  12938. #ifdef WOLFSSL_SMALL_STACK
  12939. XFREE(aes, ctx->heap, DYNAMIC_TYPE_AES);
  12940. #endif
  12941. break;
  12942. }
  12943. #endif
  12944. default:
  12945. ret = BAD_FUNC_ARG;
  12946. break;
  12947. }
  12948. }
  12949. if (ret == 0)
  12950. *outSz = msgSz - digestSz;
  12951. RESTORE_VECTOR_REGISTERS();
  12952. #ifndef WOLFSSL_ECIES_OLD
  12953. if (pubKey == peerKey)
  12954. wc_ecc_free(peerKey);
  12955. #endif
  12956. #ifdef WOLFSSL_SMALL_STACK
  12957. #ifndef WOLFSSL_ECIES_OLD
  12958. if (peerKey != NULL) {
  12959. XFREE(peerKey, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12960. }
  12961. #endif
  12962. XFREE(sharedSecret, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12963. XFREE(keys, ctx->heap, DYNAMIC_TYPE_ECC_BUFFER);
  12964. #endif
  12965. return ret;
  12966. }
  12967. #endif /* HAVE_ECC_ENCRYPT */
  12968. #ifdef HAVE_COMP_KEY
  12969. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  12970. !defined(WOLFSSL_CRYPTOCELL)
  12971. #ifndef WOLFSSL_SP_MATH
  12972. #if !defined(SQRTMOD_USE_MOD_EXP)
  12973. /* computes the jacobi c = (a | n) (or Legendre if n is prime)
  12974. */
  12975. static int mp_jacobi(mp_int* a, mp_int* n, int* c)
  12976. {
  12977. #ifdef WOLFSSL_SMALL_STACK
  12978. mp_int* a1 = NULL;
  12979. mp_int* n1 = NULL;
  12980. #else
  12981. mp_int a1[1], n1[1];
  12982. #endif
  12983. int res;
  12984. int s = 1;
  12985. int k;
  12986. mp_int* t[2];
  12987. mp_int* ts;
  12988. mp_digit residue;
  12989. if (mp_isneg(a) == MP_YES) {
  12990. return MP_VAL;
  12991. }
  12992. if (mp_isneg(n) == MP_YES) {
  12993. return MP_VAL;
  12994. }
  12995. if (mp_iseven(n) == MP_YES) {
  12996. return MP_VAL;
  12997. }
  12998. #ifdef WOLFSSL_SMALL_STACK
  12999. a1 = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  13000. if (a1 == NULL) {
  13001. return MP_MEM;
  13002. }
  13003. n1 = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  13004. if (n1 == NULL) {
  13005. XFREE(a1, NULL, DYNAMIC_TYPE_BIGINT);
  13006. return MP_MEM;
  13007. }
  13008. #endif
  13009. if ((res = mp_init_multi(a1, n1, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  13010. #ifdef WOLFSSL_SMALL_STACK
  13011. XFREE(a1, NULL, DYNAMIC_TYPE_BIGINT);
  13012. XFREE(n1, NULL, DYNAMIC_TYPE_BIGINT);
  13013. #endif
  13014. return res;
  13015. }
  13016. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  13017. if ((res = mp_mod(a, n, a1)) != MP_OKAY) {
  13018. goto done;
  13019. }
  13020. if ((res = mp_copy(n, n1)) != MP_OKAY) {
  13021. goto done;
  13022. }
  13023. t[0] = a1;
  13024. t[1] = n1;
  13025. /* Keep reducing until first number is 0. */
  13026. while (!mp_iszero(t[0])) {
  13027. /* Divide by 2 until odd. */
  13028. k = mp_cnt_lsb(t[0]);
  13029. if (k > 0) {
  13030. mp_rshb(t[0], k);
  13031. /* Negate s each time we divide by 2 if t[1] mod 8 == 3 or 5.
  13032. * Odd number of divides results in a negate.
  13033. */
  13034. residue = t[1]->dp[0] & 7;
  13035. if ((k & 1) && ((residue == 3) || (residue == 5))) {
  13036. s = -s;
  13037. }
  13038. }
  13039. /* Swap t[0] and t[1]. */
  13040. ts = t[0];
  13041. t[0] = t[1];
  13042. t[1] = ts;
  13043. /* Negate s if both numbers == 3 mod 4. */
  13044. if (((t[0]->dp[0] & 3) == 3) && ((t[1]->dp[0] & 3) == 3)) {
  13045. s = -s;
  13046. }
  13047. /* Reduce first number modulo second. */
  13048. if ((k == 0) && (mp_count_bits(t[0]) == mp_count_bits(t[1]))) {
  13049. res = mp_sub(t[0], t[1], t[0]);
  13050. }
  13051. else {
  13052. res = mp_mod(t[0], t[1], t[0]);
  13053. }
  13054. if (res != MP_OKAY) {
  13055. goto done;
  13056. }
  13057. }
  13058. /* When the two numbers have divisors in common. */
  13059. if (!mp_isone(t[1])) {
  13060. s = 0;
  13061. }
  13062. *c = s;
  13063. done:
  13064. RESTORE_VECTOR_REGISTERS();
  13065. /* cleanup */
  13066. mp_clear(n1);
  13067. mp_clear(a1);
  13068. #ifdef WOLFSSL_SMALL_STACK
  13069. XFREE(a1, NULL, DYNAMIC_TYPE_BIGINT);
  13070. XFREE(n1, NULL, DYNAMIC_TYPE_BIGINT);
  13071. #endif
  13072. return res;
  13073. }
  13074. #endif /* !SQRTMOD_USE_MOD_EXP */
  13075. /* Solves the modular equation x^2 = n (mod p)
  13076. * where prime number is greater than 2 (odd prime).
  13077. * The result is returned in the third argument x
  13078. * the function returns MP_OKAY on success, MP_VAL or another error on failure
  13079. */
  13080. static int mp_sqrtmod_prime(mp_int* n, mp_int* prime, mp_int* ret)
  13081. {
  13082. #if defined(SQRTMOD_USE_MOD_EXP)
  13083. int res;
  13084. mp_digit i;
  13085. mp_int e;
  13086. /* first handle the simple cases n = 0 or n = 1 */
  13087. if (mp_cmp_d(n, 0) == MP_EQ) {
  13088. mp_zero(ret);
  13089. return MP_OKAY;
  13090. }
  13091. if (mp_cmp_d(n, 1) == MP_EQ) {
  13092. return mp_set(ret, 1);
  13093. }
  13094. if (mp_iseven(prime)) {
  13095. return MP_VAL;
  13096. }
  13097. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  13098. res = mp_init(&e);
  13099. if (res == MP_OKAY)
  13100. res = mp_mod_d(prime, 8, &i);
  13101. if (res == MP_OKAY && i == 1) {
  13102. return MP_VAL;
  13103. }
  13104. /* prime mod 8 = 5 */
  13105. else if (res == MP_OKAY && i == 5) {
  13106. res = mp_sub_d(prime, 1, &e);
  13107. if (res == MP_OKAY)
  13108. res = mp_div_2d(&e, 2, &e, NULL);
  13109. }
  13110. /* prime mod 4 = 3 */
  13111. else if (res == MP_OKAY && ((i == 3) || (i == 7))) {
  13112. res = mp_add_d(prime, 1, &e);
  13113. if (res == MP_OKAY)
  13114. res = mp_div_2d(&e, 2, &e, NULL);
  13115. }
  13116. if (res == MP_OKAY)
  13117. res = mp_exptmod(n, &e, prime, ret);
  13118. mp_clear(&e);
  13119. RESTORE_VECTOR_REGISTERS();
  13120. return res;
  13121. #else
  13122. int res, legendre, done = 0;
  13123. mp_digit i;
  13124. #ifdef WOLFSSL_SMALL_STACK
  13125. mp_int *t1 = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13126. mp_int *C = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13127. mp_int *Q = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13128. mp_int *S = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13129. mp_int *Z = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13130. mp_int *M = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13131. mp_int *T = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13132. mp_int *R = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13133. mp_int *N = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13134. mp_int *two = (mp_int *)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13135. #else
  13136. mp_int t1[1], C[1], Q[1], S[1], Z[1], M[1], T[1], R[1], N[1], two[1];
  13137. #endif
  13138. SAVE_VECTOR_REGISTERS(res = _svr_ret; goto out;);
  13139. if ((mp_init_multi(t1, C, Q, S, Z, M) != MP_OKAY) ||
  13140. (mp_init_multi(T, R, N, two, NULL, NULL) != MP_OKAY)) {
  13141. res = MP_INIT_E;
  13142. goto out;
  13143. }
  13144. #ifdef WOLFSSL_SMALL_STACK
  13145. if ((t1 == NULL) ||
  13146. (C == NULL) ||
  13147. (Q == NULL) ||
  13148. (S == NULL) ||
  13149. (Z == NULL) ||
  13150. (M == NULL) ||
  13151. (T == NULL) ||
  13152. (R == NULL) ||
  13153. (N == NULL) ||
  13154. (two == NULL)) {
  13155. res = MP_MEM;
  13156. goto out;
  13157. }
  13158. #endif
  13159. /* first handle the simple cases n = 0 or n = 1 */
  13160. if (mp_cmp_d(n, 0) == MP_EQ) {
  13161. mp_zero(ret);
  13162. res = MP_OKAY;
  13163. goto out;
  13164. }
  13165. if (mp_cmp_d(n, 1) == MP_EQ) {
  13166. res = mp_set(ret, 1);
  13167. goto out;
  13168. }
  13169. /* prime must be odd */
  13170. if (mp_cmp_d(prime, 2) == MP_EQ) {
  13171. res = MP_VAL;
  13172. goto out;
  13173. }
  13174. /* reduce n to less than prime */
  13175. res = mp_mod(n, prime, N);
  13176. if (res != MP_OKAY) {
  13177. goto out;
  13178. }
  13179. /* when N is zero, sqrt is zero */
  13180. if (mp_iszero(N)) {
  13181. mp_set(ret, 0);
  13182. goto out;
  13183. }
  13184. /* is quadratic non-residue mod prime */
  13185. if ((res = mp_jacobi(N, prime, &legendre)) != MP_OKAY) {
  13186. goto out;
  13187. }
  13188. if (legendre == -1) {
  13189. res = MP_VAL;
  13190. goto out;
  13191. }
  13192. /* SPECIAL CASE: if prime mod 4 == 3
  13193. * compute directly: res = n^(prime+1)/4 mod prime
  13194. * Handbook of Applied Cryptography algorithm 3.36
  13195. */
  13196. res = mp_mod_d(prime, 4, &i);
  13197. if (res == MP_OKAY && i == 3) {
  13198. res = mp_add_d(prime, 1, t1);
  13199. if (res == MP_OKAY)
  13200. res = mp_div_2(t1, t1);
  13201. if (res == MP_OKAY)
  13202. res = mp_div_2(t1, t1);
  13203. if (res == MP_OKAY)
  13204. res = mp_exptmod(N, t1, prime, ret);
  13205. done = 1;
  13206. }
  13207. /* NOW: TonelliShanks algorithm */
  13208. if (res == MP_OKAY && done == 0) {
  13209. /* factor out powers of 2 from prime-1, defining Q and S
  13210. * as: prime-1 = Q*2^S */
  13211. /* Q = prime - 1 */
  13212. res = mp_copy(prime, Q);
  13213. if (res == MP_OKAY)
  13214. res = mp_sub_d(Q, 1, Q);
  13215. /* S = 0 */
  13216. if (res == MP_OKAY)
  13217. mp_zero(S);
  13218. while (res == MP_OKAY && mp_iseven(Q) == MP_YES) {
  13219. /* Q = Q / 2 */
  13220. res = mp_div_2(Q, Q);
  13221. /* S = S + 1 */
  13222. if (res == MP_OKAY)
  13223. res = mp_add_d(S, 1, S);
  13224. }
  13225. /* find a Z such that the Legendre symbol (Z|prime) == -1 */
  13226. /* Z = 2 */
  13227. if (res == MP_OKAY)
  13228. res = mp_set_int(Z, 2);
  13229. while (res == MP_OKAY) {
  13230. res = mp_jacobi(Z, prime, &legendre);
  13231. if (res == MP_OKAY && legendre == -1)
  13232. break;
  13233. #if defined(WOLFSSL_CUSTOM_CURVES)
  13234. /* P224R1 succeeds with a value of 11. */
  13235. if (mp_cmp_d(Z, 22) == MP_EQ) {
  13236. /* This is to clamp the loop in case 'prime' is not really prime */
  13237. res = MP_VAL;
  13238. break;
  13239. }
  13240. #endif
  13241. /* Z = Z + 1 */
  13242. if (res == MP_OKAY)
  13243. res = mp_add_d(Z, 1, Z);
  13244. if ((res == MP_OKAY) && (mp_cmp(Z,prime) == MP_EQ)) {
  13245. /* This is to clamp the loop in case 'prime' is not really prime */
  13246. res = MP_VAL;
  13247. break;
  13248. }
  13249. }
  13250. /* C = Z ^ Q mod prime */
  13251. if (res == MP_OKAY)
  13252. res = mp_exptmod(Z, Q, prime, C);
  13253. /* t1 = (Q + 1) / 2 */
  13254. if (res == MP_OKAY)
  13255. res = mp_add_d(Q, 1, t1);
  13256. if (res == MP_OKAY)
  13257. res = mp_div_2(t1, t1);
  13258. /* R = n ^ ((Q + 1) / 2) mod prime */
  13259. if (res == MP_OKAY)
  13260. res = mp_exptmod(N, t1, prime, R);
  13261. /* T = n ^ Q mod prime */
  13262. if (res == MP_OKAY)
  13263. res = mp_exptmod(N, Q, prime, T);
  13264. /* M = S */
  13265. if (res == MP_OKAY)
  13266. res = mp_copy(S, M);
  13267. if (res == MP_OKAY)
  13268. res = mp_set_int(two, 2);
  13269. while (res == MP_OKAY && done == 0) {
  13270. res = mp_copy(T, t1);
  13271. /* reduce to 1 and count */
  13272. i = 0;
  13273. while (res == MP_OKAY) {
  13274. if (mp_cmp_d(t1, 1) == MP_EQ)
  13275. break;
  13276. res = mp_exptmod(t1, two, prime, t1);
  13277. if ((res == MP_OKAY) && (mp_cmp_d(M,i) == MP_EQ)) {
  13278. /* This is to clamp the loop in case 'prime' is not really prime */
  13279. res = MP_VAL;
  13280. break;
  13281. }
  13282. if (res == MP_OKAY)
  13283. i++;
  13284. }
  13285. if (res == MP_OKAY && i == 0) {
  13286. res = mp_copy(R, ret);
  13287. done = 1;
  13288. }
  13289. if (done == 0) {
  13290. /* t1 = 2 ^ (M - i - 1) */
  13291. if (res == MP_OKAY)
  13292. res = mp_sub_d(M, i, t1);
  13293. if (res == MP_OKAY)
  13294. res = mp_sub_d(t1, 1, t1);
  13295. if (res == MP_OKAY)
  13296. res = mp_exptmod(two, t1, prime, t1);
  13297. /* t1 = C ^ (2 ^ (M - i - 1)) mod prime */
  13298. if (res == MP_OKAY)
  13299. res = mp_exptmod(C, t1, prime, t1);
  13300. /* C = (t1 * t1) mod prime */
  13301. if (res == MP_OKAY)
  13302. res = mp_sqrmod(t1, prime, C);
  13303. /* R = (R * t1) mod prime */
  13304. if (res == MP_OKAY)
  13305. res = mp_mulmod(R, t1, prime, R);
  13306. /* T = (T * C) mod prime */
  13307. if (res == MP_OKAY)
  13308. res = mp_mulmod(T, C, prime, T);
  13309. /* M = i */
  13310. if (res == MP_OKAY)
  13311. res = mp_set(M, i);
  13312. }
  13313. }
  13314. }
  13315. out:
  13316. RESTORE_VECTOR_REGISTERS();
  13317. #ifdef WOLFSSL_SMALL_STACK
  13318. if (t1) {
  13319. if (res != MP_INIT_E)
  13320. mp_clear(t1);
  13321. XFREE(t1, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13322. }
  13323. if (C) {
  13324. if (res != MP_INIT_E)
  13325. mp_clear(C);
  13326. XFREE(C, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13327. }
  13328. if (Q) {
  13329. if (res != MP_INIT_E)
  13330. mp_clear(Q);
  13331. XFREE(Q, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13332. }
  13333. if (S) {
  13334. if (res != MP_INIT_E)
  13335. mp_clear(S);
  13336. XFREE(S, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13337. }
  13338. if (Z) {
  13339. if (res != MP_INIT_E)
  13340. mp_clear(Z);
  13341. XFREE(Z, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13342. }
  13343. if (M) {
  13344. if (res != MP_INIT_E)
  13345. mp_clear(M);
  13346. XFREE(M, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13347. }
  13348. if (T) {
  13349. if (res != MP_INIT_E)
  13350. mp_clear(T);
  13351. XFREE(T, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13352. }
  13353. if (R) {
  13354. if (res != MP_INIT_E)
  13355. mp_clear(R);
  13356. XFREE(R, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13357. }
  13358. if (N) {
  13359. if (res != MP_INIT_E)
  13360. mp_clear(N);
  13361. XFREE(N, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13362. }
  13363. if (two) {
  13364. if (res != MP_INIT_E)
  13365. mp_clear(two);
  13366. XFREE(two, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  13367. }
  13368. #else
  13369. if (res != MP_INIT_E) {
  13370. mp_clear(t1);
  13371. mp_clear(C);
  13372. mp_clear(Q);
  13373. mp_clear(S);
  13374. mp_clear(Z);
  13375. mp_clear(M);
  13376. mp_clear(T);
  13377. mp_clear(R);
  13378. mp_clear(N);
  13379. mp_clear(two);
  13380. }
  13381. #endif
  13382. return res;
  13383. #endif
  13384. }
  13385. #endif /* !WOLFSSL_SP_MATH */
  13386. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A && !WOLFSSL_CRYPTOCELL */
  13387. #ifdef HAVE_ECC_KEY_EXPORT
  13388. /* export public ECC key in ANSI X9.63 format compressed */
  13389. static int wc_ecc_export_x963_compressed(ecc_key* key, byte* out, word32* outLen)
  13390. {
  13391. word32 numlen;
  13392. int ret = MP_OKAY;
  13393. if (key == NULL || outLen == NULL)
  13394. return BAD_FUNC_ARG;
  13395. if (key->type == ECC_PRIVATEKEY_ONLY)
  13396. return ECC_PRIVATEONLY_E;
  13397. if (key->type == 0 || wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL){
  13398. return ECC_BAD_ARG_E;
  13399. }
  13400. numlen = (word32)key->dp->size;
  13401. if (*outLen < (1 + numlen)) {
  13402. *outLen = 1 + numlen;
  13403. return LENGTH_ONLY_E;
  13404. }
  13405. if (out == NULL)
  13406. return BAD_FUNC_ARG;
  13407. if (mp_unsigned_bin_size(key->pubkey.x) > (int)numlen)
  13408. return ECC_BAD_ARG_E;
  13409. /* store first byte */
  13410. out[0] = mp_isodd(key->pubkey.y) == MP_YES ? ECC_POINT_COMP_ODD : ECC_POINT_COMP_EVEN;
  13411. /* pad and store x */
  13412. XMEMSET(out+1, 0, numlen);
  13413. ret = mp_to_unsigned_bin(
  13414. key->pubkey.x,
  13415. out+1 + (numlen - (word32)mp_unsigned_bin_size(key->pubkey.x)));
  13416. *outLen = 1 + numlen;
  13417. return ret;
  13418. }
  13419. #endif /* HAVE_ECC_KEY_EXPORT */
  13420. #endif /* HAVE_COMP_KEY */
  13421. int wc_ecc_get_oid(word32 oidSum, const byte** oid, word32* oidSz)
  13422. {
  13423. int x;
  13424. if (oidSum == 0) {
  13425. return BAD_FUNC_ARG;
  13426. }
  13427. /* find matching OID sum (based on encoded value) */
  13428. for (x = 0; ecc_sets[x].size != 0; x++) {
  13429. if (ecc_sets[x].oidSum == oidSum) {
  13430. int ret;
  13431. #ifdef HAVE_OID_ENCODING
  13432. ret = 0;
  13433. /* check cache */
  13434. oid_cache_t* o = &ecc_oid_cache[x];
  13435. if (o->oidSz == 0) {
  13436. o->oidSz = sizeof(o->oid);
  13437. ret = EncodeObjectId(ecc_sets[x].oid, ecc_sets[x].oidSz,
  13438. o->oid, &o->oidSz);
  13439. }
  13440. if (oidSz) {
  13441. *oidSz = o->oidSz;
  13442. }
  13443. if (oid) {
  13444. *oid = o->oid;
  13445. }
  13446. /* on success return curve id */
  13447. if (ret == 0) {
  13448. ret = ecc_sets[x].id;
  13449. }
  13450. #else
  13451. if (oidSz) {
  13452. *oidSz = ecc_sets[x].oidSz;
  13453. }
  13454. if (oid) {
  13455. *oid = ecc_sets[x].oid;
  13456. }
  13457. ret = ecc_sets[x].id;
  13458. #endif
  13459. return ret;
  13460. }
  13461. }
  13462. return NOT_COMPILED_IN;
  13463. }
  13464. #ifdef WOLFSSL_CUSTOM_CURVES
  13465. int wc_ecc_set_custom_curve(ecc_key* key, const ecc_set_type* dp)
  13466. {
  13467. if (key == NULL || dp == NULL) {
  13468. return BAD_FUNC_ARG;
  13469. }
  13470. key->idx = ECC_CUSTOM_IDX;
  13471. key->dp = dp;
  13472. return 0;
  13473. }
  13474. #endif /* WOLFSSL_CUSTOM_CURVES */
  13475. #if defined(HAVE_X963_KDF) && !defined(NO_HASH_WRAPPER)
  13476. static WC_INLINE void IncrementX963KdfCounter(byte* inOutCtr)
  13477. {
  13478. int i;
  13479. /* in network byte order so start at end and work back */
  13480. for (i = 3; i >= 0; i--) {
  13481. if (++inOutCtr[i]) /* we're done unless we overflow */
  13482. return;
  13483. }
  13484. }
  13485. /* ASN X9.63 Key Derivation Function (SEC1) */
  13486. int wc_X963_KDF(enum wc_HashType type, const byte* secret, word32 secretSz,
  13487. const byte* sinfo, word32 sinfoSz, byte* out, word32 outSz)
  13488. {
  13489. int ret;
  13490. word32 digestSz, copySz, remaining = outSz;
  13491. byte* outIdx;
  13492. byte counter[4];
  13493. byte tmp[WC_MAX_DIGEST_SIZE];
  13494. #ifdef WOLFSSL_SMALL_STACK
  13495. wc_HashAlg* hash;
  13496. #else
  13497. wc_HashAlg hash[1];
  13498. #endif
  13499. if (secret == NULL || secretSz == 0 || out == NULL)
  13500. return BAD_FUNC_ARG;
  13501. /* X9.63 allowed algos only */
  13502. if (type != WC_HASH_TYPE_SHA && type != WC_HASH_TYPE_SHA224 &&
  13503. type != WC_HASH_TYPE_SHA256 && type != WC_HASH_TYPE_SHA384 &&
  13504. type != WC_HASH_TYPE_SHA512)
  13505. return BAD_FUNC_ARG;
  13506. ret = wc_HashGetDigestSize(type);
  13507. if (ret < 0)
  13508. return ret;
  13509. digestSz = (word32)ret;
  13510. #ifdef WOLFSSL_SMALL_STACK
  13511. hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), NULL,
  13512. DYNAMIC_TYPE_HASHES);
  13513. if (hash == NULL)
  13514. return MEMORY_E;
  13515. #endif
  13516. ret = wc_HashInit(hash, type);
  13517. if (ret != 0) {
  13518. #ifdef WOLFSSL_SMALL_STACK
  13519. XFREE(hash, NULL, DYNAMIC_TYPE_HASHES);
  13520. #endif
  13521. return ret;
  13522. }
  13523. outIdx = out;
  13524. XMEMSET(counter, 0, sizeof(counter));
  13525. while (remaining > 0) {
  13526. IncrementX963KdfCounter(counter);
  13527. ret = wc_HashUpdate(hash, type, secret, secretSz);
  13528. if (ret != 0) {
  13529. break;
  13530. }
  13531. ret = wc_HashUpdate(hash, type, counter, sizeof(counter));
  13532. if (ret != 0) {
  13533. break;
  13534. }
  13535. if (sinfo) {
  13536. ret = wc_HashUpdate(hash, type, sinfo, sinfoSz);
  13537. if (ret != 0) {
  13538. break;
  13539. }
  13540. }
  13541. ret = wc_HashFinal(hash, type, tmp);
  13542. if (ret != 0) {
  13543. break;
  13544. }
  13545. copySz = min(remaining, digestSz);
  13546. XMEMCPY(outIdx, tmp, copySz);
  13547. remaining -= copySz;
  13548. outIdx += copySz;
  13549. }
  13550. wc_HashFree(hash, type);
  13551. #ifdef WOLFSSL_SMALL_STACK
  13552. XFREE(hash, NULL, DYNAMIC_TYPE_HASHES);
  13553. #endif
  13554. return ret;
  13555. }
  13556. #endif /* HAVE_X963_KDF && !NO_HASH_WRAPPER */
  13557. #ifdef WOLFSSL_SE050
  13558. /* Use specified hardware key ID with ecc_key operations. Unlike devId,
  13559. * keyId is a word32, can be used for key IDs larger than an int.
  13560. *
  13561. * key initialized ecc_key struct
  13562. * keyId hardware key ID which stores ECC key
  13563. * flags optional flags, currently unused
  13564. *
  13565. * Return 0 on success, negative on error */
  13566. int wc_ecc_use_key_id(ecc_key* key, word32 keyId, word32 flags)
  13567. {
  13568. (void)flags;
  13569. if (key == NULL) {
  13570. return BAD_FUNC_ARG;
  13571. }
  13572. return se050_ecc_use_key_id(key, keyId);
  13573. }
  13574. /* Get hardware key ID associated with this ecc_key structure.
  13575. *
  13576. * key initialized ecc_key struct
  13577. * keyId [OUT] output for key ID associated with this structure
  13578. *
  13579. * Returns 0 on success, negative on error.
  13580. */
  13581. int wc_ecc_get_key_id(ecc_key* key, word32* keyId)
  13582. {
  13583. if (key == NULL || keyId == NULL) {
  13584. return BAD_FUNC_ARG;
  13585. }
  13586. return se050_ecc_get_key_id(key, keyId);
  13587. }
  13588. #endif /* WOLFSSL_SE050 */
  13589. #ifdef WC_ECC_NONBLOCK
  13590. /* Enable ECC support for non-blocking operations */
  13591. int wc_ecc_set_nonblock(ecc_key *key, ecc_nb_ctx_t* ctx)
  13592. {
  13593. if (key) {
  13594. if (ctx) {
  13595. XMEMSET(ctx, 0, sizeof(ecc_nb_ctx_t));
  13596. }
  13597. key->nb_ctx = ctx;
  13598. }
  13599. return 0;
  13600. }
  13601. #endif /* WC_ECC_NONBLOCK */
  13602. #endif /* HAVE_ECC */