ecc.c 295 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184
  1. /* ecc.c
  2. *
  3. * Copyright (C) 2006-2017 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. /*
  27. Possible ECC enable options:
  28. * HAVE_ECC: Overall control of ECC default: on
  29. * HAVE_ECC_ENCRYPT: ECC encrypt/decrypt w/AES and HKDF default: off
  30. * HAVE_ECC_SIGN: ECC sign default: on
  31. * HAVE_ECC_VERIFY: ECC verify default: on
  32. * HAVE_ECC_DHE: ECC build shared secret default: on
  33. * HAVE_ECC_CDH: ECC cofactor DH shared secret default: off
  34. * HAVE_ECC_KEY_IMPORT: ECC Key import default: on
  35. * HAVE_ECC_KEY_EXPORT: ECC Key export default: on
  36. * ECC_SHAMIR: Enables Shamir calc method default: on
  37. * HAVE_COMP_KEY: Enables compressed key default: off
  38. * WOLFSSL_VALIDATE_ECC_IMPORT: Validate ECC key on import default: off
  39. * WOLFSSL_VALIDATE_ECC_KEYGEN: Validate ECC key gen default: off
  40. * WOLFSSL_CUSTOM_CURVES: Allow non-standard curves. default: off
  41. * Includes the curve "a" variable in calculation
  42. * ECC_DUMP_OID: Enables dump of OID encoding and sum default: off
  43. * ECC_CACHE_CURVE: Enables cache of curve info to improve perofrmance
  44. default: off
  45. * FP_ECC: ECC Fixed Point Cache default: off
  46. * USE_ECC_B_PARAM: Enable ECC curve B param default: off
  47. (on for HAVE_COMP_KEY)
  48. */
  49. /*
  50. ECC Curve Types:
  51. * NO_ECC_SECP Disables SECP curves default: off (not defined)
  52. * HAVE_ECC_SECPR2 Enables SECP R2 curves default: off
  53. * HAVE_ECC_SECPR3 Enables SECP R3 curves default: off
  54. * HAVE_ECC_BRAINPOOL Enables Brainpool curves default: off
  55. * HAVE_ECC_KOBLITZ Enables Koblitz curves default: off
  56. */
  57. /*
  58. ECC Curve Sizes:
  59. * ECC_USER_CURVES: Allows custom combination of key sizes below
  60. * HAVE_ALL_CURVES: Enable all key sizes (on unless ECC_USER_CURVES is defined)
  61. * HAVE_ECC112: 112 bit key
  62. * HAVE_ECC128: 128 bit key
  63. * HAVE_ECC160: 160 bit key
  64. * HAVE_ECC192: 192 bit key
  65. * HAVE_ECC224: 224 bit key
  66. * HAVE_ECC239: 239 bit key
  67. * NO_ECC256: Disables 256 bit key (on by default)
  68. * HAVE_ECC320: 320 bit key
  69. * HAVE_ECC384: 384 bit key
  70. * HAVE_ECC512: 512 bit key
  71. * HAVE_ECC521: 521 bit key
  72. */
  73. #ifdef HAVE_ECC
  74. /* Make sure custom curves is enabled for Brainpool or Koblitz curve types */
  75. #if (defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)) &&\
  76. !defined(WOLFSSL_CUSTOM_CURVES)
  77. #error Brainpool and Koblitz curves requires WOLFSSL_CUSTOM_CURVES
  78. #endif
  79. /* Make sure ASN is enabled for ECC sign/verify */
  80. #if (defined(HAVE_ECC_SIGN) || defined(HAVE_ECC_VERIFY)) && defined(NO_ASN)
  81. #error ASN must be enabled for ECC sign/verify
  82. #endif
  83. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  84. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  85. #define FIPS_NO_WRAPPERS
  86. #ifdef USE_WINDOWS_API
  87. #pragma code_seg(".fipsA$e2")
  88. #pragma const_seg(".fipsB$e2")
  89. #endif
  90. #endif
  91. #include <wolfssl/wolfcrypt/ecc.h>
  92. #include <wolfssl/wolfcrypt/asn.h>
  93. #include <wolfssl/wolfcrypt/error-crypt.h>
  94. #include <wolfssl/wolfcrypt/logging.h>
  95. #include <wolfssl/wolfcrypt/types.h>
  96. #ifdef WOLFSSL_HAVE_SP_ECC
  97. #include <wolfssl/wolfcrypt/sp.h>
  98. #endif
  99. #ifdef HAVE_ECC_ENCRYPT
  100. #include <wolfssl/wolfcrypt/hmac.h>
  101. #include <wolfssl/wolfcrypt/aes.h>
  102. #endif
  103. #ifdef HAVE_X963_KDF
  104. #include <wolfssl/wolfcrypt/hash.h>
  105. #endif
  106. #ifdef NO_INLINE
  107. #include <wolfssl/wolfcrypt/misc.h>
  108. #else
  109. #define WOLFSSL_MISC_INCLUDED
  110. #include <wolfcrypt/src/misc.c>
  111. #endif
  112. #if defined(FREESCALE_LTC_ECC)
  113. #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
  114. #endif
  115. #ifdef USE_FAST_MATH
  116. #define GEN_MEM_ERR FP_MEM
  117. #else
  118. #define GEN_MEM_ERR MP_MEM
  119. #endif
  120. /* internal ECC states */
  121. enum {
  122. ECC_STATE_NONE = 0,
  123. ECC_STATE_SHARED_SEC_GEN,
  124. ECC_STATE_SHARED_SEC_RES,
  125. ECC_STATE_SIGN_DO,
  126. ECC_STATE_SIGN_ENCODE,
  127. ECC_STATE_VERIFY_DECODE,
  128. ECC_STATE_VERIFY_DO,
  129. ECC_STATE_VERIFY_RES,
  130. };
  131. /* map
  132. ptmul -> mulmod
  133. */
  134. /* 256-bit curve on by default whether user curves or not */
  135. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  136. #define ECC112
  137. #endif
  138. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  139. #define ECC128
  140. #endif
  141. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  142. #define ECC160
  143. #endif
  144. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  145. #define ECC192
  146. #endif
  147. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  148. #define ECC224
  149. #endif
  150. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  151. #define ECC239
  152. #endif
  153. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  154. #define ECC256
  155. #endif
  156. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  157. #define ECC320
  158. #endif
  159. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  160. #define ECC384
  161. #endif
  162. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  163. #define ECC512
  164. #endif
  165. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  166. #define ECC521
  167. #endif
  168. /* The encoded OID's for ECC curves */
  169. #ifdef ECC112
  170. #ifndef NO_ECC_SECP
  171. static const ecc_oid_t ecc_oid_secp112r1[] = {
  172. #ifdef HAVE_OID_ENCODING
  173. 1,3,132,0,6
  174. #else
  175. 0x2B,0x81,0x04,0x00,0x06
  176. #endif
  177. };
  178. #endif /* !NO_ECC_SECP */
  179. #ifdef HAVE_ECC_SECPR2
  180. static const ecc_oid_t ecc_oid_secp112r2[] = {
  181. #ifdef HAVE_OID_ENCODING
  182. 1,3,132,0,7
  183. #else
  184. 0x2B,0x81,0x04,0x00,0x07
  185. #endif
  186. };
  187. #endif /* HAVE_ECC_SECPR2 */
  188. #endif /* ECC112 */
  189. #ifdef ECC128
  190. #ifndef NO_ECC_SECP
  191. static const ecc_oid_t ecc_oid_secp128r1[] = {
  192. #ifdef HAVE_OID_ENCODING
  193. 1,3,132,0,28
  194. #else
  195. 0x2B,0x81,0x04,0x00,0x1C
  196. #endif
  197. };
  198. #endif /* !NO_ECC_SECP */
  199. #ifdef HAVE_ECC_SECPR2
  200. static const ecc_oid_t ecc_oid_secp128r2[] = {
  201. #ifdef HAVE_OID_ENCODING
  202. 1,3,132,0,29
  203. #else
  204. 0x2B,0x81,0x04,0x00,0x1D
  205. #endif
  206. };
  207. #endif /* HAVE_ECC_SECPR2 */
  208. #endif /* ECC128 */
  209. #ifdef ECC160
  210. #ifndef NO_ECC_SECP
  211. static const ecc_oid_t ecc_oid_secp160r1[] = {
  212. #ifdef HAVE_OID_ENCODING
  213. 1,3,132,0,8
  214. #else
  215. 0x2B,0x81,0x04,0x00,0x08
  216. #endif
  217. };
  218. #endif /* !NO_ECC_SECP */
  219. #ifdef HAVE_ECC_SECPR2
  220. static const ecc_oid_t ecc_oid_secp160r2[] = {
  221. #ifdef HAVE_OID_ENCODING
  222. 1,3,132,0,30
  223. #else
  224. 0x2B,0x81,0x04,0x00,0x1E
  225. #endif
  226. };
  227. #endif /* HAVE_ECC_SECPR2 */
  228. #ifdef HAVE_ECC_KOBLITZ
  229. static const ecc_oid_t ecc_oid_secp160k1[] = {
  230. #ifdef HAVE_OID_ENCODING
  231. 1,3,132,0,9
  232. #else
  233. 0x2B,0x81,0x04,0x00,0x09
  234. #endif
  235. };
  236. #endif /* HAVE_ECC_KOBLITZ */
  237. #ifdef HAVE_ECC_BRAINPOOL
  238. static const ecc_oid_t ecc_oid_brainpoolp160r1[] = {
  239. #ifdef HAVE_OID_ENCODING
  240. 1,3,36,3,3,2,8,1,1,1
  241. #else
  242. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x01
  243. #endif
  244. };
  245. #endif /* HAVE_ECC_BRAINPOOL */
  246. #endif /* ECC160 */
  247. #ifdef ECC192
  248. #ifndef NO_ECC_SECP
  249. static const ecc_oid_t ecc_oid_secp192r1[] = {
  250. #ifdef HAVE_OID_ENCODING
  251. 1,2,840,10045,3,1,1
  252. #else
  253. 0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x01
  254. #endif
  255. };
  256. #endif /* !NO_ECC_SECP */
  257. #ifdef HAVE_ECC_SECPR2
  258. static const ecc_oid_t ecc_oid_prime192v2[] = {
  259. #ifdef HAVE_OID_ENCODING
  260. 1,2,840,10045,3,1,2
  261. #else
  262. 0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x02
  263. #endif
  264. };
  265. #endif /* HAVE_ECC_SECPR2 */
  266. #ifdef HAVE_ECC_SECPR3
  267. static const ecc_oid_t ecc_oid_prime192v3[] = {
  268. #ifdef HAVE_OID_ENCODING
  269. 1,2,840,10045,3,1,3
  270. #else
  271. 0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x03
  272. #endif
  273. };
  274. #endif /* HAVE_ECC_SECPR3 */
  275. #ifdef HAVE_ECC_KOBLITZ
  276. static const ecc_oid_t ecc_oid_secp192k1[] = {
  277. #ifdef HAVE_OID_ENCODING
  278. 1,3,132,0,31
  279. #else
  280. 0x2B,0x81,0x04,0x00,0x1F
  281. #endif
  282. };
  283. #endif /* HAVE_ECC_KOBLITZ */
  284. #ifdef HAVE_ECC_BRAINPOOL
  285. static const ecc_oid_t ecc_oid_brainpoolp192r1[] = {
  286. #ifdef HAVE_OID_ENCODING
  287. 1,3,36,3,3,2,8,1,1,3
  288. #else
  289. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x03
  290. #endif
  291. };
  292. #endif /* HAVE_ECC_BRAINPOOL */
  293. #endif /* ECC192 */
  294. #ifdef ECC224
  295. #ifndef NO_ECC_SECP
  296. static const ecc_oid_t ecc_oid_secp224r1[] = {
  297. #ifdef HAVE_OID_ENCODING
  298. 1,3,132,0,33
  299. #else
  300. 0x2B,0x81,0x04,0x00,0x21
  301. #endif
  302. };
  303. #endif /* !NO_ECC_SECP */
  304. #ifdef HAVE_ECC_KOBLITZ
  305. static const ecc_oid_t ecc_oid_secp224k1[] = {
  306. #ifdef HAVE_OID_ENCODING
  307. 1,3,132,0,32
  308. #else
  309. 0x2B,0x81,0x04,0x00,0x20
  310. #endif
  311. };
  312. #endif /* HAVE_ECC_KOBLITZ */
  313. #ifdef HAVE_ECC_BRAINPOOL
  314. static const ecc_oid_t ecc_oid_brainpoolp224r1[] = {
  315. #ifdef HAVE_OID_ENCODING
  316. 1,3,36,3,3,2,8,1,1,5
  317. #else
  318. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x05
  319. #endif
  320. };
  321. #endif /* HAVE_ECC_BRAINPOOL */
  322. #endif /* ECC224 */
  323. #ifdef ECC239
  324. #ifndef NO_ECC_SECP
  325. static const ecc_oid_t ecc_oid_prime239v1[] = {
  326. #ifdef HAVE_OID_ENCODING
  327. 1,2,840,10045,3,1,4
  328. #else
  329. 0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x04
  330. #endif
  331. };
  332. #endif /* !NO_ECC_SECP */
  333. #ifdef HAVE_ECC_SECPR2
  334. static const ecc_oid_t ecc_oid_prime239v2[] = {
  335. #ifdef HAVE_OID_ENCODING
  336. 1,2,840,10045,3,1,5
  337. #else
  338. 0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x05
  339. #endif
  340. };
  341. #endif /* HAVE_ECC_SECPR2 */
  342. #ifdef HAVE_ECC_SECPR3
  343. static const ecc_oid_t ecc_oid_prime239v3[] = {
  344. #ifdef HAVE_OID_ENCODING
  345. 1,2,840,10045,3,1,6
  346. #else
  347. 0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x06
  348. #endif
  349. };
  350. #endif /* HAVE_ECC_SECPR3 */
  351. #endif /* ECC239 */
  352. #ifdef ECC256
  353. #ifndef NO_ECC_SECP
  354. static const ecc_oid_t ecc_oid_secp256r1[] = {
  355. #ifdef HAVE_OID_ENCODING
  356. 1,2,840,10045,3,1,7
  357. #else
  358. 0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07
  359. #endif
  360. };
  361. #endif /* !NO_ECC_SECP */
  362. #ifdef HAVE_ECC_KOBLITZ
  363. static const ecc_oid_t ecc_oid_secp256k1[] = {
  364. #ifdef HAVE_OID_ENCODING
  365. 1,3,132,0,10
  366. #else
  367. 0x2B,0x81,0x04,0x00,0x0A
  368. #endif
  369. };
  370. #endif /* HAVE_ECC_KOBLITZ */
  371. #ifdef HAVE_ECC_BRAINPOOL
  372. static const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  373. #ifdef HAVE_OID_ENCODING
  374. 1,3,36,3,3,2,8,1,1,7
  375. #else
  376. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  377. #endif
  378. };
  379. #endif /* HAVE_ECC_BRAINPOOL */
  380. #endif /* ECC256 */
  381. #ifdef ECC320
  382. #ifdef HAVE_ECC_BRAINPOOL
  383. static const ecc_oid_t ecc_oid_brainpoolp320r1[] = {
  384. #ifdef HAVE_OID_ENCODING
  385. 1,3,36,3,3,2,8,1,1,9
  386. #else
  387. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x09
  388. #endif
  389. };
  390. #endif /* HAVE_ECC_BRAINPOOL */
  391. #endif /* ECC320 */
  392. #ifdef ECC384
  393. #ifndef NO_ECC_SECP
  394. static const ecc_oid_t ecc_oid_secp384r1[] = {
  395. #ifdef HAVE_OID_ENCODING
  396. 1,3,132,0,34
  397. #else
  398. 0x2B,0x81,0x04,0x00,0x22
  399. #endif
  400. };
  401. #endif /* !NO_ECC_SECP */
  402. #ifdef HAVE_ECC_BRAINPOOL
  403. static const ecc_oid_t ecc_oid_brainpoolp384r1[] = {
  404. #ifdef HAVE_OID_ENCODING
  405. 1,3,36,3,3,2,8,1,1,11
  406. #else
  407. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x0B
  408. #endif
  409. };
  410. #endif /* HAVE_ECC_BRAINPOOL */
  411. #endif /* ECC384 */
  412. #ifdef ECC512
  413. #ifdef HAVE_ECC_BRAINPOOL
  414. static const ecc_oid_t ecc_oid_brainpoolp512r1[] = {
  415. #ifdef HAVE_OID_ENCODING
  416. 1,3,36,3,3,2,8,1,1,13
  417. #else
  418. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x0D
  419. #endif
  420. };
  421. #endif /* HAVE_ECC_BRAINPOOL */
  422. #endif /* ECC512 */
  423. #ifdef ECC521
  424. #ifndef NO_ECC_SECP
  425. static const ecc_oid_t ecc_oid_secp521r1[] = {
  426. #ifdef HAVE_OID_ENCODING
  427. 1,3,132,0,35
  428. #else
  429. 0x2B,0x81,0x04,0x00,0x23
  430. #endif
  431. };
  432. #endif /* !NO_ECC_SECP */
  433. #endif /* ECC521 */
  434. /* This holds the key settings.
  435. ***MUST*** be organized by size from smallest to largest. */
  436. const ecc_set_type ecc_sets[] = {
  437. #ifdef ECC112
  438. #ifndef NO_ECC_SECP
  439. {
  440. 14, /* size/bytes */
  441. ECC_SECP112R1, /* ID */
  442. "SECP112R1", /* curve name */
  443. "DB7C2ABF62E35E668076BEAD208B", /* prime */
  444. "DB7C2ABF62E35E668076BEAD2088", /* A */
  445. "659EF8BA043916EEDE8911702B22", /* B */
  446. "DB7C2ABF62E35E7628DFAC6561C5", /* order */
  447. "9487239995A5EE76B55F9C2F098", /* Gx */
  448. "A89CE5AF8724C0A23E0E0FF77500", /* Gy */
  449. ecc_oid_secp112r1, /* oid/oidSz */
  450. sizeof(ecc_oid_secp112r1) / sizeof(ecc_oid_t),
  451. ECC_SECP112R1_OID, /* oid sum */
  452. 1, /* cofactor */
  453. },
  454. #endif /* !NO_ECC_SECP */
  455. #ifdef HAVE_ECC_SECPR2
  456. {
  457. 14, /* size/bytes */
  458. ECC_SECP112R2, /* ID */
  459. "SECP112R2", /* curve name */
  460. "DB7C2ABF62E35E668076BEAD208B", /* prime */
  461. "6127C24C05F38A0AAAF65C0EF02C", /* A */
  462. "51DEF1815DB5ED74FCC34C85D709", /* B */
  463. "36DF0AAFD8B8D7597CA10520D04B", /* order */
  464. "4BA30AB5E892B4E1649DD0928643", /* Gx */
  465. "ADCD46F5882E3747DEF36E956E97", /* Gy */
  466. ecc_oid_secp112r2, /* oid/oidSz */
  467. sizeof(ecc_oid_secp112r2) / sizeof(ecc_oid_t),
  468. ECC_SECP112R2_OID, /* oid sum */
  469. 4, /* cofactor */
  470. },
  471. #endif /* HAVE_ECC_SECPR2 */
  472. #endif /* ECC112 */
  473. #ifdef ECC128
  474. #ifndef NO_ECC_SECP
  475. {
  476. 16, /* size/bytes */
  477. ECC_SECP128R1, /* ID */
  478. "SECP128R1", /* curve name */
  479. "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF", /* prime */
  480. "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC", /* A */
  481. "E87579C11079F43DD824993C2CEE5ED3", /* B */
  482. "FFFFFFFE0000000075A30D1B9038A115", /* order */
  483. "161FF7528B899B2D0C28607CA52C5B86", /* Gx */
  484. "CF5AC8395BAFEB13C02DA292DDED7A83", /* Gy */
  485. ecc_oid_secp128r1, /* oid/oidSz */
  486. sizeof(ecc_oid_secp128r1) / sizeof(ecc_oid_t),
  487. ECC_SECP128R1_OID, /* oid sum */
  488. 1, /* cofactor */
  489. },
  490. #endif /* !NO_ECC_SECP */
  491. #ifdef HAVE_ECC_SECPR2
  492. {
  493. 16, /* size/bytes */
  494. ECC_SECP128R2, /* ID */
  495. "SECP128R2", /* curve name */
  496. "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF", /* prime */
  497. "D6031998D1B3BBFEBF59CC9BBFF9AEE1", /* A */
  498. "5EEEFCA380D02919DC2C6558BB6D8A5D", /* B */
  499. "3FFFFFFF7FFFFFFFBE0024720613B5A3", /* order */
  500. "7B6AA5D85E572983E6FB32A7CDEBC140", /* Gx */
  501. "27B6916A894D3AEE7106FE805FC34B44", /* Gy */
  502. ecc_oid_secp128r2, /* oid/oidSz */
  503. sizeof(ecc_oid_secp128r2) / sizeof(ecc_oid_t),
  504. ECC_SECP128R2_OID, /* oid sum */
  505. 4, /* cofactor */
  506. },
  507. #endif /* HAVE_ECC_SECPR2 */
  508. #endif /* ECC128 */
  509. #ifdef ECC160
  510. #ifndef NO_ECC_SECP
  511. {
  512. 20, /* size/bytes */
  513. ECC_SECP160R1, /* ID */
  514. "SECP160R1", /* curve name */
  515. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF", /* prime */
  516. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC", /* A */
  517. "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45", /* B */
  518. "100000000000000000001F4C8F927AED3CA752257",/* order */
  519. "4A96B5688EF573284664698968C38BB913CBFC82", /* Gx */
  520. "23A628553168947D59DCC912042351377AC5FB32", /* Gy */
  521. ecc_oid_secp160r1, /* oid/oidSz */
  522. sizeof(ecc_oid_secp160r1) / sizeof(ecc_oid_t),
  523. ECC_SECP160R1_OID, /* oid sum */
  524. 1, /* cofactor */
  525. },
  526. #endif /* !NO_ECC_SECP */
  527. #ifdef HAVE_ECC_SECPR2
  528. {
  529. 20, /* size/bytes */
  530. ECC_SECP160R2, /* ID */
  531. "SECP160R2", /* curve name */
  532. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", /* prime */
  533. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC70", /* A */
  534. "B4E134D3FB59EB8BAB57274904664D5AF50388BA", /* B */
  535. "100000000000000000000351EE786A818F3A1A16B",/* order */
  536. "52DCB034293A117E1F4FF11B30F7199D3144CE6D", /* Gx */
  537. "FEAFFEF2E331F296E071FA0DF9982CFEA7D43F2E", /* Gy */
  538. ecc_oid_secp160r2, /* oid/oidSz */
  539. sizeof(ecc_oid_secp160r2) / sizeof(ecc_oid_t),
  540. ECC_SECP160R2_OID, /* oid sum */
  541. 1, /* cofactor */
  542. },
  543. #endif /* HAVE_ECC_SECPR2 */
  544. #ifdef HAVE_ECC_KOBLITZ
  545. {
  546. 20, /* size/bytes */
  547. ECC_SECP160K1, /* ID */
  548. "SECP160K1", /* curve name */
  549. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", /* prime */
  550. "0000000000000000000000000000000000000000", /* A */
  551. "0000000000000000000000000000000000000007", /* B */
  552. "100000000000000000001B8FA16DFAB9ACA16B6B3",/* order */
  553. "3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", /* Gx */
  554. "938CF935318FDCED6BC28286531733C3F03C4FEE", /* Gy */
  555. ecc_oid_secp160k1, /* oid/oidSz */
  556. sizeof(ecc_oid_secp160k1) / sizeof(ecc_oid_t),
  557. ECC_SECP160K1_OID, /* oid sum */
  558. 1, /* cofactor */
  559. },
  560. #endif /* HAVE_ECC_KOBLITZ */
  561. #ifdef HAVE_ECC_BRAINPOOL
  562. {
  563. 20, /* size/bytes */
  564. ECC_BRAINPOOLP160R1, /* ID */
  565. "BRAINPOOLP160R1", /* curve name */
  566. "E95E4A5F737059DC60DFC7AD95B3D8139515620F", /* prime */
  567. "340E7BE2A280EB74E2BE61BADA745D97E8F7C300", /* A */
  568. "1E589A8595423412134FAA2DBDEC95C8D8675E58", /* B */
  569. "E95E4A5F737059DC60DF5991D45029409E60FC09", /* order */
  570. "BED5AF16EA3F6A4F62938C4631EB5AF7BDBCDBC3", /* Gx */
  571. "1667CB477A1A8EC338F94741669C976316DA6321", /* Gy */
  572. ecc_oid_brainpoolp160r1, /* oid/oidSz */
  573. sizeof(ecc_oid_brainpoolp160r1) / sizeof(ecc_oid_t),
  574. ECC_BRAINPOOLP160R1_OID, /* oid sum */
  575. 1, /* cofactor */
  576. },
  577. #endif /* HAVE_ECC_BRAINPOOL */
  578. #endif /* ECC160 */
  579. #ifdef ECC192
  580. #ifndef NO_ECC_SECP
  581. {
  582. 24, /* size/bytes */
  583. ECC_SECP192R1, /* ID */
  584. "SECP192R1", /* curve name */
  585. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", /* prime */
  586. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", /* A */
  587. "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1", /* B */
  588. "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", /* order */
  589. "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012", /* Gx */
  590. "7192B95FFC8DA78631011ED6B24CDD573F977A11E794811", /* Gy */
  591. ecc_oid_secp192r1, /* oid/oidSz */
  592. sizeof(ecc_oid_secp192r1) / sizeof(ecc_oid_t),
  593. ECC_SECP192R1_OID, /* oid sum */
  594. 1, /* cofactor */
  595. },
  596. #endif /* !NO_ECC_SECP */
  597. #ifdef HAVE_ECC_SECPR2
  598. {
  599. 24, /* size/bytes */
  600. ECC_PRIME192V2, /* ID */
  601. "PRIME192V2", /* curve name */
  602. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", /* prime */
  603. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", /* A */
  604. "CC22D6DFB95C6B25E49C0D6364A4E5980C393AA21668D953", /* B */
  605. "FFFFFFFFFFFFFFFFFFFFFFFE5FB1A724DC80418648D8DD31", /* order */
  606. "EEA2BAE7E1497842F2DE7769CFE9C989C072AD696F48034A", /* Gx */
  607. "6574D11D69B6EC7A672BB82A083DF2F2B0847DE970B2DE15", /* Gy */
  608. ecc_oid_prime192v2, /* oid/oidSz */
  609. sizeof(ecc_oid_prime192v2) / sizeof(ecc_oid_t),
  610. ECC_PRIME192V2_OID, /* oid sum */
  611. 1, /* cofactor */
  612. },
  613. #endif /* HAVE_ECC_SECPR2 */
  614. #ifdef HAVE_ECC_SECPR3
  615. {
  616. 24, /* size/bytes */
  617. ECC_PRIME192V3, /* ID */
  618. "PRIME192V3", /* curve name */
  619. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", /* prime */
  620. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", /* A */
  621. "22123DC2395A05CAA7423DAECCC94760A7D462256BD56916", /* B */
  622. "FFFFFFFFFFFFFFFFFFFFFFFF7A62D031C83F4294F640EC13", /* order */
  623. "7D29778100C65A1DA1783716588DCE2B8B4AEE8E228F1896", /* Gx */
  624. "38A90F22637337334B49DCB66A6DC8F9978ACA7648A943B0", /* Gy */
  625. ecc_oid_prime192v3, /* oid/oidSz */
  626. sizeof(ecc_oid_prime192v3) / sizeof(ecc_oid_t),
  627. ECC_PRIME192V3_OID, /* oid sum */
  628. 1, /* cofactor */
  629. },
  630. #endif /* HAVE_ECC_SECPR3 */
  631. #ifdef HAVE_ECC_KOBLITZ
  632. {
  633. 24, /* size/bytes */
  634. ECC_SECP192K1, /* ID */
  635. "SECP192K1", /* curve name */
  636. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", /* prime */
  637. "000000000000000000000000000000000000000000000000", /* A */
  638. "000000000000000000000000000000000000000000000003", /* B */
  639. "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", /* order */
  640. "DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", /* Gx */
  641. "9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", /* Gy */
  642. ecc_oid_secp192k1, /* oid/oidSz */
  643. sizeof(ecc_oid_secp192k1) / sizeof(ecc_oid_t),
  644. ECC_SECP192K1_OID, /* oid sum */
  645. 1, /* cofactor */
  646. },
  647. #endif /* HAVE_ECC_KOBLITZ */
  648. #ifdef HAVE_ECC_BRAINPOOL
  649. {
  650. 24, /* size/bytes */
  651. ECC_BRAINPOOLP192R1, /* ID */
  652. "BRAINPOOLP192R1", /* curve name */
  653. "C302F41D932A36CDA7A3463093D18DB78FCE476DE1A86297", /* prime */
  654. "6A91174076B1E0E19C39C031FE8685C1CAE040E5C69A28EF", /* A */
  655. "469A28EF7C28CCA3DC721D044F4496BCCA7EF4146FBF25C9", /* B */
  656. "C302F41D932A36CDA7A3462F9E9E916B5BE8F1029AC4ACC1", /* order */
  657. "C0A0647EAAB6A48753B033C56CB0F0900A2F5C4853375FD6", /* Gx */
  658. "14B690866ABD5BB88B5F4828C1490002E6773FA2FA299B8F", /* Gy */
  659. ecc_oid_brainpoolp192r1, /* oid/oidSz */
  660. sizeof(ecc_oid_brainpoolp192r1) / sizeof(ecc_oid_t),
  661. ECC_BRAINPOOLP192R1_OID, /* oid sum */
  662. 1, /* cofactor */
  663. },
  664. #endif /* HAVE_ECC_BRAINPOOL */
  665. #endif /* ECC192 */
  666. #ifdef ECC224
  667. #ifndef NO_ECC_SECP
  668. {
  669. 28, /* size/bytes */
  670. ECC_SECP224R1, /* ID */
  671. "SECP224R1", /* curve name */
  672. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", /* prime */
  673. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", /* A */
  674. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", /* B */
  675. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", /* order */
  676. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", /* Gx */
  677. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", /* Gy */
  678. ecc_oid_secp224r1, /* oid/oidSz */
  679. sizeof(ecc_oid_secp224r1) / sizeof(ecc_oid_t),
  680. ECC_SECP224R1_OID, /* oid sum */
  681. 1, /* cofactor */
  682. },
  683. #endif /* !NO_ECC_SECP */
  684. #ifdef HAVE_ECC_KOBLITZ
  685. {
  686. 28, /* size/bytes */
  687. ECC_SECP224K1, /* ID */
  688. "SECP224K1", /* curve name */
  689. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D", /* prime */
  690. "00000000000000000000000000000000000000000000000000000000", /* A */
  691. "00000000000000000000000000000000000000000000000000000005", /* B */
  692. "10000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7",/* order */
  693. "A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C", /* Gx */
  694. "7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5", /* Gy */
  695. ecc_oid_secp224k1, /* oid/oidSz */
  696. sizeof(ecc_oid_secp224k1) / sizeof(ecc_oid_t),
  697. ECC_SECP224K1_OID, /* oid sum */
  698. 1, /* cofactor */
  699. },
  700. #endif /* HAVE_ECC_KOBLITZ */
  701. #ifdef HAVE_ECC_BRAINPOOL
  702. {
  703. 28, /* size/bytes */
  704. ECC_BRAINPOOLP224R1, /* ID */
  705. "BRAINPOOLP224R1", /* curve name */
  706. "D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF", /* prime */
  707. "68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43", /* A */
  708. "2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B", /* B */
  709. "D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F", /* order */
  710. "0D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D", /* Gx */
  711. "58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD", /* Gy */
  712. ecc_oid_brainpoolp224r1, /* oid/oidSz */
  713. sizeof(ecc_oid_brainpoolp224r1) / sizeof(ecc_oid_t),
  714. ECC_BRAINPOOLP224R1_OID, /* oid sum */
  715. 1, /* cofactor */
  716. },
  717. #endif /* HAVE_ECC_BRAINPOOL */
  718. #endif /* ECC224 */
  719. #ifdef ECC239
  720. #ifndef NO_ECC_SECP
  721. {
  722. 30, /* size/bytes */
  723. ECC_PRIME239V1, /* ID */
  724. "PRIME239V1", /* curve name */
  725. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFF", /* prime */
  726. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFC", /* A */
  727. "6B016C3BDCF18941D0D654921475CA71A9DB2FB27D1D37796185C2942C0A", /* B */
  728. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFF9E5E9A9F5D9071FBD1522688909D0B", /* order */
  729. "0FFA963CDCA8816CCC33B8642BEDF905C3D358573D3F27FBBD3B3CB9AAAF", /* Gx */
  730. "7DEBE8E4E90A5DAE6E4054CA530BA04654B36818CE226B39FCCB7B02F1AE", /* Gy */
  731. ecc_oid_prime239v1, /* oid/oidSz */
  732. sizeof(ecc_oid_prime239v1) / sizeof(ecc_oid_t),
  733. ECC_PRIME239V1_OID, /* oid sum */
  734. 1, /* cofactor */
  735. },
  736. #endif /* !NO_ECC_SECP */
  737. #ifdef HAVE_ECC_SECPR2
  738. {
  739. 30, /* size/bytes */
  740. ECC_PRIME239V2, /* ID */
  741. "PRIME239V2", /* curve name */
  742. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFF", /* prime */
  743. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFC", /* A */
  744. "617FAB6832576CBBFED50D99F0249C3FEE58B94BA0038C7AE84C8C832F2C", /* B */
  745. "7FFFFFFFFFFFFFFFFFFFFFFF800000CFA7E8594377D414C03821BC582063", /* order */
  746. "38AF09D98727705120C921BB5E9E26296A3CDCF2F35757A0EAFD87B830E7", /* Gx */
  747. "5B0125E4DBEA0EC7206DA0FC01D9B081329FB555DE6EF460237DFF8BE4BA", /* Gy */
  748. ecc_oid_prime239v2, /* oid/oidSz */
  749. sizeof(ecc_oid_prime239v2) / sizeof(ecc_oid_t),
  750. ECC_PRIME239V2_OID, /* oid sum */
  751. 1, /* cofactor */
  752. },
  753. #endif /* HAVE_ECC_SECPR2 */
  754. #ifdef HAVE_ECC_SECPR3
  755. {
  756. 30, /* size/bytes */
  757. ECC_PRIME239V3, /* ID */
  758. "PRIME239V3", /* curve name */
  759. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFF", /* prime */
  760. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFFFFFFFF8000000000007FFFFFFFFFFC", /* A */
  761. "255705FA2A306654B1F4CB03D6A750A30C250102D4988717D9BA15AB6D3E", /* B */
  762. "7FFFFFFFFFFFFFFFFFFFFFFF7FFFFF975DEB41B3A6057C3C432146526551", /* order */
  763. "6768AE8E18BB92CFCF005C949AA2C6D94853D0E660BBF854B1C9505FE95A", /* Gx */
  764. "1607E6898F390C06BC1D552BAD226F3B6FCFE48B6E818499AF18E3ED6CF3", /* Gy */
  765. ecc_oid_prime239v3, /* oid/oidSz */
  766. sizeof(ecc_oid_prime239v3) / sizeof(ecc_oid_t),
  767. ECC_PRIME239V3_OID, /* oid sum */
  768. 1, /* cofactor */
  769. },
  770. #endif /* HAVE_ECC_SECPR3 */
  771. #endif /* ECC239 */
  772. #ifdef ECC256
  773. #ifndef NO_ECC_SECP
  774. {
  775. 32, /* size/bytes */
  776. ECC_SECP256R1, /* ID */
  777. "SECP256R1", /* curve name */
  778. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", /* prime */
  779. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", /* A */
  780. "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B", /* B */
  781. "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551", /* order */
  782. "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296", /* Gx */
  783. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5", /* Gy */
  784. ecc_oid_secp256r1, /* oid/oidSz */
  785. sizeof(ecc_oid_secp256r1) / sizeof(ecc_oid_t),
  786. ECC_SECP256R1_OID, /* oid sum */
  787. 1, /* cofactor */
  788. },
  789. #endif /* !NO_ECC_SECP */
  790. #ifdef HAVE_ECC_KOBLITZ
  791. {
  792. 32, /* size/bytes */
  793. ECC_SECP256K1, /* ID */
  794. "SECP256K1", /* curve name */
  795. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", /* prime */
  796. "0000000000000000000000000000000000000000000000000000000000000000", /* A */
  797. "0000000000000000000000000000000000000000000000000000000000000007", /* B */
  798. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", /* order */
  799. "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", /* Gx */
  800. "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", /* Gy */
  801. ecc_oid_secp256k1, /* oid/oidSz */
  802. sizeof(ecc_oid_secp256k1) / sizeof(ecc_oid_t),
  803. ECC_SECP256K1_OID, /* oid sum */
  804. 1, /* cofactor */
  805. },
  806. #endif /* HAVE_ECC_KOBLITZ */
  807. #ifdef HAVE_ECC_BRAINPOOL
  808. {
  809. 32, /* size/bytes */
  810. ECC_BRAINPOOLP256R1, /* ID */
  811. "BRAINPOOLP256R1", /* curve name */
  812. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  813. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  814. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  815. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  816. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  817. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  818. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  819. sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t),
  820. ECC_BRAINPOOLP256R1_OID, /* oid sum */
  821. 1, /* cofactor */
  822. },
  823. #endif /* HAVE_ECC_BRAINPOOL */
  824. #endif /* ECC256 */
  825. #ifdef ECC320
  826. #ifdef HAVE_ECC_BRAINPOOL
  827. {
  828. 40, /* size/bytes */
  829. ECC_BRAINPOOLP320R1, /* ID */
  830. "BRAINPOOLP320R1", /* curve name */
  831. "D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E27", /* prime */
  832. "3EE30B568FBAB0F883CCEBD46D3F3BB8A2A73513F5EB79DA66190EB085FFA9F492F375A97D860EB4", /* A */
  833. "520883949DFDBC42D3AD198640688A6FE13F41349554B49ACC31DCCD884539816F5EB4AC8FB1F1A6", /* B */
  834. "D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658E98691555B44C59311", /* order */
  835. "43BD7E9AFB53D8B85289BCC48EE5BFE6F20137D10A087EB6E7871E2A10A599C710AF8D0D39E20611", /* Gx */
  836. "14FDD05545EC1CC8AB4093247F77275E0743FFED117182EAA9C77877AAAC6AC7D35245D1692E8EE1", /* Gy */
  837. ecc_oid_brainpoolp320r1, sizeof(ecc_oid_brainpoolp320r1) / sizeof(ecc_oid_t), /* oid/oidSz */
  838. ECC_BRAINPOOLP320R1_OID, /* oid sum */
  839. 1, /* cofactor */
  840. },
  841. #endif /* HAVE_ECC_BRAINPOOL */
  842. #endif /* ECC320 */
  843. #ifdef ECC384
  844. #ifndef NO_ECC_SECP
  845. {
  846. 48, /* size/bytes */
  847. ECC_SECP384R1, /* ID */
  848. "SECP384R1", /* curve name */
  849. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF", /* prime */
  850. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC", /* A */
  851. "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF", /* B */
  852. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973", /* order */
  853. "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7", /* Gx */
  854. "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F", /* Gy */
  855. ecc_oid_secp384r1, sizeof(ecc_oid_secp384r1) / sizeof(ecc_oid_t), /* oid/oidSz */
  856. ECC_SECP384R1_OID, /* oid sum */
  857. 1, /* cofactor */
  858. },
  859. #endif /* !NO_ECC_SECP */
  860. #ifdef HAVE_ECC_BRAINPOOL
  861. {
  862. 48, /* size/bytes */
  863. ECC_BRAINPOOLP384R1, /* ID */
  864. "BRAINPOOLP384R1", /* curve name */
  865. "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53", /* prime */
  866. "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F90F8AA5814A503AD4EB04A8C7DD22CE2826", /* A */
  867. "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62D57CB4390295DBC9943AB78696FA504C11", /* B */
  868. "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565", /* order */
  869. "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E", /* Gx */
  870. "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315", /* Gy */
  871. ecc_oid_brainpoolp384r1, sizeof(ecc_oid_brainpoolp384r1) / sizeof(ecc_oid_t), /* oid/oidSz */
  872. ECC_BRAINPOOLP384R1_OID, /* oid sum */
  873. 1, /* cofactor */
  874. },
  875. #endif /* HAVE_ECC_BRAINPOOL */
  876. #endif /* ECC384 */
  877. #ifdef ECC512
  878. #ifdef HAVE_ECC_BRAINPOOL
  879. {
  880. 64, /* size/bytes */
  881. ECC_BRAINPOOLP512R1, /* ID */
  882. "BRAINPOOLP512R1", /* curve name */
  883. "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3", /* prime */
  884. "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA", /* A */
  885. "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723", /* B */
  886. "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069", /* order */
  887. "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822", /* Gx */
  888. "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892", /* Gy */
  889. ecc_oid_brainpoolp512r1, sizeof(ecc_oid_brainpoolp512r1) / sizeof(ecc_oid_t), /* oid/oidSz */
  890. ECC_BRAINPOOLP512R1_OID, /* oid sum */
  891. 1, /* cofactor */
  892. },
  893. #endif /* HAVE_ECC_BRAINPOOL */
  894. #endif /* ECC512 */
  895. #ifdef ECC521
  896. #ifndef NO_ECC_SECP
  897. {
  898. 66, /* size/bytes */
  899. ECC_SECP521R1, /* ID */
  900. "SECP521R1", /* curve name */
  901. "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* prime */
  902. "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", /* A */
  903. "51953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00", /* B */
  904. "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409", /* order */
  905. "C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66", /* Gx */
  906. "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650", /* Gy */
  907. ecc_oid_secp521r1, sizeof(ecc_oid_secp521r1) / sizeof(ecc_oid_t), /* oid/oidSz */
  908. ECC_SECP521R1_OID, /* oid sum */
  909. 1, /* cofactor */
  910. },
  911. #endif /* !NO_ECC_SECP */
  912. #endif /* ECC521 */
  913. #if defined(WOLFSSL_CUSTOM_CURVES) && defined(ECC_CACHE_CURVE)
  914. /* place holder for custom curve index for cache */
  915. {
  916. 1, /* non-zero */
  917. ECC_CURVE_CUSTOM,
  918. NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  919. NULL, 0, 0, 0
  920. },
  921. #endif
  922. {
  923. 0, -1,
  924. NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  925. NULL, 0, 0, 0
  926. }
  927. };
  928. #define ECC_SET_COUNT (sizeof(ecc_sets)/sizeof(ecc_set_type))
  929. #ifdef HAVE_OID_ENCODING
  930. /* encoded OID cache */
  931. typedef struct {
  932. word32 oidSz;
  933. byte oid[ECC_MAX_OID_LEN];
  934. } oid_cache_t;
  935. static oid_cache_t ecc_oid_cache[ECC_SET_COUNT];
  936. #endif
  937. #ifdef HAVE_COMP_KEY
  938. static int wc_ecc_export_x963_compressed(ecc_key*, byte* out, word32* outLen);
  939. #endif
  940. #ifdef WOLFSSL_ATECC508A
  941. typedef void* ecc_curve_spec;
  942. #else
  943. static int ecc_check_pubkey_order(ecc_key* key, ecc_point* pubkey, mp_int* a,
  944. mp_int* prime, mp_int* order);
  945. #ifdef ECC_SHAMIR
  946. static int ecc_mul2add(ecc_point* A, mp_int* kA, ecc_point* B, mp_int* kB,
  947. ecc_point* C, mp_int* a, mp_int* modulus, void* heap);
  948. #endif
  949. int mp_jacobi(mp_int* a, mp_int* n, int* c);
  950. int mp_sqrtmod_prime(mp_int* n, mp_int* prime, mp_int* ret);
  951. /* Curve Specs */
  952. typedef struct ecc_curve_spec {
  953. const ecc_set_type* dp;
  954. mp_int* prime;
  955. mp_int* Af;
  956. #ifdef USE_ECC_B_PARAM
  957. mp_int* Bf;
  958. #endif
  959. mp_int* order;
  960. mp_int* Gx;
  961. mp_int* Gy;
  962. #ifdef ECC_CACHE_CURVE
  963. mp_int prime_lcl;
  964. mp_int Af_lcl;
  965. #ifdef USE_ECC_B_PARAM
  966. mp_int Bf_lcl;
  967. #endif
  968. mp_int order_lcl;
  969. mp_int Gx_lcl;
  970. mp_int Gy_lcl;
  971. #else
  972. mp_int* spec_ints;
  973. word32 spec_count;
  974. word32 spec_use;
  975. #endif
  976. byte load_mask;
  977. } ecc_curve_spec;
  978. enum ecc_curve_load_mask {
  979. ECC_CURVE_FIELD_NONE = 0x00,
  980. ECC_CURVE_FIELD_PRIME = 0x01,
  981. ECC_CURVE_FIELD_AF = 0x02,
  982. #ifdef USE_ECC_B_PARAM
  983. ECC_CURVE_FIELD_BF = 0x04,
  984. #endif
  985. ECC_CURVE_FIELD_ORDER = 0x08,
  986. ECC_CURVE_FIELD_GX = 0x10,
  987. ECC_CURVE_FIELD_GY = 0x20,
  988. #ifdef USE_ECC_B_PARAM
  989. ECC_CURVE_FIELD_ALL = 0x3F,
  990. ECC_CURVE_FIELD_COUNT = 6,
  991. #else
  992. ECC_CURVE_FIELD_ALL = 0x3B,
  993. ECC_CURVE_FIELD_COUNT = 5,
  994. #endif
  995. };
  996. #ifdef ECC_CACHE_CURVE
  997. /* cache (mp_int) of the curve parameters */
  998. static ecc_curve_spec* ecc_curve_spec_cache[ECC_SET_COUNT];
  999. #ifndef SINGLE_THREADED
  1000. static wolfSSL_Mutex ecc_curve_cache_mutex;
  1001. #endif
  1002. #define DECLARE_CURVE_SPECS(intcount) ecc_curve_spec* curve = NULL;
  1003. #else
  1004. #define DECLARE_CURVE_SPECS(intcount) \
  1005. mp_int spec_ints[(intcount)]; \
  1006. ecc_curve_spec curve_lcl; \
  1007. ecc_curve_spec* curve = &curve_lcl; \
  1008. XMEMSET(curve, 0, sizeof(ecc_curve_spec)); \
  1009. curve->spec_ints = spec_ints; \
  1010. curve->spec_count = intcount;
  1011. #endif /* ECC_CACHE_CURVE */
  1012. static void _wc_ecc_curve_free(ecc_curve_spec* curve)
  1013. {
  1014. if (curve == NULL) {
  1015. return;
  1016. }
  1017. if (curve->load_mask & ECC_CURVE_FIELD_PRIME)
  1018. mp_clear(curve->prime);
  1019. if (curve->load_mask & ECC_CURVE_FIELD_AF)
  1020. mp_clear(curve->Af);
  1021. #ifdef USE_ECC_B_PARAM
  1022. if (curve->load_mask & ECC_CURVE_FIELD_BF)
  1023. mp_clear(curve->Bf);
  1024. #endif
  1025. if (curve->load_mask & ECC_CURVE_FIELD_ORDER)
  1026. mp_clear(curve->order);
  1027. if (curve->load_mask & ECC_CURVE_FIELD_GX)
  1028. mp_clear(curve->Gx);
  1029. if (curve->load_mask & ECC_CURVE_FIELD_GY)
  1030. mp_clear(curve->Gy);
  1031. curve->load_mask = 0;
  1032. }
  1033. static void wc_ecc_curve_free(ecc_curve_spec* curve)
  1034. {
  1035. /* don't free cached curves */
  1036. #ifndef ECC_CACHE_CURVE
  1037. _wc_ecc_curve_free(curve);
  1038. #endif
  1039. (void)curve;
  1040. }
  1041. static int wc_ecc_curve_load_item(const char* src, mp_int** dst,
  1042. ecc_curve_spec* curve, byte mask)
  1043. {
  1044. int err;
  1045. #ifndef ECC_CACHE_CURVE
  1046. /* get mp_int from temp */
  1047. if (curve->spec_use >= curve->spec_count) {
  1048. WOLFSSL_MSG("Invalid DECLARE_CURVE_SPECS count");
  1049. return ECC_BAD_ARG_E;
  1050. }
  1051. *dst = &curve->spec_ints[curve->spec_use++];
  1052. #endif
  1053. err = mp_init(*dst);
  1054. if (err == MP_OKAY) {
  1055. curve->load_mask |= mask;
  1056. err = mp_read_radix(*dst, src, MP_RADIX_HEX);
  1057. #ifdef HAVE_WOLF_BIGINT
  1058. if (err == MP_OKAY)
  1059. err = wc_mp_to_bigint(*dst, &(*dst)->raw);
  1060. #endif
  1061. }
  1062. return err;
  1063. }
  1064. static int wc_ecc_curve_load(const ecc_set_type* dp, ecc_curve_spec** pCurve,
  1065. byte load_mask)
  1066. {
  1067. int ret = 0, x;
  1068. ecc_curve_spec* curve;
  1069. byte load_items = 0; /* mask of items to load */
  1070. if (dp == NULL || pCurve == NULL)
  1071. return BAD_FUNC_ARG;
  1072. #ifdef ECC_CACHE_CURVE
  1073. x = wc_ecc_get_curve_idx(dp->id);
  1074. if (x == ECC_CURVE_INVALID)
  1075. return ECC_BAD_ARG_E;
  1076. #if !defined(SINGLE_THREADED)
  1077. ret = wc_LockMutex(&ecc_curve_cache_mutex);
  1078. if (ret != 0) {
  1079. return ret;
  1080. }
  1081. #endif
  1082. /* make sure cache has been allocated */
  1083. if (ecc_curve_spec_cache[x] == NULL) {
  1084. ecc_curve_spec_cache[x] = (ecc_curve_spec*)XMALLOC(
  1085. sizeof(ecc_curve_spec), NULL, DYNAMIC_TYPE_ECC);
  1086. if (ecc_curve_spec_cache[x] == NULL) {
  1087. #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED)
  1088. wc_UnLockMutex(&ecc_curve_cache_mutex);
  1089. #endif
  1090. return MEMORY_E;
  1091. }
  1092. XMEMSET(ecc_curve_spec_cache[x], 0, sizeof(ecc_curve_spec));
  1093. }
  1094. /* set curve pointer to cache */
  1095. *pCurve = ecc_curve_spec_cache[x];
  1096. #endif /* ECC_CACHE_CURVE */
  1097. curve = *pCurve;
  1098. /* make sure the curve is initialized */
  1099. if (curve->dp != dp) {
  1100. curve->load_mask = 0;
  1101. #ifdef ECC_CACHE_CURVE
  1102. curve->prime = &curve->prime_lcl;
  1103. curve->Af = &curve->Af_lcl;
  1104. #ifdef USE_ECC_B_PARAM
  1105. curve->Bf = &curve->Bf_lcl;
  1106. #endif
  1107. curve->order = &curve->order_lcl;
  1108. curve->Gx = &curve->Gx_lcl;
  1109. curve->Gy = &curve->Gy_lcl;
  1110. #endif
  1111. }
  1112. curve->dp = dp; /* set dp info */
  1113. /* determine items to load */
  1114. load_items = (~curve->load_mask & load_mask);
  1115. curve->load_mask |= load_items;
  1116. /* load items */
  1117. x = 0;
  1118. if (load_items & ECC_CURVE_FIELD_PRIME)
  1119. x += wc_ecc_curve_load_item(dp->prime, &curve->prime, curve,
  1120. ECC_CURVE_FIELD_PRIME);
  1121. if (load_items & ECC_CURVE_FIELD_AF)
  1122. x += wc_ecc_curve_load_item(dp->Af, &curve->Af, curve,
  1123. ECC_CURVE_FIELD_AF);
  1124. #ifdef USE_ECC_B_PARAM
  1125. if (load_items & ECC_CURVE_FIELD_BF)
  1126. x += wc_ecc_curve_load_item(dp->Bf, &curve->Bf, curve,
  1127. ECC_CURVE_FIELD_BF);
  1128. #endif
  1129. if (load_items & ECC_CURVE_FIELD_ORDER)
  1130. x += wc_ecc_curve_load_item(dp->order, &curve->order, curve,
  1131. ECC_CURVE_FIELD_ORDER);
  1132. if (load_items & ECC_CURVE_FIELD_GX)
  1133. x += wc_ecc_curve_load_item(dp->Gx, &curve->Gx, curve,
  1134. ECC_CURVE_FIELD_GX);
  1135. if (load_items & ECC_CURVE_FIELD_GY)
  1136. x += wc_ecc_curve_load_item(dp->Gy, &curve->Gy, curve,
  1137. ECC_CURVE_FIELD_GY);
  1138. /* check for error */
  1139. if (x != 0) {
  1140. wc_ecc_curve_free(curve);
  1141. ret = MP_READ_E;
  1142. }
  1143. #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED)
  1144. wc_UnLockMutex(&ecc_curve_cache_mutex);
  1145. #endif
  1146. return ret;
  1147. }
  1148. #ifdef ECC_CACHE_CURVE
  1149. int wc_ecc_curve_cache_init(void)
  1150. {
  1151. int ret = 0;
  1152. #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED)
  1153. ret = wc_InitMutex(&ecc_curve_cache_mutex);
  1154. #endif
  1155. return ret;
  1156. }
  1157. void wc_ecc_curve_cache_free(void)
  1158. {
  1159. int x;
  1160. /* free all ECC curve caches */
  1161. for (x = 0; x < (int)ECC_SET_COUNT; x++) {
  1162. if (ecc_curve_spec_cache[x]) {
  1163. _wc_ecc_curve_free(ecc_curve_spec_cache[x]);
  1164. XFREE(ecc_curve_spec_cache[x], NULL, DYNAMIC_TYPE_ECC);
  1165. ecc_curve_spec_cache[x] = NULL;
  1166. }
  1167. }
  1168. #if defined(ECC_CACHE_CURVE) && !defined(SINGLE_THREADED)
  1169. wc_FreeMutex(&ecc_curve_cache_mutex);
  1170. #endif
  1171. }
  1172. #endif /* ECC_CACHE_CURVE */
  1173. #endif /* WOLFSSL_ATECC508A */
  1174. /* Retrieve the curve name for the ECC curve id.
  1175. *
  1176. * curve_id The id of the curve.
  1177. * returns the name stored from the curve if available, otherwise NULL.
  1178. */
  1179. const char* wc_ecc_get_name(int curve_id)
  1180. {
  1181. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  1182. if (curve_idx == ECC_CURVE_INVALID)
  1183. return NULL;
  1184. return ecc_sets[curve_idx].name;
  1185. }
  1186. int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id)
  1187. {
  1188. if (keysize <= 0 && curve_id < 0) {
  1189. return BAD_FUNC_ARG;
  1190. }
  1191. if (keysize > ECC_MAXSIZE) {
  1192. return ECC_BAD_ARG_E;
  1193. }
  1194. /* handle custom case */
  1195. if (key->idx != ECC_CUSTOM_IDX) {
  1196. int x;
  1197. /* default values */
  1198. key->idx = 0;
  1199. key->dp = NULL;
  1200. /* find ecc_set based on curve_id or key size */
  1201. for (x = 0; ecc_sets[x].size != 0; x++) {
  1202. if (curve_id > ECC_CURVE_DEF) {
  1203. if (curve_id == ecc_sets[x].id)
  1204. break;
  1205. }
  1206. else if (keysize <= ecc_sets[x].size) {
  1207. break;
  1208. }
  1209. }
  1210. if (ecc_sets[x].size == 0) {
  1211. WOLFSSL_MSG("ECC Curve not found");
  1212. return ECC_CURVE_OID_E;
  1213. }
  1214. key->idx = x;
  1215. key->dp = &ecc_sets[x];
  1216. }
  1217. return 0;
  1218. }
  1219. #ifdef ALT_ECC_SIZE
  1220. static void alt_fp_init(fp_int* a)
  1221. {
  1222. a->size = FP_SIZE_ECC;
  1223. fp_zero(a);
  1224. }
  1225. #endif /* ALT_ECC_SIZE */
  1226. #ifndef WOLFSSL_ATECC508A
  1227. /**
  1228. Add two ECC points
  1229. P The point to add
  1230. Q The point to add
  1231. R [out] The destination of the double
  1232. a ECC curve parameter a
  1233. modulus The modulus of the field the ECC curve is in
  1234. mp The "b" value from montgomery_setup()
  1235. return MP_OKAY on success
  1236. */
  1237. int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
  1238. mp_int* a, mp_int* modulus, mp_digit mp)
  1239. {
  1240. mp_int t1, t2;
  1241. #ifdef ALT_ECC_SIZE
  1242. mp_int rx, ry, rz;
  1243. #endif
  1244. mp_int *x, *y, *z;
  1245. int err;
  1246. if (P == NULL || Q == NULL || R == NULL || modulus == NULL) {
  1247. return ECC_BAD_ARG_E;
  1248. }
  1249. /* if Q == R then swap P and Q, so we don't require a local x,y,z */
  1250. if (Q == R) {
  1251. ecc_point* tPt = P;
  1252. P = Q;
  1253. Q = tPt;
  1254. }
  1255. if ((err = mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  1256. return err;
  1257. }
  1258. /* should we dbl instead? */
  1259. if (err == MP_OKAY)
  1260. err = mp_sub(modulus, Q->y, &t1);
  1261. if (err == MP_OKAY) {
  1262. if ( (mp_cmp(P->x, Q->x) == MP_EQ) &&
  1263. (get_digit_count(Q->z) && mp_cmp(P->z, Q->z) == MP_EQ) &&
  1264. (mp_cmp(P->y, Q->y) == MP_EQ || mp_cmp(P->y, &t1) == MP_EQ)) {
  1265. mp_clear(&t1);
  1266. mp_clear(&t2);
  1267. return ecc_projective_dbl_point(P, R, a, modulus, mp);
  1268. }
  1269. }
  1270. if (err != MP_OKAY) {
  1271. goto done;
  1272. }
  1273. /* If use ALT_ECC_SIZE we need to use local stack variable since
  1274. ecc_point x,y,z is reduced size */
  1275. #ifdef ALT_ECC_SIZE
  1276. /* Use local stack variable */
  1277. x = &rx;
  1278. y = &ry;
  1279. z = &rz;
  1280. if ((err = mp_init_multi(x, y, z, NULL, NULL, NULL)) != MP_OKAY) {
  1281. goto done;
  1282. }
  1283. #else
  1284. /* Use destination directly */
  1285. x = R->x;
  1286. y = R->y;
  1287. z = R->z;
  1288. #endif
  1289. if (err == MP_OKAY)
  1290. err = mp_copy(P->x, x);
  1291. if (err == MP_OKAY)
  1292. err = mp_copy(P->y, y);
  1293. if (err == MP_OKAY)
  1294. err = mp_copy(P->z, z);
  1295. /* if Z is one then these are no-operations */
  1296. if (err == MP_OKAY) {
  1297. if (!mp_iszero(Q->z)) {
  1298. /* T1 = Z' * Z' */
  1299. err = mp_sqr(Q->z, &t1);
  1300. if (err == MP_OKAY)
  1301. err = mp_montgomery_reduce(&t1, modulus, mp);
  1302. /* X = X * T1 */
  1303. if (err == MP_OKAY)
  1304. err = mp_mul(&t1, x, x);
  1305. if (err == MP_OKAY)
  1306. err = mp_montgomery_reduce(x, modulus, mp);
  1307. /* T1 = Z' * T1 */
  1308. if (err == MP_OKAY)
  1309. err = mp_mul(Q->z, &t1, &t1);
  1310. if (err == MP_OKAY)
  1311. err = mp_montgomery_reduce(&t1, modulus, mp);
  1312. /* Y = Y * T1 */
  1313. if (err == MP_OKAY)
  1314. err = mp_mul(&t1, y, y);
  1315. if (err == MP_OKAY)
  1316. err = mp_montgomery_reduce(y, modulus, mp);
  1317. }
  1318. }
  1319. /* T1 = Z*Z */
  1320. if (err == MP_OKAY)
  1321. err = mp_sqr(z, &t1);
  1322. if (err == MP_OKAY)
  1323. err = mp_montgomery_reduce(&t1, modulus, mp);
  1324. /* T2 = X' * T1 */
  1325. if (err == MP_OKAY)
  1326. err = mp_mul(Q->x, &t1, &t2);
  1327. if (err == MP_OKAY)
  1328. err = mp_montgomery_reduce(&t2, modulus, mp);
  1329. /* T1 = Z * T1 */
  1330. if (err == MP_OKAY)
  1331. err = mp_mul(z, &t1, &t1);
  1332. if (err == MP_OKAY)
  1333. err = mp_montgomery_reduce(&t1, modulus, mp);
  1334. /* T1 = Y' * T1 */
  1335. if (err == MP_OKAY)
  1336. err = mp_mul(Q->y, &t1, &t1);
  1337. if (err == MP_OKAY)
  1338. err = mp_montgomery_reduce(&t1, modulus, mp);
  1339. /* Y = Y - T1 */
  1340. if (err == MP_OKAY)
  1341. err = mp_sub(y, &t1, y);
  1342. if (err == MP_OKAY) {
  1343. if (mp_isneg(y))
  1344. err = mp_add(y, modulus, y);
  1345. }
  1346. /* T1 = 2T1 */
  1347. if (err == MP_OKAY)
  1348. err = mp_add(&t1, &t1, &t1);
  1349. if (err == MP_OKAY) {
  1350. if (mp_cmp(&t1, modulus) != MP_LT)
  1351. err = mp_sub(&t1, modulus, &t1);
  1352. }
  1353. /* T1 = Y + T1 */
  1354. if (err == MP_OKAY)
  1355. err = mp_add(&t1, y, &t1);
  1356. if (err == MP_OKAY) {
  1357. if (mp_cmp(&t1, modulus) != MP_LT)
  1358. err = mp_sub(&t1, modulus, &t1);
  1359. }
  1360. /* X = X - T2 */
  1361. if (err == MP_OKAY)
  1362. err = mp_sub(x, &t2, x);
  1363. if (err == MP_OKAY) {
  1364. if (mp_isneg(x))
  1365. err = mp_add(x, modulus, x);
  1366. }
  1367. /* T2 = 2T2 */
  1368. if (err == MP_OKAY)
  1369. err = mp_add(&t2, &t2, &t2);
  1370. if (err == MP_OKAY) {
  1371. if (mp_cmp(&t2, modulus) != MP_LT)
  1372. err = mp_sub(&t2, modulus, &t2);
  1373. }
  1374. /* T2 = X + T2 */
  1375. if (err == MP_OKAY)
  1376. err = mp_add(&t2, x, &t2);
  1377. if (err == MP_OKAY) {
  1378. if (mp_cmp(&t2, modulus) != MP_LT)
  1379. err = mp_sub(&t2, modulus, &t2);
  1380. }
  1381. if (err == MP_OKAY) {
  1382. if (!mp_iszero(Q->z)) {
  1383. /* Z = Z * Z' */
  1384. err = mp_mul(z, Q->z, z);
  1385. if (err == MP_OKAY)
  1386. err = mp_montgomery_reduce(z, modulus, mp);
  1387. }
  1388. }
  1389. /* Z = Z * X */
  1390. if (err == MP_OKAY)
  1391. err = mp_mul(z, x, z);
  1392. if (err == MP_OKAY)
  1393. err = mp_montgomery_reduce(z, modulus, mp);
  1394. /* T1 = T1 * X */
  1395. if (err == MP_OKAY)
  1396. err = mp_mul(&t1, x, &t1);
  1397. if (err == MP_OKAY)
  1398. err = mp_montgomery_reduce(&t1, modulus, mp);
  1399. /* X = X * X */
  1400. if (err == MP_OKAY)
  1401. err = mp_sqr(x, x);
  1402. if (err == MP_OKAY)
  1403. err = mp_montgomery_reduce(x, modulus, mp);
  1404. /* T2 = T2 * x */
  1405. if (err == MP_OKAY)
  1406. err = mp_mul(&t2, x, &t2);
  1407. if (err == MP_OKAY)
  1408. err = mp_montgomery_reduce(&t2, modulus, mp);
  1409. /* T1 = T1 * X */
  1410. if (err == MP_OKAY)
  1411. err = mp_mul(&t1, x, &t1);
  1412. if (err == MP_OKAY)
  1413. err = mp_montgomery_reduce(&t1, modulus, mp);
  1414. /* X = Y*Y */
  1415. if (err == MP_OKAY)
  1416. err = mp_sqr(y, x);
  1417. if (err == MP_OKAY)
  1418. err = mp_montgomery_reduce(x, modulus, mp);
  1419. /* X = X - T2 */
  1420. if (err == MP_OKAY)
  1421. err = mp_sub(x, &t2, x);
  1422. if (err == MP_OKAY) {
  1423. if (mp_isneg(x))
  1424. err = mp_add(x, modulus, x);
  1425. }
  1426. /* T2 = T2 - X */
  1427. if (err == MP_OKAY)
  1428. err = mp_sub(&t2, x, &t2);
  1429. if (err == MP_OKAY) {
  1430. if (mp_isneg(&t2))
  1431. err = mp_add(&t2, modulus, &t2);
  1432. }
  1433. /* T2 = T2 - X */
  1434. if (err == MP_OKAY)
  1435. err = mp_sub(&t2, x, &t2);
  1436. if (err == MP_OKAY) {
  1437. if (mp_isneg(&t2))
  1438. err = mp_add(&t2, modulus, &t2);
  1439. }
  1440. /* T2 = T2 * Y */
  1441. if (err == MP_OKAY)
  1442. err = mp_mul(&t2, y, &t2);
  1443. if (err == MP_OKAY)
  1444. err = mp_montgomery_reduce(&t2, modulus, mp);
  1445. /* Y = T2 - T1 */
  1446. if (err == MP_OKAY)
  1447. err = mp_sub(&t2, &t1, y);
  1448. if (err == MP_OKAY) {
  1449. if (mp_isneg(y))
  1450. err = mp_add(y, modulus, y);
  1451. }
  1452. /* Y = Y/2 */
  1453. if (err == MP_OKAY) {
  1454. if (mp_isodd(y) == MP_YES)
  1455. err = mp_add(y, modulus, y);
  1456. }
  1457. if (err == MP_OKAY)
  1458. err = mp_div_2(y, y);
  1459. #ifdef ALT_ECC_SIZE
  1460. if (err == MP_OKAY)
  1461. err = mp_copy(x, R->x);
  1462. if (err == MP_OKAY)
  1463. err = mp_copy(y, R->y);
  1464. if (err == MP_OKAY)
  1465. err = mp_copy(z, R->z);
  1466. #endif
  1467. done:
  1468. /* clean up */
  1469. mp_clear(&t1);
  1470. mp_clear(&t2);
  1471. return err;
  1472. }
  1473. /* ### Point doubling in Jacobian coordinate system ###
  1474. *
  1475. * let us have a curve: y^2 = x^3 + a*x + b
  1476. * in Jacobian coordinates it becomes: y^2 = x^3 + a*x*z^4 + b*z^6
  1477. *
  1478. * The doubling of P = (Xp, Yp, Zp) is given by R = (Xr, Yr, Zr) where:
  1479. * Xr = M^2 - 2*S
  1480. * Yr = M * (S - Xr) - 8*T
  1481. * Zr = 2 * Yp * Zp
  1482. *
  1483. * M = 3 * Xp^2 + a*Zp^4
  1484. * T = Yp^4
  1485. * S = 4 * Xp * Yp^2
  1486. *
  1487. * SPECIAL CASE: when a == 3 we can compute M as
  1488. * M = 3 * (Xp^2 - Zp^4) = 3 * (Xp + Zp^2) * (Xp - Zp^2)
  1489. */
  1490. /**
  1491. Double an ECC point
  1492. P The point to double
  1493. R [out] The destination of the double
  1494. a ECC curve parameter a
  1495. modulus The modulus of the field the ECC curve is in
  1496. mp The "b" value from montgomery_setup()
  1497. return MP_OKAY on success
  1498. */
  1499. int ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a,
  1500. mp_int* modulus, mp_digit mp)
  1501. {
  1502. mp_int t1, t2;
  1503. #ifdef ALT_ECC_SIZE
  1504. mp_int rx, ry, rz;
  1505. #endif
  1506. mp_int *x, *y, *z;
  1507. int err;
  1508. if (P == NULL || R == NULL || modulus == NULL)
  1509. return ECC_BAD_ARG_E;
  1510. if ((err = mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  1511. return err;
  1512. }
  1513. /* If use ALT_ECC_SIZE we need to use local stack variable since
  1514. ecc_point x,y,z is reduced size */
  1515. #ifdef ALT_ECC_SIZE
  1516. /* Use local stack variable */
  1517. x = &rx;
  1518. y = &ry;
  1519. z = &rz;
  1520. if ((err = mp_init_multi(x, y, z, NULL, NULL, NULL)) != MP_OKAY) {
  1521. mp_clear(&t1);
  1522. mp_clear(&t2);
  1523. return err;
  1524. }
  1525. #else
  1526. /* Use destination directly */
  1527. x = R->x;
  1528. y = R->y;
  1529. z = R->z;
  1530. #endif
  1531. if (err == MP_OKAY)
  1532. err = mp_copy(P->x, x);
  1533. if (err == MP_OKAY)
  1534. err = mp_copy(P->y, y);
  1535. if (err == MP_OKAY)
  1536. err = mp_copy(P->z, z);
  1537. /* T1 = Z * Z */
  1538. if (err == MP_OKAY)
  1539. err = mp_sqr(z, &t1);
  1540. if (err == MP_OKAY)
  1541. err = mp_montgomery_reduce(&t1, modulus, mp);
  1542. /* Z = Y * Z */
  1543. if (err == MP_OKAY)
  1544. err = mp_mul(z, y, z);
  1545. if (err == MP_OKAY)
  1546. err = mp_montgomery_reduce(z, modulus, mp);
  1547. /* Z = 2Z */
  1548. if (err == MP_OKAY)
  1549. err = mp_add(z, z, z);
  1550. if (err == MP_OKAY) {
  1551. if (mp_cmp(z, modulus) != MP_LT)
  1552. err = mp_sub(z, modulus, z);
  1553. }
  1554. /* Determine if curve "a" should be used in calc */
  1555. #ifdef WOLFSSL_CUSTOM_CURVES
  1556. if (err == MP_OKAY) {
  1557. /* Use a and prime to determine if a == 3 */
  1558. err = mp_submod(modulus, a, modulus, &t2);
  1559. }
  1560. if (err == MP_OKAY && mp_cmp_d(&t2, 3) != MP_EQ) {
  1561. /* use "a" in calc */
  1562. /* T2 = T1 * T1 */
  1563. if (err == MP_OKAY)
  1564. err = mp_sqr(&t1, &t2);
  1565. if (err == MP_OKAY)
  1566. err = mp_montgomery_reduce(&t2, modulus, mp);
  1567. /* T1 = T2 * a */
  1568. if (err == MP_OKAY)
  1569. err = mp_mulmod(&t2, a, modulus, &t1);
  1570. /* T2 = X * X */
  1571. if (err == MP_OKAY)
  1572. err = mp_sqr(x, &t2);
  1573. if (err == MP_OKAY)
  1574. err = mp_montgomery_reduce(&t2, modulus, mp);
  1575. /* T1 = T2 + T1 */
  1576. if (err == MP_OKAY)
  1577. err = mp_add(&t1, &t2, &t1);
  1578. if (err == MP_OKAY) {
  1579. if (mp_cmp(&t1, modulus) != MP_LT)
  1580. err = mp_sub(&t1, modulus, &t1);
  1581. }
  1582. /* T1 = T2 + T1 */
  1583. if (err == MP_OKAY)
  1584. err = mp_add(&t1, &t2, &t1);
  1585. if (err == MP_OKAY) {
  1586. if (mp_cmp(&t1, modulus) != MP_LT)
  1587. err = mp_sub(&t1, modulus, &t1);
  1588. }
  1589. /* T1 = T2 + T1 */
  1590. if (err == MP_OKAY)
  1591. err = mp_add(&t1, &t2, &t1);
  1592. if (err == MP_OKAY) {
  1593. if (mp_cmp(&t1, modulus) != MP_LT)
  1594. err = mp_sub(&t1, modulus, &t1);
  1595. }
  1596. }
  1597. else
  1598. #endif /* WOLFSSL_CUSTOM_CURVES */
  1599. {
  1600. /* assumes "a" == 3 */
  1601. (void)a;
  1602. /* T2 = X - T1 */
  1603. if (err == MP_OKAY)
  1604. err = mp_sub(x, &t1, &t2);
  1605. if (err == MP_OKAY) {
  1606. if (mp_isneg(&t2))
  1607. err = mp_add(&t2, modulus, &t2);
  1608. }
  1609. /* T1 = X + T1 */
  1610. if (err == MP_OKAY)
  1611. err = mp_add(&t1, x, &t1);
  1612. if (err == MP_OKAY) {
  1613. if (mp_cmp(&t1, modulus) != MP_LT)
  1614. err = mp_sub(&t1, modulus, &t1);
  1615. }
  1616. /* T2 = T1 * T2 */
  1617. if (err == MP_OKAY)
  1618. err = mp_mul(&t1, &t2, &t2);
  1619. if (err == MP_OKAY)
  1620. err = mp_montgomery_reduce(&t2, modulus, mp);
  1621. /* T1 = 2T2 */
  1622. if (err == MP_OKAY)
  1623. err = mp_add(&t2, &t2, &t1);
  1624. if (err == MP_OKAY) {
  1625. if (mp_cmp(&t1, modulus) != MP_LT)
  1626. err = mp_sub(&t1, modulus, &t1);
  1627. }
  1628. /* T1 = T1 + T2 */
  1629. if (err == MP_OKAY)
  1630. err = mp_add(&t1, &t2, &t1);
  1631. if (err == MP_OKAY) {
  1632. if (mp_cmp(&t1, modulus) != MP_LT)
  1633. err = mp_sub(&t1, modulus, &t1);
  1634. }
  1635. }
  1636. /* Y = 2Y */
  1637. if (err == MP_OKAY)
  1638. err = mp_add(y, y, y);
  1639. if (err == MP_OKAY) {
  1640. if (mp_cmp(y, modulus) != MP_LT)
  1641. err = mp_sub(y, modulus, y);
  1642. }
  1643. /* Y = Y * Y */
  1644. if (err == MP_OKAY)
  1645. err = mp_sqr(y, y);
  1646. if (err == MP_OKAY)
  1647. err = mp_montgomery_reduce(y, modulus, mp);
  1648. /* T2 = Y * Y */
  1649. if (err == MP_OKAY)
  1650. err = mp_sqr(y, &t2);
  1651. if (err == MP_OKAY)
  1652. err = mp_montgomery_reduce(&t2, modulus, mp);
  1653. /* T2 = T2/2 */
  1654. if (err == MP_OKAY) {
  1655. if (mp_isodd(&t2) == MP_YES)
  1656. err = mp_add(&t2, modulus, &t2);
  1657. }
  1658. if (err == MP_OKAY)
  1659. err = mp_div_2(&t2, &t2);
  1660. /* Y = Y * X */
  1661. if (err == MP_OKAY)
  1662. err = mp_mul(y, x, y);
  1663. if (err == MP_OKAY)
  1664. err = mp_montgomery_reduce(y, modulus, mp);
  1665. /* X = T1 * T1 */
  1666. if (err == MP_OKAY)
  1667. err = mp_sqr(&t1, x);
  1668. if (err == MP_OKAY)
  1669. err = mp_montgomery_reduce(x, modulus, mp);
  1670. /* X = X - Y */
  1671. if (err == MP_OKAY)
  1672. err = mp_sub(x, y, x);
  1673. if (err == MP_OKAY) {
  1674. if (mp_isneg(x))
  1675. err = mp_add(x, modulus, x);
  1676. }
  1677. /* X = X - Y */
  1678. if (err == MP_OKAY)
  1679. err = mp_sub(x, y, x);
  1680. if (err == MP_OKAY) {
  1681. if (mp_isneg(x))
  1682. err = mp_add(x, modulus, x);
  1683. }
  1684. /* Y = Y - X */
  1685. if (err == MP_OKAY)
  1686. err = mp_sub(y, x, y);
  1687. if (err == MP_OKAY) {
  1688. if (mp_isneg(y))
  1689. err = mp_add(y, modulus, y);
  1690. }
  1691. /* Y = Y * T1 */
  1692. if (err == MP_OKAY)
  1693. err = mp_mul(y, &t1, y);
  1694. if (err == MP_OKAY)
  1695. err = mp_montgomery_reduce(y, modulus, mp);
  1696. /* Y = Y - T2 */
  1697. if (err == MP_OKAY)
  1698. err = mp_sub(y, &t2, y);
  1699. if (err == MP_OKAY) {
  1700. if (mp_isneg(y))
  1701. err = mp_add(y, modulus, y);
  1702. }
  1703. #ifdef ALT_ECC_SIZE
  1704. if (err == MP_OKAY)
  1705. err = mp_copy(x, R->x);
  1706. if (err == MP_OKAY)
  1707. err = mp_copy(y, R->y);
  1708. if (err == MP_OKAY)
  1709. err = mp_copy(z, R->z);
  1710. #endif
  1711. /* clean up */
  1712. mp_clear(&t1);
  1713. mp_clear(&t2);
  1714. return err;
  1715. }
  1716. /**
  1717. Map a projective jacbobian point back to affine space
  1718. P [in/out] The point to map
  1719. modulus The modulus of the field the ECC curve is in
  1720. mp The "b" value from montgomery_setup()
  1721. return MP_OKAY on success
  1722. */
  1723. int ecc_map(ecc_point* P, mp_int* modulus, mp_digit mp)
  1724. {
  1725. mp_int t1, t2;
  1726. #ifdef ALT_ECC_SIZE
  1727. mp_int rx, ry, rz;
  1728. #endif
  1729. mp_int *x, *y, *z;
  1730. int err;
  1731. if (P == NULL || modulus == NULL)
  1732. return ECC_BAD_ARG_E;
  1733. /* special case for point at infinity */
  1734. if (mp_cmp_d(P->z, 0) == MP_EQ) {
  1735. err = mp_set(P->x, 0);
  1736. if (err == MP_OKAY)
  1737. err = mp_set(P->y, 0);
  1738. if (err == MP_OKAY)
  1739. err = mp_set(P->z, 1);
  1740. return err;
  1741. }
  1742. if ((err = mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  1743. return MEMORY_E;
  1744. }
  1745. #ifdef ALT_ECC_SIZE
  1746. /* Use local stack variable */
  1747. x = &rx;
  1748. y = &ry;
  1749. z = &rz;
  1750. if ((err = mp_init_multi(x, y, z, NULL, NULL, NULL)) != MP_OKAY) {
  1751. goto done;
  1752. }
  1753. if (err == MP_OKAY)
  1754. err = mp_copy(P->x, x);
  1755. if (err == MP_OKAY)
  1756. err = mp_copy(P->y, y);
  1757. if (err == MP_OKAY)
  1758. err = mp_copy(P->z, z);
  1759. if (err != MP_OKAY) {
  1760. goto done;
  1761. }
  1762. #else
  1763. /* Use destination directly */
  1764. x = P->x;
  1765. y = P->y;
  1766. z = P->z;
  1767. #endif
  1768. /* first map z back to normal */
  1769. err = mp_montgomery_reduce(z, modulus, mp);
  1770. /* get 1/z */
  1771. if (err == MP_OKAY)
  1772. err = mp_invmod(z, modulus, &t1);
  1773. /* get 1/z^2 and 1/z^3 */
  1774. if (err == MP_OKAY)
  1775. err = mp_sqr(&t1, &t2);
  1776. if (err == MP_OKAY)
  1777. err = mp_mod(&t2, modulus, &t2);
  1778. if (err == MP_OKAY)
  1779. err = mp_mul(&t1, &t2, &t1);
  1780. if (err == MP_OKAY)
  1781. err = mp_mod(&t1, modulus, &t1);
  1782. /* multiply against x/y */
  1783. if (err == MP_OKAY)
  1784. err = mp_mul(x, &t2, x);
  1785. if (err == MP_OKAY)
  1786. err = mp_montgomery_reduce(x, modulus, mp);
  1787. if (err == MP_OKAY)
  1788. err = mp_mul(y, &t1, y);
  1789. if (err == MP_OKAY)
  1790. err = mp_montgomery_reduce(y, modulus, mp);
  1791. if (err == MP_OKAY)
  1792. err = mp_set(z, 1);
  1793. #ifdef ALT_ECC_SIZE
  1794. /* return result */
  1795. if (err == MP_OKAY)
  1796. err = mp_copy(x, P->x);
  1797. if (err == MP_OKAY)
  1798. err = mp_copy(y, P->y);
  1799. if (err == MP_OKAY)
  1800. err = mp_copy(z, P->z);
  1801. done:
  1802. #endif
  1803. /* clean up */
  1804. mp_clear(&t1);
  1805. mp_clear(&t2);
  1806. return err;
  1807. }
  1808. #if !defined(FREESCALE_LTC_ECC)
  1809. /**
  1810. Perform a point multiplication
  1811. k The scalar to multiply by
  1812. G The base point
  1813. R [out] Destination for kG
  1814. a ECC curve parameter a
  1815. modulus The modulus of the field the ECC curve is in
  1816. map Boolean whether to map back to affine or not
  1817. (1==map, 0 == leave in projective)
  1818. return MP_OKAY on success
  1819. */
  1820. #ifdef FP_ECC
  1821. static int normal_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R,
  1822. mp_int* a, mp_int* modulus, int map,
  1823. void* heap)
  1824. #else
  1825. int wc_ecc_mulmod_ex(mp_int* k, ecc_point *G, ecc_point *R,
  1826. mp_int* a, mp_int* modulus, int map,
  1827. void* heap)
  1828. #endif
  1829. {
  1830. #ifndef ECC_TIMING_RESISTANT
  1831. /* size of sliding window, don't change this! */
  1832. #define WINSIZE 4
  1833. #define M_POINTS 8
  1834. int first = 1, bitbuf = 0, bitcpy = 0, j;
  1835. #else
  1836. #define M_POINTS 3
  1837. #endif
  1838. ecc_point *tG, *M[M_POINTS];
  1839. int i, err;
  1840. mp_int mu;
  1841. mp_digit mp;
  1842. mp_digit buf;
  1843. int bitcnt = 0, mode = 0, digidx = 0;
  1844. if (k == NULL || G == NULL || R == NULL || modulus == NULL) {
  1845. return ECC_BAD_ARG_E;
  1846. }
  1847. /* init variables */
  1848. tG = NULL;
  1849. XMEMSET(M, 0, sizeof(M));
  1850. /* init montgomery reduction */
  1851. if ((err = mp_montgomery_setup(modulus, &mp)) != MP_OKAY) {
  1852. return err;
  1853. }
  1854. if ((err = mp_init(&mu)) != MP_OKAY) {
  1855. return err;
  1856. }
  1857. if ((err = mp_montgomery_calc_normalization(&mu, modulus)) != MP_OKAY) {
  1858. mp_clear(&mu);
  1859. return err;
  1860. }
  1861. /* alloc ram for window temps */
  1862. for (i = 0; i < M_POINTS; i++) {
  1863. M[i] = wc_ecc_new_point_h(heap);
  1864. if (M[i] == NULL) {
  1865. mp_clear(&mu);
  1866. err = MEMORY_E; goto exit;
  1867. }
  1868. }
  1869. /* make a copy of G in case R==G */
  1870. tG = wc_ecc_new_point_h(heap);
  1871. if (tG == NULL)
  1872. err = MEMORY_E;
  1873. /* tG = G and convert to montgomery */
  1874. if (err == MP_OKAY) {
  1875. if (mp_cmp_d(&mu, 1) == MP_EQ) {
  1876. err = mp_copy(G->x, tG->x);
  1877. if (err == MP_OKAY)
  1878. err = mp_copy(G->y, tG->y);
  1879. if (err == MP_OKAY)
  1880. err = mp_copy(G->z, tG->z);
  1881. } else {
  1882. err = mp_mulmod(G->x, &mu, modulus, tG->x);
  1883. if (err == MP_OKAY)
  1884. err = mp_mulmod(G->y, &mu, modulus, tG->y);
  1885. if (err == MP_OKAY)
  1886. err = mp_mulmod(G->z, &mu, modulus, tG->z);
  1887. }
  1888. }
  1889. /* done with mu */
  1890. mp_clear(&mu);
  1891. #ifndef ECC_TIMING_RESISTANT
  1892. /* calc the M tab, which holds kG for k==8..15 */
  1893. /* M[0] == 8G */
  1894. if (err == MP_OKAY)
  1895. err = ecc_projective_dbl_point(tG, M[0], a, modulus, mp);
  1896. if (err == MP_OKAY)
  1897. err = ecc_projective_dbl_point(M[0], M[0], a, modulus, mp);
  1898. if (err == MP_OKAY)
  1899. err = ecc_projective_dbl_point(M[0], M[0], a, modulus, mp);
  1900. /* now find (8+k)G for k=1..7 */
  1901. if (err == MP_OKAY)
  1902. for (j = 9; j < 16; j++) {
  1903. err = ecc_projective_add_point(M[j-9], tG, M[j-M_POINTS], a,
  1904. modulus, mp);
  1905. if (err != MP_OKAY) break;
  1906. }
  1907. /* setup sliding window */
  1908. if (err == MP_OKAY) {
  1909. mode = 0;
  1910. bitcnt = 1;
  1911. buf = 0;
  1912. digidx = get_digit_count(k) - 1;
  1913. bitcpy = bitbuf = 0;
  1914. first = 1;
  1915. /* perform ops */
  1916. for (;;) {
  1917. /* grab next digit as required */
  1918. if (--bitcnt == 0) {
  1919. if (digidx == -1) {
  1920. break;
  1921. }
  1922. buf = get_digit(k, digidx);
  1923. bitcnt = (int) DIGIT_BIT;
  1924. --digidx;
  1925. }
  1926. /* grab the next msb from the ltiplicand */
  1927. i = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  1928. buf <<= 1;
  1929. /* skip leading zero bits */
  1930. if (mode == 0 && i == 0)
  1931. continue;
  1932. /* if the bit is zero and mode == 1 then we double */
  1933. if (mode == 1 && i == 0) {
  1934. err = ecc_projective_dbl_point(R, R, a, modulus, mp);
  1935. if (err != MP_OKAY) break;
  1936. continue;
  1937. }
  1938. /* else we add it to the window */
  1939. bitbuf |= (i << (WINSIZE - ++bitcpy));
  1940. mode = 2;
  1941. if (bitcpy == WINSIZE) {
  1942. /* if this is the first window we do a simple copy */
  1943. if (first == 1) {
  1944. /* R = kG [k = first window] */
  1945. err = mp_copy(M[bitbuf-M_POINTS]->x, R->x);
  1946. if (err != MP_OKAY) break;
  1947. err = mp_copy(M[bitbuf-M_POINTS]->y, R->y);
  1948. if (err != MP_OKAY) break;
  1949. err = mp_copy(M[bitbuf-M_POINTS]->z, R->z);
  1950. first = 0;
  1951. } else {
  1952. /* normal window */
  1953. /* ok window is filled so double as required and add */
  1954. /* double first */
  1955. for (j = 0; j < WINSIZE; j++) {
  1956. err = ecc_projective_dbl_point(R, R, a, modulus, mp);
  1957. if (err != MP_OKAY) break;
  1958. }
  1959. if (err != MP_OKAY) break; /* out of first for(;;) */
  1960. /* then add, bitbuf will be 8..15 [8..2^WINSIZE] guaranteed */
  1961. err = ecc_projective_add_point(R, M[bitbuf-M_POINTS], R, a,
  1962. modulus, mp);
  1963. }
  1964. if (err != MP_OKAY) break;
  1965. /* empty window and reset */
  1966. bitcpy = bitbuf = 0;
  1967. mode = 1;
  1968. }
  1969. }
  1970. }
  1971. /* if bits remain then double/add */
  1972. if (err == MP_OKAY) {
  1973. if (mode == 2 && bitcpy > 0) {
  1974. /* double then add */
  1975. for (j = 0; j < bitcpy; j++) {
  1976. /* only double if we have had at least one add first */
  1977. if (first == 0) {
  1978. err = ecc_projective_dbl_point(R, R, a, modulus, mp);
  1979. if (err != MP_OKAY) break;
  1980. }
  1981. bitbuf <<= 1;
  1982. if ((bitbuf & (1 << WINSIZE)) != 0) {
  1983. if (first == 1) {
  1984. /* first add, so copy */
  1985. err = mp_copy(tG->x, R->x);
  1986. if (err != MP_OKAY) break;
  1987. err = mp_copy(tG->y, R->y);
  1988. if (err != MP_OKAY) break;
  1989. err = mp_copy(tG->z, R->z);
  1990. if (err != MP_OKAY) break;
  1991. first = 0;
  1992. } else {
  1993. /* then add */
  1994. err = ecc_projective_add_point(R, tG, R, a, modulus,
  1995. mp);
  1996. if (err != MP_OKAY) break;
  1997. }
  1998. }
  1999. }
  2000. }
  2001. }
  2002. #undef WINSIZE
  2003. #else /* ECC_TIMING_RESISTANT */
  2004. /* calc the M tab */
  2005. /* M[0] == G */
  2006. if (err == MP_OKAY)
  2007. err = mp_copy(tG->x, M[0]->x);
  2008. if (err == MP_OKAY)
  2009. err = mp_copy(tG->y, M[0]->y);
  2010. if (err == MP_OKAY)
  2011. err = mp_copy(tG->z, M[0]->z);
  2012. /* M[1] == 2G */
  2013. if (err == MP_OKAY)
  2014. err = ecc_projective_dbl_point(tG, M[1], a, modulus, mp);
  2015. /* setup sliding window */
  2016. mode = 0;
  2017. bitcnt = 1;
  2018. buf = 0;
  2019. digidx = get_digit_count(k) - 1;
  2020. /* perform ops */
  2021. if (err == MP_OKAY) {
  2022. for (;;) {
  2023. /* grab next digit as required */
  2024. if (--bitcnt == 0) {
  2025. if (digidx == -1) {
  2026. break;
  2027. }
  2028. buf = get_digit(k, digidx);
  2029. bitcnt = (int)DIGIT_BIT;
  2030. --digidx;
  2031. }
  2032. /* grab the next msb from the multiplicand */
  2033. i = (buf >> (DIGIT_BIT - 1)) & 1;
  2034. buf <<= 1;
  2035. if (mode == 0 && i == 0) {
  2036. /* timing resistant - dummy operations */
  2037. if (err == MP_OKAY)
  2038. err = ecc_projective_add_point(M[0], M[1], M[2], a, modulus,
  2039. mp);
  2040. if (err == MP_OKAY)
  2041. err = ecc_projective_dbl_point(M[1], M[2], a, modulus, mp);
  2042. if (err == MP_OKAY)
  2043. continue;
  2044. }
  2045. if (mode == 0 && i == 1) {
  2046. mode = 1;
  2047. /* timing resistant - dummy operations */
  2048. if (err == MP_OKAY)
  2049. err = ecc_projective_add_point(M[0], M[1], M[2], a, modulus,
  2050. mp);
  2051. if (err == MP_OKAY)
  2052. err = ecc_projective_dbl_point(M[1], M[2], a, modulus, mp);
  2053. if (err == MP_OKAY)
  2054. continue;
  2055. }
  2056. if (err == MP_OKAY)
  2057. err = ecc_projective_add_point(M[0], M[1], M[i^1], a, modulus,
  2058. mp);
  2059. #ifdef WC_NO_CACHE_RESISTANT
  2060. if (err == MP_OKAY)
  2061. err = ecc_projective_dbl_point(M[i], M[i], a, modulus, mp);
  2062. #else
  2063. /* instead of using M[i] for double, which leaks key bit to cache
  2064. * monitor, use M[2] as temp, make sure address calc is constant,
  2065. * keep M[0] and M[1] in cache */
  2066. if (err == MP_OKAY)
  2067. err = mp_copy((mp_int*)
  2068. ( ((wolfssl_word)M[0]->x & wc_off_on_addr[i^1]) +
  2069. ((wolfssl_word)M[1]->x & wc_off_on_addr[i])),
  2070. M[2]->x);
  2071. if (err == MP_OKAY)
  2072. err = mp_copy((mp_int*)
  2073. ( ((wolfssl_word)M[0]->y & wc_off_on_addr[i^1]) +
  2074. ((wolfssl_word)M[1]->y & wc_off_on_addr[i])),
  2075. M[2]->y);
  2076. if (err == MP_OKAY)
  2077. err = mp_copy((mp_int*)
  2078. ( ((wolfssl_word)M[0]->z & wc_off_on_addr[i^1]) +
  2079. ((wolfssl_word)M[1]->z & wc_off_on_addr[i])),
  2080. M[2]->z);
  2081. if (err == MP_OKAY)
  2082. err = ecc_projective_dbl_point(M[2], M[2], a, modulus, mp);
  2083. /* copy M[2] back to M[i] */
  2084. if (err == MP_OKAY)
  2085. err = mp_copy(M[2]->x,
  2086. (mp_int*)
  2087. ( ((wolfssl_word)M[0]->x & wc_off_on_addr[i^1]) +
  2088. ((wolfssl_word)M[1]->x & wc_off_on_addr[i])) );
  2089. if (err == MP_OKAY)
  2090. err = mp_copy(M[2]->y,
  2091. (mp_int*)
  2092. ( ((wolfssl_word)M[0]->y & wc_off_on_addr[i^1]) +
  2093. ((wolfssl_word)M[1]->y & wc_off_on_addr[i])) );
  2094. if (err == MP_OKAY)
  2095. err = mp_copy(M[2]->z,
  2096. (mp_int*)
  2097. ( ((wolfssl_word)M[0]->z & wc_off_on_addr[i^1]) +
  2098. ((wolfssl_word)M[1]->z & wc_off_on_addr[i])) );
  2099. if (err != MP_OKAY)
  2100. break;
  2101. #endif /* WC_NO_CACHE_RESISTANT */
  2102. } /* end for */
  2103. }
  2104. /* copy result out */
  2105. if (err == MP_OKAY)
  2106. err = mp_copy(M[0]->x, R->x);
  2107. if (err == MP_OKAY)
  2108. err = mp_copy(M[0]->y, R->y);
  2109. if (err == MP_OKAY)
  2110. err = mp_copy(M[0]->z, R->z);
  2111. #endif /* ECC_TIMING_RESISTANT */
  2112. /* map R back from projective space */
  2113. if (err == MP_OKAY && map)
  2114. err = ecc_map(R, modulus, mp);
  2115. exit:
  2116. /* done */
  2117. wc_ecc_del_point_h(tG, heap);
  2118. for (i = 0; i < M_POINTS; i++) {
  2119. wc_ecc_del_point_h(M[i], heap);
  2120. }
  2121. return err;
  2122. }
  2123. #endif /* !FREESCALE_LTC_ECC */
  2124. /** ECC Fixed Point mulmod global
  2125. k The multiplicand
  2126. G Base point to multiply
  2127. R [out] Destination of product
  2128. a ECC curve parameter a
  2129. modulus The modulus for the curve
  2130. map [boolean] If non-zero maps the point back to affine co-ordinates,
  2131. otherwise it's left in jacobian-montgomery form
  2132. return MP_OKAY if successful
  2133. */
  2134. int wc_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
  2135. mp_int* modulus, int map)
  2136. {
  2137. return wc_ecc_mulmod_ex(k, G, R, a, modulus, map, NULL);
  2138. }
  2139. #endif /* !WOLFSSL_ATECC508A */
  2140. /**
  2141. * use a heap hint when creating new ecc_point
  2142. * return an allocated point on success or NULL on failure
  2143. */
  2144. ecc_point* wc_ecc_new_point_h(void* heap)
  2145. {
  2146. ecc_point* p;
  2147. p = (ecc_point*)XMALLOC(sizeof(ecc_point), heap, DYNAMIC_TYPE_ECC);
  2148. if (p == NULL) {
  2149. return NULL;
  2150. }
  2151. XMEMSET(p, 0, sizeof(ecc_point));
  2152. #ifndef ALT_ECC_SIZE
  2153. if (mp_init_multi(p->x, p->y, p->z, NULL, NULL, NULL) != MP_OKAY) {
  2154. XFREE(p, heap, DYNAMIC_TYPE_ECC);
  2155. return NULL;
  2156. }
  2157. #else
  2158. p->x = (mp_int*)&p->xyz[0];
  2159. p->y = (mp_int*)&p->xyz[1];
  2160. p->z = (mp_int*)&p->xyz[2];
  2161. alt_fp_init(p->x);
  2162. alt_fp_init(p->y);
  2163. alt_fp_init(p->z);
  2164. #endif
  2165. return p;
  2166. }
  2167. /**
  2168. Allocate a new ECC point
  2169. return A newly allocated point or NULL on error
  2170. */
  2171. ecc_point* wc_ecc_new_point(void)
  2172. {
  2173. return wc_ecc_new_point_h(NULL);
  2174. }
  2175. void wc_ecc_del_point_h(ecc_point* p, void* heap)
  2176. {
  2177. /* prevents free'ing null arguments */
  2178. if (p != NULL) {
  2179. mp_clear(p->x);
  2180. mp_clear(p->y);
  2181. mp_clear(p->z);
  2182. XFREE(p, heap, DYNAMIC_TYPE_ECC);
  2183. }
  2184. (void)heap;
  2185. }
  2186. /** Free an ECC point from memory
  2187. p The point to free
  2188. */
  2189. void wc_ecc_del_point(ecc_point* p)
  2190. {
  2191. wc_ecc_del_point_h(p, NULL);
  2192. }
  2193. /** Copy the value of a point to an other one
  2194. p The point to copy
  2195. r The created point
  2196. */
  2197. int wc_ecc_copy_point(ecc_point* p, ecc_point *r)
  2198. {
  2199. int ret;
  2200. /* prevents null arguments */
  2201. if (p == NULL || r == NULL)
  2202. return ECC_BAD_ARG_E;
  2203. ret = mp_copy(p->x, r->x);
  2204. if (ret != MP_OKAY)
  2205. return ret;
  2206. ret = mp_copy(p->y, r->y);
  2207. if (ret != MP_OKAY)
  2208. return ret;
  2209. ret = mp_copy(p->z, r->z);
  2210. if (ret != MP_OKAY)
  2211. return ret;
  2212. return MP_OKAY;
  2213. }
  2214. /** Compare the value of a point with an other one
  2215. a The point to compare
  2216. b The other point to compare
  2217. return MP_EQ if equal, MP_LT/MP_GT if not, < 0 in case of error
  2218. */
  2219. int wc_ecc_cmp_point(ecc_point* a, ecc_point *b)
  2220. {
  2221. int ret;
  2222. /* prevents null arguments */
  2223. if (a == NULL || b == NULL)
  2224. return BAD_FUNC_ARG;
  2225. ret = mp_cmp(a->x, b->x);
  2226. if (ret != MP_EQ)
  2227. return ret;
  2228. ret = mp_cmp(a->y, b->y);
  2229. if (ret != MP_EQ)
  2230. return ret;
  2231. ret = mp_cmp(a->z, b->z);
  2232. if (ret != MP_EQ)
  2233. return ret;
  2234. return MP_EQ;
  2235. }
  2236. /** Returns whether an ECC idx is valid or not
  2237. n The idx number to check
  2238. return 1 if valid, 0 if not
  2239. */
  2240. int wc_ecc_is_valid_idx(int n)
  2241. {
  2242. int x;
  2243. for (x = 0; ecc_sets[x].size != 0; x++)
  2244. ;
  2245. /* -1 is a valid index --- indicating that the domain params
  2246. were supplied by the user */
  2247. if ((n >= ECC_CUSTOM_IDX) && (n < x)) {
  2248. return 1;
  2249. }
  2250. return 0;
  2251. }
  2252. int wc_ecc_get_curve_idx(int curve_id)
  2253. {
  2254. int curve_idx;
  2255. for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) {
  2256. if (curve_id == ecc_sets[curve_idx].id)
  2257. break;
  2258. }
  2259. if (ecc_sets[curve_idx].size == 0) {
  2260. return ECC_CURVE_INVALID;
  2261. }
  2262. return curve_idx;
  2263. }
  2264. int wc_ecc_get_curve_id(int curve_idx)
  2265. {
  2266. if (wc_ecc_is_valid_idx(curve_idx)) {
  2267. return ecc_sets[curve_idx].id;
  2268. }
  2269. return ECC_CURVE_INVALID;
  2270. }
  2271. /* Returns the curve size that corresponds to a given ecc_curve_id identifier
  2272. *
  2273. * id curve id, from ecc_curve_id enum in ecc.h
  2274. * return curve size, from ecc_sets[] on success, negative on error
  2275. */
  2276. int wc_ecc_get_curve_size_from_id(int curve_id)
  2277. {
  2278. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  2279. if (curve_idx == ECC_CURVE_INVALID)
  2280. return ECC_BAD_ARG_E;
  2281. return ecc_sets[curve_idx].size;
  2282. }
  2283. /* Returns the curve index that corresponds to a given curve name in
  2284. * ecc_sets[] of ecc.c
  2285. *
  2286. * name curve name, from ecc_sets[].name in ecc.c
  2287. * return curve index in ecc_sets[] on success, negative on error
  2288. */
  2289. int wc_ecc_get_curve_idx_from_name(const char* curveName)
  2290. {
  2291. int curve_idx;
  2292. word32 len;
  2293. if (curveName == NULL)
  2294. return BAD_FUNC_ARG;
  2295. len = (word32)XSTRLEN(curveName);
  2296. for (curve_idx = 0; ecc_sets[curve_idx].size != 0; curve_idx++) {
  2297. if (ecc_sets[curve_idx].name &&
  2298. XSTRNCASECMP(ecc_sets[curve_idx].name, curveName, len) == 0) {
  2299. break;
  2300. }
  2301. }
  2302. if (ecc_sets[curve_idx].size == 0) {
  2303. WOLFSSL_MSG("ecc_set curve name not found");
  2304. return ECC_CURVE_INVALID;
  2305. }
  2306. return curve_idx;
  2307. }
  2308. /* Returns the curve size that corresponds to a given curve name,
  2309. * as listed in ecc_sets[] of ecc.c.
  2310. *
  2311. * name curve name, from ecc_sets[].name in ecc.c
  2312. * return curve size, from ecc_sets[] on success, negative on error
  2313. */
  2314. int wc_ecc_get_curve_size_from_name(const char* curveName)
  2315. {
  2316. int curve_idx;
  2317. if (curveName == NULL)
  2318. return BAD_FUNC_ARG;
  2319. curve_idx = wc_ecc_get_curve_idx_from_name(curveName);
  2320. if (curve_idx < 0)
  2321. return curve_idx;
  2322. return ecc_sets[curve_idx].size;
  2323. }
  2324. /* Returns the curve id that corresponds to a given curve name,
  2325. * as listed in ecc_sets[] of ecc.c.
  2326. *
  2327. * name curve name, from ecc_sets[].name in ecc.c
  2328. * return curve id, from ecc_sets[] on success, negative on error
  2329. */
  2330. int wc_ecc_get_curve_id_from_name(const char* curveName)
  2331. {
  2332. int curve_idx;
  2333. if (curveName == NULL)
  2334. return BAD_FUNC_ARG;
  2335. curve_idx = wc_ecc_get_curve_idx_from_name(curveName);
  2336. if (curve_idx < 0)
  2337. return curve_idx;
  2338. return ecc_sets[curve_idx].id;
  2339. }
  2340. /* Compares a curve parameter (hex, from ecc_sets[]) to given input
  2341. * parameter (byte array) for equality.
  2342. *
  2343. * Returns MP_EQ on success, negative on error */
  2344. static int wc_ecc_cmp_param(const char* curveParam,
  2345. const byte* param, word32 paramSz)
  2346. {
  2347. int err = MP_OKAY;
  2348. mp_int a, b;
  2349. if (param == NULL || curveParam == NULL)
  2350. return BAD_FUNC_ARG;
  2351. if ((err = mp_init_multi(&a, &b, NULL, NULL, NULL, NULL)) != MP_OKAY)
  2352. return err;
  2353. if (err == MP_OKAY)
  2354. err = mp_read_unsigned_bin(&a, param, paramSz);
  2355. if (err == MP_OKAY)
  2356. err = mp_read_radix(&b, curveParam, MP_RADIX_HEX);
  2357. if (err == MP_OKAY) {
  2358. if (mp_cmp(&a, &b) != MP_EQ) {
  2359. err = -1;
  2360. } else {
  2361. err = MP_EQ;
  2362. }
  2363. }
  2364. mp_clear(&a);
  2365. mp_clear(&b);
  2366. return err;
  2367. }
  2368. /* Returns the curve id in ecc_sets[] that corresponds to a given set of
  2369. * curve parameters.
  2370. *
  2371. * fieldSize the field size in bits
  2372. * prime prime of the finite field
  2373. * primeSz size of prime in octets
  2374. * Af first coefficient a of the curve
  2375. * AfSz size of Af in octets
  2376. * Bf second coefficient b of the curve
  2377. * BfSz size of Bf in octets
  2378. * order curve order
  2379. * orderSz size of curve in octets
  2380. * Gx affine x coordinate of base point
  2381. * GxSz size of Gx in octets
  2382. * Gy affine y coordinate of base point
  2383. * GySz size of Gy in octets
  2384. * cofactor curve cofactor
  2385. *
  2386. * return curve id, from ecc_sets[] on success, negative on error
  2387. */
  2388. int wc_ecc_get_curve_id_from_params(int fieldSize,
  2389. const byte* prime, word32 primeSz, const byte* Af, word32 AfSz,
  2390. const byte* Bf, word32 BfSz, const byte* order, word32 orderSz,
  2391. const byte* Gx, word32 GxSz, const byte* Gy, word32 GySz, int cofactor)
  2392. {
  2393. int idx;
  2394. int curveSz;
  2395. if (prime == NULL || Af == NULL || Bf == NULL || order == NULL ||
  2396. Gx == NULL || Gy == NULL)
  2397. return BAD_FUNC_ARG;
  2398. curveSz = (fieldSize + 1) / 8; /* round up */
  2399. for (idx = 0; ecc_sets[idx].size != 0; idx++) {
  2400. if (curveSz == ecc_sets[idx].size) {
  2401. if ((wc_ecc_cmp_param(ecc_sets[idx].prime, prime,
  2402. primeSz) == MP_EQ) &&
  2403. (wc_ecc_cmp_param(ecc_sets[idx].Af, Af, AfSz) == MP_EQ) &&
  2404. (wc_ecc_cmp_param(ecc_sets[idx].Bf, Bf, BfSz) == MP_EQ) &&
  2405. (wc_ecc_cmp_param(ecc_sets[idx].order, order,
  2406. orderSz) == MP_EQ) &&
  2407. (wc_ecc_cmp_param(ecc_sets[idx].Gx, Gx, GxSz) == MP_EQ) &&
  2408. (wc_ecc_cmp_param(ecc_sets[idx].Gy, Gy, GySz) == MP_EQ) &&
  2409. (cofactor == ecc_sets[idx].cofactor)) {
  2410. break;
  2411. }
  2412. }
  2413. }
  2414. if (ecc_sets[idx].size == 0)
  2415. return ECC_CURVE_INVALID;
  2416. return ecc_sets[idx].id;
  2417. }
  2418. #ifdef HAVE_ECC_DHE
  2419. /**
  2420. Create an ECC shared secret between two keys
  2421. private_key The private ECC key (heap hint based off of private key)
  2422. public_key The public key
  2423. out [out] Destination of the shared secret
  2424. Conforms to EC-DH from ANSI X9.63
  2425. outlen [in/out] The max size and resulting size of the shared secret
  2426. return MP_OKAY if successful
  2427. */
  2428. int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
  2429. word32* outlen)
  2430. {
  2431. int err;
  2432. if (private_key == NULL || public_key == NULL || out == NULL ||
  2433. outlen == NULL) {
  2434. return BAD_FUNC_ARG;
  2435. }
  2436. /* type valid? */
  2437. if (private_key->type != ECC_PRIVATEKEY &&
  2438. private_key->type != ECC_PRIVATEKEY_ONLY) {
  2439. return ECC_BAD_ARG_E;
  2440. }
  2441. /* Verify domain params supplied */
  2442. if (wc_ecc_is_valid_idx(private_key->idx) == 0 ||
  2443. wc_ecc_is_valid_idx(public_key->idx) == 0) {
  2444. return ECC_BAD_ARG_E;
  2445. }
  2446. /* Verify curve id matches */
  2447. if (private_key->dp->id != public_key->dp->id) {
  2448. return ECC_BAD_ARG_E;
  2449. }
  2450. #ifdef WOLFSSL_ATECC508A
  2451. err = atcatls_ecdh(private_key->slot, public_key->pubkey_raw, out);
  2452. if (err != ATCA_SUCCESS) {
  2453. err = BAD_COND_E;
  2454. }
  2455. *outlen = private_key->dp->size;
  2456. #else
  2457. err = wc_ecc_shared_secret_ex(private_key, &public_key->pubkey, out, outlen);
  2458. #endif /* WOLFSSL_ATECC508A */
  2459. return err;
  2460. }
  2461. #ifndef WOLFSSL_ATECC508A
  2462. static int wc_ecc_shared_secret_gen_sync(ecc_key* private_key, ecc_point* point,
  2463. byte* out, word32* outlen, ecc_curve_spec* curve)
  2464. {
  2465. int err;
  2466. ecc_point* result = NULL;
  2467. word32 x = 0;
  2468. mp_int* k = &private_key->k;
  2469. #ifdef HAVE_ECC_CDH
  2470. mp_int k_lcl;
  2471. /* if cofactor flag has been set */
  2472. if (private_key->flags & WC_ECC_FLAG_COFACTOR) {
  2473. mp_digit cofactor = (mp_digit)private_key->dp->cofactor;
  2474. /* only perform cofactor calc if not equal to 1 */
  2475. if (cofactor != 1) {
  2476. k = &k_lcl;
  2477. if (mp_init(k) != MP_OKAY)
  2478. return MEMORY_E;
  2479. /* multiply cofactor times private key "k" */
  2480. err = mp_mul_d(&private_key->k, cofactor, k);
  2481. if (err != MP_OKAY) {
  2482. mp_clear(k);
  2483. return err;
  2484. }
  2485. }
  2486. }
  2487. #endif
  2488. #ifdef WOLFSSL_HAVE_SP_ECC
  2489. #ifndef WOLFSSL_SP_NO_256
  2490. if (private_key->idx != ECC_CUSTOM_IDX &&
  2491. ecc_sets[private_key->idx].id == ECC_SECP256R1) {
  2492. err = sp_ecc_secret_gen_256(k, point, out, outlen, private_key->heap);
  2493. }
  2494. else
  2495. #endif
  2496. #endif
  2497. {
  2498. /* make new point */
  2499. result = wc_ecc_new_point_h(private_key->heap);
  2500. if (result == NULL) {
  2501. #ifdef HAVE_ECC_CDH
  2502. if (k == &k_lcl)
  2503. mp_clear(k);
  2504. #endif
  2505. return MEMORY_E;
  2506. }
  2507. err = wc_ecc_mulmod_ex(k, point, result, curve->Af, curve->prime, 1,
  2508. private_key->heap);
  2509. if (err == MP_OKAY) {
  2510. x = mp_unsigned_bin_size(curve->prime);
  2511. if (*outlen < x) {
  2512. err = BUFFER_E;
  2513. }
  2514. }
  2515. if (err == MP_OKAY) {
  2516. XMEMSET(out, 0, x);
  2517. err = mp_to_unsigned_bin(result->x,out +
  2518. (x - mp_unsigned_bin_size(result->x)));
  2519. }
  2520. *outlen = x;
  2521. wc_ecc_del_point_h(result, private_key->heap);
  2522. }
  2523. #ifdef HAVE_ECC_CDH
  2524. if (k == &k_lcl)
  2525. mp_clear(k);
  2526. #endif
  2527. return err;
  2528. }
  2529. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  2530. static int wc_ecc_shared_secret_gen_async(ecc_key* private_key,
  2531. ecc_point* point, byte* out, word32 *outlen,
  2532. ecc_curve_spec* curve)
  2533. {
  2534. int err;
  2535. #ifdef HAVE_CAVIUM
  2536. /* TODO: Not implemented - use software for now */
  2537. err = wc_ecc_shared_secret_gen_sync(private_key, point, out, outlen, curve);
  2538. #elif defined(HAVE_INTEL_QA)
  2539. /* sync public key x/y */
  2540. err = wc_ecc_curve_load(private_key->dp, &curve, ECC_CURVE_FIELD_BF);
  2541. if (err == MP_OKAY)
  2542. err = wc_mp_to_bigint(&private_key->k, &private_key->k.raw);
  2543. if (err == MP_OKAY)
  2544. err = wc_mp_to_bigint(point->x, &point->x->raw);
  2545. if (err == MP_OKAY)
  2546. err = wc_mp_to_bigint(point->y, &point->y->raw);
  2547. if (err == MP_OKAY)
  2548. err = IntelQaEcdh(&private_key->asyncDev,
  2549. &private_key->k.raw, &point->x->raw, &point->y->raw,
  2550. out, outlen,
  2551. &curve->Af->raw, &curve->Bf->raw, &curve->prime->raw,
  2552. private_key->dp->cofactor);
  2553. #else /* WOLFSSL_ASYNC_CRYPT_TEST */
  2554. if (wc_AsyncTestInit(&private_key->asyncDev, ASYNC_TEST_ECC_SHARED_SEC)) {
  2555. WC_ASYNC_TEST* testDev = &private_key->asyncDev.test;
  2556. testDev->eccSharedSec.private_key = private_key;
  2557. testDev->eccSharedSec.public_point = point;
  2558. testDev->eccSharedSec.out = out;
  2559. testDev->eccSharedSec.outLen = outlen;
  2560. return WC_PENDING_E;
  2561. }
  2562. err = wc_ecc_shared_secret_gen_sync(private_key, point, out, outlen, curve);
  2563. #endif
  2564. return err;
  2565. }
  2566. #endif /* WOLFSSL_ASYNC_CRYPT */
  2567. int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
  2568. byte* out, word32 *outlen)
  2569. {
  2570. int err;
  2571. DECLARE_CURVE_SPECS(2)
  2572. if (private_key == NULL || point == NULL || out == NULL ||
  2573. outlen == NULL) {
  2574. return BAD_FUNC_ARG;
  2575. }
  2576. /* load curve info */
  2577. err = wc_ecc_curve_load(private_key->dp, &curve,
  2578. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF));
  2579. if (err != MP_OKAY)
  2580. return err;
  2581. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  2582. if (private_key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  2583. err = wc_ecc_shared_secret_gen_async(private_key, point,
  2584. out, outlen, curve);
  2585. }
  2586. else
  2587. #endif
  2588. {
  2589. err = wc_ecc_shared_secret_gen_sync(private_key, point,
  2590. out, outlen, curve);
  2591. }
  2592. wc_ecc_curve_free(curve);
  2593. return err;
  2594. }
  2595. /**
  2596. Create an ECC shared secret between private key and public point
  2597. private_key The private ECC key (heap hint based on private key)
  2598. point The point to use (public key)
  2599. out [out] Destination of the shared secret
  2600. Conforms to EC-DH from ANSI X9.63
  2601. outlen [in/out] The max size and resulting size of the shared secret
  2602. return MP_OKAY if successful
  2603. */
  2604. int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
  2605. byte* out, word32 *outlen)
  2606. {
  2607. int err;
  2608. if (private_key == NULL || point == NULL || out == NULL ||
  2609. outlen == NULL) {
  2610. return BAD_FUNC_ARG;
  2611. }
  2612. /* type valid? */
  2613. if (private_key->type != ECC_PRIVATEKEY &&
  2614. private_key->type != ECC_PRIVATEKEY_ONLY) {
  2615. return ECC_BAD_ARG_E;
  2616. }
  2617. /* Verify domain params supplied */
  2618. if (wc_ecc_is_valid_idx(private_key->idx) == 0)
  2619. return ECC_BAD_ARG_E;
  2620. switch(private_key->state) {
  2621. case ECC_STATE_NONE:
  2622. case ECC_STATE_SHARED_SEC_GEN:
  2623. private_key->state = ECC_STATE_SHARED_SEC_GEN;
  2624. err = wc_ecc_shared_secret_gen(private_key, point, out, outlen);
  2625. if (err < 0) {
  2626. break;
  2627. }
  2628. FALL_THROUGH;
  2629. case ECC_STATE_SHARED_SEC_RES:
  2630. private_key->state = ECC_STATE_SHARED_SEC_RES;
  2631. err = 0;
  2632. break;
  2633. default:
  2634. err = BAD_STATE_E;
  2635. } /* switch */
  2636. /* if async pending then return and skip done cleanup below */
  2637. if (err == WC_PENDING_E) {
  2638. private_key->state++;
  2639. return err;
  2640. }
  2641. private_key->state = ECC_STATE_NONE;
  2642. return err;
  2643. }
  2644. #endif /* !WOLFSSL_ATECC508A */
  2645. #endif /* HAVE_ECC_DHE */
  2646. #ifndef WOLFSSL_ATECC508A
  2647. /* return 1 if point is at infinity, 0 if not, < 0 on error */
  2648. int wc_ecc_point_is_at_infinity(ecc_point* p)
  2649. {
  2650. if (p == NULL)
  2651. return BAD_FUNC_ARG;
  2652. if (get_digit_count(p->x) == 0 && get_digit_count(p->y) == 0)
  2653. return 1;
  2654. return 0;
  2655. }
  2656. /* generate random and ensure its greater than 0 and less than order */
  2657. static int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order)
  2658. {
  2659. int err;
  2660. #ifdef WOLFSSL_SMALL_STACK
  2661. byte* buf;
  2662. #else
  2663. byte buf[ECC_MAXSIZE_GEN];
  2664. #endif
  2665. #ifdef WOLFSSL_SMALL_STACK
  2666. buf = (byte*)XMALLOC(ECC_MAXSIZE_GEN, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  2667. if (buf == NULL)
  2668. return MEMORY_E;
  2669. #endif
  2670. /*generate 8 extra bytes to mitigate bias from the modulo operation below*/
  2671. /*see section A.1.2 in 'Suite B Implementor's Guide to FIPS 186-3 (ECDSA)'*/
  2672. size += 8;
  2673. /* make up random string */
  2674. err = wc_RNG_GenerateBlock(rng, buf, size);
  2675. /* load random buffer data into k */
  2676. if (err == 0)
  2677. err = mp_read_unsigned_bin(k, (byte*)buf, size);
  2678. /* quick sanity check to make sure we're not dealing with a 0 key */
  2679. if (err == MP_OKAY) {
  2680. if (mp_iszero(k) == MP_YES)
  2681. err = MP_ZERO_E;
  2682. }
  2683. /* the key should be smaller than the order of base point */
  2684. if (err == MP_OKAY) {
  2685. if (mp_cmp(k, order) != MP_LT) {
  2686. err = mp_mod(k, order, k);
  2687. }
  2688. }
  2689. ForceZero(buf, ECC_MAXSIZE);
  2690. #ifdef WOLFSSL_SMALL_STACK
  2691. XFREE(buf, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  2692. #endif
  2693. return err;
  2694. }
  2695. #endif /* !WOLFSSL_ATECC508A */
  2696. static INLINE void wc_ecc_reset(ecc_key* key)
  2697. {
  2698. /* make sure required key variables are reset */
  2699. key->state = ECC_STATE_NONE;
  2700. }
  2701. /* create the public ECC key from a private key
  2702. *
  2703. * key an initialized private key to generate public part from
  2704. * curveIn [in]curve for key, can be NULL
  2705. * pubOut [out]ecc_point holding the public key, if NULL then public key part
  2706. * is cached in key instead.
  2707. *
  2708. * Note this function is local to the file because of the argument type
  2709. * ecc_curve_spec. Having this argument allows for not having to load the
  2710. * curve type multiple times when generating a key with wc_ecc_make_key().
  2711. *
  2712. * returns MP_OKAY on success
  2713. */
  2714. static int wc_ecc_make_pub_ex(ecc_key* key, ecc_curve_spec* curveIn,
  2715. ecc_point* pubOut)
  2716. {
  2717. int err = MP_OKAY;
  2718. #ifndef WOLFSSL_ATECC508A
  2719. ecc_point* base = NULL;
  2720. ecc_point* pub;
  2721. DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT)
  2722. #endif
  2723. if (key == NULL) {
  2724. return BAD_FUNC_ARG;
  2725. }
  2726. #ifndef WOLFSSL_ATECC508A
  2727. /* if ecc_point passed in then use it as output for public key point */
  2728. if (pubOut != NULL) {
  2729. pub = pubOut;
  2730. }
  2731. else {
  2732. /* caching public key making it a ECC_PRIVATEKEY instead of
  2733. ECC_PRIVATEKEY_ONLY */
  2734. pub = &key->pubkey;
  2735. key->type = ECC_PRIVATEKEY_ONLY;
  2736. }
  2737. /* avoid loading the curve unless it is not passed in */
  2738. if (curveIn != NULL) {
  2739. curve = curveIn;
  2740. }
  2741. else {
  2742. /* load curve info */
  2743. if (err == MP_OKAY)
  2744. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  2745. }
  2746. if (err == MP_OKAY) {
  2747. #ifndef ALT_ECC_SIZE
  2748. err = mp_init_multi(pub->x, pub->y, pub->z, NULL, NULL, NULL);
  2749. #else
  2750. pub->x = (mp_int*)&pub->xyz[0];
  2751. pub->y = (mp_int*)&pub->xyz[1];
  2752. pub->z = (mp_int*)&pub->xyz[2];
  2753. alt_fp_init(pub->x);
  2754. alt_fp_init(pub->y);
  2755. alt_fp_init(pub->z);
  2756. #endif
  2757. }
  2758. #ifdef WOLFSSL_HAVE_SP_ECC
  2759. #ifndef WOLFSSL_SP_NO_256
  2760. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1) {
  2761. if (err == MP_OKAY)
  2762. err = sp_ecc_mulmod_base_256(&key->k, pub, 1, key->heap);
  2763. }
  2764. else
  2765. #endif
  2766. #endif
  2767. {
  2768. if (err == MP_OKAY) {
  2769. base = wc_ecc_new_point_h(key->heap);
  2770. if (base == NULL)
  2771. err = MEMORY_E;
  2772. }
  2773. /* read in the x/y for this key */
  2774. if (err == MP_OKAY)
  2775. err = mp_copy(curve->Gx, base->x);
  2776. if (err == MP_OKAY)
  2777. err = mp_copy(curve->Gy, base->y);
  2778. if (err == MP_OKAY)
  2779. err = mp_set(base->z, 1);
  2780. /* make the public key */
  2781. if (err == MP_OKAY) {
  2782. err = wc_ecc_mulmod_ex(&key->k, base, pub, curve->Af, curve->prime,
  2783. 1, key->heap);
  2784. }
  2785. wc_ecc_del_point_h(base, key->heap);
  2786. }
  2787. #ifdef WOLFSSL_VALIDATE_ECC_KEYGEN
  2788. /* validate the public key, order * pubkey = point at infinity */
  2789. if (err == MP_OKAY)
  2790. err = ecc_check_pubkey_order(key, pub, curve->Af, curve->prime,
  2791. curve->order);
  2792. #endif /* WOLFSSL_VALIDATE_KEYGEN */
  2793. if (err != MP_OKAY) {
  2794. /* clean up if failed */
  2795. #ifndef ALT_ECC_SIZE
  2796. mp_clear(pub->x);
  2797. mp_clear(pub->y);
  2798. mp_clear(pub->z);
  2799. #endif
  2800. }
  2801. /* free up local curve */
  2802. if (curveIn == NULL) {
  2803. wc_ecc_curve_free(curve);
  2804. }
  2805. #endif /* WOLFSSL_ATECC508A */
  2806. /* change key state if public part is cached */
  2807. if (key->type == ECC_PRIVATEKEY_ONLY && pubOut == NULL) {
  2808. key->type = ECC_PRIVATEKEY;
  2809. }
  2810. return err;
  2811. }
  2812. /* create the public ECC key from a private key
  2813. *
  2814. * key an initialized private key to generate public part from
  2815. * pubOut [out]ecc_point holding the public key, if NULL then public key part
  2816. * is cached in key instead.
  2817. *
  2818. *
  2819. * returns MP_OKAY on success
  2820. */
  2821. int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut)
  2822. {
  2823. WOLFSSL_ENTER("wc_ecc_make_pub");
  2824. return wc_ecc_make_pub_ex(key, NULL, pubOut);
  2825. }
  2826. int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id)
  2827. {
  2828. int err;
  2829. #ifndef WOLFSSL_ATECC508A
  2830. DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT)
  2831. #endif
  2832. if (key == NULL || rng == NULL) {
  2833. return BAD_FUNC_ARG;
  2834. }
  2835. /* make sure required variables are reset */
  2836. wc_ecc_reset(key);
  2837. err = wc_ecc_set_curve(key, keysize, curve_id);
  2838. if (err != 0) {
  2839. return err;
  2840. }
  2841. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  2842. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  2843. #ifdef HAVE_CAVIUM
  2844. /* TODO: Not implemented */
  2845. #elif defined(HAVE_INTEL_QA)
  2846. /* TODO: Not implemented */
  2847. #else
  2848. if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_MAKE)) {
  2849. WC_ASYNC_TEST* testDev = &key->asyncDev.test;
  2850. testDev->eccMake.rng = rng;
  2851. testDev->eccMake.key = key;
  2852. testDev->eccMake.size = keysize;
  2853. testDev->eccMake.curve_id = curve_id;
  2854. return WC_PENDING_E;
  2855. }
  2856. #endif
  2857. }
  2858. #endif /* WOLFSSL_ASYNC_CRYPT */
  2859. #ifdef WOLFSSL_ATECC508A
  2860. key->type = ECC_PRIVATEKEY;
  2861. err = atcatls_create_key(key->slot, key->pubkey_raw);
  2862. if (err != ATCA_SUCCESS) {
  2863. err = BAD_COND_E;
  2864. }
  2865. /* populate key->pubkey */
  2866. err = mp_read_unsigned_bin(key->pubkey.x, key->pubkey_raw, 32);
  2867. if (err = MP_OKAY)
  2868. err = mp_read_unsigned_bin(key->pubkey.y, key->pubkey_raw + 32, 32);
  2869. #else
  2870. #ifdef WOLFSSL_HAVE_SP_ECC
  2871. #ifndef WOLFSSL_SP_NO_256
  2872. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1) {
  2873. err = sp_ecc_make_key_256(rng, &key->k, &key->pubkey, key->heap);
  2874. if (err == MP_OKAY)
  2875. key->type = ECC_PRIVATEKEY;
  2876. }
  2877. else
  2878. #endif
  2879. #endif
  2880. {
  2881. /* setup the key variables */
  2882. err = mp_init(&key->k);
  2883. /* load curve info */
  2884. if (err == MP_OKAY)
  2885. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  2886. /* generate k */
  2887. if (err == MP_OKAY)
  2888. err = wc_ecc_gen_k(rng, key->dp->size, &key->k, curve->order);
  2889. /* generate public key from k */
  2890. if (err == MP_OKAY)
  2891. err = wc_ecc_make_pub_ex(key, curve, NULL);
  2892. if (err == MP_OKAY)
  2893. key->type = ECC_PRIVATEKEY;
  2894. /* cleanup these on failure case only */
  2895. if (err != MP_OKAY) {
  2896. /* clean up */
  2897. mp_forcezero(&key->k);
  2898. }
  2899. /* cleanup allocations */
  2900. wc_ecc_curve_free(curve);
  2901. }
  2902. #endif /* WOLFSSL_ATECC508A */
  2903. return err;
  2904. }
  2905. #ifdef ECC_DUMP_OID
  2906. /* Optional dump of encoded OID for adding new curves */
  2907. static int mOidDumpDone;
  2908. static void wc_ecc_dump_oids(void)
  2909. {
  2910. int x;
  2911. if (mOidDumpDone) {
  2912. return;
  2913. }
  2914. /* find matching OID sum (based on encoded value) */
  2915. for (x = 0; ecc_sets[x].size != 0; x++) {
  2916. int i;
  2917. byte* oid;
  2918. word32 oidSz, sum = 0;
  2919. printf("ECC %s (%d):\n", ecc_sets[x].name, x);
  2920. #ifdef HAVE_OID_ENCODING
  2921. byte oidEnc[ECC_MAX_OID_LEN];
  2922. oid = oidEnc;
  2923. oidSz = ECC_MAX_OID_LEN;
  2924. printf("OID: ");
  2925. for (i = 0; i < (int)ecc_sets[x].oidSz; i++) {
  2926. printf("%d.", ecc_sets[x].oid[i]);
  2927. }
  2928. printf("\n");
  2929. EncodeObjectId(ecc_sets[x].oid, ecc_sets[x].oidSz, oidEnc, &oidSz);
  2930. #else
  2931. oid = (byte*)ecc_sets[x].oid;
  2932. oidSz = ecc_sets[x].oidSz;
  2933. #endif
  2934. printf("OID Encoded: ");
  2935. for (i = 0; i < (int)oidSz; i++) {
  2936. printf("0x%02X,", oid[i]);
  2937. }
  2938. printf("\n");
  2939. for (i = 0; i < (int)oidSz; i++) {
  2940. sum += oid[i];
  2941. }
  2942. printf("Sum: %d\n", sum);
  2943. /* validate sum */
  2944. if (ecc_sets[x].oidSum != sum) {
  2945. printf(" Sum %d Not Valid!\n", ecc_sets[x].oidSum);
  2946. }
  2947. }
  2948. mOidDumpDone = 1;
  2949. }
  2950. #endif /* ECC_DUMP_OID */
  2951. /**
  2952. Make a new ECC key
  2953. rng An active RNG state
  2954. keysize The keysize for the new key (in octets from 20 to 65 bytes)
  2955. key [out] Destination of the newly created key
  2956. return MP_OKAY if successful,
  2957. upon error all allocated memory will be freed
  2958. */
  2959. int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key)
  2960. {
  2961. return wc_ecc_make_key_ex(rng, keysize, key, ECC_CURVE_DEF);
  2962. }
  2963. static INLINE int wc_ecc_alloc_rs(ecc_key* key, mp_int** r, mp_int** s)
  2964. {
  2965. int err = 0;
  2966. #ifndef WOLFSSL_ASYNC_CRYPT
  2967. (void)key;
  2968. #endif
  2969. if (*r == NULL) {
  2970. #ifdef WOLFSSL_ASYNC_CRYPT
  2971. *r = (mp_int*)XMALLOC(sizeof(mp_int), key->heap, DYNAMIC_TYPE_BIGINT);
  2972. if (*r == NULL) {
  2973. return MEMORY_E;
  2974. }
  2975. key->r = *r;
  2976. #endif
  2977. }
  2978. if (*s == NULL) {
  2979. #ifdef WOLFSSL_ASYNC_CRYPT
  2980. *s = (mp_int*)XMALLOC(sizeof(mp_int), key->heap, DYNAMIC_TYPE_BIGINT);
  2981. if (*s == NULL) {
  2982. XFREE(*r, key->heap, DYNAMIC_TYPE_BIGINT);
  2983. return MEMORY_E;
  2984. }
  2985. key->s = *s;
  2986. #endif
  2987. }
  2988. /* initialize mp_int */
  2989. if (*r)
  2990. XMEMSET(*r, 0, sizeof(mp_int));
  2991. if (*s)
  2992. XMEMSET(*s, 0, sizeof(mp_int));
  2993. return err;
  2994. }
  2995. static INLINE void wc_ecc_free_rs(ecc_key* key, mp_int** r, mp_int** s)
  2996. {
  2997. if (*r) {
  2998. mp_clear(*r);
  2999. #ifdef WOLFSSL_ASYNC_CRYPT
  3000. XFREE(*r, key->heap, DYNAMIC_TYPE_BIGINT);
  3001. key->r = NULL;
  3002. #endif
  3003. *r = NULL;
  3004. }
  3005. if (*s) {
  3006. mp_clear(*s);
  3007. #ifdef WOLFSSL_ASYNC_CRYPT
  3008. XFREE(*s, key->heap, DYNAMIC_TYPE_BIGINT);
  3009. key->s = NULL;
  3010. #endif
  3011. *s = NULL;
  3012. }
  3013. (void)key;
  3014. }
  3015. /* Setup dynamic pointers if using normal math for proper freeing */
  3016. int wc_ecc_init_ex(ecc_key* key, void* heap, int devId)
  3017. {
  3018. int ret = 0;
  3019. if (key == NULL) {
  3020. return BAD_FUNC_ARG;
  3021. }
  3022. #ifdef ECC_DUMP_OID
  3023. wc_ecc_dump_oids();
  3024. #endif
  3025. XMEMSET(key, 0, sizeof(ecc_key));
  3026. key->state = ECC_STATE_NONE;
  3027. #ifdef PLUTON_CRYPTO_ECC
  3028. key->devId = devId;
  3029. #endif
  3030. #ifdef WOLFSSL_ATECC508A
  3031. key->slot = atmel_ecc_alloc();
  3032. if (key->slot == ATECC_INVALID_SLOT) {
  3033. return ECC_BAD_ARG_E;
  3034. }
  3035. #else
  3036. #ifdef ALT_ECC_SIZE
  3037. key->pubkey.x = (mp_int*)&key->pubkey.xyz[0];
  3038. key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
  3039. key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
  3040. alt_fp_init(key->pubkey.x);
  3041. alt_fp_init(key->pubkey.y);
  3042. alt_fp_init(key->pubkey.z);
  3043. ret = mp_init(&key->k);
  3044. #else
  3045. ret = mp_init_multi(&key->k, key->pubkey.x, key->pubkey.y, key->pubkey.z,
  3046. NULL, NULL);
  3047. #endif /* ALT_ECC_SIZE */
  3048. if (ret != MP_OKAY) {
  3049. return MEMORY_E;
  3050. }
  3051. #endif /* WOLFSSL_ATECC508A */
  3052. #ifdef WOLFSSL_HEAP_TEST
  3053. key->heap = (void*)WOLFSSL_HEAP_TEST;
  3054. #else
  3055. key->heap = heap;
  3056. #endif
  3057. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  3058. /* handle as async */
  3059. ret = wolfAsync_DevCtxInit(&key->asyncDev, WOLFSSL_ASYNC_MARKER_ECC,
  3060. key->heap, devId);
  3061. #else
  3062. (void)devId;
  3063. #endif
  3064. return ret;
  3065. }
  3066. int wc_ecc_init(ecc_key* key)
  3067. {
  3068. return wc_ecc_init_ex(key, NULL, INVALID_DEVID);
  3069. }
  3070. int wc_ecc_set_flags(ecc_key* key, word32 flags)
  3071. {
  3072. if (key == NULL) {
  3073. return BAD_FUNC_ARG;
  3074. }
  3075. key->flags |= flags;
  3076. return 0;
  3077. }
  3078. #ifdef HAVE_ECC_SIGN
  3079. #ifndef NO_ASN
  3080. /**
  3081. Sign a message digest
  3082. in The message digest to sign
  3083. inlen The length of the digest
  3084. out [out] The destination for the signature
  3085. outlen [in/out] The max size and resulting size of the signature
  3086. key A private ECC key
  3087. return MP_OKAY if successful
  3088. */
  3089. int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
  3090. WC_RNG* rng, ecc_key* key)
  3091. {
  3092. int err;
  3093. mp_int *r = NULL, *s = NULL;
  3094. #ifndef WOLFSSL_ASYNC_CRYPT
  3095. mp_int r_lcl, s_lcl;
  3096. r = &r_lcl;
  3097. s = &s_lcl;
  3098. #endif
  3099. if (in == NULL || out == NULL || outlen == NULL || key == NULL ||
  3100. rng == NULL) {
  3101. return ECC_BAD_ARG_E;
  3102. }
  3103. switch(key->state) {
  3104. case ECC_STATE_NONE:
  3105. case ECC_STATE_SIGN_DO:
  3106. key->state = ECC_STATE_SIGN_DO;
  3107. err = wc_ecc_alloc_rs(key, &r, &s);
  3108. if (err != 0)
  3109. break;
  3110. if ((err = mp_init_multi(r, s, NULL, NULL, NULL, NULL)) != MP_OKAY){
  3111. break;
  3112. }
  3113. /* hardware crypto */
  3114. #if defined(WOLFSSL_ATECC508A) || defined(PLUTON_CRYPTO_ECC)
  3115. #ifdef PLUTON_CRYPTO_ECC
  3116. if (key->devId != INVALID_DEVID) /* use hardware */
  3117. #endif
  3118. {
  3119. /* Check args */
  3120. if ( inlen != ECC_MAX_CRYPTO_HW_SIZE ||
  3121. *outlen < ECC_MAX_CRYPTO_HW_SIZE*2) {
  3122. return ECC_BAD_ARG_E;
  3123. }
  3124. #if defined(WOLFSSL_ATECC508A)
  3125. /* Sign: Result is 32-bytes of R then 32-bytes of S */
  3126. err = atcatls_sign(key->slot, in, out);
  3127. if (err != ATCA_SUCCESS) {
  3128. return BAD_COND_E;
  3129. }
  3130. #elif defined(PLUTON_CRYPTO_ECC)
  3131. /* perform ECC sign */
  3132. err = Crypto_EccSign(in, inlen, out, &outlen);
  3133. if (err != CRYPTO_RES_SUCCESS) {
  3134. return BAD_COND_E;
  3135. }
  3136. #endif
  3137. /* Load R and S */
  3138. err = mp_read_unsigned_bin(r, &out[0], ECC_MAX_CRYPTO_HW_SIZE);
  3139. if (err != MP_OKAY) {
  3140. return err;
  3141. }
  3142. err = mp_read_unsigned_bin(s, &out[ECC_MAX_CRYPTO_HW_SIZE],
  3143. ECC_MAX_CRYPTO_HW_SIZE);
  3144. if (err != MP_OKAY) {
  3145. return err;
  3146. }
  3147. /* Check for zeros */
  3148. if (mp_iszero(r) || mp_iszero(s)) {
  3149. return MP_ZERO_E;
  3150. }
  3151. }
  3152. #ifdef PLUTON_CRYPTO_ECC
  3153. else {
  3154. err = wc_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
  3155. }
  3156. #endif
  3157. #else
  3158. err = wc_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
  3159. #endif
  3160. if (err < 0) {
  3161. break;
  3162. }
  3163. FALL_THROUGH;
  3164. case ECC_STATE_SIGN_ENCODE:
  3165. key->state = ECC_STATE_SIGN_ENCODE;
  3166. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  3167. /* restore r/s */
  3168. r = key->r;
  3169. s = key->s;
  3170. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  3171. /* only do this if not simulator, since it overwrites result */
  3172. #ifndef WOLFSSL_ASYNC_CRYPT_TEST
  3173. wc_bigint_to_mp(&r->raw, r);
  3174. wc_bigint_to_mp(&s->raw, s);
  3175. #endif
  3176. }
  3177. #endif /* WOLFSSL_ASYNC_CRYPT */
  3178. /* encoded with DSA header */
  3179. err = StoreECC_DSA_Sig(out, outlen, r, s);
  3180. /* always free r/s */
  3181. mp_clear(r);
  3182. mp_clear(s);
  3183. break;
  3184. default:
  3185. err = BAD_STATE_E;
  3186. }
  3187. /* if async pending then return and skip done cleanup below */
  3188. if (err == WC_PENDING_E) {
  3189. key->state++;
  3190. return err;
  3191. }
  3192. /* cleanup */
  3193. wc_ecc_free_rs(key, &r, &s);
  3194. key->state = ECC_STATE_NONE;
  3195. return err;
  3196. }
  3197. #endif /* !NO_ASN */
  3198. #ifndef WOLFSSL_ATECC508A
  3199. /**
  3200. Sign a message digest
  3201. in The message digest to sign
  3202. inlen The length of the digest
  3203. key A private ECC key
  3204. r [out] The destination for r component of the signature
  3205. s [out] The destination for s component of the signature
  3206. return MP_OKAY if successful
  3207. */
  3208. int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
  3209. ecc_key* key, mp_int *r, mp_int *s)
  3210. {
  3211. int err;
  3212. mp_int e;
  3213. DECLARE_CURVE_SPECS(1)
  3214. if (in == NULL || r == NULL || s == NULL || key == NULL || rng == NULL)
  3215. return ECC_BAD_ARG_E;
  3216. /* is this a private key? */
  3217. if (key->type != ECC_PRIVATEKEY && key->type != ECC_PRIVATEKEY_ONLY) {
  3218. return ECC_BAD_ARG_E;
  3219. }
  3220. /* is the IDX valid ? */
  3221. if (wc_ecc_is_valid_idx(key->idx) != 1) {
  3222. return ECC_BAD_ARG_E;
  3223. }
  3224. #ifdef WOLFSSL_HAVE_SP_ECC
  3225. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  3226. defined(WOLFSSL_ASYNC_CRYPT_TEST)
  3227. if (key->asyncDev.marker != WOLFSSL_ASYNC_MARKER_ECC)
  3228. #endif
  3229. {
  3230. #ifndef WOLFSSL_SP_NO_256
  3231. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1)
  3232. return sp_ecc_sign_256(in, inlen, rng, &key->k, r, s, key->heap);
  3233. #endif
  3234. }
  3235. #endif
  3236. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  3237. defined(WOLFSSL_ASYNC_CRYPT_TEST)
  3238. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  3239. if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_SIGN)) {
  3240. WC_ASYNC_TEST* testDev = &key->asyncDev.test;
  3241. testDev->eccSign.in = in;
  3242. testDev->eccSign.inSz = inlen;
  3243. testDev->eccSign.rng = rng;
  3244. testDev->eccSign.key = key;
  3245. testDev->eccSign.r = r;
  3246. testDev->eccSign.s = s;
  3247. return WC_PENDING_E;
  3248. }
  3249. }
  3250. #endif
  3251. /* get the hash and load it as a bignum into 'e' */
  3252. /* init the bignums */
  3253. if ((err = mp_init(&e)) != MP_OKAY) {
  3254. return err;
  3255. }
  3256. /* load curve info */
  3257. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ORDER);
  3258. /* load digest into e */
  3259. if (err == MP_OKAY) {
  3260. /* we may need to truncate if hash is longer than key size */
  3261. word32 orderBits = mp_count_bits(curve->order);
  3262. /* truncate down to byte size, may be all that's needed */
  3263. if ((WOLFSSL_BIT_SIZE * inlen) > orderBits)
  3264. inlen = (orderBits + WOLFSSL_BIT_SIZE - 1) / WOLFSSL_BIT_SIZE;
  3265. err = mp_read_unsigned_bin(&e, (byte*)in, inlen);
  3266. /* may still need bit truncation too */
  3267. if (err == MP_OKAY && (WOLFSSL_BIT_SIZE * inlen) > orderBits)
  3268. mp_rshb(&e, WOLFSSL_BIT_SIZE - (orderBits & 0x7));
  3269. }
  3270. /* make up a key and export the public copy */
  3271. if (err == MP_OKAY) {
  3272. int loop_check = 0;
  3273. ecc_key pubkey;
  3274. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  3275. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  3276. #ifdef HAVE_CAVIUM
  3277. /* TODO: Not implemented */
  3278. #elif defined(HAVE_INTEL_QA)
  3279. mp_int k;
  3280. err = mp_init(&k);
  3281. /* make sure r and s are allocated */
  3282. if (err == MP_OKAY)
  3283. err = wc_bigint_alloc(&key->r->raw, key->dp->size);
  3284. if (err == MP_OKAY)
  3285. err = wc_bigint_alloc(&key->s->raw, key->dp->size);
  3286. /* load e and k */
  3287. if (err == MP_OKAY)
  3288. err = wc_mp_to_bigint(&e, &e.raw);
  3289. if (err == MP_OKAY)
  3290. err = wc_mp_to_bigint(&key->k, &key->k.raw);
  3291. if (err == MP_OKAY)
  3292. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  3293. if (err == MP_OKAY)
  3294. err = wc_ecc_gen_k(rng, key->dp->size, &k, curve->order);
  3295. if (err == MP_OKAY)
  3296. err = wc_mp_to_bigint(&k, &k.raw);
  3297. if (err == MP_OKAY)
  3298. err = IntelQaEcdsaSign(&key->asyncDev, &e.raw, &key->k.raw,
  3299. &k.raw, &r->raw, &s->raw, &curve->Af->raw, &curve->Bf->raw,
  3300. &curve->prime->raw, &curve->order->raw, &curve->Gx->raw,
  3301. &curve->Gy->raw);
  3302. mp_clear(&e);
  3303. mp_clear(&k);
  3304. wc_ecc_curve_free(curve);
  3305. return err;
  3306. #endif
  3307. }
  3308. #endif /* WOLFSSL_ASYNC_CRYPT */
  3309. /* don't use async for key, since we don't support async return here */
  3310. if (wc_ecc_init_ex(&pubkey, key->heap, INVALID_DEVID) == MP_OKAY) {
  3311. #ifdef WOLFSSL_CUSTOM_CURVES
  3312. /* if custom curve, apply params to pubkey */
  3313. if (key->idx == ECC_CUSTOM_IDX) {
  3314. wc_ecc_set_custom_curve(&pubkey, key->dp);
  3315. }
  3316. #endif
  3317. for (;;) {
  3318. if (++loop_check > 64) {
  3319. err = RNG_FAILURE_E;
  3320. break;
  3321. }
  3322. err = wc_ecc_make_key_ex(rng, key->dp->size, &pubkey,
  3323. key->dp->id);
  3324. if (err != MP_OKAY) break;
  3325. /* find r = x1 mod n */
  3326. err = mp_mod(pubkey.pubkey.x, curve->order, r);
  3327. if (err != MP_OKAY) break;
  3328. if (mp_iszero(r) == MP_YES) {
  3329. #ifndef ALT_ECC_SIZE
  3330. mp_clear(pubkey.pubkey.x);
  3331. mp_clear(pubkey.pubkey.y);
  3332. mp_clear(pubkey.pubkey.z);
  3333. #endif
  3334. mp_forcezero(&pubkey.k);
  3335. }
  3336. else {
  3337. /* find s = (e + xr)/k */
  3338. err = mp_invmod(&pubkey.k, curve->order, &pubkey.k);
  3339. if (err != MP_OKAY) break;
  3340. /* s = xr */
  3341. err = mp_mulmod(&key->k, r, curve->order, s);
  3342. if (err != MP_OKAY) break;
  3343. /* s = e + xr */
  3344. err = mp_add(&e, s, s);
  3345. if (err != MP_OKAY) break;
  3346. /* s = e + xr */
  3347. err = mp_mod(s, curve->order, s);
  3348. if (err != MP_OKAY) break;
  3349. /* s = (e + xr)/k */
  3350. err = mp_mulmod(s, &pubkey.k, curve->order, s);
  3351. if (mp_iszero(s) == MP_NO)
  3352. break;
  3353. }
  3354. }
  3355. wc_ecc_free(&pubkey);
  3356. }
  3357. }
  3358. mp_clear(&e);
  3359. wc_ecc_curve_free(curve);
  3360. return err;
  3361. }
  3362. #endif /* WOLFSSL_ATECC508A */
  3363. #endif /* HAVE_ECC_SIGN */
  3364. /**
  3365. Free an ECC key from memory
  3366. key The key you wish to free
  3367. */
  3368. int wc_ecc_free(ecc_key* key)
  3369. {
  3370. if (key == NULL) {
  3371. return 0;
  3372. }
  3373. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  3374. wolfAsync_DevCtxFree(&key->asyncDev, WOLFSSL_ASYNC_MARKER_ECC);
  3375. wc_ecc_free_rs(key, &key->r, &key->s);
  3376. #endif
  3377. #ifdef WOLFSSL_ATECC508A
  3378. atmel_ecc_free(key->slot);
  3379. key->slot = -1;
  3380. #else
  3381. mp_clear(key->pubkey.x);
  3382. mp_clear(key->pubkey.y);
  3383. mp_clear(key->pubkey.z);
  3384. mp_forcezero(&key->k);
  3385. #endif /* WOLFSSL_ATECC508A */
  3386. return 0;
  3387. }
  3388. #ifdef ECC_SHAMIR
  3389. /** Computes kA*A + kB*B = C using Shamir's Trick
  3390. A First point to multiply
  3391. kA What to multiple A by
  3392. B Second point to multiply
  3393. kB What to multiple B by
  3394. C [out] Destination point (can overlap with A or B)
  3395. a ECC curve parameter a
  3396. modulus Modulus for curve
  3397. return MP_OKAY on success
  3398. */
  3399. #ifdef FP_ECC
  3400. static int normal_ecc_mul2add(ecc_point* A, mp_int* kA,
  3401. ecc_point* B, mp_int* kB,
  3402. ecc_point* C, mp_int* a, mp_int* modulus,
  3403. void* heap)
  3404. #else
  3405. static int ecc_mul2add(ecc_point* A, mp_int* kA,
  3406. ecc_point* B, mp_int* kB,
  3407. ecc_point* C, mp_int* a, mp_int* modulus,
  3408. void* heap)
  3409. #endif
  3410. {
  3411. ecc_point* precomp[16];
  3412. unsigned bitbufA, bitbufB, lenA, lenB, len, nA, nB, nibble;
  3413. unsigned char* tA;
  3414. unsigned char* tB;
  3415. int err = MP_OKAY, first, x, y;
  3416. mp_digit mp = 0;
  3417. /* argchks */
  3418. if (A == NULL || kA == NULL || B == NULL || kB == NULL || C == NULL ||
  3419. modulus == NULL) {
  3420. return ECC_BAD_ARG_E;
  3421. }
  3422. /* allocate memory */
  3423. tA = (unsigned char*)XMALLOC(ECC_BUFSIZE, heap, DYNAMIC_TYPE_ECC_BUFFER);
  3424. if (tA == NULL) {
  3425. return GEN_MEM_ERR;
  3426. }
  3427. tB = (unsigned char*)XMALLOC(ECC_BUFSIZE, heap, DYNAMIC_TYPE_ECC_BUFFER);
  3428. if (tB == NULL) {
  3429. XFREE(tA, heap, DYNAMIC_TYPE_ECC_BUFFER);
  3430. return GEN_MEM_ERR;
  3431. }
  3432. /* init variables */
  3433. XMEMSET(tA, 0, ECC_BUFSIZE);
  3434. XMEMSET(tB, 0, ECC_BUFSIZE);
  3435. XMEMSET(precomp, 0, sizeof(precomp));
  3436. /* get sizes */
  3437. lenA = mp_unsigned_bin_size(kA);
  3438. lenB = mp_unsigned_bin_size(kB);
  3439. len = MAX(lenA, lenB);
  3440. /* sanity check */
  3441. if ((lenA > ECC_BUFSIZE) || (lenB > ECC_BUFSIZE)) {
  3442. err = BAD_FUNC_ARG;
  3443. }
  3444. if (err == MP_OKAY) {
  3445. /* extract and justify kA */
  3446. err = mp_to_unsigned_bin(kA, (len - lenA) + tA);
  3447. /* extract and justify kB */
  3448. if (err == MP_OKAY)
  3449. err = mp_to_unsigned_bin(kB, (len - lenB) + tB);
  3450. /* allocate the table */
  3451. if (err == MP_OKAY) {
  3452. for (x = 0; x < 16; x++) {
  3453. precomp[x] = wc_ecc_new_point_h(heap);
  3454. if (precomp[x] == NULL) {
  3455. err = GEN_MEM_ERR;
  3456. break;
  3457. }
  3458. }
  3459. }
  3460. }
  3461. if (err == MP_OKAY)
  3462. /* init montgomery reduction */
  3463. err = mp_montgomery_setup(modulus, &mp);
  3464. if (err == MP_OKAY) {
  3465. mp_int mu;
  3466. err = mp_init(&mu);
  3467. if (err == MP_OKAY) {
  3468. err = mp_montgomery_calc_normalization(&mu, modulus);
  3469. if (err == MP_OKAY)
  3470. /* copy ones ... */
  3471. err = mp_mulmod(A->x, &mu, modulus, precomp[1]->x);
  3472. if (err == MP_OKAY)
  3473. err = mp_mulmod(A->y, &mu, modulus, precomp[1]->y);
  3474. if (err == MP_OKAY)
  3475. err = mp_mulmod(A->z, &mu, modulus, precomp[1]->z);
  3476. if (err == MP_OKAY)
  3477. err = mp_mulmod(B->x, &mu, modulus, precomp[1<<2]->x);
  3478. if (err == MP_OKAY)
  3479. err = mp_mulmod(B->y, &mu, modulus, precomp[1<<2]->y);
  3480. if (err == MP_OKAY)
  3481. err = mp_mulmod(B->z, &mu, modulus, precomp[1<<2]->z);
  3482. /* done with mu */
  3483. mp_clear(&mu);
  3484. }
  3485. }
  3486. if (err == MP_OKAY)
  3487. /* precomp [i,0](A + B) table */
  3488. err = ecc_projective_dbl_point(precomp[1], precomp[2], a, modulus, mp);
  3489. if (err == MP_OKAY)
  3490. err = ecc_projective_add_point(precomp[1], precomp[2], precomp[3],
  3491. a, modulus, mp);
  3492. if (err == MP_OKAY)
  3493. /* precomp [0,i](A + B) table */
  3494. err = ecc_projective_dbl_point(precomp[1<<2], precomp[2<<2], a, modulus, mp);
  3495. if (err == MP_OKAY)
  3496. err = ecc_projective_add_point(precomp[1<<2], precomp[2<<2], precomp[3<<2],
  3497. a, modulus, mp);
  3498. if (err == MP_OKAY) {
  3499. /* precomp [i,j](A + B) table (i != 0, j != 0) */
  3500. for (x = 1; x < 4; x++) {
  3501. for (y = 1; y < 4; y++) {
  3502. if (err == MP_OKAY)
  3503. err = ecc_projective_add_point(precomp[x], precomp[(y<<2)],
  3504. precomp[x+(y<<2)], a, modulus, mp);
  3505. }
  3506. }
  3507. }
  3508. if (err == MP_OKAY) {
  3509. nibble = 3;
  3510. first = 1;
  3511. bitbufA = tA[0];
  3512. bitbufB = tB[0];
  3513. /* for every byte of the multiplicands */
  3514. for (x = 0;; ) {
  3515. /* grab a nibble */
  3516. if (++nibble == 4) {
  3517. if (x == (int)len) break;
  3518. bitbufA = tA[x];
  3519. bitbufB = tB[x];
  3520. nibble = 0;
  3521. x++;
  3522. }
  3523. /* extract two bits from both, shift/update */
  3524. nA = (bitbufA >> 6) & 0x03;
  3525. nB = (bitbufB >> 6) & 0x03;
  3526. bitbufA = (bitbufA << 2) & 0xFF;
  3527. bitbufB = (bitbufB << 2) & 0xFF;
  3528. /* if both zero, if first, continue */
  3529. if ((nA == 0) && (nB == 0) && (first == 1)) {
  3530. continue;
  3531. }
  3532. /* double twice, only if this isn't the first */
  3533. if (first == 0) {
  3534. /* double twice */
  3535. if (err == MP_OKAY)
  3536. err = ecc_projective_dbl_point(C, C, a, modulus, mp);
  3537. if (err == MP_OKAY)
  3538. err = ecc_projective_dbl_point(C, C, a, modulus, mp);
  3539. else
  3540. break;
  3541. }
  3542. /* if not both zero */
  3543. if ((nA != 0) || (nB != 0)) {
  3544. if (first == 1) {
  3545. /* if first, copy from table */
  3546. first = 0;
  3547. if (err == MP_OKAY)
  3548. err = mp_copy(precomp[nA + (nB<<2)]->x, C->x);
  3549. if (err == MP_OKAY)
  3550. err = mp_copy(precomp[nA + (nB<<2)]->y, C->y);
  3551. if (err == MP_OKAY)
  3552. err = mp_copy(precomp[nA + (nB<<2)]->z, C->z);
  3553. else
  3554. break;
  3555. } else {
  3556. /* if not first, add from table */
  3557. if (err == MP_OKAY)
  3558. err = ecc_projective_add_point(C, precomp[nA + (nB<<2)], C,
  3559. a, modulus, mp);
  3560. else
  3561. break;
  3562. }
  3563. }
  3564. }
  3565. }
  3566. /* reduce to affine */
  3567. if (err == MP_OKAY)
  3568. err = ecc_map(C, modulus, mp);
  3569. /* clean up */
  3570. for (x = 0; x < 16; x++) {
  3571. wc_ecc_del_point_h(precomp[x], heap);
  3572. }
  3573. ForceZero(tA, ECC_BUFSIZE);
  3574. ForceZero(tB, ECC_BUFSIZE);
  3575. XFREE(tA, heap, DYNAMIC_TYPE_ECC_BUFFER);
  3576. XFREE(tB, heap, DYNAMIC_TYPE_ECC_BUFFER);
  3577. return err;
  3578. }
  3579. #endif /* ECC_SHAMIR */
  3580. #ifdef HAVE_ECC_VERIFY
  3581. #ifndef NO_ASN
  3582. /* verify
  3583. *
  3584. * w = s^-1 mod n
  3585. * u1 = xw
  3586. * u2 = rw
  3587. * X = u1*G + u2*Q
  3588. * v = X_x1 mod n
  3589. * accept if v == r
  3590. */
  3591. /**
  3592. Verify an ECC signature
  3593. sig The signature to verify
  3594. siglen The length of the signature (octets)
  3595. hash The hash (message digest) that was signed
  3596. hashlen The length of the hash (octets)
  3597. res Result of signature, 1==valid, 0==invalid
  3598. key The corresponding public ECC key
  3599. return MP_OKAY if successful (even if the signature is not valid)
  3600. */
  3601. int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
  3602. word32 hashlen, int* res, ecc_key* key)
  3603. {
  3604. int err;
  3605. mp_int *r = NULL, *s = NULL;
  3606. #ifndef WOLFSSL_ASYNC_CRYPT
  3607. mp_int r_lcl, s_lcl;
  3608. r = &r_lcl;
  3609. s = &s_lcl;
  3610. #endif
  3611. if (sig == NULL || hash == NULL || res == NULL || key == NULL) {
  3612. return ECC_BAD_ARG_E;
  3613. }
  3614. switch(key->state) {
  3615. case ECC_STATE_NONE:
  3616. case ECC_STATE_VERIFY_DECODE:
  3617. key->state = ECC_STATE_VERIFY_DECODE;
  3618. /* default to invalid signature */
  3619. *res = 0;
  3620. /* Note, DecodeECC_DSA_Sig() calls mp_init() on r and s.
  3621. * If either of those don't allocate correctly, none of
  3622. * the rest of this function will execute, and everything
  3623. * gets cleaned up at the end. */
  3624. err = wc_ecc_alloc_rs(key, &r, &s);
  3625. if (err != 0)
  3626. break;
  3627. /* decode DSA header */
  3628. err = DecodeECC_DSA_Sig(sig, siglen, r, s);
  3629. if (err < 0) {
  3630. break;
  3631. }
  3632. FALL_THROUGH;
  3633. case ECC_STATE_VERIFY_DO:
  3634. key->state = ECC_STATE_VERIFY_DO;
  3635. err = wc_ecc_verify_hash_ex(r, s, hash, hashlen, res, key);
  3636. if (err < 0) {
  3637. break;
  3638. }
  3639. FALL_THROUGH;
  3640. case ECC_STATE_VERIFY_RES:
  3641. key->state = ECC_STATE_VERIFY_RES;
  3642. err = 0;
  3643. #ifdef WOLFSSL_ASYNC_CRYPT
  3644. /* restore r/s */
  3645. r = key->r;
  3646. s = key->s;
  3647. #endif
  3648. /* done with R/S */
  3649. mp_clear(r);
  3650. mp_clear(s);
  3651. break;
  3652. default:
  3653. err = BAD_STATE_E;
  3654. }
  3655. /* if async pending then return and skip done cleanup below */
  3656. if (err == WC_PENDING_E) {
  3657. key->state++;
  3658. return err;
  3659. }
  3660. /* cleanup */
  3661. wc_ecc_free_rs(key, &r, &s);
  3662. key->state = ECC_STATE_NONE;
  3663. return err;
  3664. }
  3665. #endif /* !NO_ASN */
  3666. /**
  3667. Verify an ECC signature
  3668. r The signature R component to verify
  3669. s The signature S component to verify
  3670. hash The hash (message digest) that was signed
  3671. hashlen The length of the hash (octets)
  3672. res Result of signature, 1==valid, 0==invalid
  3673. key The corresponding public ECC key
  3674. return MP_OKAY if successful (even if the signature is not valid)
  3675. */
  3676. int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
  3677. word32 hashlen, int* res, ecc_key* key)
  3678. {
  3679. int err;
  3680. #ifndef WOLFSSL_ATECC508A
  3681. int did_init = 0;
  3682. ecc_point *mG = NULL, *mQ = NULL;
  3683. mp_int v;
  3684. mp_int w;
  3685. mp_int u1;
  3686. mp_int u2;
  3687. mp_int e;
  3688. DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT)
  3689. #else
  3690. byte sigRS[ATECC_KEY_SIZE*2];
  3691. #endif
  3692. if (r == NULL || s == NULL || hash == NULL || res == NULL || key == NULL)
  3693. return ECC_BAD_ARG_E;
  3694. /* default to invalid signature */
  3695. *res = 0;
  3696. /* is the IDX valid ? */
  3697. if (wc_ecc_is_valid_idx(key->idx) != 1) {
  3698. return ECC_BAD_ARG_E;
  3699. }
  3700. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  3701. defined(WOLFSSL_ASYNC_CRYPT_TEST)
  3702. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  3703. if (wc_AsyncTestInit(&key->asyncDev, ASYNC_TEST_ECC_VERIFY)) {
  3704. WC_ASYNC_TEST* testDev = &key->asyncDev.test;
  3705. testDev->eccVerify.r = r;
  3706. testDev->eccVerify.s = s;
  3707. testDev->eccVerify.hash = hash;
  3708. testDev->eccVerify.hashlen = hashlen;
  3709. testDev->eccVerify.stat = res;
  3710. testDev->eccVerify.key = key;
  3711. return WC_PENDING_E;
  3712. }
  3713. }
  3714. #endif
  3715. #ifdef WOLFSSL_ATECC508A
  3716. /* Extract R and S */
  3717. err = mp_to_unsigned_bin(r, &sigRS[0]);
  3718. if (err != MP_OKAY) {
  3719. return err;
  3720. }
  3721. err = mp_to_unsigned_bin(s, &sigRS[ATECC_KEY_SIZE]);
  3722. if (err != MP_OKAY) {
  3723. return err;
  3724. }
  3725. err = atcatls_verify(hash, sigRS, key->pubkey_raw, (bool*)res);
  3726. if (err != ATCA_SUCCESS) {
  3727. return BAD_COND_E;
  3728. }
  3729. #else
  3730. /* checking if private key with no public part */
  3731. if (key->type == ECC_PRIVATEKEY_ONLY) {
  3732. WOLFSSL_MSG("Verify called with private key, generating public part");
  3733. err = wc_ecc_make_pub_ex(key, NULL, NULL);
  3734. if (err != MP_OKAY) {
  3735. WOLFSSL_MSG("Unable to extract public key");
  3736. return err;
  3737. }
  3738. }
  3739. #ifdef WOLFSSL_HAVE_SP_ECC
  3740. #ifndef WOLFSSL_SP_NO_256
  3741. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  3742. defined(WOLFSSL_ASYNC_CRYPT_TEST)
  3743. if (key->asyncDev.marker != WOLFSSL_ASYNC_MARKER_ECC)
  3744. #endif
  3745. {
  3746. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1)
  3747. return sp_ecc_verify_256(hash, hashlen, key->pubkey.x, key->pubkey.y,
  3748. key->pubkey.z,r, s, res, key->heap);
  3749. }
  3750. #endif
  3751. #endif
  3752. err = mp_init(&e);
  3753. if (err != MP_OKAY)
  3754. return MEMORY_E;
  3755. /* read in the specs for this curve */
  3756. err = wc_ecc_curve_load(key->dp, &curve, ECC_CURVE_FIELD_ALL);
  3757. /* check for zero */
  3758. if (err == MP_OKAY) {
  3759. if (mp_iszero(r) == MP_YES || mp_iszero(s) == MP_YES ||
  3760. mp_cmp(r, curve->order) != MP_LT ||
  3761. mp_cmp(s, curve->order) != MP_LT) {
  3762. err = MP_ZERO_E;
  3763. }
  3764. }
  3765. /* read hash */
  3766. if (err == MP_OKAY) {
  3767. /* we may need to truncate if hash is longer than key size */
  3768. unsigned int orderBits = mp_count_bits(curve->order);
  3769. /* truncate down to byte size, may be all that's needed */
  3770. if ( (WOLFSSL_BIT_SIZE * hashlen) > orderBits)
  3771. hashlen = (orderBits + WOLFSSL_BIT_SIZE - 1) / WOLFSSL_BIT_SIZE;
  3772. err = mp_read_unsigned_bin(&e, hash, hashlen);
  3773. /* may still need bit truncation too */
  3774. if (err == MP_OKAY && (WOLFSSL_BIT_SIZE * hashlen) > orderBits)
  3775. mp_rshb(&e, WOLFSSL_BIT_SIZE - (orderBits & 0x7));
  3776. }
  3777. /* check for async hardware acceleration */
  3778. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  3779. if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ECC) {
  3780. #ifdef HAVE_CAVIUM
  3781. /* TODO: Not implemented */
  3782. #elif defined(HAVE_INTEL_QA)
  3783. err = wc_mp_to_bigint(&e, &e.raw);
  3784. if (err == MP_OKAY)
  3785. err = wc_mp_to_bigint(key->pubkey.x, &key->pubkey.x->raw);
  3786. if (err == MP_OKAY)
  3787. err = wc_mp_to_bigint(key->pubkey.y, &key->pubkey.y->raw);
  3788. if (err == MP_OKAY)
  3789. err = IntelQaEcdsaVerify(&key->asyncDev, &e.raw, &key->pubkey.x->raw,
  3790. &key->pubkey.y->raw, &r->raw, &s->raw, &curve->Af->raw,
  3791. &curve->Bf->raw, &curve->prime->raw, &curve->order->raw,
  3792. &curve->Gx->raw, &curve->Gy->raw, res);
  3793. mp_clear(&e);
  3794. wc_ecc_curve_free(curve);
  3795. return err;
  3796. #endif
  3797. }
  3798. #endif /* WOLFSSL_ASYNC_CRYPT */
  3799. /* allocate ints */
  3800. if (err == MP_OKAY) {
  3801. if ((err = mp_init_multi(&v, &w, &u1, &u2, NULL, NULL)) != MP_OKAY) {
  3802. err = MEMORY_E;
  3803. }
  3804. did_init = 1;
  3805. }
  3806. /* allocate points */
  3807. if (err == MP_OKAY) {
  3808. mG = wc_ecc_new_point_h(key->heap);
  3809. mQ = wc_ecc_new_point_h(key->heap);
  3810. if (mQ == NULL || mG == NULL)
  3811. err = MEMORY_E;
  3812. }
  3813. /* w = s^-1 mod n */
  3814. if (err == MP_OKAY)
  3815. err = mp_invmod(s, curve->order, &w);
  3816. /* u1 = ew */
  3817. if (err == MP_OKAY)
  3818. err = mp_mulmod(&e, &w, curve->order, &u1);
  3819. /* u2 = rw */
  3820. if (err == MP_OKAY)
  3821. err = mp_mulmod(r, &w, curve->order, &u2);
  3822. /* find mG and mQ */
  3823. if (err == MP_OKAY)
  3824. err = mp_copy(curve->Gx, mG->x);
  3825. if (err == MP_OKAY)
  3826. err = mp_copy(curve->Gy, mG->y);
  3827. if (err == MP_OKAY)
  3828. err = mp_set(mG->z, 1);
  3829. if (err == MP_OKAY)
  3830. err = mp_copy(key->pubkey.x, mQ->x);
  3831. if (err == MP_OKAY)
  3832. err = mp_copy(key->pubkey.y, mQ->y);
  3833. if (err == MP_OKAY)
  3834. err = mp_copy(key->pubkey.z, mQ->z);
  3835. #ifdef FREESCALE_LTC_ECC
  3836. /* use PKHA to compute u1*mG + u2*mQ */
  3837. if (err == MP_OKAY)
  3838. err = wc_ecc_mulmod_ex(&u1, mG, mG, curve->Af, curve->prime, 0, key->heap);
  3839. if (err == MP_OKAY)
  3840. err = wc_ecc_mulmod_ex(&u2, mQ, mQ, curve->Af, curve->prime, 0, key->heap);
  3841. if (err == MP_OKAY)
  3842. err = wc_ecc_point_add(mG, mQ, mG, curve->prime);
  3843. #else /* FREESCALE_LTC_ECC */
  3844. #ifndef ECC_SHAMIR
  3845. {
  3846. mp_digit mp = 0;
  3847. /* compute u1*mG + u2*mQ = mG */
  3848. if (err == MP_OKAY) {
  3849. err = wc_ecc_mulmod_ex(&u1, mG, mG, curve->Af, curve->prime, 0,
  3850. key->heap);
  3851. }
  3852. if (err == MP_OKAY) {
  3853. err = wc_ecc_mulmod_ex(&u2, mQ, mQ, curve->Af, curve->prime, 0,
  3854. key->heap);
  3855. }
  3856. /* find the montgomery mp */
  3857. if (err == MP_OKAY)
  3858. err = mp_montgomery_setup(curve->prime, &mp);
  3859. /* add them */
  3860. if (err == MP_OKAY)
  3861. err = ecc_projective_add_point(mQ, mG, mG, curve->Af,
  3862. curve->prime, mp);
  3863. /* reduce */
  3864. if (err == MP_OKAY)
  3865. err = ecc_map(mG, curve->prime, mp);
  3866. }
  3867. #else
  3868. /* use Shamir's trick to compute u1*mG + u2*mQ using half the doubles */
  3869. if (err == MP_OKAY) {
  3870. err = ecc_mul2add(mG, &u1, mQ, &u2, mG, curve->Af, curve->prime,
  3871. key->heap);
  3872. }
  3873. #endif /* ECC_SHAMIR */
  3874. #endif /* FREESCALE_LTC_ECC */
  3875. /* v = X_x1 mod n */
  3876. if (err == MP_OKAY)
  3877. err = mp_mod(mG->x, curve->order, &v);
  3878. /* does v == r */
  3879. if (err == MP_OKAY) {
  3880. if (mp_cmp(&v, r) == MP_EQ)
  3881. *res = 1;
  3882. }
  3883. /* cleanup */
  3884. wc_ecc_del_point_h(mG, key->heap);
  3885. wc_ecc_del_point_h(mQ, key->heap);
  3886. mp_clear(&e);
  3887. if (did_init) {
  3888. mp_clear(&v);
  3889. mp_clear(&w);
  3890. mp_clear(&u1);
  3891. mp_clear(&u2);
  3892. }
  3893. wc_ecc_curve_free(curve);
  3894. #endif /* WOLFSSL_ATECC508A */
  3895. return err;
  3896. }
  3897. #endif /* HAVE_ECC_VERIFY */
  3898. #ifdef HAVE_ECC_KEY_IMPORT
  3899. #ifndef WOLFSSL_ATECC508A
  3900. /* import point from der */
  3901. int wc_ecc_import_point_der(byte* in, word32 inLen, const int curve_idx,
  3902. ecc_point* point)
  3903. {
  3904. int err = 0;
  3905. int compressed = 0;
  3906. if (in == NULL || point == NULL || (curve_idx < 0) ||
  3907. (wc_ecc_is_valid_idx(curve_idx) == 0))
  3908. return ECC_BAD_ARG_E;
  3909. /* must be odd */
  3910. if ((inLen & 1) == 0) {
  3911. return ECC_BAD_ARG_E;
  3912. }
  3913. /* init point */
  3914. #ifdef ALT_ECC_SIZE
  3915. point->x = (mp_int*)&point->xyz[0];
  3916. point->y = (mp_int*)&point->xyz[1];
  3917. point->z = (mp_int*)&point->xyz[2];
  3918. alt_fp_init(point->x);
  3919. alt_fp_init(point->y);
  3920. alt_fp_init(point->z);
  3921. #else
  3922. err = mp_init_multi(point->x, point->y, point->z, NULL, NULL, NULL);
  3923. #endif
  3924. if (err != MP_OKAY)
  3925. return MEMORY_E;
  3926. /* check for 4, 2, or 3 */
  3927. if (in[0] != 0x04 && in[0] != 0x02 && in[0] != 0x03) {
  3928. err = ASN_PARSE_E;
  3929. }
  3930. if (in[0] == 0x02 || in[0] == 0x03) {
  3931. #ifdef HAVE_COMP_KEY
  3932. compressed = 1;
  3933. #else
  3934. err = NOT_COMPILED_IN;
  3935. #endif
  3936. }
  3937. #ifdef WOLFSSL_ATECC508A
  3938. /* populate key->pubkey_raw */
  3939. XMEMCPY(key->pubkey_raw, (byte*)in+1, PUB_KEY_SIZE);
  3940. #endif
  3941. /* read data */
  3942. if (err == MP_OKAY)
  3943. err = mp_read_unsigned_bin(point->x, (byte*)in+1, (inLen-1)>>1);
  3944. #ifdef HAVE_COMP_KEY
  3945. if (err == MP_OKAY && compressed == 1) { /* build y */
  3946. int did_init = 0;
  3947. mp_int t1, t2;
  3948. DECLARE_CURVE_SPECS(3)
  3949. if (mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL) != MP_OKAY)
  3950. err = MEMORY_E;
  3951. else
  3952. did_init = 1;
  3953. /* load curve info */
  3954. if (err == MP_OKAY)
  3955. err = wc_ecc_curve_load(&ecc_sets[curve_idx], &curve,
  3956. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
  3957. ECC_CURVE_FIELD_BF));
  3958. /* compute x^3 */
  3959. if (err == MP_OKAY)
  3960. err = mp_sqr(point->x, &t1);
  3961. if (err == MP_OKAY)
  3962. err = mp_mulmod(&t1, point->x, curve->prime, &t1);
  3963. /* compute x^3 + a*x */
  3964. if (err == MP_OKAY)
  3965. err = mp_mulmod(curve->Af, point->x, curve->prime, &t2);
  3966. if (err == MP_OKAY)
  3967. err = mp_add(&t1, &t2, &t1);
  3968. /* compute x^3 + a*x + b */
  3969. if (err == MP_OKAY)
  3970. err = mp_add(&t1, curve->Bf, &t1);
  3971. /* compute sqrt(x^3 + a*x + b) */
  3972. if (err == MP_OKAY)
  3973. err = mp_sqrtmod_prime(&t1, curve->prime, &t2);
  3974. /* adjust y */
  3975. if (err == MP_OKAY) {
  3976. if ((mp_isodd(&t2) == MP_YES && in[0] == 0x03) ||
  3977. (mp_isodd(&t2) == MP_NO && in[0] == 0x02)) {
  3978. err = mp_mod(&t2, curve->prime, point->y);
  3979. }
  3980. else {
  3981. err = mp_submod(curve->prime, &t2, curve->prime, point->y);
  3982. }
  3983. }
  3984. if (did_init) {
  3985. mp_clear(&t2);
  3986. mp_clear(&t1);
  3987. }
  3988. wc_ecc_curve_free(curve);
  3989. }
  3990. #endif
  3991. if (err == MP_OKAY && compressed == 0)
  3992. err = mp_read_unsigned_bin(point->y,
  3993. (byte*)in+1+((inLen-1)>>1), (inLen-1)>>1);
  3994. if (err == MP_OKAY)
  3995. err = mp_set(point->z, 1);
  3996. if (err != MP_OKAY) {
  3997. mp_clear(point->x);
  3998. mp_clear(point->y);
  3999. mp_clear(point->z);
  4000. }
  4001. return err;
  4002. }
  4003. #endif /* !WOLFSSL_ATECC508A */
  4004. #endif /* HAVE_ECC_KEY_IMPORT */
  4005. #ifdef HAVE_ECC_KEY_EXPORT
  4006. /* export point to der */
  4007. int wc_ecc_export_point_der(const int curve_idx, ecc_point* point, byte* out,
  4008. word32* outLen)
  4009. {
  4010. int ret = MP_OKAY;
  4011. word32 numlen;
  4012. #ifndef WOLFSSL_ATECC508A
  4013. #ifdef WOLFSSL_SMALL_STACK
  4014. byte* buf;
  4015. #else
  4016. byte buf[ECC_BUFSIZE];
  4017. #endif
  4018. #endif /* !WOLFSSL_ATECC508A */
  4019. if ((curve_idx < 0) || (wc_ecc_is_valid_idx(curve_idx) == 0))
  4020. return ECC_BAD_ARG_E;
  4021. /* return length needed only */
  4022. if (point != NULL && out == NULL && outLen != NULL) {
  4023. numlen = ecc_sets[curve_idx].size;
  4024. *outLen = 1 + 2*numlen;
  4025. return LENGTH_ONLY_E;
  4026. }
  4027. if (point == NULL || out == NULL || outLen == NULL)
  4028. return ECC_BAD_ARG_E;
  4029. numlen = ecc_sets[curve_idx].size;
  4030. if (*outLen < (1 + 2*numlen)) {
  4031. *outLen = 1 + 2*numlen;
  4032. return BUFFER_E;
  4033. }
  4034. #ifdef WOLFSSL_ATECC508A
  4035. /* TODO: Implement equiv call to ATECC508A */
  4036. ret = BAD_COND_E;
  4037. #else
  4038. /* store byte 0x04 */
  4039. out[0] = 0x04;
  4040. #ifdef WOLFSSL_SMALL_STACK
  4041. buf = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  4042. if (buf == NULL)
  4043. return MEMORY_E;
  4044. #endif
  4045. /* pad and store x */
  4046. XMEMSET(buf, 0, ECC_BUFSIZE);
  4047. ret = mp_to_unsigned_bin(point->x, buf +
  4048. (numlen - mp_unsigned_bin_size(point->x)));
  4049. if (ret != MP_OKAY)
  4050. goto done;
  4051. XMEMCPY(out+1, buf, numlen);
  4052. /* pad and store y */
  4053. XMEMSET(buf, 0, ECC_BUFSIZE);
  4054. ret = mp_to_unsigned_bin(point->y, buf +
  4055. (numlen - mp_unsigned_bin_size(point->y)));
  4056. if (ret != MP_OKAY)
  4057. goto done;
  4058. XMEMCPY(out+1+numlen, buf, numlen);
  4059. *outLen = 1 + 2*numlen;
  4060. done:
  4061. #ifdef WOLFSSL_SMALL_STACK
  4062. XFREE(buf, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  4063. #endif
  4064. #endif /* WOLFSSL_ATECC508A */
  4065. return ret;
  4066. }
  4067. /* export public ECC key in ANSI X9.63 format */
  4068. int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
  4069. {
  4070. int ret = MP_OKAY;
  4071. word32 numlen;
  4072. #ifdef WOLFSSL_SMALL_STACK
  4073. byte* buf;
  4074. #else
  4075. byte buf[ECC_BUFSIZE];
  4076. #endif
  4077. word32 pubxlen, pubylen;
  4078. /* return length needed only */
  4079. if (key != NULL && out == NULL && outLen != NULL) {
  4080. numlen = key->dp->size;
  4081. *outLen = 1 + 2*numlen;
  4082. return LENGTH_ONLY_E;
  4083. }
  4084. if (key == NULL || out == NULL || outLen == NULL)
  4085. return ECC_BAD_ARG_E;
  4086. if (key->type == ECC_PRIVATEKEY_ONLY)
  4087. return ECC_PRIVATEONLY_E;
  4088. if (wc_ecc_is_valid_idx(key->idx) == 0) {
  4089. return ECC_BAD_ARG_E;
  4090. }
  4091. numlen = key->dp->size;
  4092. /* verify room in out buffer */
  4093. if (*outLen < (1 + 2*numlen)) {
  4094. *outLen = 1 + 2*numlen;
  4095. return BUFFER_E;
  4096. }
  4097. /* verify public key length is less than key size */
  4098. pubxlen = mp_unsigned_bin_size(key->pubkey.x);
  4099. pubylen = mp_unsigned_bin_size(key->pubkey.y);
  4100. if ((pubxlen > numlen) || (pubylen > numlen)) {
  4101. WOLFSSL_MSG("Public key x/y invalid!");
  4102. return BUFFER_E;
  4103. }
  4104. /* store byte 0x04 */
  4105. out[0] = 0x04;
  4106. #ifdef WOLFSSL_SMALL_STACK
  4107. buf = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  4108. if (buf == NULL)
  4109. return MEMORY_E;
  4110. #endif
  4111. /* pad and store x */
  4112. XMEMSET(buf, 0, ECC_BUFSIZE);
  4113. ret = mp_to_unsigned_bin(key->pubkey.x, buf + (numlen - pubxlen));
  4114. if (ret != MP_OKAY)
  4115. goto done;
  4116. XMEMCPY(out+1, buf, numlen);
  4117. /* pad and store y */
  4118. XMEMSET(buf, 0, ECC_BUFSIZE);
  4119. ret = mp_to_unsigned_bin(key->pubkey.y, buf + (numlen - pubylen));
  4120. if (ret != MP_OKAY)
  4121. goto done;
  4122. XMEMCPY(out+1+numlen, buf, numlen);
  4123. *outLen = 1 + 2*numlen;
  4124. done:
  4125. #ifdef WOLFSSL_SMALL_STACK
  4126. XFREE(buf, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  4127. #endif
  4128. return ret;
  4129. }
  4130. /* export public ECC key in ANSI X9.63 format, extended with
  4131. * compression option */
  4132. int wc_ecc_export_x963_ex(ecc_key* key, byte* out, word32* outLen,
  4133. int compressed)
  4134. {
  4135. if (compressed == 0)
  4136. return wc_ecc_export_x963(key, out, outLen);
  4137. #ifdef HAVE_COMP_KEY
  4138. else
  4139. return wc_ecc_export_x963_compressed(key, out, outLen);
  4140. #else
  4141. return NOT_COMPILED_IN;
  4142. #endif
  4143. }
  4144. #endif /* HAVE_ECC_KEY_EXPORT */
  4145. #ifndef WOLFSSL_ATECC508A
  4146. /* is ecc point on curve described by dp ? */
  4147. int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime)
  4148. {
  4149. int err;
  4150. mp_int t1, t2;
  4151. if ((err = mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  4152. return err;
  4153. }
  4154. /* compute y^2 */
  4155. if (err == MP_OKAY)
  4156. err = mp_sqr(ecp->y, &t1);
  4157. /* compute x^3 */
  4158. if (err == MP_OKAY)
  4159. err = mp_sqr(ecp->x, &t2);
  4160. if (err == MP_OKAY)
  4161. err = mp_mod(&t2, prime, &t2);
  4162. if (err == MP_OKAY)
  4163. err = mp_mul(ecp->x, &t2, &t2);
  4164. /* compute y^2 - x^3 */
  4165. if (err == MP_OKAY)
  4166. err = mp_sub(&t1, &t2, &t1);
  4167. /* Determine if curve "a" should be used in calc */
  4168. #ifdef WOLFSSL_CUSTOM_CURVES
  4169. if (err == MP_OKAY) {
  4170. /* Use a and prime to determine if a == 3 */
  4171. err = mp_set(&t2, 0);
  4172. if (err == MP_OKAY)
  4173. err = mp_submod(prime, a, prime, &t2);
  4174. }
  4175. if (err == MP_OKAY && mp_cmp_d(&t2, 3) != MP_EQ) {
  4176. /* compute y^2 - x^3 + a*x */
  4177. if (err == MP_OKAY)
  4178. err = mp_mulmod(&t2, ecp->x, prime, &t2);
  4179. if (err == MP_OKAY)
  4180. err = mp_addmod(&t1, &t2, prime, &t1);
  4181. }
  4182. else
  4183. #endif /* WOLFSSL_CUSTOM_CURVES */
  4184. {
  4185. /* assumes "a" == 3 */
  4186. (void)a;
  4187. /* compute y^2 - x^3 + 3x */
  4188. if (err == MP_OKAY)
  4189. err = mp_add(&t1, ecp->x, &t1);
  4190. if (err == MP_OKAY)
  4191. err = mp_add(&t1, ecp->x, &t1);
  4192. if (err == MP_OKAY)
  4193. err = mp_add(&t1, ecp->x, &t1);
  4194. if (err == MP_OKAY)
  4195. err = mp_mod(&t1, prime, &t1);
  4196. }
  4197. /* adjust range (0, prime) */
  4198. while (err == MP_OKAY && mp_isneg(&t1)) {
  4199. err = mp_add(&t1, prime, &t1);
  4200. }
  4201. while (err == MP_OKAY && mp_cmp(&t1, prime) != MP_LT) {
  4202. err = mp_sub(&t1, prime, &t1);
  4203. }
  4204. /* compare to b */
  4205. if (err == MP_OKAY) {
  4206. if (mp_cmp(&t1, b) != MP_EQ) {
  4207. err = MP_VAL;
  4208. } else {
  4209. err = MP_OKAY;
  4210. }
  4211. }
  4212. mp_clear(&t1);
  4213. mp_clear(&t2);
  4214. return err;
  4215. }
  4216. /* validate privkey * generator == pubkey, 0 on success */
  4217. static int ecc_check_privkey_gen(ecc_key* key, mp_int* a, mp_int* prime)
  4218. {
  4219. int err = MP_OKAY;
  4220. ecc_point* base = NULL;
  4221. ecc_point* res = NULL;
  4222. DECLARE_CURVE_SPECS(2)
  4223. if (key == NULL)
  4224. return BAD_FUNC_ARG;
  4225. res = wc_ecc_new_point_h(key->heap);
  4226. if (res == NULL)
  4227. err = MEMORY_E;
  4228. #ifdef WOLFSSL_HAVE_SP_ECC
  4229. #ifndef WOLFSSL_SP_NO_256
  4230. if (key->idx != ECC_CUSTOM_IDX && ecc_sets[key->idx].id == ECC_SECP256R1) {
  4231. if (err == MP_OKAY)
  4232. err = sp_ecc_mulmod_base_256(&key->k, res, 1, key->heap);
  4233. }
  4234. else
  4235. #endif
  4236. #endif
  4237. {
  4238. base = wc_ecc_new_point_h(key->heap);
  4239. if (base == NULL)
  4240. err = MEMORY_E;
  4241. if (err == MP_OKAY) {
  4242. /* load curve info */
  4243. err = wc_ecc_curve_load(key->dp, &curve,
  4244. (ECC_CURVE_FIELD_GX | ECC_CURVE_FIELD_GY));
  4245. }
  4246. /* set up base generator */
  4247. if (err == MP_OKAY)
  4248. err = mp_copy(curve->Gx, base->x);
  4249. if (err == MP_OKAY)
  4250. err = mp_copy(curve->Gy, base->y);
  4251. if (err == MP_OKAY)
  4252. err = mp_set(base->z, 1);
  4253. if (err == MP_OKAY)
  4254. err = wc_ecc_mulmod_ex(&key->k, base, res, a, prime, 1, key->heap);
  4255. }
  4256. if (err == MP_OKAY) {
  4257. /* compare result to public key */
  4258. if (mp_cmp(res->x, key->pubkey.x) != MP_EQ ||
  4259. mp_cmp(res->y, key->pubkey.y) != MP_EQ ||
  4260. mp_cmp(res->z, key->pubkey.z) != MP_EQ) {
  4261. /* didn't match */
  4262. err = ECC_PRIV_KEY_E;
  4263. }
  4264. }
  4265. wc_ecc_curve_free(curve);
  4266. wc_ecc_del_point_h(res, key->heap);
  4267. wc_ecc_del_point_h(base, key->heap);
  4268. return err;
  4269. }
  4270. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  4271. /* check privkey generator helper, creates prime needed */
  4272. static int ecc_check_privkey_gen_helper(ecc_key* key)
  4273. {
  4274. int err;
  4275. #ifndef WOLFSSL_ATECC508A
  4276. DECLARE_CURVE_SPECS(2)
  4277. #endif
  4278. if (key == NULL)
  4279. return BAD_FUNC_ARG;
  4280. #ifdef WOLFSSL_ATECC508A
  4281. /* TODO: Implement equiv call to ATECC508A */
  4282. err = BAD_COND_E;
  4283. #else
  4284. /* load curve info */
  4285. err = wc_ecc_curve_load(key->dp, &curve,
  4286. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF));
  4287. if (err == MP_OKAY)
  4288. err = ecc_check_privkey_gen(key, curve->Af, curve->prime);
  4289. wc_ecc_curve_free(curve);
  4290. #endif /* WOLFSSL_ATECC508A */
  4291. return err;
  4292. }
  4293. #endif /* WOLFSSL_VALIDATE_ECC_IMPORT */
  4294. /* validate order * pubkey = point at infinity, 0 on success */
  4295. static int ecc_check_pubkey_order(ecc_key* key, ecc_point* pubkey, mp_int* a,
  4296. mp_int* prime, mp_int* order)
  4297. {
  4298. ecc_point* inf = NULL;
  4299. int err;
  4300. if (key == NULL)
  4301. return BAD_FUNC_ARG;
  4302. inf = wc_ecc_new_point_h(key->heap);
  4303. if (inf == NULL)
  4304. err = MEMORY_E;
  4305. else {
  4306. #ifdef WOLFSSL_HAVE_SP_ECC
  4307. #ifndef WOLFSSL_SP_NO_256
  4308. if (key->idx != ECC_CUSTOM_IDX &&
  4309. ecc_sets[key->idx].id == ECC_SECP256R1) {
  4310. err = sp_ecc_mulmod_256(order, pubkey, inf, 1, key->heap);
  4311. }
  4312. else
  4313. #endif
  4314. #endif
  4315. err = wc_ecc_mulmod_ex(order, pubkey, inf, a, prime, 1, key->heap);
  4316. if (err == MP_OKAY && !wc_ecc_point_is_at_infinity(inf))
  4317. err = ECC_INF_E;
  4318. }
  4319. wc_ecc_del_point_h(inf, key->heap);
  4320. return err;
  4321. }
  4322. #endif /* !WOLFSSL_ATECC508A */
  4323. /* perform sanity checks on ecc key validity, 0 on success */
  4324. int wc_ecc_check_key(ecc_key* key)
  4325. {
  4326. int err;
  4327. #ifndef WOLFSSL_ATECC508A
  4328. mp_int* b;
  4329. #ifdef USE_ECC_B_PARAM
  4330. DECLARE_CURVE_SPECS(4)
  4331. #else
  4332. mp_int b_lcl;
  4333. DECLARE_CURVE_SPECS(3)
  4334. b = &b_lcl;
  4335. XMEMSET(b, 0, sizeof(mp_int));
  4336. #endif
  4337. #endif /* WOLFSSL_ATECC508A */
  4338. if (key == NULL)
  4339. return BAD_FUNC_ARG;
  4340. #ifdef WOLFSSL_ATECC508A
  4341. if (key->slot == ATECC_INVALID_SLOT)
  4342. return ECC_BAD_ARG_E;
  4343. err = 0; /* consider key check success on ECC508A */
  4344. #else
  4345. /* pubkey point cannot be at infinity */
  4346. if (wc_ecc_point_is_at_infinity(&key->pubkey))
  4347. return ECC_INF_E;
  4348. /* load curve info */
  4349. err = wc_ecc_curve_load(key->dp, &curve, (ECC_CURVE_FIELD_PRIME |
  4350. ECC_CURVE_FIELD_AF | ECC_CURVE_FIELD_ORDER
  4351. #ifdef USE_ECC_B_PARAM
  4352. | ECC_CURVE_FIELD_BF
  4353. #endif
  4354. ));
  4355. #ifndef USE_ECC_B_PARAM
  4356. /* load curve b parameter */
  4357. if (err == MP_OKAY)
  4358. err = mp_init(b);
  4359. if (err == MP_OKAY)
  4360. err = mp_read_radix(b, key->dp->Bf, MP_RADIX_HEX);
  4361. #else
  4362. b = curve->Bf;
  4363. #endif
  4364. /* Qx must be in the range [0, p-1] */
  4365. if (mp_cmp(key->pubkey.x, curve->prime) != MP_LT)
  4366. err = ECC_OUT_OF_RANGE_E;
  4367. /* Qy must be in the range [0, p-1] */
  4368. if (mp_cmp(key->pubkey.y, curve->prime) != MP_LT)
  4369. err = ECC_OUT_OF_RANGE_E;
  4370. /* make sure point is actually on curve */
  4371. if (err == MP_OKAY)
  4372. err = wc_ecc_is_point(&key->pubkey, curve->Af, b, curve->prime);
  4373. /* pubkey * order must be at infinity */
  4374. if (err == MP_OKAY)
  4375. err = ecc_check_pubkey_order(key, &key->pubkey, curve->Af, curve->prime,
  4376. curve->order);
  4377. /* private * base generator must equal pubkey */
  4378. if (err == MP_OKAY && key->type == ECC_PRIVATEKEY)
  4379. err = ecc_check_privkey_gen(key, curve->Af, curve->prime);
  4380. wc_ecc_curve_free(curve);
  4381. #ifndef USE_ECC_B_PARAM
  4382. mp_clear(b);
  4383. #endif
  4384. #endif /* WOLFSSL_ATECC508A */
  4385. return err;
  4386. }
  4387. #ifdef HAVE_ECC_KEY_IMPORT
  4388. /* import public ECC key in ANSI X9.63 format */
  4389. int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
  4390. int curve_id)
  4391. {
  4392. int err = MP_OKAY;
  4393. int compressed = 0;
  4394. if (in == NULL || key == NULL)
  4395. return BAD_FUNC_ARG;
  4396. /* must be odd */
  4397. if ((inLen & 1) == 0) {
  4398. return ECC_BAD_ARG_E;
  4399. }
  4400. /* make sure required variables are reset */
  4401. wc_ecc_reset(key);
  4402. /* init key */
  4403. #ifdef ALT_ECC_SIZE
  4404. key->pubkey.x = (mp_int*)&key->pubkey.xyz[0];
  4405. key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
  4406. key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
  4407. alt_fp_init(key->pubkey.x);
  4408. alt_fp_init(key->pubkey.y);
  4409. alt_fp_init(key->pubkey.z);
  4410. err = mp_init(&key->k);
  4411. #else
  4412. err = mp_init_multi(&key->k,
  4413. key->pubkey.x, key->pubkey.y, key->pubkey.z, NULL, NULL);
  4414. #endif
  4415. if (err != MP_OKAY)
  4416. return MEMORY_E;
  4417. /* check for 4, 2, or 3 */
  4418. if (in[0] != 0x04 && in[0] != 0x02 && in[0] != 0x03) {
  4419. err = ASN_PARSE_E;
  4420. }
  4421. if (in[0] == 0x02 || in[0] == 0x03) {
  4422. #ifdef HAVE_COMP_KEY
  4423. compressed = 1;
  4424. #else
  4425. err = NOT_COMPILED_IN;
  4426. #endif
  4427. }
  4428. if (err == MP_OKAY) {
  4429. int keysize;
  4430. #ifdef HAVE_COMP_KEY
  4431. /* adjust inLen if compressed */
  4432. if (compressed)
  4433. inLen = (inLen-1)*2 + 1; /* used uncompressed len */
  4434. #endif
  4435. /* determine key size */
  4436. keysize = ((inLen-1)>>1);
  4437. err = wc_ecc_set_curve(key, keysize, curve_id);
  4438. key->type = ECC_PUBLICKEY;
  4439. }
  4440. /* read data */
  4441. if (err == MP_OKAY)
  4442. err = mp_read_unsigned_bin(key->pubkey.x, (byte*)in+1, (inLen-1)>>1);
  4443. #ifdef HAVE_COMP_KEY
  4444. if (err == MP_OKAY && compressed == 1) { /* build y */
  4445. mp_int t1, t2;
  4446. int did_init = 0;
  4447. DECLARE_CURVE_SPECS(3)
  4448. if (mp_init_multi(&t1, &t2, NULL, NULL, NULL, NULL) != MP_OKAY)
  4449. err = MEMORY_E;
  4450. else
  4451. did_init = 1;
  4452. /* load curve info */
  4453. if (err == MP_OKAY)
  4454. err = wc_ecc_curve_load(key->dp, &curve,
  4455. (ECC_CURVE_FIELD_PRIME | ECC_CURVE_FIELD_AF |
  4456. ECC_CURVE_FIELD_BF));
  4457. /* compute x^3 */
  4458. if (err == MP_OKAY)
  4459. err = mp_sqr(key->pubkey.x, &t1);
  4460. if (err == MP_OKAY)
  4461. err = mp_mulmod(&t1, key->pubkey.x, curve->prime, &t1);
  4462. /* compute x^3 + a*x */
  4463. if (err == MP_OKAY)
  4464. err = mp_mulmod(curve->Af, key->pubkey.x, curve->prime, &t2);
  4465. if (err == MP_OKAY)
  4466. err = mp_add(&t1, &t2, &t1);
  4467. /* compute x^3 + a*x + b */
  4468. if (err == MP_OKAY)
  4469. err = mp_add(&t1, curve->Bf, &t1);
  4470. /* compute sqrt(x^3 + a*x + b) */
  4471. if (err == MP_OKAY)
  4472. err = mp_sqrtmod_prime(&t1, curve->prime, &t2);
  4473. /* adjust y */
  4474. if (err == MP_OKAY) {
  4475. if ((mp_isodd(&t2) == MP_YES && in[0] == 0x03) ||
  4476. (mp_isodd(&t2) == MP_NO && in[0] == 0x02)) {
  4477. err = mp_mod(&t2, curve->prime, &t2);
  4478. }
  4479. else {
  4480. err = mp_submod(curve->prime, &t2, curve->prime, &t2);
  4481. }
  4482. if (err == MP_OKAY)
  4483. err = mp_copy(&t2, key->pubkey.y);
  4484. }
  4485. if (did_init) {
  4486. mp_clear(&t2);
  4487. mp_clear(&t1);
  4488. }
  4489. wc_ecc_curve_free(curve);
  4490. }
  4491. #endif /* HAVE_COMP_KEY */
  4492. if (err == MP_OKAY && compressed == 0)
  4493. err = mp_read_unsigned_bin(key->pubkey.y, (byte*)in+1+((inLen-1)>>1),
  4494. (inLen-1)>>1);
  4495. if (err == MP_OKAY)
  4496. err = mp_set(key->pubkey.z, 1);
  4497. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  4498. if (err == MP_OKAY)
  4499. err = wc_ecc_check_key(key);
  4500. #endif
  4501. if (err != MP_OKAY) {
  4502. mp_clear(key->pubkey.x);
  4503. mp_clear(key->pubkey.y);
  4504. mp_clear(key->pubkey.z);
  4505. mp_clear(&key->k);
  4506. }
  4507. return err;
  4508. }
  4509. int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key)
  4510. {
  4511. return wc_ecc_import_x963_ex(in, inLen, key, ECC_CURVE_DEF);
  4512. }
  4513. #endif /* HAVE_ECC_KEY_IMPORT */
  4514. #ifdef HAVE_ECC_KEY_EXPORT
  4515. /* export ecc private key only raw, outLen is in/out size
  4516. return MP_OKAY on success */
  4517. int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen)
  4518. {
  4519. word32 numlen;
  4520. if (key == NULL || out == NULL || outLen == NULL) {
  4521. return BAD_FUNC_ARG;
  4522. }
  4523. if (wc_ecc_is_valid_idx(key->idx) == 0) {
  4524. return ECC_BAD_ARG_E;
  4525. }
  4526. numlen = key->dp->size;
  4527. if (*outLen < numlen) {
  4528. *outLen = numlen;
  4529. return BUFFER_E;
  4530. }
  4531. *outLen = numlen;
  4532. XMEMSET(out, 0, *outLen);
  4533. #ifdef WOLFSSL_ATECC508A
  4534. /* TODO: Implement equiv call to ATECC508A */
  4535. return BAD_COND_E;
  4536. #else
  4537. return mp_to_unsigned_bin(&key->k, out + (numlen -
  4538. mp_unsigned_bin_size(&key->k)));
  4539. #endif /* WOLFSSL_ATECC508A */
  4540. }
  4541. /* export ecc key to component form, d is optional if only exporting public
  4542. * return MP_OKAY on success */
  4543. static int wc_ecc_export_raw(ecc_key* key, byte* qx, word32* qxLen,
  4544. byte* qy, word32* qyLen, byte* d, word32* dLen)
  4545. {
  4546. int err;
  4547. byte exportPriv = 0;
  4548. word32 numLen;
  4549. if (key == NULL || qx == NULL || qxLen == NULL || qy == NULL ||
  4550. qyLen == NULL) {
  4551. return BAD_FUNC_ARG;
  4552. }
  4553. if (key->type == ECC_PRIVATEKEY_ONLY) {
  4554. return ECC_PRIVATEONLY_E;
  4555. }
  4556. if (wc_ecc_is_valid_idx(key->idx) == 0) {
  4557. return ECC_BAD_ARG_E;
  4558. }
  4559. numLen = key->dp->size;
  4560. if (d != NULL) {
  4561. if (dLen == NULL || key->type != ECC_PRIVATEKEY)
  4562. return BAD_FUNC_ARG;
  4563. exportPriv = 1;
  4564. }
  4565. /* check public buffer sizes */
  4566. if ((*qxLen < numLen) || (*qyLen < numLen)) {
  4567. *qxLen = numLen;
  4568. *qyLen = numLen;
  4569. return BUFFER_E;
  4570. }
  4571. *qxLen = numLen;
  4572. *qyLen = numLen;
  4573. XMEMSET(qx, 0, *qxLen);
  4574. XMEMSET(qy, 0, *qyLen);
  4575. /* private d component */
  4576. if (exportPriv == 1) {
  4577. /* check private buffer size */
  4578. if (*dLen < numLen) {
  4579. *dLen = numLen;
  4580. return BUFFER_E;
  4581. }
  4582. *dLen = numLen;
  4583. XMEMSET(d, 0, *dLen);
  4584. #ifdef WOLFSSL_ATECC508A
  4585. /* TODO: Implement equiv call to ATECC508A */
  4586. return BAD_COND_E;
  4587. #else
  4588. /* private key, d */
  4589. err = mp_to_unsigned_bin(&key->k, d +
  4590. (numLen - mp_unsigned_bin_size(&key->k)));
  4591. if (err != MP_OKAY)
  4592. return err;
  4593. #endif /* WOLFSSL_ATECC508A */
  4594. }
  4595. /* public x component */
  4596. err = mp_to_unsigned_bin(key->pubkey.x, qx +
  4597. (numLen - mp_unsigned_bin_size(key->pubkey.x)));
  4598. if (err != MP_OKAY)
  4599. return err;
  4600. /* public y component */
  4601. err = mp_to_unsigned_bin(key->pubkey.y, qy +
  4602. (numLen - mp_unsigned_bin_size(key->pubkey.y)));
  4603. if (err != MP_OKAY)
  4604. return err;
  4605. return 0;
  4606. }
  4607. /* export public key to raw elements including public (Qx,Qy)
  4608. * return MP_OKAY on success, negative on error */
  4609. int wc_ecc_export_public_raw(ecc_key* key, byte* qx, word32* qxLen,
  4610. byte* qy, word32* qyLen)
  4611. {
  4612. return wc_ecc_export_raw(key, qx, qxLen, qy, qyLen, NULL, NULL);
  4613. }
  4614. /* export ecc key to raw elements including public (Qx,Qy) and private (d)
  4615. * return MP_OKAY on success, negative on error */
  4616. int wc_ecc_export_private_raw(ecc_key* key, byte* qx, word32* qxLen,
  4617. byte* qy, word32* qyLen, byte* d, word32* dLen)
  4618. {
  4619. /* sanitize d and dLen, other args are checked later */
  4620. if (d == NULL || dLen == NULL)
  4621. return BAD_FUNC_ARG;
  4622. return wc_ecc_export_raw(key, qx, qxLen, qy, qyLen, d, dLen);
  4623. }
  4624. #endif /* HAVE_ECC_KEY_EXPORT */
  4625. #ifdef HAVE_ECC_KEY_IMPORT
  4626. /* import private key, public part optional if (pub) passed as NULL */
  4627. int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz,
  4628. const byte* pub, word32 pubSz, ecc_key* key,
  4629. int curve_id)
  4630. {
  4631. int ret;
  4632. if (key == NULL || priv == NULL)
  4633. return BAD_FUNC_ARG;
  4634. /* public optional, NULL if only importing private */
  4635. if (pub != NULL) {
  4636. ret = wc_ecc_import_x963_ex(pub, pubSz, key, curve_id);
  4637. key->type = ECC_PRIVATEKEY;
  4638. }
  4639. else {
  4640. /* make sure required variables are reset */
  4641. wc_ecc_reset(key);
  4642. /* set key size */
  4643. ret = wc_ecc_set_curve(key, privSz, curve_id);
  4644. key->type = ECC_PRIVATEKEY_ONLY;
  4645. }
  4646. if (ret != 0)
  4647. return ret;
  4648. #ifdef WOLFSSL_ATECC508A
  4649. /* TODO: Implement equiv call to ATECC508A */
  4650. return BAD_COND_E;
  4651. #else
  4652. ret = mp_read_unsigned_bin(&key->k, priv, privSz);
  4653. #endif /* WOLFSSL_ATECC508A */
  4654. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  4655. if ((pub != NULL) && (ret == MP_OKAY))
  4656. /* public key needed to perform key validation */
  4657. ret = ecc_check_privkey_gen_helper(key);
  4658. #endif
  4659. return ret;
  4660. }
  4661. /* ecc private key import, public key in ANSI X9.63 format, private raw */
  4662. int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
  4663. word32 pubSz, ecc_key* key)
  4664. {
  4665. return wc_ecc_import_private_key_ex(priv, privSz, pub, pubSz, key,
  4666. ECC_CURVE_DEF);
  4667. }
  4668. #endif /* HAVE_ECC_KEY_IMPORT */
  4669. #ifndef NO_ASN
  4670. /**
  4671. Convert ECC R,S to signature
  4672. r R component of signature
  4673. s S component of signature
  4674. out DER-encoded ECDSA signature
  4675. outlen [in/out] output buffer size, output signature size
  4676. return MP_OKAY on success
  4677. */
  4678. int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen)
  4679. {
  4680. int err;
  4681. mp_int rtmp;
  4682. mp_int stmp;
  4683. if (r == NULL || s == NULL || out == NULL || outlen == NULL)
  4684. return ECC_BAD_ARG_E;
  4685. err = mp_init_multi(&rtmp, &stmp, NULL, NULL, NULL, NULL);
  4686. if (err != MP_OKAY)
  4687. return err;
  4688. err = mp_read_radix(&rtmp, r, MP_RADIX_HEX);
  4689. if (err == MP_OKAY)
  4690. err = mp_read_radix(&stmp, s, MP_RADIX_HEX);
  4691. /* convert mp_ints to ECDSA sig, initializes rtmp and stmp internally */
  4692. if (err == MP_OKAY)
  4693. err = StoreECC_DSA_Sig(out, outlen, &rtmp, &stmp);
  4694. if (err == MP_OKAY) {
  4695. if (mp_iszero(&rtmp) == MP_YES || mp_iszero(&stmp) == MP_YES)
  4696. err = MP_ZERO_E;
  4697. }
  4698. mp_clear(&rtmp);
  4699. mp_clear(&stmp);
  4700. return err;
  4701. }
  4702. /**
  4703. Convert ECC signature to R,S
  4704. sig DER-encoded ECDSA signature
  4705. sigLen length of signature in octets
  4706. r R component of signature
  4707. rLen [in/out] output "r" buffer size, output "r" size
  4708. s S component of signature
  4709. sLen [in/out] output "s" buffer size, output "s" size
  4710. return MP_OKAY on success, negative on error
  4711. */
  4712. int wc_ecc_sig_to_rs(const byte* sig, word32 sigLen, byte* r, word32* rLen,
  4713. byte* s, word32* sLen)
  4714. {
  4715. int err;
  4716. word32 x = 0;
  4717. mp_int rtmp;
  4718. mp_int stmp;
  4719. if (sig == NULL || r == NULL || rLen == NULL || s == NULL || sLen == NULL)
  4720. return ECC_BAD_ARG_E;
  4721. err = DecodeECC_DSA_Sig(sig, sigLen, &rtmp, &stmp);
  4722. /* extract r */
  4723. if (err == MP_OKAY) {
  4724. x = mp_unsigned_bin_size(&rtmp);
  4725. if (*rLen < x)
  4726. err = BUFFER_E;
  4727. if (err == MP_OKAY) {
  4728. *rLen = x;
  4729. err = mp_to_unsigned_bin(&rtmp, r);
  4730. }
  4731. }
  4732. /* extract s */
  4733. if (err == MP_OKAY) {
  4734. x = mp_unsigned_bin_size(&stmp);
  4735. if (*sLen < x)
  4736. err = BUFFER_E;
  4737. if (err == MP_OKAY) {
  4738. *sLen = x;
  4739. err = mp_to_unsigned_bin(&stmp, s);
  4740. }
  4741. }
  4742. mp_clear(&rtmp);
  4743. mp_clear(&stmp);
  4744. return err;
  4745. }
  4746. #endif /* !NO_ASN */
  4747. #ifdef HAVE_ECC_KEY_IMPORT
  4748. static int wc_ecc_import_raw_private(ecc_key* key, const char* qx,
  4749. const char* qy, const char* d, int curve_id)
  4750. {
  4751. int err = MP_OKAY;
  4752. /* if d is NULL, only import as public key using Qx,Qy */
  4753. if (key == NULL || qx == NULL || qy == NULL) {
  4754. return BAD_FUNC_ARG;
  4755. }
  4756. /* make sure required variables are reset */
  4757. wc_ecc_reset(key);
  4758. /* set curve type and index */
  4759. err = wc_ecc_set_curve(key, 0, curve_id);
  4760. if (err != 0) {
  4761. return err;
  4762. }
  4763. #ifdef WOLFSSL_ATECC508A
  4764. /* TODO: Implement equiv call to ATECC508A */
  4765. err = BAD_COND_E;
  4766. #else
  4767. /* init key */
  4768. #ifdef ALT_ECC_SIZE
  4769. key->pubkey.x = (mp_int*)&key->pubkey.xyz[0];
  4770. key->pubkey.y = (mp_int*)&key->pubkey.xyz[1];
  4771. key->pubkey.z = (mp_int*)&key->pubkey.xyz[2];
  4772. alt_fp_init(key->pubkey.x);
  4773. alt_fp_init(key->pubkey.y);
  4774. alt_fp_init(key->pubkey.z);
  4775. err = mp_init(&key->k);
  4776. #else
  4777. err = mp_init_multi(&key->k, key->pubkey.x, key->pubkey.y, key->pubkey.z,
  4778. NULL, NULL);
  4779. #endif
  4780. if (err != MP_OKAY)
  4781. return MEMORY_E;
  4782. /* read Qx */
  4783. if (err == MP_OKAY)
  4784. err = mp_read_radix(key->pubkey.x, qx, MP_RADIX_HEX);
  4785. /* read Qy */
  4786. if (err == MP_OKAY)
  4787. err = mp_read_radix(key->pubkey.y, qy, MP_RADIX_HEX);
  4788. if (err == MP_OKAY)
  4789. err = mp_set(key->pubkey.z, 1);
  4790. /* import private key */
  4791. if (err == MP_OKAY) {
  4792. if (d != NULL) {
  4793. key->type = ECC_PRIVATEKEY;
  4794. err = mp_read_radix(&key->k, d, MP_RADIX_HEX);
  4795. } else {
  4796. key->type = ECC_PUBLICKEY;
  4797. }
  4798. }
  4799. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  4800. if (err == MP_OKAY)
  4801. err = wc_ecc_check_key(key);
  4802. #endif
  4803. if (err != MP_OKAY) {
  4804. mp_clear(key->pubkey.x);
  4805. mp_clear(key->pubkey.y);
  4806. mp_clear(key->pubkey.z);
  4807. mp_clear(&key->k);
  4808. }
  4809. #endif /* WOLFSSL_ATECC508A */
  4810. return err;
  4811. }
  4812. /**
  4813. Import raw ECC key
  4814. key The destination ecc_key structure
  4815. qx x component of the public key, as ASCII hex string
  4816. qy y component of the public key, as ASCII hex string
  4817. d private key, as ASCII hex string, optional if importing public
  4818. key only
  4819. dp Custom ecc_set_type
  4820. return MP_OKAY on success
  4821. */
  4822. int wc_ecc_import_raw_ex(ecc_key* key, const char* qx, const char* qy,
  4823. const char* d, int curve_id)
  4824. {
  4825. return wc_ecc_import_raw_private(key, qx, qy, d, curve_id);
  4826. }
  4827. /**
  4828. Import raw ECC key
  4829. key The destination ecc_key structure
  4830. qx x component of the public key, as ASCII hex string
  4831. qy y component of the public key, as ASCII hex string
  4832. d private key, as ASCII hex string, optional if importing public
  4833. key only
  4834. curveName ECC curve name, from ecc_sets[]
  4835. return MP_OKAY on success
  4836. */
  4837. int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
  4838. const char* d, const char* curveName)
  4839. {
  4840. int err, x;
  4841. /* if d is NULL, only import as public key using Qx,Qy */
  4842. if (key == NULL || qx == NULL || qy == NULL || curveName == NULL) {
  4843. return BAD_FUNC_ARG;
  4844. }
  4845. /* set curve type and index */
  4846. for (x = 0; ecc_sets[x].size != 0; x++) {
  4847. if (XSTRNCMP(ecc_sets[x].name, curveName,
  4848. XSTRLEN(curveName)) == 0) {
  4849. break;
  4850. }
  4851. }
  4852. if (ecc_sets[x].size == 0) {
  4853. WOLFSSL_MSG("ecc_set curve name not found");
  4854. err = ASN_PARSE_E;
  4855. } else {
  4856. return wc_ecc_import_raw_private(key, qx, qy, d, ecc_sets[x].id);
  4857. }
  4858. return err;
  4859. }
  4860. #endif /* HAVE_ECC_KEY_IMPORT */
  4861. /* key size in octets */
  4862. int wc_ecc_size(ecc_key* key)
  4863. {
  4864. if (key == NULL) return 0;
  4865. return key->dp->size;
  4866. }
  4867. /* worst case estimate, check actual return from wc_ecc_sign_hash for actual
  4868. value of signature size in octets */
  4869. int wc_ecc_sig_size(ecc_key* key)
  4870. {
  4871. int sz = wc_ecc_size(key);
  4872. if (sz <= 0)
  4873. return sz;
  4874. return (sz * 2) + SIG_HEADER_SZ + ECC_MAX_PAD_SZ;
  4875. }
  4876. #ifdef FP_ECC
  4877. /* fixed point ECC cache */
  4878. /* number of entries in the cache */
  4879. #ifndef FP_ENTRIES
  4880. #define FP_ENTRIES 15
  4881. #endif
  4882. /* number of bits in LUT */
  4883. #ifndef FP_LUT
  4884. #define FP_LUT 8U
  4885. #endif
  4886. #ifdef ECC_SHAMIR
  4887. /* Sharmir requires a bigger LUT, TAO */
  4888. #if (FP_LUT > 12) || (FP_LUT < 4)
  4889. #error FP_LUT must be between 4 and 12 inclusively
  4890. #endif
  4891. #else
  4892. #if (FP_LUT > 12) || (FP_LUT < 2)
  4893. #error FP_LUT must be between 2 and 12 inclusively
  4894. #endif
  4895. #endif
  4896. /** Our FP cache */
  4897. typedef struct {
  4898. ecc_point* g; /* cached COPY of base point */
  4899. ecc_point* LUT[1U<<FP_LUT]; /* fixed point lookup */
  4900. mp_int mu; /* copy of the montgomery constant */
  4901. int lru_count; /* amount of times this entry has been used */
  4902. int lock; /* flag to indicate cache eviction */
  4903. /* permitted (0) or not (1) */
  4904. } fp_cache_t;
  4905. /* if HAVE_THREAD_LS this cache is per thread, no locking needed */
  4906. static THREAD_LS_T fp_cache_t fp_cache[FP_ENTRIES];
  4907. #ifndef HAVE_THREAD_LS
  4908. static volatile int initMutex = 0; /* prevent multiple mutex inits */
  4909. static wolfSSL_Mutex ecc_fp_lock;
  4910. #endif /* HAVE_THREAD_LS */
  4911. /* simple table to help direct the generation of the LUT */
  4912. static const struct {
  4913. int ham, terma, termb;
  4914. } lut_orders[] = {
  4915. { 0, 0, 0 }, { 1, 0, 0 }, { 1, 0, 0 }, { 2, 1, 2 }, { 1, 0, 0 }, { 2, 1, 4 }, { 2, 2, 4 }, { 3, 3, 4 },
  4916. { 1, 0, 0 }, { 2, 1, 8 }, { 2, 2, 8 }, { 3, 3, 8 }, { 2, 4, 8 }, { 3, 5, 8 }, { 3, 6, 8 }, { 4, 7, 8 },
  4917. { 1, 0, 0 }, { 2, 1, 16 }, { 2, 2, 16 }, { 3, 3, 16 }, { 2, 4, 16 }, { 3, 5, 16 }, { 3, 6, 16 }, { 4, 7, 16 },
  4918. { 2, 8, 16 }, { 3, 9, 16 }, { 3, 10, 16 }, { 4, 11, 16 }, { 3, 12, 16 }, { 4, 13, 16 }, { 4, 14, 16 }, { 5, 15, 16 },
  4919. { 1, 0, 0 }, { 2, 1, 32 }, { 2, 2, 32 }, { 3, 3, 32 }, { 2, 4, 32 }, { 3, 5, 32 }, { 3, 6, 32 }, { 4, 7, 32 },
  4920. { 2, 8, 32 }, { 3, 9, 32 }, { 3, 10, 32 }, { 4, 11, 32 }, { 3, 12, 32 }, { 4, 13, 32 }, { 4, 14, 32 }, { 5, 15, 32 },
  4921. { 2, 16, 32 }, { 3, 17, 32 }, { 3, 18, 32 }, { 4, 19, 32 }, { 3, 20, 32 }, { 4, 21, 32 }, { 4, 22, 32 }, { 5, 23, 32 },
  4922. { 3, 24, 32 }, { 4, 25, 32 }, { 4, 26, 32 }, { 5, 27, 32 }, { 4, 28, 32 }, { 5, 29, 32 }, { 5, 30, 32 }, { 6, 31, 32 },
  4923. #if FP_LUT > 6
  4924. { 1, 0, 0 }, { 2, 1, 64 }, { 2, 2, 64 }, { 3, 3, 64 }, { 2, 4, 64 }, { 3, 5, 64 }, { 3, 6, 64 }, { 4, 7, 64 },
  4925. { 2, 8, 64 }, { 3, 9, 64 }, { 3, 10, 64 }, { 4, 11, 64 }, { 3, 12, 64 }, { 4, 13, 64 }, { 4, 14, 64 }, { 5, 15, 64 },
  4926. { 2, 16, 64 }, { 3, 17, 64 }, { 3, 18, 64 }, { 4, 19, 64 }, { 3, 20, 64 }, { 4, 21, 64 }, { 4, 22, 64 }, { 5, 23, 64 },
  4927. { 3, 24, 64 }, { 4, 25, 64 }, { 4, 26, 64 }, { 5, 27, 64 }, { 4, 28, 64 }, { 5, 29, 64 }, { 5, 30, 64 }, { 6, 31, 64 },
  4928. { 2, 32, 64 }, { 3, 33, 64 }, { 3, 34, 64 }, { 4, 35, 64 }, { 3, 36, 64 }, { 4, 37, 64 }, { 4, 38, 64 }, { 5, 39, 64 },
  4929. { 3, 40, 64 }, { 4, 41, 64 }, { 4, 42, 64 }, { 5, 43, 64 }, { 4, 44, 64 }, { 5, 45, 64 }, { 5, 46, 64 }, { 6, 47, 64 },
  4930. { 3, 48, 64 }, { 4, 49, 64 }, { 4, 50, 64 }, { 5, 51, 64 }, { 4, 52, 64 }, { 5, 53, 64 }, { 5, 54, 64 }, { 6, 55, 64 },
  4931. { 4, 56, 64 }, { 5, 57, 64 }, { 5, 58, 64 }, { 6, 59, 64 }, { 5, 60, 64 }, { 6, 61, 64 }, { 6, 62, 64 }, { 7, 63, 64 },
  4932. #if FP_LUT > 7
  4933. { 1, 0, 0 }, { 2, 1, 128 }, { 2, 2, 128 }, { 3, 3, 128 }, { 2, 4, 128 }, { 3, 5, 128 }, { 3, 6, 128 }, { 4, 7, 128 },
  4934. { 2, 8, 128 }, { 3, 9, 128 }, { 3, 10, 128 }, { 4, 11, 128 }, { 3, 12, 128 }, { 4, 13, 128 }, { 4, 14, 128 }, { 5, 15, 128 },
  4935. { 2, 16, 128 }, { 3, 17, 128 }, { 3, 18, 128 }, { 4, 19, 128 }, { 3, 20, 128 }, { 4, 21, 128 }, { 4, 22, 128 }, { 5, 23, 128 },
  4936. { 3, 24, 128 }, { 4, 25, 128 }, { 4, 26, 128 }, { 5, 27, 128 }, { 4, 28, 128 }, { 5, 29, 128 }, { 5, 30, 128 }, { 6, 31, 128 },
  4937. { 2, 32, 128 }, { 3, 33, 128 }, { 3, 34, 128 }, { 4, 35, 128 }, { 3, 36, 128 }, { 4, 37, 128 }, { 4, 38, 128 }, { 5, 39, 128 },
  4938. { 3, 40, 128 }, { 4, 41, 128 }, { 4, 42, 128 }, { 5, 43, 128 }, { 4, 44, 128 }, { 5, 45, 128 }, { 5, 46, 128 }, { 6, 47, 128 },
  4939. { 3, 48, 128 }, { 4, 49, 128 }, { 4, 50, 128 }, { 5, 51, 128 }, { 4, 52, 128 }, { 5, 53, 128 }, { 5, 54, 128 }, { 6, 55, 128 },
  4940. { 4, 56, 128 }, { 5, 57, 128 }, { 5, 58, 128 }, { 6, 59, 128 }, { 5, 60, 128 }, { 6, 61, 128 }, { 6, 62, 128 }, { 7, 63, 128 },
  4941. { 2, 64, 128 }, { 3, 65, 128 }, { 3, 66, 128 }, { 4, 67, 128 }, { 3, 68, 128 }, { 4, 69, 128 }, { 4, 70, 128 }, { 5, 71, 128 },
  4942. { 3, 72, 128 }, { 4, 73, 128 }, { 4, 74, 128 }, { 5, 75, 128 }, { 4, 76, 128 }, { 5, 77, 128 }, { 5, 78, 128 }, { 6, 79, 128 },
  4943. { 3, 80, 128 }, { 4, 81, 128 }, { 4, 82, 128 }, { 5, 83, 128 }, { 4, 84, 128 }, { 5, 85, 128 }, { 5, 86, 128 }, { 6, 87, 128 },
  4944. { 4, 88, 128 }, { 5, 89, 128 }, { 5, 90, 128 }, { 6, 91, 128 }, { 5, 92, 128 }, { 6, 93, 128 }, { 6, 94, 128 }, { 7, 95, 128 },
  4945. { 3, 96, 128 }, { 4, 97, 128 }, { 4, 98, 128 }, { 5, 99, 128 }, { 4, 100, 128 }, { 5, 101, 128 }, { 5, 102, 128 }, { 6, 103, 128 },
  4946. { 4, 104, 128 }, { 5, 105, 128 }, { 5, 106, 128 }, { 6, 107, 128 }, { 5, 108, 128 }, { 6, 109, 128 }, { 6, 110, 128 }, { 7, 111, 128 },
  4947. { 4, 112, 128 }, { 5, 113, 128 }, { 5, 114, 128 }, { 6, 115, 128 }, { 5, 116, 128 }, { 6, 117, 128 }, { 6, 118, 128 }, { 7, 119, 128 },
  4948. { 5, 120, 128 }, { 6, 121, 128 }, { 6, 122, 128 }, { 7, 123, 128 }, { 6, 124, 128 }, { 7, 125, 128 }, { 7, 126, 128 }, { 8, 127, 128 },
  4949. #if FP_LUT > 8
  4950. { 1, 0, 0 }, { 2, 1, 256 }, { 2, 2, 256 }, { 3, 3, 256 }, { 2, 4, 256 }, { 3, 5, 256 }, { 3, 6, 256 }, { 4, 7, 256 },
  4951. { 2, 8, 256 }, { 3, 9, 256 }, { 3, 10, 256 }, { 4, 11, 256 }, { 3, 12, 256 }, { 4, 13, 256 }, { 4, 14, 256 }, { 5, 15, 256 },
  4952. { 2, 16, 256 }, { 3, 17, 256 }, { 3, 18, 256 }, { 4, 19, 256 }, { 3, 20, 256 }, { 4, 21, 256 }, { 4, 22, 256 }, { 5, 23, 256 },
  4953. { 3, 24, 256 }, { 4, 25, 256 }, { 4, 26, 256 }, { 5, 27, 256 }, { 4, 28, 256 }, { 5, 29, 256 }, { 5, 30, 256 }, { 6, 31, 256 },
  4954. { 2, 32, 256 }, { 3, 33, 256 }, { 3, 34, 256 }, { 4, 35, 256 }, { 3, 36, 256 }, { 4, 37, 256 }, { 4, 38, 256 }, { 5, 39, 256 },
  4955. { 3, 40, 256 }, { 4, 41, 256 }, { 4, 42, 256 }, { 5, 43, 256 }, { 4, 44, 256 }, { 5, 45, 256 }, { 5, 46, 256 }, { 6, 47, 256 },
  4956. { 3, 48, 256 }, { 4, 49, 256 }, { 4, 50, 256 }, { 5, 51, 256 }, { 4, 52, 256 }, { 5, 53, 256 }, { 5, 54, 256 }, { 6, 55, 256 },
  4957. { 4, 56, 256 }, { 5, 57, 256 }, { 5, 58, 256 }, { 6, 59, 256 }, { 5, 60, 256 }, { 6, 61, 256 }, { 6, 62, 256 }, { 7, 63, 256 },
  4958. { 2, 64, 256 }, { 3, 65, 256 }, { 3, 66, 256 }, { 4, 67, 256 }, { 3, 68, 256 }, { 4, 69, 256 }, { 4, 70, 256 }, { 5, 71, 256 },
  4959. { 3, 72, 256 }, { 4, 73, 256 }, { 4, 74, 256 }, { 5, 75, 256 }, { 4, 76, 256 }, { 5, 77, 256 }, { 5, 78, 256 }, { 6, 79, 256 },
  4960. { 3, 80, 256 }, { 4, 81, 256 }, { 4, 82, 256 }, { 5, 83, 256 }, { 4, 84, 256 }, { 5, 85, 256 }, { 5, 86, 256 }, { 6, 87, 256 },
  4961. { 4, 88, 256 }, { 5, 89, 256 }, { 5, 90, 256 }, { 6, 91, 256 }, { 5, 92, 256 }, { 6, 93, 256 }, { 6, 94, 256 }, { 7, 95, 256 },
  4962. { 3, 96, 256 }, { 4, 97, 256 }, { 4, 98, 256 }, { 5, 99, 256 }, { 4, 100, 256 }, { 5, 101, 256 }, { 5, 102, 256 }, { 6, 103, 256 },
  4963. { 4, 104, 256 }, { 5, 105, 256 }, { 5, 106, 256 }, { 6, 107, 256 }, { 5, 108, 256 }, { 6, 109, 256 }, { 6, 110, 256 }, { 7, 111, 256 },
  4964. { 4, 112, 256 }, { 5, 113, 256 }, { 5, 114, 256 }, { 6, 115, 256 }, { 5, 116, 256 }, { 6, 117, 256 }, { 6, 118, 256 }, { 7, 119, 256 },
  4965. { 5, 120, 256 }, { 6, 121, 256 }, { 6, 122, 256 }, { 7, 123, 256 }, { 6, 124, 256 }, { 7, 125, 256 }, { 7, 126, 256 }, { 8, 127, 256 },
  4966. { 2, 128, 256 }, { 3, 129, 256 }, { 3, 130, 256 }, { 4, 131, 256 }, { 3, 132, 256 }, { 4, 133, 256 }, { 4, 134, 256 }, { 5, 135, 256 },
  4967. { 3, 136, 256 }, { 4, 137, 256 }, { 4, 138, 256 }, { 5, 139, 256 }, { 4, 140, 256 }, { 5, 141, 256 }, { 5, 142, 256 }, { 6, 143, 256 },
  4968. { 3, 144, 256 }, { 4, 145, 256 }, { 4, 146, 256 }, { 5, 147, 256 }, { 4, 148, 256 }, { 5, 149, 256 }, { 5, 150, 256 }, { 6, 151, 256 },
  4969. { 4, 152, 256 }, { 5, 153, 256 }, { 5, 154, 256 }, { 6, 155, 256 }, { 5, 156, 256 }, { 6, 157, 256 }, { 6, 158, 256 }, { 7, 159, 256 },
  4970. { 3, 160, 256 }, { 4, 161, 256 }, { 4, 162, 256 }, { 5, 163, 256 }, { 4, 164, 256 }, { 5, 165, 256 }, { 5, 166, 256 }, { 6, 167, 256 },
  4971. { 4, 168, 256 }, { 5, 169, 256 }, { 5, 170, 256 }, { 6, 171, 256 }, { 5, 172, 256 }, { 6, 173, 256 }, { 6, 174, 256 }, { 7, 175, 256 },
  4972. { 4, 176, 256 }, { 5, 177, 256 }, { 5, 178, 256 }, { 6, 179, 256 }, { 5, 180, 256 }, { 6, 181, 256 }, { 6, 182, 256 }, { 7, 183, 256 },
  4973. { 5, 184, 256 }, { 6, 185, 256 }, { 6, 186, 256 }, { 7, 187, 256 }, { 6, 188, 256 }, { 7, 189, 256 }, { 7, 190, 256 }, { 8, 191, 256 },
  4974. { 3, 192, 256 }, { 4, 193, 256 }, { 4, 194, 256 }, { 5, 195, 256 }, { 4, 196, 256 }, { 5, 197, 256 }, { 5, 198, 256 }, { 6, 199, 256 },
  4975. { 4, 200, 256 }, { 5, 201, 256 }, { 5, 202, 256 }, { 6, 203, 256 }, { 5, 204, 256 }, { 6, 205, 256 }, { 6, 206, 256 }, { 7, 207, 256 },
  4976. { 4, 208, 256 }, { 5, 209, 256 }, { 5, 210, 256 }, { 6, 211, 256 }, { 5, 212, 256 }, { 6, 213, 256 }, { 6, 214, 256 }, { 7, 215, 256 },
  4977. { 5, 216, 256 }, { 6, 217, 256 }, { 6, 218, 256 }, { 7, 219, 256 }, { 6, 220, 256 }, { 7, 221, 256 }, { 7, 222, 256 }, { 8, 223, 256 },
  4978. { 4, 224, 256 }, { 5, 225, 256 }, { 5, 226, 256 }, { 6, 227, 256 }, { 5, 228, 256 }, { 6, 229, 256 }, { 6, 230, 256 }, { 7, 231, 256 },
  4979. { 5, 232, 256 }, { 6, 233, 256 }, { 6, 234, 256 }, { 7, 235, 256 }, { 6, 236, 256 }, { 7, 237, 256 }, { 7, 238, 256 }, { 8, 239, 256 },
  4980. { 5, 240, 256 }, { 6, 241, 256 }, { 6, 242, 256 }, { 7, 243, 256 }, { 6, 244, 256 }, { 7, 245, 256 }, { 7, 246, 256 }, { 8, 247, 256 },
  4981. { 6, 248, 256 }, { 7, 249, 256 }, { 7, 250, 256 }, { 8, 251, 256 }, { 7, 252, 256 }, { 8, 253, 256 }, { 8, 254, 256 }, { 9, 255, 256 },
  4982. #if FP_LUT > 9
  4983. { 1, 0, 0 }, { 2, 1, 512 }, { 2, 2, 512 }, { 3, 3, 512 }, { 2, 4, 512 }, { 3, 5, 512 }, { 3, 6, 512 }, { 4, 7, 512 },
  4984. { 2, 8, 512 }, { 3, 9, 512 }, { 3, 10, 512 }, { 4, 11, 512 }, { 3, 12, 512 }, { 4, 13, 512 }, { 4, 14, 512 }, { 5, 15, 512 },
  4985. { 2, 16, 512 }, { 3, 17, 512 }, { 3, 18, 512 }, { 4, 19, 512 }, { 3, 20, 512 }, { 4, 21, 512 }, { 4, 22, 512 }, { 5, 23, 512 },
  4986. { 3, 24, 512 }, { 4, 25, 512 }, { 4, 26, 512 }, { 5, 27, 512 }, { 4, 28, 512 }, { 5, 29, 512 }, { 5, 30, 512 }, { 6, 31, 512 },
  4987. { 2, 32, 512 }, { 3, 33, 512 }, { 3, 34, 512 }, { 4, 35, 512 }, { 3, 36, 512 }, { 4, 37, 512 }, { 4, 38, 512 }, { 5, 39, 512 },
  4988. { 3, 40, 512 }, { 4, 41, 512 }, { 4, 42, 512 }, { 5, 43, 512 }, { 4, 44, 512 }, { 5, 45, 512 }, { 5, 46, 512 }, { 6, 47, 512 },
  4989. { 3, 48, 512 }, { 4, 49, 512 }, { 4, 50, 512 }, { 5, 51, 512 }, { 4, 52, 512 }, { 5, 53, 512 }, { 5, 54, 512 }, { 6, 55, 512 },
  4990. { 4, 56, 512 }, { 5, 57, 512 }, { 5, 58, 512 }, { 6, 59, 512 }, { 5, 60, 512 }, { 6, 61, 512 }, { 6, 62, 512 }, { 7, 63, 512 },
  4991. { 2, 64, 512 }, { 3, 65, 512 }, { 3, 66, 512 }, { 4, 67, 512 }, { 3, 68, 512 }, { 4, 69, 512 }, { 4, 70, 512 }, { 5, 71, 512 },
  4992. { 3, 72, 512 }, { 4, 73, 512 }, { 4, 74, 512 }, { 5, 75, 512 }, { 4, 76, 512 }, { 5, 77, 512 }, { 5, 78, 512 }, { 6, 79, 512 },
  4993. { 3, 80, 512 }, { 4, 81, 512 }, { 4, 82, 512 }, { 5, 83, 512 }, { 4, 84, 512 }, { 5, 85, 512 }, { 5, 86, 512 }, { 6, 87, 512 },
  4994. { 4, 88, 512 }, { 5, 89, 512 }, { 5, 90, 512 }, { 6, 91, 512 }, { 5, 92, 512 }, { 6, 93, 512 }, { 6, 94, 512 }, { 7, 95, 512 },
  4995. { 3, 96, 512 }, { 4, 97, 512 }, { 4, 98, 512 }, { 5, 99, 512 }, { 4, 100, 512 }, { 5, 101, 512 }, { 5, 102, 512 }, { 6, 103, 512 },
  4996. { 4, 104, 512 }, { 5, 105, 512 }, { 5, 106, 512 }, { 6, 107, 512 }, { 5, 108, 512 }, { 6, 109, 512 }, { 6, 110, 512 }, { 7, 111, 512 },
  4997. { 4, 112, 512 }, { 5, 113, 512 }, { 5, 114, 512 }, { 6, 115, 512 }, { 5, 116, 512 }, { 6, 117, 512 }, { 6, 118, 512 }, { 7, 119, 512 },
  4998. { 5, 120, 512 }, { 6, 121, 512 }, { 6, 122, 512 }, { 7, 123, 512 }, { 6, 124, 512 }, { 7, 125, 512 }, { 7, 126, 512 }, { 8, 127, 512 },
  4999. { 2, 128, 512 }, { 3, 129, 512 }, { 3, 130, 512 }, { 4, 131, 512 }, { 3, 132, 512 }, { 4, 133, 512 }, { 4, 134, 512 }, { 5, 135, 512 },
  5000. { 3, 136, 512 }, { 4, 137, 512 }, { 4, 138, 512 }, { 5, 139, 512 }, { 4, 140, 512 }, { 5, 141, 512 }, { 5, 142, 512 }, { 6, 143, 512 },
  5001. { 3, 144, 512 }, { 4, 145, 512 }, { 4, 146, 512 }, { 5, 147, 512 }, { 4, 148, 512 }, { 5, 149, 512 }, { 5, 150, 512 }, { 6, 151, 512 },
  5002. { 4, 152, 512 }, { 5, 153, 512 }, { 5, 154, 512 }, { 6, 155, 512 }, { 5, 156, 512 }, { 6, 157, 512 }, { 6, 158, 512 }, { 7, 159, 512 },
  5003. { 3, 160, 512 }, { 4, 161, 512 }, { 4, 162, 512 }, { 5, 163, 512 }, { 4, 164, 512 }, { 5, 165, 512 }, { 5, 166, 512 }, { 6, 167, 512 },
  5004. { 4, 168, 512 }, { 5, 169, 512 }, { 5, 170, 512 }, { 6, 171, 512 }, { 5, 172, 512 }, { 6, 173, 512 }, { 6, 174, 512 }, { 7, 175, 512 },
  5005. { 4, 176, 512 }, { 5, 177, 512 }, { 5, 178, 512 }, { 6, 179, 512 }, { 5, 180, 512 }, { 6, 181, 512 }, { 6, 182, 512 }, { 7, 183, 512 },
  5006. { 5, 184, 512 }, { 6, 185, 512 }, { 6, 186, 512 }, { 7, 187, 512 }, { 6, 188, 512 }, { 7, 189, 512 }, { 7, 190, 512 }, { 8, 191, 512 },
  5007. { 3, 192, 512 }, { 4, 193, 512 }, { 4, 194, 512 }, { 5, 195, 512 }, { 4, 196, 512 }, { 5, 197, 512 }, { 5, 198, 512 }, { 6, 199, 512 },
  5008. { 4, 200, 512 }, { 5, 201, 512 }, { 5, 202, 512 }, { 6, 203, 512 }, { 5, 204, 512 }, { 6, 205, 512 }, { 6, 206, 512 }, { 7, 207, 512 },
  5009. { 4, 208, 512 }, { 5, 209, 512 }, { 5, 210, 512 }, { 6, 211, 512 }, { 5, 212, 512 }, { 6, 213, 512 }, { 6, 214, 512 }, { 7, 215, 512 },
  5010. { 5, 216, 512 }, { 6, 217, 512 }, { 6, 218, 512 }, { 7, 219, 512 }, { 6, 220, 512 }, { 7, 221, 512 }, { 7, 222, 512 }, { 8, 223, 512 },
  5011. { 4, 224, 512 }, { 5, 225, 512 }, { 5, 226, 512 }, { 6, 227, 512 }, { 5, 228, 512 }, { 6, 229, 512 }, { 6, 230, 512 }, { 7, 231, 512 },
  5012. { 5, 232, 512 }, { 6, 233, 512 }, { 6, 234, 512 }, { 7, 235, 512 }, { 6, 236, 512 }, { 7, 237, 512 }, { 7, 238, 512 }, { 8, 239, 512 },
  5013. { 5, 240, 512 }, { 6, 241, 512 }, { 6, 242, 512 }, { 7, 243, 512 }, { 6, 244, 512 }, { 7, 245, 512 }, { 7, 246, 512 }, { 8, 247, 512 },
  5014. { 6, 248, 512 }, { 7, 249, 512 }, { 7, 250, 512 }, { 8, 251, 512 }, { 7, 252, 512 }, { 8, 253, 512 }, { 8, 254, 512 }, { 9, 255, 512 },
  5015. { 2, 256, 512 }, { 3, 257, 512 }, { 3, 258, 512 }, { 4, 259, 512 }, { 3, 260, 512 }, { 4, 261, 512 }, { 4, 262, 512 }, { 5, 263, 512 },
  5016. { 3, 264, 512 }, { 4, 265, 512 }, { 4, 266, 512 }, { 5, 267, 512 }, { 4, 268, 512 }, { 5, 269, 512 }, { 5, 270, 512 }, { 6, 271, 512 },
  5017. { 3, 272, 512 }, { 4, 273, 512 }, { 4, 274, 512 }, { 5, 275, 512 }, { 4, 276, 512 }, { 5, 277, 512 }, { 5, 278, 512 }, { 6, 279, 512 },
  5018. { 4, 280, 512 }, { 5, 281, 512 }, { 5, 282, 512 }, { 6, 283, 512 }, { 5, 284, 512 }, { 6, 285, 512 }, { 6, 286, 512 }, { 7, 287, 512 },
  5019. { 3, 288, 512 }, { 4, 289, 512 }, { 4, 290, 512 }, { 5, 291, 512 }, { 4, 292, 512 }, { 5, 293, 512 }, { 5, 294, 512 }, { 6, 295, 512 },
  5020. { 4, 296, 512 }, { 5, 297, 512 }, { 5, 298, 512 }, { 6, 299, 512 }, { 5, 300, 512 }, { 6, 301, 512 }, { 6, 302, 512 }, { 7, 303, 512 },
  5021. { 4, 304, 512 }, { 5, 305, 512 }, { 5, 306, 512 }, { 6, 307, 512 }, { 5, 308, 512 }, { 6, 309, 512 }, { 6, 310, 512 }, { 7, 311, 512 },
  5022. { 5, 312, 512 }, { 6, 313, 512 }, { 6, 314, 512 }, { 7, 315, 512 }, { 6, 316, 512 }, { 7, 317, 512 }, { 7, 318, 512 }, { 8, 319, 512 },
  5023. { 3, 320, 512 }, { 4, 321, 512 }, { 4, 322, 512 }, { 5, 323, 512 }, { 4, 324, 512 }, { 5, 325, 512 }, { 5, 326, 512 }, { 6, 327, 512 },
  5024. { 4, 328, 512 }, { 5, 329, 512 }, { 5, 330, 512 }, { 6, 331, 512 }, { 5, 332, 512 }, { 6, 333, 512 }, { 6, 334, 512 }, { 7, 335, 512 },
  5025. { 4, 336, 512 }, { 5, 337, 512 }, { 5, 338, 512 }, { 6, 339, 512 }, { 5, 340, 512 }, { 6, 341, 512 }, { 6, 342, 512 }, { 7, 343, 512 },
  5026. { 5, 344, 512 }, { 6, 345, 512 }, { 6, 346, 512 }, { 7, 347, 512 }, { 6, 348, 512 }, { 7, 349, 512 }, { 7, 350, 512 }, { 8, 351, 512 },
  5027. { 4, 352, 512 }, { 5, 353, 512 }, { 5, 354, 512 }, { 6, 355, 512 }, { 5, 356, 512 }, { 6, 357, 512 }, { 6, 358, 512 }, { 7, 359, 512 },
  5028. { 5, 360, 512 }, { 6, 361, 512 }, { 6, 362, 512 }, { 7, 363, 512 }, { 6, 364, 512 }, { 7, 365, 512 }, { 7, 366, 512 }, { 8, 367, 512 },
  5029. { 5, 368, 512 }, { 6, 369, 512 }, { 6, 370, 512 }, { 7, 371, 512 }, { 6, 372, 512 }, { 7, 373, 512 }, { 7, 374, 512 }, { 8, 375, 512 },
  5030. { 6, 376, 512 }, { 7, 377, 512 }, { 7, 378, 512 }, { 8, 379, 512 }, { 7, 380, 512 }, { 8, 381, 512 }, { 8, 382, 512 }, { 9, 383, 512 },
  5031. { 3, 384, 512 }, { 4, 385, 512 }, { 4, 386, 512 }, { 5, 387, 512 }, { 4, 388, 512 }, { 5, 389, 512 }, { 5, 390, 512 }, { 6, 391, 512 },
  5032. { 4, 392, 512 }, { 5, 393, 512 }, { 5, 394, 512 }, { 6, 395, 512 }, { 5, 396, 512 }, { 6, 397, 512 }, { 6, 398, 512 }, { 7, 399, 512 },
  5033. { 4, 400, 512 }, { 5, 401, 512 }, { 5, 402, 512 }, { 6, 403, 512 }, { 5, 404, 512 }, { 6, 405, 512 }, { 6, 406, 512 }, { 7, 407, 512 },
  5034. { 5, 408, 512 }, { 6, 409, 512 }, { 6, 410, 512 }, { 7, 411, 512 }, { 6, 412, 512 }, { 7, 413, 512 }, { 7, 414, 512 }, { 8, 415, 512 },
  5035. { 4, 416, 512 }, { 5, 417, 512 }, { 5, 418, 512 }, { 6, 419, 512 }, { 5, 420, 512 }, { 6, 421, 512 }, { 6, 422, 512 }, { 7, 423, 512 },
  5036. { 5, 424, 512 }, { 6, 425, 512 }, { 6, 426, 512 }, { 7, 427, 512 }, { 6, 428, 512 }, { 7, 429, 512 }, { 7, 430, 512 }, { 8, 431, 512 },
  5037. { 5, 432, 512 }, { 6, 433, 512 }, { 6, 434, 512 }, { 7, 435, 512 }, { 6, 436, 512 }, { 7, 437, 512 }, { 7, 438, 512 }, { 8, 439, 512 },
  5038. { 6, 440, 512 }, { 7, 441, 512 }, { 7, 442, 512 }, { 8, 443, 512 }, { 7, 444, 512 }, { 8, 445, 512 }, { 8, 446, 512 }, { 9, 447, 512 },
  5039. { 4, 448, 512 }, { 5, 449, 512 }, { 5, 450, 512 }, { 6, 451, 512 }, { 5, 452, 512 }, { 6, 453, 512 }, { 6, 454, 512 }, { 7, 455, 512 },
  5040. { 5, 456, 512 }, { 6, 457, 512 }, { 6, 458, 512 }, { 7, 459, 512 }, { 6, 460, 512 }, { 7, 461, 512 }, { 7, 462, 512 }, { 8, 463, 512 },
  5041. { 5, 464, 512 }, { 6, 465, 512 }, { 6, 466, 512 }, { 7, 467, 512 }, { 6, 468, 512 }, { 7, 469, 512 }, { 7, 470, 512 }, { 8, 471, 512 },
  5042. { 6, 472, 512 }, { 7, 473, 512 }, { 7, 474, 512 }, { 8, 475, 512 }, { 7, 476, 512 }, { 8, 477, 512 }, { 8, 478, 512 }, { 9, 479, 512 },
  5043. { 5, 480, 512 }, { 6, 481, 512 }, { 6, 482, 512 }, { 7, 483, 512 }, { 6, 484, 512 }, { 7, 485, 512 }, { 7, 486, 512 }, { 8, 487, 512 },
  5044. { 6, 488, 512 }, { 7, 489, 512 }, { 7, 490, 512 }, { 8, 491, 512 }, { 7, 492, 512 }, { 8, 493, 512 }, { 8, 494, 512 }, { 9, 495, 512 },
  5045. { 6, 496, 512 }, { 7, 497, 512 }, { 7, 498, 512 }, { 8, 499, 512 }, { 7, 500, 512 }, { 8, 501, 512 }, { 8, 502, 512 }, { 9, 503, 512 },
  5046. { 7, 504, 512 }, { 8, 505, 512 }, { 8, 506, 512 }, { 9, 507, 512 }, { 8, 508, 512 }, { 9, 509, 512 }, { 9, 510, 512 }, { 10, 511, 512 },
  5047. #if FP_LUT > 10
  5048. { 1, 0, 0 }, { 2, 1, 1024 }, { 2, 2, 1024 }, { 3, 3, 1024 }, { 2, 4, 1024 }, { 3, 5, 1024 }, { 3, 6, 1024 }, { 4, 7, 1024 },
  5049. { 2, 8, 1024 }, { 3, 9, 1024 }, { 3, 10, 1024 }, { 4, 11, 1024 }, { 3, 12, 1024 }, { 4, 13, 1024 }, { 4, 14, 1024 }, { 5, 15, 1024 },
  5050. { 2, 16, 1024 }, { 3, 17, 1024 }, { 3, 18, 1024 }, { 4, 19, 1024 }, { 3, 20, 1024 }, { 4, 21, 1024 }, { 4, 22, 1024 }, { 5, 23, 1024 },
  5051. { 3, 24, 1024 }, { 4, 25, 1024 }, { 4, 26, 1024 }, { 5, 27, 1024 }, { 4, 28, 1024 }, { 5, 29, 1024 }, { 5, 30, 1024 }, { 6, 31, 1024 },
  5052. { 2, 32, 1024 }, { 3, 33, 1024 }, { 3, 34, 1024 }, { 4, 35, 1024 }, { 3, 36, 1024 }, { 4, 37, 1024 }, { 4, 38, 1024 }, { 5, 39, 1024 },
  5053. { 3, 40, 1024 }, { 4, 41, 1024 }, { 4, 42, 1024 }, { 5, 43, 1024 }, { 4, 44, 1024 }, { 5, 45, 1024 }, { 5, 46, 1024 }, { 6, 47, 1024 },
  5054. { 3, 48, 1024 }, { 4, 49, 1024 }, { 4, 50, 1024 }, { 5, 51, 1024 }, { 4, 52, 1024 }, { 5, 53, 1024 }, { 5, 54, 1024 }, { 6, 55, 1024 },
  5055. { 4, 56, 1024 }, { 5, 57, 1024 }, { 5, 58, 1024 }, { 6, 59, 1024 }, { 5, 60, 1024 }, { 6, 61, 1024 }, { 6, 62, 1024 }, { 7, 63, 1024 },
  5056. { 2, 64, 1024 }, { 3, 65, 1024 }, { 3, 66, 1024 }, { 4, 67, 1024 }, { 3, 68, 1024 }, { 4, 69, 1024 }, { 4, 70, 1024 }, { 5, 71, 1024 },
  5057. { 3, 72, 1024 }, { 4, 73, 1024 }, { 4, 74, 1024 }, { 5, 75, 1024 }, { 4, 76, 1024 }, { 5, 77, 1024 }, { 5, 78, 1024 }, { 6, 79, 1024 },
  5058. { 3, 80, 1024 }, { 4, 81, 1024 }, { 4, 82, 1024 }, { 5, 83, 1024 }, { 4, 84, 1024 }, { 5, 85, 1024 }, { 5, 86, 1024 }, { 6, 87, 1024 },
  5059. { 4, 88, 1024 }, { 5, 89, 1024 }, { 5, 90, 1024 }, { 6, 91, 1024 }, { 5, 92, 1024 }, { 6, 93, 1024 }, { 6, 94, 1024 }, { 7, 95, 1024 },
  5060. { 3, 96, 1024 }, { 4, 97, 1024 }, { 4, 98, 1024 }, { 5, 99, 1024 }, { 4, 100, 1024 }, { 5, 101, 1024 }, { 5, 102, 1024 }, { 6, 103, 1024 },
  5061. { 4, 104, 1024 }, { 5, 105, 1024 }, { 5, 106, 1024 }, { 6, 107, 1024 }, { 5, 108, 1024 }, { 6, 109, 1024 }, { 6, 110, 1024 }, { 7, 111, 1024 },
  5062. { 4, 112, 1024 }, { 5, 113, 1024 }, { 5, 114, 1024 }, { 6, 115, 1024 }, { 5, 116, 1024 }, { 6, 117, 1024 }, { 6, 118, 1024 }, { 7, 119, 1024 },
  5063. { 5, 120, 1024 }, { 6, 121, 1024 }, { 6, 122, 1024 }, { 7, 123, 1024 }, { 6, 124, 1024 }, { 7, 125, 1024 }, { 7, 126, 1024 }, { 8, 127, 1024 },
  5064. { 2, 128, 1024 }, { 3, 129, 1024 }, { 3, 130, 1024 }, { 4, 131, 1024 }, { 3, 132, 1024 }, { 4, 133, 1024 }, { 4, 134, 1024 }, { 5, 135, 1024 },
  5065. { 3, 136, 1024 }, { 4, 137, 1024 }, { 4, 138, 1024 }, { 5, 139, 1024 }, { 4, 140, 1024 }, { 5, 141, 1024 }, { 5, 142, 1024 }, { 6, 143, 1024 },
  5066. { 3, 144, 1024 }, { 4, 145, 1024 }, { 4, 146, 1024 }, { 5, 147, 1024 }, { 4, 148, 1024 }, { 5, 149, 1024 }, { 5, 150, 1024 }, { 6, 151, 1024 },
  5067. { 4, 152, 1024 }, { 5, 153, 1024 }, { 5, 154, 1024 }, { 6, 155, 1024 }, { 5, 156, 1024 }, { 6, 157, 1024 }, { 6, 158, 1024 }, { 7, 159, 1024 },
  5068. { 3, 160, 1024 }, { 4, 161, 1024 }, { 4, 162, 1024 }, { 5, 163, 1024 }, { 4, 164, 1024 }, { 5, 165, 1024 }, { 5, 166, 1024 }, { 6, 167, 1024 },
  5069. { 4, 168, 1024 }, { 5, 169, 1024 }, { 5, 170, 1024 }, { 6, 171, 1024 }, { 5, 172, 1024 }, { 6, 173, 1024 }, { 6, 174, 1024 }, { 7, 175, 1024 },
  5070. { 4, 176, 1024 }, { 5, 177, 1024 }, { 5, 178, 1024 }, { 6, 179, 1024 }, { 5, 180, 1024 }, { 6, 181, 1024 }, { 6, 182, 1024 }, { 7, 183, 1024 },
  5071. { 5, 184, 1024 }, { 6, 185, 1024 }, { 6, 186, 1024 }, { 7, 187, 1024 }, { 6, 188, 1024 }, { 7, 189, 1024 }, { 7, 190, 1024 }, { 8, 191, 1024 },
  5072. { 3, 192, 1024 }, { 4, 193, 1024 }, { 4, 194, 1024 }, { 5, 195, 1024 }, { 4, 196, 1024 }, { 5, 197, 1024 }, { 5, 198, 1024 }, { 6, 199, 1024 },
  5073. { 4, 200, 1024 }, { 5, 201, 1024 }, { 5, 202, 1024 }, { 6, 203, 1024 }, { 5, 204, 1024 }, { 6, 205, 1024 }, { 6, 206, 1024 }, { 7, 207, 1024 },
  5074. { 4, 208, 1024 }, { 5, 209, 1024 }, { 5, 210, 1024 }, { 6, 211, 1024 }, { 5, 212, 1024 }, { 6, 213, 1024 }, { 6, 214, 1024 }, { 7, 215, 1024 },
  5075. { 5, 216, 1024 }, { 6, 217, 1024 }, { 6, 218, 1024 }, { 7, 219, 1024 }, { 6, 220, 1024 }, { 7, 221, 1024 }, { 7, 222, 1024 }, { 8, 223, 1024 },
  5076. { 4, 224, 1024 }, { 5, 225, 1024 }, { 5, 226, 1024 }, { 6, 227, 1024 }, { 5, 228, 1024 }, { 6, 229, 1024 }, { 6, 230, 1024 }, { 7, 231, 1024 },
  5077. { 5, 232, 1024 }, { 6, 233, 1024 }, { 6, 234, 1024 }, { 7, 235, 1024 }, { 6, 236, 1024 }, { 7, 237, 1024 }, { 7, 238, 1024 }, { 8, 239, 1024 },
  5078. { 5, 240, 1024 }, { 6, 241, 1024 }, { 6, 242, 1024 }, { 7, 243, 1024 }, { 6, 244, 1024 }, { 7, 245, 1024 }, { 7, 246, 1024 }, { 8, 247, 1024 },
  5079. { 6, 248, 1024 }, { 7, 249, 1024 }, { 7, 250, 1024 }, { 8, 251, 1024 }, { 7, 252, 1024 }, { 8, 253, 1024 }, { 8, 254, 1024 }, { 9, 255, 1024 },
  5080. { 2, 256, 1024 }, { 3, 257, 1024 }, { 3, 258, 1024 }, { 4, 259, 1024 }, { 3, 260, 1024 }, { 4, 261, 1024 }, { 4, 262, 1024 }, { 5, 263, 1024 },
  5081. { 3, 264, 1024 }, { 4, 265, 1024 }, { 4, 266, 1024 }, { 5, 267, 1024 }, { 4, 268, 1024 }, { 5, 269, 1024 }, { 5, 270, 1024 }, { 6, 271, 1024 },
  5082. { 3, 272, 1024 }, { 4, 273, 1024 }, { 4, 274, 1024 }, { 5, 275, 1024 }, { 4, 276, 1024 }, { 5, 277, 1024 }, { 5, 278, 1024 }, { 6, 279, 1024 },
  5083. { 4, 280, 1024 }, { 5, 281, 1024 }, { 5, 282, 1024 }, { 6, 283, 1024 }, { 5, 284, 1024 }, { 6, 285, 1024 }, { 6, 286, 1024 }, { 7, 287, 1024 },
  5084. { 3, 288, 1024 }, { 4, 289, 1024 }, { 4, 290, 1024 }, { 5, 291, 1024 }, { 4, 292, 1024 }, { 5, 293, 1024 }, { 5, 294, 1024 }, { 6, 295, 1024 },
  5085. { 4, 296, 1024 }, { 5, 297, 1024 }, { 5, 298, 1024 }, { 6, 299, 1024 }, { 5, 300, 1024 }, { 6, 301, 1024 }, { 6, 302, 1024 }, { 7, 303, 1024 },
  5086. { 4, 304, 1024 }, { 5, 305, 1024 }, { 5, 306, 1024 }, { 6, 307, 1024 }, { 5, 308, 1024 }, { 6, 309, 1024 }, { 6, 310, 1024 }, { 7, 311, 1024 },
  5087. { 5, 312, 1024 }, { 6, 313, 1024 }, { 6, 314, 1024 }, { 7, 315, 1024 }, { 6, 316, 1024 }, { 7, 317, 1024 }, { 7, 318, 1024 }, { 8, 319, 1024 },
  5088. { 3, 320, 1024 }, { 4, 321, 1024 }, { 4, 322, 1024 }, { 5, 323, 1024 }, { 4, 324, 1024 }, { 5, 325, 1024 }, { 5, 326, 1024 }, { 6, 327, 1024 },
  5089. { 4, 328, 1024 }, { 5, 329, 1024 }, { 5, 330, 1024 }, { 6, 331, 1024 }, { 5, 332, 1024 }, { 6, 333, 1024 }, { 6, 334, 1024 }, { 7, 335, 1024 },
  5090. { 4, 336, 1024 }, { 5, 337, 1024 }, { 5, 338, 1024 }, { 6, 339, 1024 }, { 5, 340, 1024 }, { 6, 341, 1024 }, { 6, 342, 1024 }, { 7, 343, 1024 },
  5091. { 5, 344, 1024 }, { 6, 345, 1024 }, { 6, 346, 1024 }, { 7, 347, 1024 }, { 6, 348, 1024 }, { 7, 349, 1024 }, { 7, 350, 1024 }, { 8, 351, 1024 },
  5092. { 4, 352, 1024 }, { 5, 353, 1024 }, { 5, 354, 1024 }, { 6, 355, 1024 }, { 5, 356, 1024 }, { 6, 357, 1024 }, { 6, 358, 1024 }, { 7, 359, 1024 },
  5093. { 5, 360, 1024 }, { 6, 361, 1024 }, { 6, 362, 1024 }, { 7, 363, 1024 }, { 6, 364, 1024 }, { 7, 365, 1024 }, { 7, 366, 1024 }, { 8, 367, 1024 },
  5094. { 5, 368, 1024 }, { 6, 369, 1024 }, { 6, 370, 1024 }, { 7, 371, 1024 }, { 6, 372, 1024 }, { 7, 373, 1024 }, { 7, 374, 1024 }, { 8, 375, 1024 },
  5095. { 6, 376, 1024 }, { 7, 377, 1024 }, { 7, 378, 1024 }, { 8, 379, 1024 }, { 7, 380, 1024 }, { 8, 381, 1024 }, { 8, 382, 1024 }, { 9, 383, 1024 },
  5096. { 3, 384, 1024 }, { 4, 385, 1024 }, { 4, 386, 1024 }, { 5, 387, 1024 }, { 4, 388, 1024 }, { 5, 389, 1024 }, { 5, 390, 1024 }, { 6, 391, 1024 },
  5097. { 4, 392, 1024 }, { 5, 393, 1024 }, { 5, 394, 1024 }, { 6, 395, 1024 }, { 5, 396, 1024 }, { 6, 397, 1024 }, { 6, 398, 1024 }, { 7, 399, 1024 },
  5098. { 4, 400, 1024 }, { 5, 401, 1024 }, { 5, 402, 1024 }, { 6, 403, 1024 }, { 5, 404, 1024 }, { 6, 405, 1024 }, { 6, 406, 1024 }, { 7, 407, 1024 },
  5099. { 5, 408, 1024 }, { 6, 409, 1024 }, { 6, 410, 1024 }, { 7, 411, 1024 }, { 6, 412, 1024 }, { 7, 413, 1024 }, { 7, 414, 1024 }, { 8, 415, 1024 },
  5100. { 4, 416, 1024 }, { 5, 417, 1024 }, { 5, 418, 1024 }, { 6, 419, 1024 }, { 5, 420, 1024 }, { 6, 421, 1024 }, { 6, 422, 1024 }, { 7, 423, 1024 },
  5101. { 5, 424, 1024 }, { 6, 425, 1024 }, { 6, 426, 1024 }, { 7, 427, 1024 }, { 6, 428, 1024 }, { 7, 429, 1024 }, { 7, 430, 1024 }, { 8, 431, 1024 },
  5102. { 5, 432, 1024 }, { 6, 433, 1024 }, { 6, 434, 1024 }, { 7, 435, 1024 }, { 6, 436, 1024 }, { 7, 437, 1024 }, { 7, 438, 1024 }, { 8, 439, 1024 },
  5103. { 6, 440, 1024 }, { 7, 441, 1024 }, { 7, 442, 1024 }, { 8, 443, 1024 }, { 7, 444, 1024 }, { 8, 445, 1024 }, { 8, 446, 1024 }, { 9, 447, 1024 },
  5104. { 4, 448, 1024 }, { 5, 449, 1024 }, { 5, 450, 1024 }, { 6, 451, 1024 }, { 5, 452, 1024 }, { 6, 453, 1024 }, { 6, 454, 1024 }, { 7, 455, 1024 },
  5105. { 5, 456, 1024 }, { 6, 457, 1024 }, { 6, 458, 1024 }, { 7, 459, 1024 }, { 6, 460, 1024 }, { 7, 461, 1024 }, { 7, 462, 1024 }, { 8, 463, 1024 },
  5106. { 5, 464, 1024 }, { 6, 465, 1024 }, { 6, 466, 1024 }, { 7, 467, 1024 }, { 6, 468, 1024 }, { 7, 469, 1024 }, { 7, 470, 1024 }, { 8, 471, 1024 },
  5107. { 6, 472, 1024 }, { 7, 473, 1024 }, { 7, 474, 1024 }, { 8, 475, 1024 }, { 7, 476, 1024 }, { 8, 477, 1024 }, { 8, 478, 1024 }, { 9, 479, 1024 },
  5108. { 5, 480, 1024 }, { 6, 481, 1024 }, { 6, 482, 1024 }, { 7, 483, 1024 }, { 6, 484, 1024 }, { 7, 485, 1024 }, { 7, 486, 1024 }, { 8, 487, 1024 },
  5109. { 6, 488, 1024 }, { 7, 489, 1024 }, { 7, 490, 1024 }, { 8, 491, 1024 }, { 7, 492, 1024 }, { 8, 493, 1024 }, { 8, 494, 1024 }, { 9, 495, 1024 },
  5110. { 6, 496, 1024 }, { 7, 497, 1024 }, { 7, 498, 1024 }, { 8, 499, 1024 }, { 7, 500, 1024 }, { 8, 501, 1024 }, { 8, 502, 1024 }, { 9, 503, 1024 },
  5111. { 7, 504, 1024 }, { 8, 505, 1024 }, { 8, 506, 1024 }, { 9, 507, 1024 }, { 8, 508, 1024 }, { 9, 509, 1024 }, { 9, 510, 1024 }, { 10, 511, 1024 },
  5112. { 2, 512, 1024 }, { 3, 513, 1024 }, { 3, 514, 1024 }, { 4, 515, 1024 }, { 3, 516, 1024 }, { 4, 517, 1024 }, { 4, 518, 1024 }, { 5, 519, 1024 },
  5113. { 3, 520, 1024 }, { 4, 521, 1024 }, { 4, 522, 1024 }, { 5, 523, 1024 }, { 4, 524, 1024 }, { 5, 525, 1024 }, { 5, 526, 1024 }, { 6, 527, 1024 },
  5114. { 3, 528, 1024 }, { 4, 529, 1024 }, { 4, 530, 1024 }, { 5, 531, 1024 }, { 4, 532, 1024 }, { 5, 533, 1024 }, { 5, 534, 1024 }, { 6, 535, 1024 },
  5115. { 4, 536, 1024 }, { 5, 537, 1024 }, { 5, 538, 1024 }, { 6, 539, 1024 }, { 5, 540, 1024 }, { 6, 541, 1024 }, { 6, 542, 1024 }, { 7, 543, 1024 },
  5116. { 3, 544, 1024 }, { 4, 545, 1024 }, { 4, 546, 1024 }, { 5, 547, 1024 }, { 4, 548, 1024 }, { 5, 549, 1024 }, { 5, 550, 1024 }, { 6, 551, 1024 },
  5117. { 4, 552, 1024 }, { 5, 553, 1024 }, { 5, 554, 1024 }, { 6, 555, 1024 }, { 5, 556, 1024 }, { 6, 557, 1024 }, { 6, 558, 1024 }, { 7, 559, 1024 },
  5118. { 4, 560, 1024 }, { 5, 561, 1024 }, { 5, 562, 1024 }, { 6, 563, 1024 }, { 5, 564, 1024 }, { 6, 565, 1024 }, { 6, 566, 1024 }, { 7, 567, 1024 },
  5119. { 5, 568, 1024 }, { 6, 569, 1024 }, { 6, 570, 1024 }, { 7, 571, 1024 }, { 6, 572, 1024 }, { 7, 573, 1024 }, { 7, 574, 1024 }, { 8, 575, 1024 },
  5120. { 3, 576, 1024 }, { 4, 577, 1024 }, { 4, 578, 1024 }, { 5, 579, 1024 }, { 4, 580, 1024 }, { 5, 581, 1024 }, { 5, 582, 1024 }, { 6, 583, 1024 },
  5121. { 4, 584, 1024 }, { 5, 585, 1024 }, { 5, 586, 1024 }, { 6, 587, 1024 }, { 5, 588, 1024 }, { 6, 589, 1024 }, { 6, 590, 1024 }, { 7, 591, 1024 },
  5122. { 4, 592, 1024 }, { 5, 593, 1024 }, { 5, 594, 1024 }, { 6, 595, 1024 }, { 5, 596, 1024 }, { 6, 597, 1024 }, { 6, 598, 1024 }, { 7, 599, 1024 },
  5123. { 5, 600, 1024 }, { 6, 601, 1024 }, { 6, 602, 1024 }, { 7, 603, 1024 }, { 6, 604, 1024 }, { 7, 605, 1024 }, { 7, 606, 1024 }, { 8, 607, 1024 },
  5124. { 4, 608, 1024 }, { 5, 609, 1024 }, { 5, 610, 1024 }, { 6, 611, 1024 }, { 5, 612, 1024 }, { 6, 613, 1024 }, { 6, 614, 1024 }, { 7, 615, 1024 },
  5125. { 5, 616, 1024 }, { 6, 617, 1024 }, { 6, 618, 1024 }, { 7, 619, 1024 }, { 6, 620, 1024 }, { 7, 621, 1024 }, { 7, 622, 1024 }, { 8, 623, 1024 },
  5126. { 5, 624, 1024 }, { 6, 625, 1024 }, { 6, 626, 1024 }, { 7, 627, 1024 }, { 6, 628, 1024 }, { 7, 629, 1024 }, { 7, 630, 1024 }, { 8, 631, 1024 },
  5127. { 6, 632, 1024 }, { 7, 633, 1024 }, { 7, 634, 1024 }, { 8, 635, 1024 }, { 7, 636, 1024 }, { 8, 637, 1024 }, { 8, 638, 1024 }, { 9, 639, 1024 },
  5128. { 3, 640, 1024 }, { 4, 641, 1024 }, { 4, 642, 1024 }, { 5, 643, 1024 }, { 4, 644, 1024 }, { 5, 645, 1024 }, { 5, 646, 1024 }, { 6, 647, 1024 },
  5129. { 4, 648, 1024 }, { 5, 649, 1024 }, { 5, 650, 1024 }, { 6, 651, 1024 }, { 5, 652, 1024 }, { 6, 653, 1024 }, { 6, 654, 1024 }, { 7, 655, 1024 },
  5130. { 4, 656, 1024 }, { 5, 657, 1024 }, { 5, 658, 1024 }, { 6, 659, 1024 }, { 5, 660, 1024 }, { 6, 661, 1024 }, { 6, 662, 1024 }, { 7, 663, 1024 },
  5131. { 5, 664, 1024 }, { 6, 665, 1024 }, { 6, 666, 1024 }, { 7, 667, 1024 }, { 6, 668, 1024 }, { 7, 669, 1024 }, { 7, 670, 1024 }, { 8, 671, 1024 },
  5132. { 4, 672, 1024 }, { 5, 673, 1024 }, { 5, 674, 1024 }, { 6, 675, 1024 }, { 5, 676, 1024 }, { 6, 677, 1024 }, { 6, 678, 1024 }, { 7, 679, 1024 },
  5133. { 5, 680, 1024 }, { 6, 681, 1024 }, { 6, 682, 1024 }, { 7, 683, 1024 }, { 6, 684, 1024 }, { 7, 685, 1024 }, { 7, 686, 1024 }, { 8, 687, 1024 },
  5134. { 5, 688, 1024 }, { 6, 689, 1024 }, { 6, 690, 1024 }, { 7, 691, 1024 }, { 6, 692, 1024 }, { 7, 693, 1024 }, { 7, 694, 1024 }, { 8, 695, 1024 },
  5135. { 6, 696, 1024 }, { 7, 697, 1024 }, { 7, 698, 1024 }, { 8, 699, 1024 }, { 7, 700, 1024 }, { 8, 701, 1024 }, { 8, 702, 1024 }, { 9, 703, 1024 },
  5136. { 4, 704, 1024 }, { 5, 705, 1024 }, { 5, 706, 1024 }, { 6, 707, 1024 }, { 5, 708, 1024 }, { 6, 709, 1024 }, { 6, 710, 1024 }, { 7, 711, 1024 },
  5137. { 5, 712, 1024 }, { 6, 713, 1024 }, { 6, 714, 1024 }, { 7, 715, 1024 }, { 6, 716, 1024 }, { 7, 717, 1024 }, { 7, 718, 1024 }, { 8, 719, 1024 },
  5138. { 5, 720, 1024 }, { 6, 721, 1024 }, { 6, 722, 1024 }, { 7, 723, 1024 }, { 6, 724, 1024 }, { 7, 725, 1024 }, { 7, 726, 1024 }, { 8, 727, 1024 },
  5139. { 6, 728, 1024 }, { 7, 729, 1024 }, { 7, 730, 1024 }, { 8, 731, 1024 }, { 7, 732, 1024 }, { 8, 733, 1024 }, { 8, 734, 1024 }, { 9, 735, 1024 },
  5140. { 5, 736, 1024 }, { 6, 737, 1024 }, { 6, 738, 1024 }, { 7, 739, 1024 }, { 6, 740, 1024 }, { 7, 741, 1024 }, { 7, 742, 1024 }, { 8, 743, 1024 },
  5141. { 6, 744, 1024 }, { 7, 745, 1024 }, { 7, 746, 1024 }, { 8, 747, 1024 }, { 7, 748, 1024 }, { 8, 749, 1024 }, { 8, 750, 1024 }, { 9, 751, 1024 },
  5142. { 6, 752, 1024 }, { 7, 753, 1024 }, { 7, 754, 1024 }, { 8, 755, 1024 }, { 7, 756, 1024 }, { 8, 757, 1024 }, { 8, 758, 1024 }, { 9, 759, 1024 },
  5143. { 7, 760, 1024 }, { 8, 761, 1024 }, { 8, 762, 1024 }, { 9, 763, 1024 }, { 8, 764, 1024 }, { 9, 765, 1024 }, { 9, 766, 1024 }, { 10, 767, 1024 },
  5144. { 3, 768, 1024 }, { 4, 769, 1024 }, { 4, 770, 1024 }, { 5, 771, 1024 }, { 4, 772, 1024 }, { 5, 773, 1024 }, { 5, 774, 1024 }, { 6, 775, 1024 },
  5145. { 4, 776, 1024 }, { 5, 777, 1024 }, { 5, 778, 1024 }, { 6, 779, 1024 }, { 5, 780, 1024 }, { 6, 781, 1024 }, { 6, 782, 1024 }, { 7, 783, 1024 },
  5146. { 4, 784, 1024 }, { 5, 785, 1024 }, { 5, 786, 1024 }, { 6, 787, 1024 }, { 5, 788, 1024 }, { 6, 789, 1024 }, { 6, 790, 1024 }, { 7, 791, 1024 },
  5147. { 5, 792, 1024 }, { 6, 793, 1024 }, { 6, 794, 1024 }, { 7, 795, 1024 }, { 6, 796, 1024 }, { 7, 797, 1024 }, { 7, 798, 1024 }, { 8, 799, 1024 },
  5148. { 4, 800, 1024 }, { 5, 801, 1024 }, { 5, 802, 1024 }, { 6, 803, 1024 }, { 5, 804, 1024 }, { 6, 805, 1024 }, { 6, 806, 1024 }, { 7, 807, 1024 },
  5149. { 5, 808, 1024 }, { 6, 809, 1024 }, { 6, 810, 1024 }, { 7, 811, 1024 }, { 6, 812, 1024 }, { 7, 813, 1024 }, { 7, 814, 1024 }, { 8, 815, 1024 },
  5150. { 5, 816, 1024 }, { 6, 817, 1024 }, { 6, 818, 1024 }, { 7, 819, 1024 }, { 6, 820, 1024 }, { 7, 821, 1024 }, { 7, 822, 1024 }, { 8, 823, 1024 },
  5151. { 6, 824, 1024 }, { 7, 825, 1024 }, { 7, 826, 1024 }, { 8, 827, 1024 }, { 7, 828, 1024 }, { 8, 829, 1024 }, { 8, 830, 1024 }, { 9, 831, 1024 },
  5152. { 4, 832, 1024 }, { 5, 833, 1024 }, { 5, 834, 1024 }, { 6, 835, 1024 }, { 5, 836, 1024 }, { 6, 837, 1024 }, { 6, 838, 1024 }, { 7, 839, 1024 },
  5153. { 5, 840, 1024 }, { 6, 841, 1024 }, { 6, 842, 1024 }, { 7, 843, 1024 }, { 6, 844, 1024 }, { 7, 845, 1024 }, { 7, 846, 1024 }, { 8, 847, 1024 },
  5154. { 5, 848, 1024 }, { 6, 849, 1024 }, { 6, 850, 1024 }, { 7, 851, 1024 }, { 6, 852, 1024 }, { 7, 853, 1024 }, { 7, 854, 1024 }, { 8, 855, 1024 },
  5155. { 6, 856, 1024 }, { 7, 857, 1024 }, { 7, 858, 1024 }, { 8, 859, 1024 }, { 7, 860, 1024 }, { 8, 861, 1024 }, { 8, 862, 1024 }, { 9, 863, 1024 },
  5156. { 5, 864, 1024 }, { 6, 865, 1024 }, { 6, 866, 1024 }, { 7, 867, 1024 }, { 6, 868, 1024 }, { 7, 869, 1024 }, { 7, 870, 1024 }, { 8, 871, 1024 },
  5157. { 6, 872, 1024 }, { 7, 873, 1024 }, { 7, 874, 1024 }, { 8, 875, 1024 }, { 7, 876, 1024 }, { 8, 877, 1024 }, { 8, 878, 1024 }, { 9, 879, 1024 },
  5158. { 6, 880, 1024 }, { 7, 881, 1024 }, { 7, 882, 1024 }, { 8, 883, 1024 }, { 7, 884, 1024 }, { 8, 885, 1024 }, { 8, 886, 1024 }, { 9, 887, 1024 },
  5159. { 7, 888, 1024 }, { 8, 889, 1024 }, { 8, 890, 1024 }, { 9, 891, 1024 }, { 8, 892, 1024 }, { 9, 893, 1024 }, { 9, 894, 1024 }, { 10, 895, 1024 },
  5160. { 4, 896, 1024 }, { 5, 897, 1024 }, { 5, 898, 1024 }, { 6, 899, 1024 }, { 5, 900, 1024 }, { 6, 901, 1024 }, { 6, 902, 1024 }, { 7, 903, 1024 },
  5161. { 5, 904, 1024 }, { 6, 905, 1024 }, { 6, 906, 1024 }, { 7, 907, 1024 }, { 6, 908, 1024 }, { 7, 909, 1024 }, { 7, 910, 1024 }, { 8, 911, 1024 },
  5162. { 5, 912, 1024 }, { 6, 913, 1024 }, { 6, 914, 1024 }, { 7, 915, 1024 }, { 6, 916, 1024 }, { 7, 917, 1024 }, { 7, 918, 1024 }, { 8, 919, 1024 },
  5163. { 6, 920, 1024 }, { 7, 921, 1024 }, { 7, 922, 1024 }, { 8, 923, 1024 }, { 7, 924, 1024 }, { 8, 925, 1024 }, { 8, 926, 1024 }, { 9, 927, 1024 },
  5164. { 5, 928, 1024 }, { 6, 929, 1024 }, { 6, 930, 1024 }, { 7, 931, 1024 }, { 6, 932, 1024 }, { 7, 933, 1024 }, { 7, 934, 1024 }, { 8, 935, 1024 },
  5165. { 6, 936, 1024 }, { 7, 937, 1024 }, { 7, 938, 1024 }, { 8, 939, 1024 }, { 7, 940, 1024 }, { 8, 941, 1024 }, { 8, 942, 1024 }, { 9, 943, 1024 },
  5166. { 6, 944, 1024 }, { 7, 945, 1024 }, { 7, 946, 1024 }, { 8, 947, 1024 }, { 7, 948, 1024 }, { 8, 949, 1024 }, { 8, 950, 1024 }, { 9, 951, 1024 },
  5167. { 7, 952, 1024 }, { 8, 953, 1024 }, { 8, 954, 1024 }, { 9, 955, 1024 }, { 8, 956, 1024 }, { 9, 957, 1024 }, { 9, 958, 1024 }, { 10, 959, 1024 },
  5168. { 5, 960, 1024 }, { 6, 961, 1024 }, { 6, 962, 1024 }, { 7, 963, 1024 }, { 6, 964, 1024 }, { 7, 965, 1024 }, { 7, 966, 1024 }, { 8, 967, 1024 },
  5169. { 6, 968, 1024 }, { 7, 969, 1024 }, { 7, 970, 1024 }, { 8, 971, 1024 }, { 7, 972, 1024 }, { 8, 973, 1024 }, { 8, 974, 1024 }, { 9, 975, 1024 },
  5170. { 6, 976, 1024 }, { 7, 977, 1024 }, { 7, 978, 1024 }, { 8, 979, 1024 }, { 7, 980, 1024 }, { 8, 981, 1024 }, { 8, 982, 1024 }, { 9, 983, 1024 },
  5171. { 7, 984, 1024 }, { 8, 985, 1024 }, { 8, 986, 1024 }, { 9, 987, 1024 }, { 8, 988, 1024 }, { 9, 989, 1024 }, { 9, 990, 1024 }, { 10, 991, 1024 },
  5172. { 6, 992, 1024 }, { 7, 993, 1024 }, { 7, 994, 1024 }, { 8, 995, 1024 }, { 7, 996, 1024 }, { 8, 997, 1024 }, { 8, 998, 1024 }, { 9, 999, 1024 },
  5173. { 7, 1000, 1024 }, { 8, 1001, 1024 }, { 8, 1002, 1024 }, { 9, 1003, 1024 }, { 8, 1004, 1024 }, { 9, 1005, 1024 }, { 9, 1006, 1024 }, { 10, 1007, 1024 },
  5174. { 7, 1008, 1024 }, { 8, 1009, 1024 }, { 8, 1010, 1024 }, { 9, 1011, 1024 }, { 8, 1012, 1024 }, { 9, 1013, 1024 }, { 9, 1014, 1024 }, { 10, 1015, 1024 },
  5175. { 8, 1016, 1024 }, { 9, 1017, 1024 }, { 9, 1018, 1024 }, { 10, 1019, 1024 }, { 9, 1020, 1024 }, { 10, 1021, 1024 }, { 10, 1022, 1024 }, { 11, 1023, 1024 },
  5176. #if FP_LUT > 11
  5177. { 1, 0, 0 }, { 2, 1, 2048 }, { 2, 2, 2048 }, { 3, 3, 2048 }, { 2, 4, 2048 }, { 3, 5, 2048 }, { 3, 6, 2048 }, { 4, 7, 2048 },
  5178. { 2, 8, 2048 }, { 3, 9, 2048 }, { 3, 10, 2048 }, { 4, 11, 2048 }, { 3, 12, 2048 }, { 4, 13, 2048 }, { 4, 14, 2048 }, { 5, 15, 2048 },
  5179. { 2, 16, 2048 }, { 3, 17, 2048 }, { 3, 18, 2048 }, { 4, 19, 2048 }, { 3, 20, 2048 }, { 4, 21, 2048 }, { 4, 22, 2048 }, { 5, 23, 2048 },
  5180. { 3, 24, 2048 }, { 4, 25, 2048 }, { 4, 26, 2048 }, { 5, 27, 2048 }, { 4, 28, 2048 }, { 5, 29, 2048 }, { 5, 30, 2048 }, { 6, 31, 2048 },
  5181. { 2, 32, 2048 }, { 3, 33, 2048 }, { 3, 34, 2048 }, { 4, 35, 2048 }, { 3, 36, 2048 }, { 4, 37, 2048 }, { 4, 38, 2048 }, { 5, 39, 2048 },
  5182. { 3, 40, 2048 }, { 4, 41, 2048 }, { 4, 42, 2048 }, { 5, 43, 2048 }, { 4, 44, 2048 }, { 5, 45, 2048 }, { 5, 46, 2048 }, { 6, 47, 2048 },
  5183. { 3, 48, 2048 }, { 4, 49, 2048 }, { 4, 50, 2048 }, { 5, 51, 2048 }, { 4, 52, 2048 }, { 5, 53, 2048 }, { 5, 54, 2048 }, { 6, 55, 2048 },
  5184. { 4, 56, 2048 }, { 5, 57, 2048 }, { 5, 58, 2048 }, { 6, 59, 2048 }, { 5, 60, 2048 }, { 6, 61, 2048 }, { 6, 62, 2048 }, { 7, 63, 2048 },
  5185. { 2, 64, 2048 }, { 3, 65, 2048 }, { 3, 66, 2048 }, { 4, 67, 2048 }, { 3, 68, 2048 }, { 4, 69, 2048 }, { 4, 70, 2048 }, { 5, 71, 2048 },
  5186. { 3, 72, 2048 }, { 4, 73, 2048 }, { 4, 74, 2048 }, { 5, 75, 2048 }, { 4, 76, 2048 }, { 5, 77, 2048 }, { 5, 78, 2048 }, { 6, 79, 2048 },
  5187. { 3, 80, 2048 }, { 4, 81, 2048 }, { 4, 82, 2048 }, { 5, 83, 2048 }, { 4, 84, 2048 }, { 5, 85, 2048 }, { 5, 86, 2048 }, { 6, 87, 2048 },
  5188. { 4, 88, 2048 }, { 5, 89, 2048 }, { 5, 90, 2048 }, { 6, 91, 2048 }, { 5, 92, 2048 }, { 6, 93, 2048 }, { 6, 94, 2048 }, { 7, 95, 2048 },
  5189. { 3, 96, 2048 }, { 4, 97, 2048 }, { 4, 98, 2048 }, { 5, 99, 2048 }, { 4, 100, 2048 }, { 5, 101, 2048 }, { 5, 102, 2048 }, { 6, 103, 2048 },
  5190. { 4, 104, 2048 }, { 5, 105, 2048 }, { 5, 106, 2048 }, { 6, 107, 2048 }, { 5, 108, 2048 }, { 6, 109, 2048 }, { 6, 110, 2048 }, { 7, 111, 2048 },
  5191. { 4, 112, 2048 }, { 5, 113, 2048 }, { 5, 114, 2048 }, { 6, 115, 2048 }, { 5, 116, 2048 }, { 6, 117, 2048 }, { 6, 118, 2048 }, { 7, 119, 2048 },
  5192. { 5, 120, 2048 }, { 6, 121, 2048 }, { 6, 122, 2048 }, { 7, 123, 2048 }, { 6, 124, 2048 }, { 7, 125, 2048 }, { 7, 126, 2048 }, { 8, 127, 2048 },
  5193. { 2, 128, 2048 }, { 3, 129, 2048 }, { 3, 130, 2048 }, { 4, 131, 2048 }, { 3, 132, 2048 }, { 4, 133, 2048 }, { 4, 134, 2048 }, { 5, 135, 2048 },
  5194. { 3, 136, 2048 }, { 4, 137, 2048 }, { 4, 138, 2048 }, { 5, 139, 2048 }, { 4, 140, 2048 }, { 5, 141, 2048 }, { 5, 142, 2048 }, { 6, 143, 2048 },
  5195. { 3, 144, 2048 }, { 4, 145, 2048 }, { 4, 146, 2048 }, { 5, 147, 2048 }, { 4, 148, 2048 }, { 5, 149, 2048 }, { 5, 150, 2048 }, { 6, 151, 2048 },
  5196. { 4, 152, 2048 }, { 5, 153, 2048 }, { 5, 154, 2048 }, { 6, 155, 2048 }, { 5, 156, 2048 }, { 6, 157, 2048 }, { 6, 158, 2048 }, { 7, 159, 2048 },
  5197. { 3, 160, 2048 }, { 4, 161, 2048 }, { 4, 162, 2048 }, { 5, 163, 2048 }, { 4, 164, 2048 }, { 5, 165, 2048 }, { 5, 166, 2048 }, { 6, 167, 2048 },
  5198. { 4, 168, 2048 }, { 5, 169, 2048 }, { 5, 170, 2048 }, { 6, 171, 2048 }, { 5, 172, 2048 }, { 6, 173, 2048 }, { 6, 174, 2048 }, { 7, 175, 2048 },
  5199. { 4, 176, 2048 }, { 5, 177, 2048 }, { 5, 178, 2048 }, { 6, 179, 2048 }, { 5, 180, 2048 }, { 6, 181, 2048 }, { 6, 182, 2048 }, { 7, 183, 2048 },
  5200. { 5, 184, 2048 }, { 6, 185, 2048 }, { 6, 186, 2048 }, { 7, 187, 2048 }, { 6, 188, 2048 }, { 7, 189, 2048 }, { 7, 190, 2048 }, { 8, 191, 2048 },
  5201. { 3, 192, 2048 }, { 4, 193, 2048 }, { 4, 194, 2048 }, { 5, 195, 2048 }, { 4, 196, 2048 }, { 5, 197, 2048 }, { 5, 198, 2048 }, { 6, 199, 2048 },
  5202. { 4, 200, 2048 }, { 5, 201, 2048 }, { 5, 202, 2048 }, { 6, 203, 2048 }, { 5, 204, 2048 }, { 6, 205, 2048 }, { 6, 206, 2048 }, { 7, 207, 2048 },
  5203. { 4, 208, 2048 }, { 5, 209, 2048 }, { 5, 210, 2048 }, { 6, 211, 2048 }, { 5, 212, 2048 }, { 6, 213, 2048 }, { 6, 214, 2048 }, { 7, 215, 2048 },
  5204. { 5, 216, 2048 }, { 6, 217, 2048 }, { 6, 218, 2048 }, { 7, 219, 2048 }, { 6, 220, 2048 }, { 7, 221, 2048 }, { 7, 222, 2048 }, { 8, 223, 2048 },
  5205. { 4, 224, 2048 }, { 5, 225, 2048 }, { 5, 226, 2048 }, { 6, 227, 2048 }, { 5, 228, 2048 }, { 6, 229, 2048 }, { 6, 230, 2048 }, { 7, 231, 2048 },
  5206. { 5, 232, 2048 }, { 6, 233, 2048 }, { 6, 234, 2048 }, { 7, 235, 2048 }, { 6, 236, 2048 }, { 7, 237, 2048 }, { 7, 238, 2048 }, { 8, 239, 2048 },
  5207. { 5, 240, 2048 }, { 6, 241, 2048 }, { 6, 242, 2048 }, { 7, 243, 2048 }, { 6, 244, 2048 }, { 7, 245, 2048 }, { 7, 246, 2048 }, { 8, 247, 2048 },
  5208. { 6, 248, 2048 }, { 7, 249, 2048 }, { 7, 250, 2048 }, { 8, 251, 2048 }, { 7, 252, 2048 }, { 8, 253, 2048 }, { 8, 254, 2048 }, { 9, 255, 2048 },
  5209. { 2, 256, 2048 }, { 3, 257, 2048 }, { 3, 258, 2048 }, { 4, 259, 2048 }, { 3, 260, 2048 }, { 4, 261, 2048 }, { 4, 262, 2048 }, { 5, 263, 2048 },
  5210. { 3, 264, 2048 }, { 4, 265, 2048 }, { 4, 266, 2048 }, { 5, 267, 2048 }, { 4, 268, 2048 }, { 5, 269, 2048 }, { 5, 270, 2048 }, { 6, 271, 2048 },
  5211. { 3, 272, 2048 }, { 4, 273, 2048 }, { 4, 274, 2048 }, { 5, 275, 2048 }, { 4, 276, 2048 }, { 5, 277, 2048 }, { 5, 278, 2048 }, { 6, 279, 2048 },
  5212. { 4, 280, 2048 }, { 5, 281, 2048 }, { 5, 282, 2048 }, { 6, 283, 2048 }, { 5, 284, 2048 }, { 6, 285, 2048 }, { 6, 286, 2048 }, { 7, 287, 2048 },
  5213. { 3, 288, 2048 }, { 4, 289, 2048 }, { 4, 290, 2048 }, { 5, 291, 2048 }, { 4, 292, 2048 }, { 5, 293, 2048 }, { 5, 294, 2048 }, { 6, 295, 2048 },
  5214. { 4, 296, 2048 }, { 5, 297, 2048 }, { 5, 298, 2048 }, { 6, 299, 2048 }, { 5, 300, 2048 }, { 6, 301, 2048 }, { 6, 302, 2048 }, { 7, 303, 2048 },
  5215. { 4, 304, 2048 }, { 5, 305, 2048 }, { 5, 306, 2048 }, { 6, 307, 2048 }, { 5, 308, 2048 }, { 6, 309, 2048 }, { 6, 310, 2048 }, { 7, 311, 2048 },
  5216. { 5, 312, 2048 }, { 6, 313, 2048 }, { 6, 314, 2048 }, { 7, 315, 2048 }, { 6, 316, 2048 }, { 7, 317, 2048 }, { 7, 318, 2048 }, { 8, 319, 2048 },
  5217. { 3, 320, 2048 }, { 4, 321, 2048 }, { 4, 322, 2048 }, { 5, 323, 2048 }, { 4, 324, 2048 }, { 5, 325, 2048 }, { 5, 326, 2048 }, { 6, 327, 2048 },
  5218. { 4, 328, 2048 }, { 5, 329, 2048 }, { 5, 330, 2048 }, { 6, 331, 2048 }, { 5, 332, 2048 }, { 6, 333, 2048 }, { 6, 334, 2048 }, { 7, 335, 2048 },
  5219. { 4, 336, 2048 }, { 5, 337, 2048 }, { 5, 338, 2048 }, { 6, 339, 2048 }, { 5, 340, 2048 }, { 6, 341, 2048 }, { 6, 342, 2048 }, { 7, 343, 2048 },
  5220. { 5, 344, 2048 }, { 6, 345, 2048 }, { 6, 346, 2048 }, { 7, 347, 2048 }, { 6, 348, 2048 }, { 7, 349, 2048 }, { 7, 350, 2048 }, { 8, 351, 2048 },
  5221. { 4, 352, 2048 }, { 5, 353, 2048 }, { 5, 354, 2048 }, { 6, 355, 2048 }, { 5, 356, 2048 }, { 6, 357, 2048 }, { 6, 358, 2048 }, { 7, 359, 2048 },
  5222. { 5, 360, 2048 }, { 6, 361, 2048 }, { 6, 362, 2048 }, { 7, 363, 2048 }, { 6, 364, 2048 }, { 7, 365, 2048 }, { 7, 366, 2048 }, { 8, 367, 2048 },
  5223. { 5, 368, 2048 }, { 6, 369, 2048 }, { 6, 370, 2048 }, { 7, 371, 2048 }, { 6, 372, 2048 }, { 7, 373, 2048 }, { 7, 374, 2048 }, { 8, 375, 2048 },
  5224. { 6, 376, 2048 }, { 7, 377, 2048 }, { 7, 378, 2048 }, { 8, 379, 2048 }, { 7, 380, 2048 }, { 8, 381, 2048 }, { 8, 382, 2048 }, { 9, 383, 2048 },
  5225. { 3, 384, 2048 }, { 4, 385, 2048 }, { 4, 386, 2048 }, { 5, 387, 2048 }, { 4, 388, 2048 }, { 5, 389, 2048 }, { 5, 390, 2048 }, { 6, 391, 2048 },
  5226. { 4, 392, 2048 }, { 5, 393, 2048 }, { 5, 394, 2048 }, { 6, 395, 2048 }, { 5, 396, 2048 }, { 6, 397, 2048 }, { 6, 398, 2048 }, { 7, 399, 2048 },
  5227. { 4, 400, 2048 }, { 5, 401, 2048 }, { 5, 402, 2048 }, { 6, 403, 2048 }, { 5, 404, 2048 }, { 6, 405, 2048 }, { 6, 406, 2048 }, { 7, 407, 2048 },
  5228. { 5, 408, 2048 }, { 6, 409, 2048 }, { 6, 410, 2048 }, { 7, 411, 2048 }, { 6, 412, 2048 }, { 7, 413, 2048 }, { 7, 414, 2048 }, { 8, 415, 2048 },
  5229. { 4, 416, 2048 }, { 5, 417, 2048 }, { 5, 418, 2048 }, { 6, 419, 2048 }, { 5, 420, 2048 }, { 6, 421, 2048 }, { 6, 422, 2048 }, { 7, 423, 2048 },
  5230. { 5, 424, 2048 }, { 6, 425, 2048 }, { 6, 426, 2048 }, { 7, 427, 2048 }, { 6, 428, 2048 }, { 7, 429, 2048 }, { 7, 430, 2048 }, { 8, 431, 2048 },
  5231. { 5, 432, 2048 }, { 6, 433, 2048 }, { 6, 434, 2048 }, { 7, 435, 2048 }, { 6, 436, 2048 }, { 7, 437, 2048 }, { 7, 438, 2048 }, { 8, 439, 2048 },
  5232. { 6, 440, 2048 }, { 7, 441, 2048 }, { 7, 442, 2048 }, { 8, 443, 2048 }, { 7, 444, 2048 }, { 8, 445, 2048 }, { 8, 446, 2048 }, { 9, 447, 2048 },
  5233. { 4, 448, 2048 }, { 5, 449, 2048 }, { 5, 450, 2048 }, { 6, 451, 2048 }, { 5, 452, 2048 }, { 6, 453, 2048 }, { 6, 454, 2048 }, { 7, 455, 2048 },
  5234. { 5, 456, 2048 }, { 6, 457, 2048 }, { 6, 458, 2048 }, { 7, 459, 2048 }, { 6, 460, 2048 }, { 7, 461, 2048 }, { 7, 462, 2048 }, { 8, 463, 2048 },
  5235. { 5, 464, 2048 }, { 6, 465, 2048 }, { 6, 466, 2048 }, { 7, 467, 2048 }, { 6, 468, 2048 }, { 7, 469, 2048 }, { 7, 470, 2048 }, { 8, 471, 2048 },
  5236. { 6, 472, 2048 }, { 7, 473, 2048 }, { 7, 474, 2048 }, { 8, 475, 2048 }, { 7, 476, 2048 }, { 8, 477, 2048 }, { 8, 478, 2048 }, { 9, 479, 2048 },
  5237. { 5, 480, 2048 }, { 6, 481, 2048 }, { 6, 482, 2048 }, { 7, 483, 2048 }, { 6, 484, 2048 }, { 7, 485, 2048 }, { 7, 486, 2048 }, { 8, 487, 2048 },
  5238. { 6, 488, 2048 }, { 7, 489, 2048 }, { 7, 490, 2048 }, { 8, 491, 2048 }, { 7, 492, 2048 }, { 8, 493, 2048 }, { 8, 494, 2048 }, { 9, 495, 2048 },
  5239. { 6, 496, 2048 }, { 7, 497, 2048 }, { 7, 498, 2048 }, { 8, 499, 2048 }, { 7, 500, 2048 }, { 8, 501, 2048 }, { 8, 502, 2048 }, { 9, 503, 2048 },
  5240. { 7, 504, 2048 }, { 8, 505, 2048 }, { 8, 506, 2048 }, { 9, 507, 2048 }, { 8, 508, 2048 }, { 9, 509, 2048 }, { 9, 510, 2048 }, { 10, 511, 2048 },
  5241. { 2, 512, 2048 }, { 3, 513, 2048 }, { 3, 514, 2048 }, { 4, 515, 2048 }, { 3, 516, 2048 }, { 4, 517, 2048 }, { 4, 518, 2048 }, { 5, 519, 2048 },
  5242. { 3, 520, 2048 }, { 4, 521, 2048 }, { 4, 522, 2048 }, { 5, 523, 2048 }, { 4, 524, 2048 }, { 5, 525, 2048 }, { 5, 526, 2048 }, { 6, 527, 2048 },
  5243. { 3, 528, 2048 }, { 4, 529, 2048 }, { 4, 530, 2048 }, { 5, 531, 2048 }, { 4, 532, 2048 }, { 5, 533, 2048 }, { 5, 534, 2048 }, { 6, 535, 2048 },
  5244. { 4, 536, 2048 }, { 5, 537, 2048 }, { 5, 538, 2048 }, { 6, 539, 2048 }, { 5, 540, 2048 }, { 6, 541, 2048 }, { 6, 542, 2048 }, { 7, 543, 2048 },
  5245. { 3, 544, 2048 }, { 4, 545, 2048 }, { 4, 546, 2048 }, { 5, 547, 2048 }, { 4, 548, 2048 }, { 5, 549, 2048 }, { 5, 550, 2048 }, { 6, 551, 2048 },
  5246. { 4, 552, 2048 }, { 5, 553, 2048 }, { 5, 554, 2048 }, { 6, 555, 2048 }, { 5, 556, 2048 }, { 6, 557, 2048 }, { 6, 558, 2048 }, { 7, 559, 2048 },
  5247. { 4, 560, 2048 }, { 5, 561, 2048 }, { 5, 562, 2048 }, { 6, 563, 2048 }, { 5, 564, 2048 }, { 6, 565, 2048 }, { 6, 566, 2048 }, { 7, 567, 2048 },
  5248. { 5, 568, 2048 }, { 6, 569, 2048 }, { 6, 570, 2048 }, { 7, 571, 2048 }, { 6, 572, 2048 }, { 7, 573, 2048 }, { 7, 574, 2048 }, { 8, 575, 2048 },
  5249. { 3, 576, 2048 }, { 4, 577, 2048 }, { 4, 578, 2048 }, { 5, 579, 2048 }, { 4, 580, 2048 }, { 5, 581, 2048 }, { 5, 582, 2048 }, { 6, 583, 2048 },
  5250. { 4, 584, 2048 }, { 5, 585, 2048 }, { 5, 586, 2048 }, { 6, 587, 2048 }, { 5, 588, 2048 }, { 6, 589, 2048 }, { 6, 590, 2048 }, { 7, 591, 2048 },
  5251. { 4, 592, 2048 }, { 5, 593, 2048 }, { 5, 594, 2048 }, { 6, 595, 2048 }, { 5, 596, 2048 }, { 6, 597, 2048 }, { 6, 598, 2048 }, { 7, 599, 2048 },
  5252. { 5, 600, 2048 }, { 6, 601, 2048 }, { 6, 602, 2048 }, { 7, 603, 2048 }, { 6, 604, 2048 }, { 7, 605, 2048 }, { 7, 606, 2048 }, { 8, 607, 2048 },
  5253. { 4, 608, 2048 }, { 5, 609, 2048 }, { 5, 610, 2048 }, { 6, 611, 2048 }, { 5, 612, 2048 }, { 6, 613, 2048 }, { 6, 614, 2048 }, { 7, 615, 2048 },
  5254. { 5, 616, 2048 }, { 6, 617, 2048 }, { 6, 618, 2048 }, { 7, 619, 2048 }, { 6, 620, 2048 }, { 7, 621, 2048 }, { 7, 622, 2048 }, { 8, 623, 2048 },
  5255. { 5, 624, 2048 }, { 6, 625, 2048 }, { 6, 626, 2048 }, { 7, 627, 2048 }, { 6, 628, 2048 }, { 7, 629, 2048 }, { 7, 630, 2048 }, { 8, 631, 2048 },
  5256. { 6, 632, 2048 }, { 7, 633, 2048 }, { 7, 634, 2048 }, { 8, 635, 2048 }, { 7, 636, 2048 }, { 8, 637, 2048 }, { 8, 638, 2048 }, { 9, 639, 2048 },
  5257. { 3, 640, 2048 }, { 4, 641, 2048 }, { 4, 642, 2048 }, { 5, 643, 2048 }, { 4, 644, 2048 }, { 5, 645, 2048 }, { 5, 646, 2048 }, { 6, 647, 2048 },
  5258. { 4, 648, 2048 }, { 5, 649, 2048 }, { 5, 650, 2048 }, { 6, 651, 2048 }, { 5, 652, 2048 }, { 6, 653, 2048 }, { 6, 654, 2048 }, { 7, 655, 2048 },
  5259. { 4, 656, 2048 }, { 5, 657, 2048 }, { 5, 658, 2048 }, { 6, 659, 2048 }, { 5, 660, 2048 }, { 6, 661, 2048 }, { 6, 662, 2048 }, { 7, 663, 2048 },
  5260. { 5, 664, 2048 }, { 6, 665, 2048 }, { 6, 666, 2048 }, { 7, 667, 2048 }, { 6, 668, 2048 }, { 7, 669, 2048 }, { 7, 670, 2048 }, { 8, 671, 2048 },
  5261. { 4, 672, 2048 }, { 5, 673, 2048 }, { 5, 674, 2048 }, { 6, 675, 2048 }, { 5, 676, 2048 }, { 6, 677, 2048 }, { 6, 678, 2048 }, { 7, 679, 2048 },
  5262. { 5, 680, 2048 }, { 6, 681, 2048 }, { 6, 682, 2048 }, { 7, 683, 2048 }, { 6, 684, 2048 }, { 7, 685, 2048 }, { 7, 686, 2048 }, { 8, 687, 2048 },
  5263. { 5, 688, 2048 }, { 6, 689, 2048 }, { 6, 690, 2048 }, { 7, 691, 2048 }, { 6, 692, 2048 }, { 7, 693, 2048 }, { 7, 694, 2048 }, { 8, 695, 2048 },
  5264. { 6, 696, 2048 }, { 7, 697, 2048 }, { 7, 698, 2048 }, { 8, 699, 2048 }, { 7, 700, 2048 }, { 8, 701, 2048 }, { 8, 702, 2048 }, { 9, 703, 2048 },
  5265. { 4, 704, 2048 }, { 5, 705, 2048 }, { 5, 706, 2048 }, { 6, 707, 2048 }, { 5, 708, 2048 }, { 6, 709, 2048 }, { 6, 710, 2048 }, { 7, 711, 2048 },
  5266. { 5, 712, 2048 }, { 6, 713, 2048 }, { 6, 714, 2048 }, { 7, 715, 2048 }, { 6, 716, 2048 }, { 7, 717, 2048 }, { 7, 718, 2048 }, { 8, 719, 2048 },
  5267. { 5, 720, 2048 }, { 6, 721, 2048 }, { 6, 722, 2048 }, { 7, 723, 2048 }, { 6, 724, 2048 }, { 7, 725, 2048 }, { 7, 726, 2048 }, { 8, 727, 2048 },
  5268. { 6, 728, 2048 }, { 7, 729, 2048 }, { 7, 730, 2048 }, { 8, 731, 2048 }, { 7, 732, 2048 }, { 8, 733, 2048 }, { 8, 734, 2048 }, { 9, 735, 2048 },
  5269. { 5, 736, 2048 }, { 6, 737, 2048 }, { 6, 738, 2048 }, { 7, 739, 2048 }, { 6, 740, 2048 }, { 7, 741, 2048 }, { 7, 742, 2048 }, { 8, 743, 2048 },
  5270. { 6, 744, 2048 }, { 7, 745, 2048 }, { 7, 746, 2048 }, { 8, 747, 2048 }, { 7, 748, 2048 }, { 8, 749, 2048 }, { 8, 750, 2048 }, { 9, 751, 2048 },
  5271. { 6, 752, 2048 }, { 7, 753, 2048 }, { 7, 754, 2048 }, { 8, 755, 2048 }, { 7, 756, 2048 }, { 8, 757, 2048 }, { 8, 758, 2048 }, { 9, 759, 2048 },
  5272. { 7, 760, 2048 }, { 8, 761, 2048 }, { 8, 762, 2048 }, { 9, 763, 2048 }, { 8, 764, 2048 }, { 9, 765, 2048 }, { 9, 766, 2048 }, { 10, 767, 2048 },
  5273. { 3, 768, 2048 }, { 4, 769, 2048 }, { 4, 770, 2048 }, { 5, 771, 2048 }, { 4, 772, 2048 }, { 5, 773, 2048 }, { 5, 774, 2048 }, { 6, 775, 2048 },
  5274. { 4, 776, 2048 }, { 5, 777, 2048 }, { 5, 778, 2048 }, { 6, 779, 2048 }, { 5, 780, 2048 }, { 6, 781, 2048 }, { 6, 782, 2048 }, { 7, 783, 2048 },
  5275. { 4, 784, 2048 }, { 5, 785, 2048 }, { 5, 786, 2048 }, { 6, 787, 2048 }, { 5, 788, 2048 }, { 6, 789, 2048 }, { 6, 790, 2048 }, { 7, 791, 2048 },
  5276. { 5, 792, 2048 }, { 6, 793, 2048 }, { 6, 794, 2048 }, { 7, 795, 2048 }, { 6, 796, 2048 }, { 7, 797, 2048 }, { 7, 798, 2048 }, { 8, 799, 2048 },
  5277. { 4, 800, 2048 }, { 5, 801, 2048 }, { 5, 802, 2048 }, { 6, 803, 2048 }, { 5, 804, 2048 }, { 6, 805, 2048 }, { 6, 806, 2048 }, { 7, 807, 2048 },
  5278. { 5, 808, 2048 }, { 6, 809, 2048 }, { 6, 810, 2048 }, { 7, 811, 2048 }, { 6, 812, 2048 }, { 7, 813, 2048 }, { 7, 814, 2048 }, { 8, 815, 2048 },
  5279. { 5, 816, 2048 }, { 6, 817, 2048 }, { 6, 818, 2048 }, { 7, 819, 2048 }, { 6, 820, 2048 }, { 7, 821, 2048 }, { 7, 822, 2048 }, { 8, 823, 2048 },
  5280. { 6, 824, 2048 }, { 7, 825, 2048 }, { 7, 826, 2048 }, { 8, 827, 2048 }, { 7, 828, 2048 }, { 8, 829, 2048 }, { 8, 830, 2048 }, { 9, 831, 2048 },
  5281. { 4, 832, 2048 }, { 5, 833, 2048 }, { 5, 834, 2048 }, { 6, 835, 2048 }, { 5, 836, 2048 }, { 6, 837, 2048 }, { 6, 838, 2048 }, { 7, 839, 2048 },
  5282. { 5, 840, 2048 }, { 6, 841, 2048 }, { 6, 842, 2048 }, { 7, 843, 2048 }, { 6, 844, 2048 }, { 7, 845, 2048 }, { 7, 846, 2048 }, { 8, 847, 2048 },
  5283. { 5, 848, 2048 }, { 6, 849, 2048 }, { 6, 850, 2048 }, { 7, 851, 2048 }, { 6, 852, 2048 }, { 7, 853, 2048 }, { 7, 854, 2048 }, { 8, 855, 2048 },
  5284. { 6, 856, 2048 }, { 7, 857, 2048 }, { 7, 858, 2048 }, { 8, 859, 2048 }, { 7, 860, 2048 }, { 8, 861, 2048 }, { 8, 862, 2048 }, { 9, 863, 2048 },
  5285. { 5, 864, 2048 }, { 6, 865, 2048 }, { 6, 866, 2048 }, { 7, 867, 2048 }, { 6, 868, 2048 }, { 7, 869, 2048 }, { 7, 870, 2048 }, { 8, 871, 2048 },
  5286. { 6, 872, 2048 }, { 7, 873, 2048 }, { 7, 874, 2048 }, { 8, 875, 2048 }, { 7, 876, 2048 }, { 8, 877, 2048 }, { 8, 878, 2048 }, { 9, 879, 2048 },
  5287. { 6, 880, 2048 }, { 7, 881, 2048 }, { 7, 882, 2048 }, { 8, 883, 2048 }, { 7, 884, 2048 }, { 8, 885, 2048 }, { 8, 886, 2048 }, { 9, 887, 2048 },
  5288. { 7, 888, 2048 }, { 8, 889, 2048 }, { 8, 890, 2048 }, { 9, 891, 2048 }, { 8, 892, 2048 }, { 9, 893, 2048 }, { 9, 894, 2048 }, { 10, 895, 2048 },
  5289. { 4, 896, 2048 }, { 5, 897, 2048 }, { 5, 898, 2048 }, { 6, 899, 2048 }, { 5, 900, 2048 }, { 6, 901, 2048 }, { 6, 902, 2048 }, { 7, 903, 2048 },
  5290. { 5, 904, 2048 }, { 6, 905, 2048 }, { 6, 906, 2048 }, { 7, 907, 2048 }, { 6, 908, 2048 }, { 7, 909, 2048 }, { 7, 910, 2048 }, { 8, 911, 2048 },
  5291. { 5, 912, 2048 }, { 6, 913, 2048 }, { 6, 914, 2048 }, { 7, 915, 2048 }, { 6, 916, 2048 }, { 7, 917, 2048 }, { 7, 918, 2048 }, { 8, 919, 2048 },
  5292. { 6, 920, 2048 }, { 7, 921, 2048 }, { 7, 922, 2048 }, { 8, 923, 2048 }, { 7, 924, 2048 }, { 8, 925, 2048 }, { 8, 926, 2048 }, { 9, 927, 2048 },
  5293. { 5, 928, 2048 }, { 6, 929, 2048 }, { 6, 930, 2048 }, { 7, 931, 2048 }, { 6, 932, 2048 }, { 7, 933, 2048 }, { 7, 934, 2048 }, { 8, 935, 2048 },
  5294. { 6, 936, 2048 }, { 7, 937, 2048 }, { 7, 938, 2048 }, { 8, 939, 2048 }, { 7, 940, 2048 }, { 8, 941, 2048 }, { 8, 942, 2048 }, { 9, 943, 2048 },
  5295. { 6, 944, 2048 }, { 7, 945, 2048 }, { 7, 946, 2048 }, { 8, 947, 2048 }, { 7, 948, 2048 }, { 8, 949, 2048 }, { 8, 950, 2048 }, { 9, 951, 2048 },
  5296. { 7, 952, 2048 }, { 8, 953, 2048 }, { 8, 954, 2048 }, { 9, 955, 2048 }, { 8, 956, 2048 }, { 9, 957, 2048 }, { 9, 958, 2048 }, { 10, 959, 2048 },
  5297. { 5, 960, 2048 }, { 6, 961, 2048 }, { 6, 962, 2048 }, { 7, 963, 2048 }, { 6, 964, 2048 }, { 7, 965, 2048 }, { 7, 966, 2048 }, { 8, 967, 2048 },
  5298. { 6, 968, 2048 }, { 7, 969, 2048 }, { 7, 970, 2048 }, { 8, 971, 2048 }, { 7, 972, 2048 }, { 8, 973, 2048 }, { 8, 974, 2048 }, { 9, 975, 2048 },
  5299. { 6, 976, 2048 }, { 7, 977, 2048 }, { 7, 978, 2048 }, { 8, 979, 2048 }, { 7, 980, 2048 }, { 8, 981, 2048 }, { 8, 982, 2048 }, { 9, 983, 2048 },
  5300. { 7, 984, 2048 }, { 8, 985, 2048 }, { 8, 986, 2048 }, { 9, 987, 2048 }, { 8, 988, 2048 }, { 9, 989, 2048 }, { 9, 990, 2048 }, { 10, 991, 2048 },
  5301. { 6, 992, 2048 }, { 7, 993, 2048 }, { 7, 994, 2048 }, { 8, 995, 2048 }, { 7, 996, 2048 }, { 8, 997, 2048 }, { 8, 998, 2048 }, { 9, 999, 2048 },
  5302. { 7, 1000, 2048 }, { 8, 1001, 2048 }, { 8, 1002, 2048 }, { 9, 1003, 2048 }, { 8, 1004, 2048 }, { 9, 1005, 2048 }, { 9, 1006, 2048 }, { 10, 1007, 2048 },
  5303. { 7, 1008, 2048 }, { 8, 1009, 2048 }, { 8, 1010, 2048 }, { 9, 1011, 2048 }, { 8, 1012, 2048 }, { 9, 1013, 2048 }, { 9, 1014, 2048 }, { 10, 1015, 2048 },
  5304. { 8, 1016, 2048 }, { 9, 1017, 2048 }, { 9, 1018, 2048 }, { 10, 1019, 2048 }, { 9, 1020, 2048 }, { 10, 1021, 2048 }, { 10, 1022, 2048 }, { 11, 1023, 2048 },
  5305. { 2, 1024, 2048 }, { 3, 1025, 2048 }, { 3, 1026, 2048 }, { 4, 1027, 2048 }, { 3, 1028, 2048 }, { 4, 1029, 2048 }, { 4, 1030, 2048 }, { 5, 1031, 2048 },
  5306. { 3, 1032, 2048 }, { 4, 1033, 2048 }, { 4, 1034, 2048 }, { 5, 1035, 2048 }, { 4, 1036, 2048 }, { 5, 1037, 2048 }, { 5, 1038, 2048 }, { 6, 1039, 2048 },
  5307. { 3, 1040, 2048 }, { 4, 1041, 2048 }, { 4, 1042, 2048 }, { 5, 1043, 2048 }, { 4, 1044, 2048 }, { 5, 1045, 2048 }, { 5, 1046, 2048 }, { 6, 1047, 2048 },
  5308. { 4, 1048, 2048 }, { 5, 1049, 2048 }, { 5, 1050, 2048 }, { 6, 1051, 2048 }, { 5, 1052, 2048 }, { 6, 1053, 2048 }, { 6, 1054, 2048 }, { 7, 1055, 2048 },
  5309. { 3, 1056, 2048 }, { 4, 1057, 2048 }, { 4, 1058, 2048 }, { 5, 1059, 2048 }, { 4, 1060, 2048 }, { 5, 1061, 2048 }, { 5, 1062, 2048 }, { 6, 1063, 2048 },
  5310. { 4, 1064, 2048 }, { 5, 1065, 2048 }, { 5, 1066, 2048 }, { 6, 1067, 2048 }, { 5, 1068, 2048 }, { 6, 1069, 2048 }, { 6, 1070, 2048 }, { 7, 1071, 2048 },
  5311. { 4, 1072, 2048 }, { 5, 1073, 2048 }, { 5, 1074, 2048 }, { 6, 1075, 2048 }, { 5, 1076, 2048 }, { 6, 1077, 2048 }, { 6, 1078, 2048 }, { 7, 1079, 2048 },
  5312. { 5, 1080, 2048 }, { 6, 1081, 2048 }, { 6, 1082, 2048 }, { 7, 1083, 2048 }, { 6, 1084, 2048 }, { 7, 1085, 2048 }, { 7, 1086, 2048 }, { 8, 1087, 2048 },
  5313. { 3, 1088, 2048 }, { 4, 1089, 2048 }, { 4, 1090, 2048 }, { 5, 1091, 2048 }, { 4, 1092, 2048 }, { 5, 1093, 2048 }, { 5, 1094, 2048 }, { 6, 1095, 2048 },
  5314. { 4, 1096, 2048 }, { 5, 1097, 2048 }, { 5, 1098, 2048 }, { 6, 1099, 2048 }, { 5, 1100, 2048 }, { 6, 1101, 2048 }, { 6, 1102, 2048 }, { 7, 1103, 2048 },
  5315. { 4, 1104, 2048 }, { 5, 1105, 2048 }, { 5, 1106, 2048 }, { 6, 1107, 2048 }, { 5, 1108, 2048 }, { 6, 1109, 2048 }, { 6, 1110, 2048 }, { 7, 1111, 2048 },
  5316. { 5, 1112, 2048 }, { 6, 1113, 2048 }, { 6, 1114, 2048 }, { 7, 1115, 2048 }, { 6, 1116, 2048 }, { 7, 1117, 2048 }, { 7, 1118, 2048 }, { 8, 1119, 2048 },
  5317. { 4, 1120, 2048 }, { 5, 1121, 2048 }, { 5, 1122, 2048 }, { 6, 1123, 2048 }, { 5, 1124, 2048 }, { 6, 1125, 2048 }, { 6, 1126, 2048 }, { 7, 1127, 2048 },
  5318. { 5, 1128, 2048 }, { 6, 1129, 2048 }, { 6, 1130, 2048 }, { 7, 1131, 2048 }, { 6, 1132, 2048 }, { 7, 1133, 2048 }, { 7, 1134, 2048 }, { 8, 1135, 2048 },
  5319. { 5, 1136, 2048 }, { 6, 1137, 2048 }, { 6, 1138, 2048 }, { 7, 1139, 2048 }, { 6, 1140, 2048 }, { 7, 1141, 2048 }, { 7, 1142, 2048 }, { 8, 1143, 2048 },
  5320. { 6, 1144, 2048 }, { 7, 1145, 2048 }, { 7, 1146, 2048 }, { 8, 1147, 2048 }, { 7, 1148, 2048 }, { 8, 1149, 2048 }, { 8, 1150, 2048 }, { 9, 1151, 2048 },
  5321. { 3, 1152, 2048 }, { 4, 1153, 2048 }, { 4, 1154, 2048 }, { 5, 1155, 2048 }, { 4, 1156, 2048 }, { 5, 1157, 2048 }, { 5, 1158, 2048 }, { 6, 1159, 2048 },
  5322. { 4, 1160, 2048 }, { 5, 1161, 2048 }, { 5, 1162, 2048 }, { 6, 1163, 2048 }, { 5, 1164, 2048 }, { 6, 1165, 2048 }, { 6, 1166, 2048 }, { 7, 1167, 2048 },
  5323. { 4, 1168, 2048 }, { 5, 1169, 2048 }, { 5, 1170, 2048 }, { 6, 1171, 2048 }, { 5, 1172, 2048 }, { 6, 1173, 2048 }, { 6, 1174, 2048 }, { 7, 1175, 2048 },
  5324. { 5, 1176, 2048 }, { 6, 1177, 2048 }, { 6, 1178, 2048 }, { 7, 1179, 2048 }, { 6, 1180, 2048 }, { 7, 1181, 2048 }, { 7, 1182, 2048 }, { 8, 1183, 2048 },
  5325. { 4, 1184, 2048 }, { 5, 1185, 2048 }, { 5, 1186, 2048 }, { 6, 1187, 2048 }, { 5, 1188, 2048 }, { 6, 1189, 2048 }, { 6, 1190, 2048 }, { 7, 1191, 2048 },
  5326. { 5, 1192, 2048 }, { 6, 1193, 2048 }, { 6, 1194, 2048 }, { 7, 1195, 2048 }, { 6, 1196, 2048 }, { 7, 1197, 2048 }, { 7, 1198, 2048 }, { 8, 1199, 2048 },
  5327. { 5, 1200, 2048 }, { 6, 1201, 2048 }, { 6, 1202, 2048 }, { 7, 1203, 2048 }, { 6, 1204, 2048 }, { 7, 1205, 2048 }, { 7, 1206, 2048 }, { 8, 1207, 2048 },
  5328. { 6, 1208, 2048 }, { 7, 1209, 2048 }, { 7, 1210, 2048 }, { 8, 1211, 2048 }, { 7, 1212, 2048 }, { 8, 1213, 2048 }, { 8, 1214, 2048 }, { 9, 1215, 2048 },
  5329. { 4, 1216, 2048 }, { 5, 1217, 2048 }, { 5, 1218, 2048 }, { 6, 1219, 2048 }, { 5, 1220, 2048 }, { 6, 1221, 2048 }, { 6, 1222, 2048 }, { 7, 1223, 2048 },
  5330. { 5, 1224, 2048 }, { 6, 1225, 2048 }, { 6, 1226, 2048 }, { 7, 1227, 2048 }, { 6, 1228, 2048 }, { 7, 1229, 2048 }, { 7, 1230, 2048 }, { 8, 1231, 2048 },
  5331. { 5, 1232, 2048 }, { 6, 1233, 2048 }, { 6, 1234, 2048 }, { 7, 1235, 2048 }, { 6, 1236, 2048 }, { 7, 1237, 2048 }, { 7, 1238, 2048 }, { 8, 1239, 2048 },
  5332. { 6, 1240, 2048 }, { 7, 1241, 2048 }, { 7, 1242, 2048 }, { 8, 1243, 2048 }, { 7, 1244, 2048 }, { 8, 1245, 2048 }, { 8, 1246, 2048 }, { 9, 1247, 2048 },
  5333. { 5, 1248, 2048 }, { 6, 1249, 2048 }, { 6, 1250, 2048 }, { 7, 1251, 2048 }, { 6, 1252, 2048 }, { 7, 1253, 2048 }, { 7, 1254, 2048 }, { 8, 1255, 2048 },
  5334. { 6, 1256, 2048 }, { 7, 1257, 2048 }, { 7, 1258, 2048 }, { 8, 1259, 2048 }, { 7, 1260, 2048 }, { 8, 1261, 2048 }, { 8, 1262, 2048 }, { 9, 1263, 2048 },
  5335. { 6, 1264, 2048 }, { 7, 1265, 2048 }, { 7, 1266, 2048 }, { 8, 1267, 2048 }, { 7, 1268, 2048 }, { 8, 1269, 2048 }, { 8, 1270, 2048 }, { 9, 1271, 2048 },
  5336. { 7, 1272, 2048 }, { 8, 1273, 2048 }, { 8, 1274, 2048 }, { 9, 1275, 2048 }, { 8, 1276, 2048 }, { 9, 1277, 2048 }, { 9, 1278, 2048 }, { 10, 1279, 2048 },
  5337. { 3, 1280, 2048 }, { 4, 1281, 2048 }, { 4, 1282, 2048 }, { 5, 1283, 2048 }, { 4, 1284, 2048 }, { 5, 1285, 2048 }, { 5, 1286, 2048 }, { 6, 1287, 2048 },
  5338. { 4, 1288, 2048 }, { 5, 1289, 2048 }, { 5, 1290, 2048 }, { 6, 1291, 2048 }, { 5, 1292, 2048 }, { 6, 1293, 2048 }, { 6, 1294, 2048 }, { 7, 1295, 2048 },
  5339. { 4, 1296, 2048 }, { 5, 1297, 2048 }, { 5, 1298, 2048 }, { 6, 1299, 2048 }, { 5, 1300, 2048 }, { 6, 1301, 2048 }, { 6, 1302, 2048 }, { 7, 1303, 2048 },
  5340. { 5, 1304, 2048 }, { 6, 1305, 2048 }, { 6, 1306, 2048 }, { 7, 1307, 2048 }, { 6, 1308, 2048 }, { 7, 1309, 2048 }, { 7, 1310, 2048 }, { 8, 1311, 2048 },
  5341. { 4, 1312, 2048 }, { 5, 1313, 2048 }, { 5, 1314, 2048 }, { 6, 1315, 2048 }, { 5, 1316, 2048 }, { 6, 1317, 2048 }, { 6, 1318, 2048 }, { 7, 1319, 2048 },
  5342. { 5, 1320, 2048 }, { 6, 1321, 2048 }, { 6, 1322, 2048 }, { 7, 1323, 2048 }, { 6, 1324, 2048 }, { 7, 1325, 2048 }, { 7, 1326, 2048 }, { 8, 1327, 2048 },
  5343. { 5, 1328, 2048 }, { 6, 1329, 2048 }, { 6, 1330, 2048 }, { 7, 1331, 2048 }, { 6, 1332, 2048 }, { 7, 1333, 2048 }, { 7, 1334, 2048 }, { 8, 1335, 2048 },
  5344. { 6, 1336, 2048 }, { 7, 1337, 2048 }, { 7, 1338, 2048 }, { 8, 1339, 2048 }, { 7, 1340, 2048 }, { 8, 1341, 2048 }, { 8, 1342, 2048 }, { 9, 1343, 2048 },
  5345. { 4, 1344, 2048 }, { 5, 1345, 2048 }, { 5, 1346, 2048 }, { 6, 1347, 2048 }, { 5, 1348, 2048 }, { 6, 1349, 2048 }, { 6, 1350, 2048 }, { 7, 1351, 2048 },
  5346. { 5, 1352, 2048 }, { 6, 1353, 2048 }, { 6, 1354, 2048 }, { 7, 1355, 2048 }, { 6, 1356, 2048 }, { 7, 1357, 2048 }, { 7, 1358, 2048 }, { 8, 1359, 2048 },
  5347. { 5, 1360, 2048 }, { 6, 1361, 2048 }, { 6, 1362, 2048 }, { 7, 1363, 2048 }, { 6, 1364, 2048 }, { 7, 1365, 2048 }, { 7, 1366, 2048 }, { 8, 1367, 2048 },
  5348. { 6, 1368, 2048 }, { 7, 1369, 2048 }, { 7, 1370, 2048 }, { 8, 1371, 2048 }, { 7, 1372, 2048 }, { 8, 1373, 2048 }, { 8, 1374, 2048 }, { 9, 1375, 2048 },
  5349. { 5, 1376, 2048 }, { 6, 1377, 2048 }, { 6, 1378, 2048 }, { 7, 1379, 2048 }, { 6, 1380, 2048 }, { 7, 1381, 2048 }, { 7, 1382, 2048 }, { 8, 1383, 2048 },
  5350. { 6, 1384, 2048 }, { 7, 1385, 2048 }, { 7, 1386, 2048 }, { 8, 1387, 2048 }, { 7, 1388, 2048 }, { 8, 1389, 2048 }, { 8, 1390, 2048 }, { 9, 1391, 2048 },
  5351. { 6, 1392, 2048 }, { 7, 1393, 2048 }, { 7, 1394, 2048 }, { 8, 1395, 2048 }, { 7, 1396, 2048 }, { 8, 1397, 2048 }, { 8, 1398, 2048 }, { 9, 1399, 2048 },
  5352. { 7, 1400, 2048 }, { 8, 1401, 2048 }, { 8, 1402, 2048 }, { 9, 1403, 2048 }, { 8, 1404, 2048 }, { 9, 1405, 2048 }, { 9, 1406, 2048 }, { 10, 1407, 2048 },
  5353. { 4, 1408, 2048 }, { 5, 1409, 2048 }, { 5, 1410, 2048 }, { 6, 1411, 2048 }, { 5, 1412, 2048 }, { 6, 1413, 2048 }, { 6, 1414, 2048 }, { 7, 1415, 2048 },
  5354. { 5, 1416, 2048 }, { 6, 1417, 2048 }, { 6, 1418, 2048 }, { 7, 1419, 2048 }, { 6, 1420, 2048 }, { 7, 1421, 2048 }, { 7, 1422, 2048 }, { 8, 1423, 2048 },
  5355. { 5, 1424, 2048 }, { 6, 1425, 2048 }, { 6, 1426, 2048 }, { 7, 1427, 2048 }, { 6, 1428, 2048 }, { 7, 1429, 2048 }, { 7, 1430, 2048 }, { 8, 1431, 2048 },
  5356. { 6, 1432, 2048 }, { 7, 1433, 2048 }, { 7, 1434, 2048 }, { 8, 1435, 2048 }, { 7, 1436, 2048 }, { 8, 1437, 2048 }, { 8, 1438, 2048 }, { 9, 1439, 2048 },
  5357. { 5, 1440, 2048 }, { 6, 1441, 2048 }, { 6, 1442, 2048 }, { 7, 1443, 2048 }, { 6, 1444, 2048 }, { 7, 1445, 2048 }, { 7, 1446, 2048 }, { 8, 1447, 2048 },
  5358. { 6, 1448, 2048 }, { 7, 1449, 2048 }, { 7, 1450, 2048 }, { 8, 1451, 2048 }, { 7, 1452, 2048 }, { 8, 1453, 2048 }, { 8, 1454, 2048 }, { 9, 1455, 2048 },
  5359. { 6, 1456, 2048 }, { 7, 1457, 2048 }, { 7, 1458, 2048 }, { 8, 1459, 2048 }, { 7, 1460, 2048 }, { 8, 1461, 2048 }, { 8, 1462, 2048 }, { 9, 1463, 2048 },
  5360. { 7, 1464, 2048 }, { 8, 1465, 2048 }, { 8, 1466, 2048 }, { 9, 1467, 2048 }, { 8, 1468, 2048 }, { 9, 1469, 2048 }, { 9, 1470, 2048 }, { 10, 1471, 2048 },
  5361. { 5, 1472, 2048 }, { 6, 1473, 2048 }, { 6, 1474, 2048 }, { 7, 1475, 2048 }, { 6, 1476, 2048 }, { 7, 1477, 2048 }, { 7, 1478, 2048 }, { 8, 1479, 2048 },
  5362. { 6, 1480, 2048 }, { 7, 1481, 2048 }, { 7, 1482, 2048 }, { 8, 1483, 2048 }, { 7, 1484, 2048 }, { 8, 1485, 2048 }, { 8, 1486, 2048 }, { 9, 1487, 2048 },
  5363. { 6, 1488, 2048 }, { 7, 1489, 2048 }, { 7, 1490, 2048 }, { 8, 1491, 2048 }, { 7, 1492, 2048 }, { 8, 1493, 2048 }, { 8, 1494, 2048 }, { 9, 1495, 2048 },
  5364. { 7, 1496, 2048 }, { 8, 1497, 2048 }, { 8, 1498, 2048 }, { 9, 1499, 2048 }, { 8, 1500, 2048 }, { 9, 1501, 2048 }, { 9, 1502, 2048 }, { 10, 1503, 2048 },
  5365. { 6, 1504, 2048 }, { 7, 1505, 2048 }, { 7, 1506, 2048 }, { 8, 1507, 2048 }, { 7, 1508, 2048 }, { 8, 1509, 2048 }, { 8, 1510, 2048 }, { 9, 1511, 2048 },
  5366. { 7, 1512, 2048 }, { 8, 1513, 2048 }, { 8, 1514, 2048 }, { 9, 1515, 2048 }, { 8, 1516, 2048 }, { 9, 1517, 2048 }, { 9, 1518, 2048 }, { 10, 1519, 2048 },
  5367. { 7, 1520, 2048 }, { 8, 1521, 2048 }, { 8, 1522, 2048 }, { 9, 1523, 2048 }, { 8, 1524, 2048 }, { 9, 1525, 2048 }, { 9, 1526, 2048 }, { 10, 1527, 2048 },
  5368. { 8, 1528, 2048 }, { 9, 1529, 2048 }, { 9, 1530, 2048 }, { 10, 1531, 2048 }, { 9, 1532, 2048 }, { 10, 1533, 2048 }, { 10, 1534, 2048 }, { 11, 1535, 2048 },
  5369. { 3, 1536, 2048 }, { 4, 1537, 2048 }, { 4, 1538, 2048 }, { 5, 1539, 2048 }, { 4, 1540, 2048 }, { 5, 1541, 2048 }, { 5, 1542, 2048 }, { 6, 1543, 2048 },
  5370. { 4, 1544, 2048 }, { 5, 1545, 2048 }, { 5, 1546, 2048 }, { 6, 1547, 2048 }, { 5, 1548, 2048 }, { 6, 1549, 2048 }, { 6, 1550, 2048 }, { 7, 1551, 2048 },
  5371. { 4, 1552, 2048 }, { 5, 1553, 2048 }, { 5, 1554, 2048 }, { 6, 1555, 2048 }, { 5, 1556, 2048 }, { 6, 1557, 2048 }, { 6, 1558, 2048 }, { 7, 1559, 2048 },
  5372. { 5, 1560, 2048 }, { 6, 1561, 2048 }, { 6, 1562, 2048 }, { 7, 1563, 2048 }, { 6, 1564, 2048 }, { 7, 1565, 2048 }, { 7, 1566, 2048 }, { 8, 1567, 2048 },
  5373. { 4, 1568, 2048 }, { 5, 1569, 2048 }, { 5, 1570, 2048 }, { 6, 1571, 2048 }, { 5, 1572, 2048 }, { 6, 1573, 2048 }, { 6, 1574, 2048 }, { 7, 1575, 2048 },
  5374. { 5, 1576, 2048 }, { 6, 1577, 2048 }, { 6, 1578, 2048 }, { 7, 1579, 2048 }, { 6, 1580, 2048 }, { 7, 1581, 2048 }, { 7, 1582, 2048 }, { 8, 1583, 2048 },
  5375. { 5, 1584, 2048 }, { 6, 1585, 2048 }, { 6, 1586, 2048 }, { 7, 1587, 2048 }, { 6, 1588, 2048 }, { 7, 1589, 2048 }, { 7, 1590, 2048 }, { 8, 1591, 2048 },
  5376. { 6, 1592, 2048 }, { 7, 1593, 2048 }, { 7, 1594, 2048 }, { 8, 1595, 2048 }, { 7, 1596, 2048 }, { 8, 1597, 2048 }, { 8, 1598, 2048 }, { 9, 1599, 2048 },
  5377. { 4, 1600, 2048 }, { 5, 1601, 2048 }, { 5, 1602, 2048 }, { 6, 1603, 2048 }, { 5, 1604, 2048 }, { 6, 1605, 2048 }, { 6, 1606, 2048 }, { 7, 1607, 2048 },
  5378. { 5, 1608, 2048 }, { 6, 1609, 2048 }, { 6, 1610, 2048 }, { 7, 1611, 2048 }, { 6, 1612, 2048 }, { 7, 1613, 2048 }, { 7, 1614, 2048 }, { 8, 1615, 2048 },
  5379. { 5, 1616, 2048 }, { 6, 1617, 2048 }, { 6, 1618, 2048 }, { 7, 1619, 2048 }, { 6, 1620, 2048 }, { 7, 1621, 2048 }, { 7, 1622, 2048 }, { 8, 1623, 2048 },
  5380. { 6, 1624, 2048 }, { 7, 1625, 2048 }, { 7, 1626, 2048 }, { 8, 1627, 2048 }, { 7, 1628, 2048 }, { 8, 1629, 2048 }, { 8, 1630, 2048 }, { 9, 1631, 2048 },
  5381. { 5, 1632, 2048 }, { 6, 1633, 2048 }, { 6, 1634, 2048 }, { 7, 1635, 2048 }, { 6, 1636, 2048 }, { 7, 1637, 2048 }, { 7, 1638, 2048 }, { 8, 1639, 2048 },
  5382. { 6, 1640, 2048 }, { 7, 1641, 2048 }, { 7, 1642, 2048 }, { 8, 1643, 2048 }, { 7, 1644, 2048 }, { 8, 1645, 2048 }, { 8, 1646, 2048 }, { 9, 1647, 2048 },
  5383. { 6, 1648, 2048 }, { 7, 1649, 2048 }, { 7, 1650, 2048 }, { 8, 1651, 2048 }, { 7, 1652, 2048 }, { 8, 1653, 2048 }, { 8, 1654, 2048 }, { 9, 1655, 2048 },
  5384. { 7, 1656, 2048 }, { 8, 1657, 2048 }, { 8, 1658, 2048 }, { 9, 1659, 2048 }, { 8, 1660, 2048 }, { 9, 1661, 2048 }, { 9, 1662, 2048 }, { 10, 1663, 2048 },
  5385. { 4, 1664, 2048 }, { 5, 1665, 2048 }, { 5, 1666, 2048 }, { 6, 1667, 2048 }, { 5, 1668, 2048 }, { 6, 1669, 2048 }, { 6, 1670, 2048 }, { 7, 1671, 2048 },
  5386. { 5, 1672, 2048 }, { 6, 1673, 2048 }, { 6, 1674, 2048 }, { 7, 1675, 2048 }, { 6, 1676, 2048 }, { 7, 1677, 2048 }, { 7, 1678, 2048 }, { 8, 1679, 2048 },
  5387. { 5, 1680, 2048 }, { 6, 1681, 2048 }, { 6, 1682, 2048 }, { 7, 1683, 2048 }, { 6, 1684, 2048 }, { 7, 1685, 2048 }, { 7, 1686, 2048 }, { 8, 1687, 2048 },
  5388. { 6, 1688, 2048 }, { 7, 1689, 2048 }, { 7, 1690, 2048 }, { 8, 1691, 2048 }, { 7, 1692, 2048 }, { 8, 1693, 2048 }, { 8, 1694, 2048 }, { 9, 1695, 2048 },
  5389. { 5, 1696, 2048 }, { 6, 1697, 2048 }, { 6, 1698, 2048 }, { 7, 1699, 2048 }, { 6, 1700, 2048 }, { 7, 1701, 2048 }, { 7, 1702, 2048 }, { 8, 1703, 2048 },
  5390. { 6, 1704, 2048 }, { 7, 1705, 2048 }, { 7, 1706, 2048 }, { 8, 1707, 2048 }, { 7, 1708, 2048 }, { 8, 1709, 2048 }, { 8, 1710, 2048 }, { 9, 1711, 2048 },
  5391. { 6, 1712, 2048 }, { 7, 1713, 2048 }, { 7, 1714, 2048 }, { 8, 1715, 2048 }, { 7, 1716, 2048 }, { 8, 1717, 2048 }, { 8, 1718, 2048 }, { 9, 1719, 2048 },
  5392. { 7, 1720, 2048 }, { 8, 1721, 2048 }, { 8, 1722, 2048 }, { 9, 1723, 2048 }, { 8, 1724, 2048 }, { 9, 1725, 2048 }, { 9, 1726, 2048 }, { 10, 1727, 2048 },
  5393. { 5, 1728, 2048 }, { 6, 1729, 2048 }, { 6, 1730, 2048 }, { 7, 1731, 2048 }, { 6, 1732, 2048 }, { 7, 1733, 2048 }, { 7, 1734, 2048 }, { 8, 1735, 2048 },
  5394. { 6, 1736, 2048 }, { 7, 1737, 2048 }, { 7, 1738, 2048 }, { 8, 1739, 2048 }, { 7, 1740, 2048 }, { 8, 1741, 2048 }, { 8, 1742, 2048 }, { 9, 1743, 2048 },
  5395. { 6, 1744, 2048 }, { 7, 1745, 2048 }, { 7, 1746, 2048 }, { 8, 1747, 2048 }, { 7, 1748, 2048 }, { 8, 1749, 2048 }, { 8, 1750, 2048 }, { 9, 1751, 2048 },
  5396. { 7, 1752, 2048 }, { 8, 1753, 2048 }, { 8, 1754, 2048 }, { 9, 1755, 2048 }, { 8, 1756, 2048 }, { 9, 1757, 2048 }, { 9, 1758, 2048 }, { 10, 1759, 2048 },
  5397. { 6, 1760, 2048 }, { 7, 1761, 2048 }, { 7, 1762, 2048 }, { 8, 1763, 2048 }, { 7, 1764, 2048 }, { 8, 1765, 2048 }, { 8, 1766, 2048 }, { 9, 1767, 2048 },
  5398. { 7, 1768, 2048 }, { 8, 1769, 2048 }, { 8, 1770, 2048 }, { 9, 1771, 2048 }, { 8, 1772, 2048 }, { 9, 1773, 2048 }, { 9, 1774, 2048 }, { 10, 1775, 2048 },
  5399. { 7, 1776, 2048 }, { 8, 1777, 2048 }, { 8, 1778, 2048 }, { 9, 1779, 2048 }, { 8, 1780, 2048 }, { 9, 1781, 2048 }, { 9, 1782, 2048 }, { 10, 1783, 2048 },
  5400. { 8, 1784, 2048 }, { 9, 1785, 2048 }, { 9, 1786, 2048 }, { 10, 1787, 2048 }, { 9, 1788, 2048 }, { 10, 1789, 2048 }, { 10, 1790, 2048 }, { 11, 1791, 2048 },
  5401. { 4, 1792, 2048 }, { 5, 1793, 2048 }, { 5, 1794, 2048 }, { 6, 1795, 2048 }, { 5, 1796, 2048 }, { 6, 1797, 2048 }, { 6, 1798, 2048 }, { 7, 1799, 2048 },
  5402. { 5, 1800, 2048 }, { 6, 1801, 2048 }, { 6, 1802, 2048 }, { 7, 1803, 2048 }, { 6, 1804, 2048 }, { 7, 1805, 2048 }, { 7, 1806, 2048 }, { 8, 1807, 2048 },
  5403. { 5, 1808, 2048 }, { 6, 1809, 2048 }, { 6, 1810, 2048 }, { 7, 1811, 2048 }, { 6, 1812, 2048 }, { 7, 1813, 2048 }, { 7, 1814, 2048 }, { 8, 1815, 2048 },
  5404. { 6, 1816, 2048 }, { 7, 1817, 2048 }, { 7, 1818, 2048 }, { 8, 1819, 2048 }, { 7, 1820, 2048 }, { 8, 1821, 2048 }, { 8, 1822, 2048 }, { 9, 1823, 2048 },
  5405. { 5, 1824, 2048 }, { 6, 1825, 2048 }, { 6, 1826, 2048 }, { 7, 1827, 2048 }, { 6, 1828, 2048 }, { 7, 1829, 2048 }, { 7, 1830, 2048 }, { 8, 1831, 2048 },
  5406. { 6, 1832, 2048 }, { 7, 1833, 2048 }, { 7, 1834, 2048 }, { 8, 1835, 2048 }, { 7, 1836, 2048 }, { 8, 1837, 2048 }, { 8, 1838, 2048 }, { 9, 1839, 2048 },
  5407. { 6, 1840, 2048 }, { 7, 1841, 2048 }, { 7, 1842, 2048 }, { 8, 1843, 2048 }, { 7, 1844, 2048 }, { 8, 1845, 2048 }, { 8, 1846, 2048 }, { 9, 1847, 2048 },
  5408. { 7, 1848, 2048 }, { 8, 1849, 2048 }, { 8, 1850, 2048 }, { 9, 1851, 2048 }, { 8, 1852, 2048 }, { 9, 1853, 2048 }, { 9, 1854, 2048 }, { 10, 1855, 2048 },
  5409. { 5, 1856, 2048 }, { 6, 1857, 2048 }, { 6, 1858, 2048 }, { 7, 1859, 2048 }, { 6, 1860, 2048 }, { 7, 1861, 2048 }, { 7, 1862, 2048 }, { 8, 1863, 2048 },
  5410. { 6, 1864, 2048 }, { 7, 1865, 2048 }, { 7, 1866, 2048 }, { 8, 1867, 2048 }, { 7, 1868, 2048 }, { 8, 1869, 2048 }, { 8, 1870, 2048 }, { 9, 1871, 2048 },
  5411. { 6, 1872, 2048 }, { 7, 1873, 2048 }, { 7, 1874, 2048 }, { 8, 1875, 2048 }, { 7, 1876, 2048 }, { 8, 1877, 2048 }, { 8, 1878, 2048 }, { 9, 1879, 2048 },
  5412. { 7, 1880, 2048 }, { 8, 1881, 2048 }, { 8, 1882, 2048 }, { 9, 1883, 2048 }, { 8, 1884, 2048 }, { 9, 1885, 2048 }, { 9, 1886, 2048 }, { 10, 1887, 2048 },
  5413. { 6, 1888, 2048 }, { 7, 1889, 2048 }, { 7, 1890, 2048 }, { 8, 1891, 2048 }, { 7, 1892, 2048 }, { 8, 1893, 2048 }, { 8, 1894, 2048 }, { 9, 1895, 2048 },
  5414. { 7, 1896, 2048 }, { 8, 1897, 2048 }, { 8, 1898, 2048 }, { 9, 1899, 2048 }, { 8, 1900, 2048 }, { 9, 1901, 2048 }, { 9, 1902, 2048 }, { 10, 1903, 2048 },
  5415. { 7, 1904, 2048 }, { 8, 1905, 2048 }, { 8, 1906, 2048 }, { 9, 1907, 2048 }, { 8, 1908, 2048 }, { 9, 1909, 2048 }, { 9, 1910, 2048 }, { 10, 1911, 2048 },
  5416. { 8, 1912, 2048 }, { 9, 1913, 2048 }, { 9, 1914, 2048 }, { 10, 1915, 2048 }, { 9, 1916, 2048 }, { 10, 1917, 2048 }, { 10, 1918, 2048 }, { 11, 1919, 2048 },
  5417. { 5, 1920, 2048 }, { 6, 1921, 2048 }, { 6, 1922, 2048 }, { 7, 1923, 2048 }, { 6, 1924, 2048 }, { 7, 1925, 2048 }, { 7, 1926, 2048 }, { 8, 1927, 2048 },
  5418. { 6, 1928, 2048 }, { 7, 1929, 2048 }, { 7, 1930, 2048 }, { 8, 1931, 2048 }, { 7, 1932, 2048 }, { 8, 1933, 2048 }, { 8, 1934, 2048 }, { 9, 1935, 2048 },
  5419. { 6, 1936, 2048 }, { 7, 1937, 2048 }, { 7, 1938, 2048 }, { 8, 1939, 2048 }, { 7, 1940, 2048 }, { 8, 1941, 2048 }, { 8, 1942, 2048 }, { 9, 1943, 2048 },
  5420. { 7, 1944, 2048 }, { 8, 1945, 2048 }, { 8, 1946, 2048 }, { 9, 1947, 2048 }, { 8, 1948, 2048 }, { 9, 1949, 2048 }, { 9, 1950, 2048 }, { 10, 1951, 2048 },
  5421. { 6, 1952, 2048 }, { 7, 1953, 2048 }, { 7, 1954, 2048 }, { 8, 1955, 2048 }, { 7, 1956, 2048 }, { 8, 1957, 2048 }, { 8, 1958, 2048 }, { 9, 1959, 2048 },
  5422. { 7, 1960, 2048 }, { 8, 1961, 2048 }, { 8, 1962, 2048 }, { 9, 1963, 2048 }, { 8, 1964, 2048 }, { 9, 1965, 2048 }, { 9, 1966, 2048 }, { 10, 1967, 2048 },
  5423. { 7, 1968, 2048 }, { 8, 1969, 2048 }, { 8, 1970, 2048 }, { 9, 1971, 2048 }, { 8, 1972, 2048 }, { 9, 1973, 2048 }, { 9, 1974, 2048 }, { 10, 1975, 2048 },
  5424. { 8, 1976, 2048 }, { 9, 1977, 2048 }, { 9, 1978, 2048 }, { 10, 1979, 2048 }, { 9, 1980, 2048 }, { 10, 1981, 2048 }, { 10, 1982, 2048 }, { 11, 1983, 2048 },
  5425. { 6, 1984, 2048 }, { 7, 1985, 2048 }, { 7, 1986, 2048 }, { 8, 1987, 2048 }, { 7, 1988, 2048 }, { 8, 1989, 2048 }, { 8, 1990, 2048 }, { 9, 1991, 2048 },
  5426. { 7, 1992, 2048 }, { 8, 1993, 2048 }, { 8, 1994, 2048 }, { 9, 1995, 2048 }, { 8, 1996, 2048 }, { 9, 1997, 2048 }, { 9, 1998, 2048 }, { 10, 1999, 2048 },
  5427. { 7, 2000, 2048 }, { 8, 2001, 2048 }, { 8, 2002, 2048 }, { 9, 2003, 2048 }, { 8, 2004, 2048 }, { 9, 2005, 2048 }, { 9, 2006, 2048 }, { 10, 2007, 2048 },
  5428. { 8, 2008, 2048 }, { 9, 2009, 2048 }, { 9, 2010, 2048 }, { 10, 2011, 2048 }, { 9, 2012, 2048 }, { 10, 2013, 2048 }, { 10, 2014, 2048 }, { 11, 2015, 2048 },
  5429. { 7, 2016, 2048 }, { 8, 2017, 2048 }, { 8, 2018, 2048 }, { 9, 2019, 2048 }, { 8, 2020, 2048 }, { 9, 2021, 2048 }, { 9, 2022, 2048 }, { 10, 2023, 2048 },
  5430. { 8, 2024, 2048 }, { 9, 2025, 2048 }, { 9, 2026, 2048 }, { 10, 2027, 2048 }, { 9, 2028, 2048 }, { 10, 2029, 2048 }, { 10, 2030, 2048 }, { 11, 2031, 2048 },
  5431. { 8, 2032, 2048 }, { 9, 2033, 2048 }, { 9, 2034, 2048 }, { 10, 2035, 2048 }, { 9, 2036, 2048 }, { 10, 2037, 2048 }, { 10, 2038, 2048 }, { 11, 2039, 2048 },
  5432. { 9, 2040, 2048 }, { 10, 2041, 2048 }, { 10, 2042, 2048 }, { 11, 2043, 2048 }, { 10, 2044, 2048 }, { 11, 2045, 2048 }, { 11, 2046, 2048 }, { 12, 2047, 2048 },
  5433. #endif
  5434. #endif
  5435. #endif
  5436. #endif
  5437. #endif
  5438. #endif
  5439. };
  5440. /* find a hole and free as required, return -1 if no hole found */
  5441. static int find_hole(void)
  5442. {
  5443. unsigned x;
  5444. int y, z;
  5445. for (z = -1, y = INT_MAX, x = 0; x < FP_ENTRIES; x++) {
  5446. if (fp_cache[x].lru_count < y && fp_cache[x].lock == 0) {
  5447. z = x;
  5448. y = fp_cache[x].lru_count;
  5449. }
  5450. }
  5451. /* decrease all */
  5452. for (x = 0; x < FP_ENTRIES; x++) {
  5453. if (fp_cache[x].lru_count > 3) {
  5454. --(fp_cache[x].lru_count);
  5455. }
  5456. }
  5457. /* free entry z */
  5458. if (z >= 0 && fp_cache[z].g) {
  5459. mp_clear(&fp_cache[z].mu);
  5460. wc_ecc_del_point(fp_cache[z].g);
  5461. fp_cache[z].g = NULL;
  5462. for (x = 0; x < (1U<<FP_LUT); x++) {
  5463. wc_ecc_del_point(fp_cache[z].LUT[x]);
  5464. fp_cache[z].LUT[x] = NULL;
  5465. }
  5466. fp_cache[z].lru_count = 0;
  5467. }
  5468. return z;
  5469. }
  5470. /* determine if a base is already in the cache and if so, where */
  5471. static int find_base(ecc_point* g)
  5472. {
  5473. int x;
  5474. for (x = 0; x < FP_ENTRIES; x++) {
  5475. if (fp_cache[x].g != NULL &&
  5476. mp_cmp(fp_cache[x].g->x, g->x) == MP_EQ &&
  5477. mp_cmp(fp_cache[x].g->y, g->y) == MP_EQ &&
  5478. mp_cmp(fp_cache[x].g->z, g->z) == MP_EQ) {
  5479. break;
  5480. }
  5481. }
  5482. if (x == FP_ENTRIES) {
  5483. x = -1;
  5484. }
  5485. return x;
  5486. }
  5487. /* add a new base to the cache */
  5488. static int add_entry(int idx, ecc_point *g)
  5489. {
  5490. unsigned x, y;
  5491. /* allocate base and LUT */
  5492. fp_cache[idx].g = wc_ecc_new_point();
  5493. if (fp_cache[idx].g == NULL) {
  5494. return GEN_MEM_ERR;
  5495. }
  5496. /* copy x and y */
  5497. if ((mp_copy(g->x, fp_cache[idx].g->x) != MP_OKAY) ||
  5498. (mp_copy(g->y, fp_cache[idx].g->y) != MP_OKAY) ||
  5499. (mp_copy(g->z, fp_cache[idx].g->z) != MP_OKAY)) {
  5500. wc_ecc_del_point(fp_cache[idx].g);
  5501. fp_cache[idx].g = NULL;
  5502. return GEN_MEM_ERR;
  5503. }
  5504. for (x = 0; x < (1U<<FP_LUT); x++) {
  5505. fp_cache[idx].LUT[x] = wc_ecc_new_point();
  5506. if (fp_cache[idx].LUT[x] == NULL) {
  5507. for (y = 0; y < x; y++) {
  5508. wc_ecc_del_point(fp_cache[idx].LUT[y]);
  5509. fp_cache[idx].LUT[y] = NULL;
  5510. }
  5511. wc_ecc_del_point(fp_cache[idx].g);
  5512. fp_cache[idx].g = NULL;
  5513. fp_cache[idx].lru_count = 0;
  5514. return GEN_MEM_ERR;
  5515. }
  5516. }
  5517. fp_cache[idx].lru_count = 0;
  5518. return MP_OKAY;
  5519. }
  5520. /* build the LUT by spacing the bits of the input by #modulus/FP_LUT bits apart
  5521. *
  5522. * The algorithm builds patterns in increasing bit order by first making all
  5523. * single bit input patterns, then all two bit input patterns and so on
  5524. */
  5525. static int build_lut(int idx, mp_int* a, mp_int* modulus, mp_digit mp,
  5526. mp_int* mu)
  5527. {
  5528. int err;
  5529. unsigned x, y, bitlen, lut_gap;
  5530. mp_int tmp;
  5531. if (mp_init(&tmp) != MP_OKAY)
  5532. return GEN_MEM_ERR;
  5533. /* sanity check to make sure lut_order table is of correct size,
  5534. should compile out to a NOP if true */
  5535. if ((sizeof(lut_orders) / sizeof(lut_orders[0])) < (1U<<FP_LUT)) {
  5536. err = BAD_FUNC_ARG;
  5537. }
  5538. else {
  5539. /* get bitlen and round up to next multiple of FP_LUT */
  5540. bitlen = mp_unsigned_bin_size(modulus) << 3;
  5541. x = bitlen % FP_LUT;
  5542. if (x) {
  5543. bitlen += FP_LUT - x;
  5544. }
  5545. lut_gap = bitlen / FP_LUT;
  5546. /* init the mu */
  5547. err = mp_init_copy(&fp_cache[idx].mu, mu);
  5548. }
  5549. /* copy base */
  5550. if (err == MP_OKAY) {
  5551. if ((mp_mulmod(fp_cache[idx].g->x, mu, modulus,
  5552. fp_cache[idx].LUT[1]->x) != MP_OKAY) ||
  5553. (mp_mulmod(fp_cache[idx].g->y, mu, modulus,
  5554. fp_cache[idx].LUT[1]->y) != MP_OKAY) ||
  5555. (mp_mulmod(fp_cache[idx].g->z, mu, modulus,
  5556. fp_cache[idx].LUT[1]->z) != MP_OKAY)) {
  5557. err = MP_MULMOD_E;
  5558. }
  5559. }
  5560. /* make all single bit entries */
  5561. for (x = 1; x < FP_LUT; x++) {
  5562. if (err != MP_OKAY)
  5563. break;
  5564. if ((mp_copy(fp_cache[idx].LUT[1<<(x-1)]->x,
  5565. fp_cache[idx].LUT[1<<x]->x) != MP_OKAY) ||
  5566. (mp_copy(fp_cache[idx].LUT[1<<(x-1)]->y,
  5567. fp_cache[idx].LUT[1<<x]->y) != MP_OKAY) ||
  5568. (mp_copy(fp_cache[idx].LUT[1<<(x-1)]->z,
  5569. fp_cache[idx].LUT[1<<x]->z) != MP_OKAY)){
  5570. err = MP_INIT_E;
  5571. break;
  5572. } else {
  5573. /* now double it bitlen/FP_LUT times */
  5574. for (y = 0; y < lut_gap; y++) {
  5575. if ((err = ecc_projective_dbl_point(fp_cache[idx].LUT[1<<x],
  5576. fp_cache[idx].LUT[1<<x], a, modulus, mp)) != MP_OKAY) {
  5577. break;
  5578. }
  5579. }
  5580. }
  5581. }
  5582. /* now make all entries in increase order of hamming weight */
  5583. for (x = 2; x <= FP_LUT; x++) {
  5584. if (err != MP_OKAY)
  5585. break;
  5586. for (y = 0; y < (1UL<<FP_LUT); y++) {
  5587. if (lut_orders[y].ham != (int)x) continue;
  5588. /* perform the add */
  5589. if ((err = ecc_projective_add_point(
  5590. fp_cache[idx].LUT[lut_orders[y].terma],
  5591. fp_cache[idx].LUT[lut_orders[y].termb],
  5592. fp_cache[idx].LUT[y], a, modulus, mp)) != MP_OKAY) {
  5593. break;
  5594. }
  5595. }
  5596. }
  5597. /* now map all entries back to affine space to make point addition faster */
  5598. for (x = 1; x < (1UL<<FP_LUT); x++) {
  5599. if (err != MP_OKAY)
  5600. break;
  5601. /* convert z to normal from montgomery */
  5602. err = mp_montgomery_reduce(fp_cache[idx].LUT[x]->z, modulus, mp);
  5603. /* invert it */
  5604. if (err == MP_OKAY)
  5605. err = mp_invmod(fp_cache[idx].LUT[x]->z, modulus,
  5606. fp_cache[idx].LUT[x]->z);
  5607. if (err == MP_OKAY)
  5608. /* now square it */
  5609. err = mp_sqrmod(fp_cache[idx].LUT[x]->z, modulus, &tmp);
  5610. if (err == MP_OKAY)
  5611. /* fix x */
  5612. err = mp_mulmod(fp_cache[idx].LUT[x]->x, &tmp, modulus,
  5613. fp_cache[idx].LUT[x]->x);
  5614. if (err == MP_OKAY)
  5615. /* get 1/z^3 */
  5616. err = mp_mulmod(&tmp, fp_cache[idx].LUT[x]->z, modulus, &tmp);
  5617. if (err == MP_OKAY)
  5618. /* fix y */
  5619. err = mp_mulmod(fp_cache[idx].LUT[x]->y, &tmp, modulus,
  5620. fp_cache[idx].LUT[x]->y);
  5621. if (err == MP_OKAY)
  5622. /* free z */
  5623. mp_clear(fp_cache[idx].LUT[x]->z);
  5624. }
  5625. mp_clear(&tmp);
  5626. if (err == MP_OKAY)
  5627. return MP_OKAY;
  5628. /* err cleanup */
  5629. for (y = 0; y < (1U<<FP_LUT); y++) {
  5630. wc_ecc_del_point(fp_cache[idx].LUT[y]);
  5631. fp_cache[idx].LUT[y] = NULL;
  5632. }
  5633. wc_ecc_del_point(fp_cache[idx].g);
  5634. fp_cache[idx].g = NULL;
  5635. fp_cache[idx].lru_count = 0;
  5636. mp_clear(&fp_cache[idx].mu);
  5637. return err;
  5638. }
  5639. /* perform a fixed point ECC mulmod */
  5640. static int accel_fp_mul(int idx, mp_int* k, ecc_point *R, mp_int* a,
  5641. mp_int* modulus, mp_digit mp, int map)
  5642. {
  5643. #define KB_SIZE 128
  5644. #ifdef WOLFSSL_SMALL_STACK
  5645. unsigned char* kb = NULL;
  5646. #else
  5647. unsigned char kb[KB_SIZE];
  5648. #endif
  5649. int x, err;
  5650. unsigned y, z = 0, bitlen, bitpos, lut_gap, first;
  5651. mp_int tk, order;
  5652. if (mp_init_multi(&tk, &order, NULL, NULL, NULL, NULL) != MP_OKAY)
  5653. return MP_INIT_E;
  5654. /* if it's smaller than modulus we fine */
  5655. if (mp_unsigned_bin_size(k) > mp_unsigned_bin_size(modulus)) {
  5656. /* find order */
  5657. y = mp_unsigned_bin_size(modulus);
  5658. for (x = 0; ecc_sets[x].size; x++) {
  5659. if (y <= (unsigned)ecc_sets[x].size) break;
  5660. }
  5661. /* back off if we are on the 521 bit curve */
  5662. if (y == 66) --x;
  5663. if ((err = mp_read_radix(&order, ecc_sets[x].order,
  5664. MP_RADIX_HEX)) != MP_OKAY) {
  5665. goto done;
  5666. }
  5667. /* k must be less than modulus */
  5668. if (mp_cmp(k, &order) != MP_LT) {
  5669. if ((err = mp_mod(k, &order, &tk)) != MP_OKAY) {
  5670. goto done;
  5671. }
  5672. } else {
  5673. if ((err = mp_copy(k, &tk)) != MP_OKAY) {
  5674. goto done;
  5675. }
  5676. }
  5677. } else {
  5678. if ((err = mp_copy(k, &tk)) != MP_OKAY) {
  5679. goto done;
  5680. }
  5681. }
  5682. /* get bitlen and round up to next multiple of FP_LUT */
  5683. bitlen = mp_unsigned_bin_size(modulus) << 3;
  5684. x = bitlen % FP_LUT;
  5685. if (x) {
  5686. bitlen += FP_LUT - x;
  5687. }
  5688. lut_gap = bitlen / FP_LUT;
  5689. /* get the k value */
  5690. if (mp_unsigned_bin_size(&tk) > (int)(KB_SIZE - 2)) {
  5691. err = BUFFER_E; goto done;
  5692. }
  5693. /* store k */
  5694. #ifdef WOLFSSL_SMALL_STACK
  5695. kb = (unsigned char*)XMALLOC(KB_SIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  5696. if (kb == NULL) {
  5697. err = MEMORY_E; goto done;
  5698. }
  5699. #endif
  5700. XMEMSET(kb, 0, KB_SIZE);
  5701. if ((err = mp_to_unsigned_bin(&tk, kb)) == MP_OKAY) {
  5702. /* let's reverse kb so it's little endian */
  5703. x = 0;
  5704. y = mp_unsigned_bin_size(&tk);
  5705. if (y > 0) {
  5706. y -= 1;
  5707. }
  5708. while ((unsigned)x < y) {
  5709. z = kb[x]; kb[x] = kb[y]; kb[y] = (byte)z;
  5710. ++x; --y;
  5711. }
  5712. /* at this point we can start, yipee */
  5713. first = 1;
  5714. for (x = lut_gap-1; x >= 0; x--) {
  5715. /* extract FP_LUT bits from kb spread out by lut_gap bits and offset
  5716. by x bits from the start */
  5717. bitpos = x;
  5718. for (y = z = 0; y < FP_LUT; y++) {
  5719. z |= ((kb[bitpos>>3] >> (bitpos&7)) & 1) << y;
  5720. bitpos += lut_gap; /* it's y*lut_gap + x, but here we can avoid
  5721. the mult in each loop */
  5722. }
  5723. /* double if not first */
  5724. if (!first) {
  5725. if ((err = ecc_projective_dbl_point(R, R, a, modulus,
  5726. mp)) != MP_OKAY) {
  5727. break;
  5728. }
  5729. }
  5730. /* add if not first, otherwise copy */
  5731. if (!first && z) {
  5732. if ((err = ecc_projective_add_point(R, fp_cache[idx].LUT[z], R,
  5733. a, modulus, mp)) != MP_OKAY) {
  5734. break;
  5735. }
  5736. } else if (z) {
  5737. if ((mp_copy(fp_cache[idx].LUT[z]->x, R->x) != MP_OKAY) ||
  5738. (mp_copy(fp_cache[idx].LUT[z]->y, R->y) != MP_OKAY) ||
  5739. (mp_copy(&fp_cache[idx].mu, R->z) != MP_OKAY)) {
  5740. err = GEN_MEM_ERR;
  5741. break;
  5742. }
  5743. first = 0;
  5744. }
  5745. }
  5746. }
  5747. if (err == MP_OKAY) {
  5748. (void) z; /* Acknowledge the unused assignment */
  5749. ForceZero(kb, KB_SIZE);
  5750. /* map R back from projective space */
  5751. if (map) {
  5752. err = ecc_map(R, modulus, mp);
  5753. } else {
  5754. err = MP_OKAY;
  5755. }
  5756. }
  5757. done:
  5758. /* cleanup */
  5759. mp_clear(&order);
  5760. mp_clear(&tk);
  5761. #ifdef WOLFSSL_SMALL_STACK
  5762. XFREE(kb, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  5763. #endif
  5764. #undef KB_SIZE
  5765. return err;
  5766. }
  5767. #ifdef ECC_SHAMIR
  5768. /* perform a fixed point ECC mulmod */
  5769. static int accel_fp_mul2add(int idx1, int idx2,
  5770. mp_int* kA, mp_int* kB,
  5771. ecc_point *R, mp_int* a,
  5772. mp_int* modulus, mp_digit mp)
  5773. {
  5774. #define KB_SIZE 128
  5775. #ifdef WOLFSSL_SMALL_STACK
  5776. unsigned char* kb[2] = {NULL, NULL};
  5777. #else
  5778. unsigned char kb[2][KB_SIZE];
  5779. #endif
  5780. int x, err;
  5781. unsigned y, z, bitlen, bitpos, lut_gap, first, zA, zB;
  5782. mp_int tka, tkb, order;
  5783. if (mp_init_multi(&tka, &tkb, &order, NULL, NULL, NULL) != MP_OKAY)
  5784. return MP_INIT_E;
  5785. /* if it's smaller than modulus we fine */
  5786. if (mp_unsigned_bin_size(kA) > mp_unsigned_bin_size(modulus)) {
  5787. /* find order */
  5788. y = mp_unsigned_bin_size(modulus);
  5789. for (x = 0; ecc_sets[x].size; x++) {
  5790. if (y <= (unsigned)ecc_sets[x].size) break;
  5791. }
  5792. /* back off if we are on the 521 bit curve */
  5793. if (y == 66) --x;
  5794. if ((err = mp_read_radix(&order, ecc_sets[x].order,
  5795. MP_RADIX_HEX)) != MP_OKAY) {
  5796. goto done;
  5797. }
  5798. /* kA must be less than modulus */
  5799. if (mp_cmp(kA, &order) != MP_LT) {
  5800. if ((err = mp_mod(kA, &order, &tka)) != MP_OKAY) {
  5801. goto done;
  5802. }
  5803. } else {
  5804. if ((err = mp_copy(kA, &tka)) != MP_OKAY) {
  5805. goto done;
  5806. }
  5807. }
  5808. } else {
  5809. if ((err = mp_copy(kA, &tka)) != MP_OKAY) {
  5810. goto done;
  5811. }
  5812. }
  5813. /* if it's smaller than modulus we fine */
  5814. if (mp_unsigned_bin_size(kB) > mp_unsigned_bin_size(modulus)) {
  5815. /* find order */
  5816. y = mp_unsigned_bin_size(modulus);
  5817. for (x = 0; ecc_sets[x].size; x++) {
  5818. if (y <= (unsigned)ecc_sets[x].size) break;
  5819. }
  5820. /* back off if we are on the 521 bit curve */
  5821. if (y == 66) --x;
  5822. if ((err = mp_read_radix(&order, ecc_sets[x].order,
  5823. MP_RADIX_HEX)) != MP_OKAY) {
  5824. goto done;
  5825. }
  5826. /* kB must be less than modulus */
  5827. if (mp_cmp(kB, &order) != MP_LT) {
  5828. if ((err = mp_mod(kB, &order, &tkb)) != MP_OKAY) {
  5829. goto done;
  5830. }
  5831. } else {
  5832. if ((err = mp_copy(kB, &tkb)) != MP_OKAY) {
  5833. goto done;
  5834. }
  5835. }
  5836. } else {
  5837. if ((err = mp_copy(kB, &tkb)) != MP_OKAY) {
  5838. goto done;
  5839. }
  5840. }
  5841. /* get bitlen and round up to next multiple of FP_LUT */
  5842. bitlen = mp_unsigned_bin_size(modulus) << 3;
  5843. x = bitlen % FP_LUT;
  5844. if (x) {
  5845. bitlen += FP_LUT - x;
  5846. }
  5847. lut_gap = bitlen / FP_LUT;
  5848. /* get the k value */
  5849. if ((mp_unsigned_bin_size(&tka) > (int)(KB_SIZE - 2)) ||
  5850. (mp_unsigned_bin_size(&tkb) > (int)(KB_SIZE - 2)) ) {
  5851. err = BUFFER_E; goto done;
  5852. }
  5853. /* store k */
  5854. #ifdef WOLFSSL_SMALL_STACK
  5855. kb[0] = (unsigned char*)XMALLOC(KB_SIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  5856. if (kb[0] == NULL) {
  5857. err = MEMORY_E; goto done;
  5858. }
  5859. #endif
  5860. XMEMSET(kb[0], 0, KB_SIZE);
  5861. if ((err = mp_to_unsigned_bin(&tka, kb[0])) != MP_OKAY) {
  5862. goto done;
  5863. }
  5864. /* let's reverse kb so it's little endian */
  5865. x = 0;
  5866. y = mp_unsigned_bin_size(&tka);
  5867. if (y > 0) {
  5868. y -= 1;
  5869. }
  5870. mp_clear(&tka);
  5871. while ((unsigned)x < y) {
  5872. z = kb[0][x]; kb[0][x] = kb[0][y]; kb[0][y] = (byte)z;
  5873. ++x; --y;
  5874. }
  5875. /* store b */
  5876. #ifdef WOLFSSL_SMALL_STACK
  5877. kb[1] = (unsigned char*)XMALLOC(KB_SIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  5878. if (kb[1] == NULL) {
  5879. err = MEMORY_E; goto done;
  5880. }
  5881. #endif
  5882. XMEMSET(kb[1], 0, KB_SIZE);
  5883. if ((err = mp_to_unsigned_bin(&tkb, kb[1])) == MP_OKAY) {
  5884. x = 0;
  5885. y = mp_unsigned_bin_size(&tkb);
  5886. if (y > 0) {
  5887. y -= 1;
  5888. }
  5889. while ((unsigned)x < y) {
  5890. z = kb[1][x]; kb[1][x] = kb[1][y]; kb[1][y] = (byte)z;
  5891. ++x; --y;
  5892. }
  5893. /* at this point we can start, yipee */
  5894. first = 1;
  5895. for (x = lut_gap-1; x >= 0; x--) {
  5896. /* extract FP_LUT bits from kb spread out by lut_gap bits and
  5897. offset by x bits from the start */
  5898. bitpos = x;
  5899. for (y = zA = zB = 0; y < FP_LUT; y++) {
  5900. zA |= ((kb[0][bitpos>>3] >> (bitpos&7)) & 1) << y;
  5901. zB |= ((kb[1][bitpos>>3] >> (bitpos&7)) & 1) << y;
  5902. bitpos += lut_gap; /* it's y*lut_gap + x, but here we can avoid
  5903. the mult in each loop */
  5904. }
  5905. /* double if not first */
  5906. if (!first) {
  5907. if ((err = ecc_projective_dbl_point(R, R, a, modulus,
  5908. mp)) != MP_OKAY) {
  5909. break;
  5910. }
  5911. }
  5912. /* add if not first, otherwise copy */
  5913. if (!first) {
  5914. if (zA) {
  5915. if ((err = ecc_projective_add_point(R, fp_cache[idx1].LUT[zA],
  5916. R, a, modulus, mp)) != MP_OKAY) {
  5917. break;
  5918. }
  5919. }
  5920. if (zB) {
  5921. if ((err = ecc_projective_add_point(R, fp_cache[idx2].LUT[zB],
  5922. R, a, modulus, mp)) != MP_OKAY) {
  5923. break;
  5924. }
  5925. }
  5926. } else {
  5927. if (zA) {
  5928. if ((mp_copy(fp_cache[idx1].LUT[zA]->x, R->x) != MP_OKAY) ||
  5929. (mp_copy(fp_cache[idx1].LUT[zA]->y, R->y) != MP_OKAY) ||
  5930. (mp_copy(&fp_cache[idx1].mu, R->z) != MP_OKAY)) {
  5931. err = GEN_MEM_ERR;
  5932. break;
  5933. }
  5934. first = 0;
  5935. }
  5936. if (zB && first == 0) {
  5937. if (zB) {
  5938. if ((err = ecc_projective_add_point(R,
  5939. fp_cache[idx2].LUT[zB], R, a, modulus, mp)) != MP_OKAY){
  5940. break;
  5941. }
  5942. }
  5943. } else if (zB && first == 1) {
  5944. if ((mp_copy(fp_cache[idx2].LUT[zB]->x, R->x) != MP_OKAY) ||
  5945. (mp_copy(fp_cache[idx2].LUT[zB]->y, R->y) != MP_OKAY) ||
  5946. (mp_copy(&fp_cache[idx2].mu, R->z) != MP_OKAY)) {
  5947. err = GEN_MEM_ERR;
  5948. break;
  5949. }
  5950. first = 0;
  5951. }
  5952. }
  5953. }
  5954. }
  5955. done:
  5956. /* cleanup */
  5957. mp_clear(&tkb);
  5958. mp_clear(&tka);
  5959. mp_clear(&order);
  5960. #ifdef WOLFSSL_SMALL_STACK
  5961. if (kb[0])
  5962. #endif
  5963. ForceZero(kb[0], KB_SIZE);
  5964. #ifdef WOLFSSL_SMALL_STACK
  5965. if (kb[1])
  5966. #endif
  5967. ForceZero(kb[1], KB_SIZE);
  5968. #ifdef WOLFSSL_SMALL_STACK
  5969. XFREE(kb[0], NULL, DYNAMIC_TYPE_ECC_BUFFER);
  5970. XFREE(kb[1], NULL, DYNAMIC_TYPE_ECC_BUFFER);
  5971. #endif
  5972. #undef KB_SIZE
  5973. if (err != MP_OKAY)
  5974. return err;
  5975. return ecc_map(R, modulus, mp);
  5976. }
  5977. /** ECC Fixed Point mulmod global with heap hint used
  5978. Computes kA*A + kB*B = C using Shamir's Trick
  5979. A First point to multiply
  5980. kA What to multiple A by
  5981. B Second point to multiply
  5982. kB What to multiple B by
  5983. C [out] Destination point (can overlap with A or B)
  5984. a ECC curve parameter a
  5985. modulus Modulus for curve
  5986. return MP_OKAY on success
  5987. */
  5988. int ecc_mul2add(ecc_point* A, mp_int* kA,
  5989. ecc_point* B, mp_int* kB,
  5990. ecc_point* C, mp_int* a, mp_int* modulus, void* heap)
  5991. {
  5992. int idx1 = -1, idx2 = -1, err = MP_OKAY, mpInit = 0;
  5993. mp_digit mp;
  5994. mp_int mu;
  5995. err = mp_init(&mu);
  5996. if (err != MP_OKAY)
  5997. return err;
  5998. #ifndef HAVE_THREAD_LS
  5999. if (initMutex == 0) {
  6000. wc_InitMutex(&ecc_fp_lock);
  6001. initMutex = 1;
  6002. }
  6003. if (wc_LockMutex(&ecc_fp_lock) != 0)
  6004. return BAD_MUTEX_E;
  6005. #endif /* HAVE_THREAD_LS */
  6006. /* find point */
  6007. idx1 = find_base(A);
  6008. /* no entry? */
  6009. if (idx1 == -1) {
  6010. /* find hole and add it */
  6011. if ((idx1 = find_hole()) >= 0) {
  6012. err = add_entry(idx1, A);
  6013. }
  6014. }
  6015. if (err == MP_OKAY && idx1 != -1) {
  6016. /* increment LRU */
  6017. ++(fp_cache[idx1].lru_count);
  6018. }
  6019. if (err == MP_OKAY)
  6020. /* find point */
  6021. idx2 = find_base(B);
  6022. if (err == MP_OKAY) {
  6023. /* no entry? */
  6024. if (idx2 == -1) {
  6025. /* find hole and add it */
  6026. if ((idx2 = find_hole()) >= 0)
  6027. err = add_entry(idx2, B);
  6028. }
  6029. }
  6030. if (err == MP_OKAY && idx2 != -1) {
  6031. /* increment LRU */
  6032. ++(fp_cache[idx2].lru_count);
  6033. }
  6034. if (err == MP_OKAY) {
  6035. /* if it's 2 build the LUT, if it's higher just use the LUT */
  6036. if (idx1 >= 0 && fp_cache[idx1].lru_count == 2) {
  6037. /* compute mp */
  6038. err = mp_montgomery_setup(modulus, &mp);
  6039. if (err == MP_OKAY) {
  6040. mpInit = 1;
  6041. err = mp_montgomery_calc_normalization(&mu, modulus);
  6042. }
  6043. if (err == MP_OKAY)
  6044. /* build the LUT */
  6045. err = build_lut(idx1, a, modulus, mp, &mu);
  6046. }
  6047. }
  6048. if (err == MP_OKAY) {
  6049. /* if it's 2 build the LUT, if it's higher just use the LUT */
  6050. if (idx2 >= 0 && fp_cache[idx2].lru_count == 2) {
  6051. if (mpInit == 0) {
  6052. /* compute mp */
  6053. err = mp_montgomery_setup(modulus, &mp);
  6054. if (err == MP_OKAY) {
  6055. mpInit = 1;
  6056. err = mp_montgomery_calc_normalization(&mu, modulus);
  6057. }
  6058. }
  6059. if (err == MP_OKAY)
  6060. /* build the LUT */
  6061. err = build_lut(idx2, a, modulus, mp, &mu);
  6062. }
  6063. }
  6064. if (err == MP_OKAY) {
  6065. if (idx1 >=0 && idx2 >= 0 && fp_cache[idx1].lru_count >= 2 &&
  6066. fp_cache[idx2].lru_count >= 2) {
  6067. if (mpInit == 0) {
  6068. /* compute mp */
  6069. err = mp_montgomery_setup(modulus, &mp);
  6070. }
  6071. if (err == MP_OKAY)
  6072. err = accel_fp_mul2add(idx1, idx2, kA, kB, C, a, modulus, mp);
  6073. } else {
  6074. err = normal_ecc_mul2add(A, kA, B, kB, C, a, modulus, heap);
  6075. }
  6076. }
  6077. #ifndef HAVE_THREAD_LS
  6078. wc_UnLockMutex(&ecc_fp_lock);
  6079. #endif /* HAVE_THREAD_LS */
  6080. mp_clear(&mu);
  6081. return err;
  6082. }
  6083. #endif /* ECC_SHAMIR */
  6084. /** ECC Fixed Point mulmod global
  6085. k The multiplicand
  6086. G Base point to multiply
  6087. R [out] Destination of product
  6088. a ECC curve parameter a
  6089. modulus The modulus for the curve
  6090. map [boolean] If non-zero maps the point back to affine co-ordinates,
  6091. otherwise it's left in jacobian-montgomery form
  6092. return MP_OKAY if successful
  6093. */
  6094. int wc_ecc_mulmod_ex(mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
  6095. mp_int* modulus, int map, void* heap)
  6096. {
  6097. int idx, err = MP_OKAY;
  6098. mp_digit mp;
  6099. mp_int mu;
  6100. int mpSetup = 0;
  6101. if (k == NULL || G == NULL || R == NULL || a == NULL || modulus == NULL) {
  6102. return ECC_BAD_ARG_E;
  6103. }
  6104. if (mp_init(&mu) != MP_OKAY)
  6105. return MP_INIT_E;
  6106. #ifndef HAVE_THREAD_LS
  6107. if (initMutex == 0) {
  6108. wc_InitMutex(&ecc_fp_lock);
  6109. initMutex = 1;
  6110. }
  6111. if (wc_LockMutex(&ecc_fp_lock) != 0)
  6112. return BAD_MUTEX_E;
  6113. #endif /* HAVE_THREAD_LS */
  6114. /* find point */
  6115. idx = find_base(G);
  6116. /* no entry? */
  6117. if (idx == -1) {
  6118. /* find hole and add it */
  6119. idx = find_hole();
  6120. if (idx >= 0)
  6121. err = add_entry(idx, G);
  6122. }
  6123. if (err == MP_OKAY && idx >= 0) {
  6124. /* increment LRU */
  6125. ++(fp_cache[idx].lru_count);
  6126. }
  6127. if (err == MP_OKAY) {
  6128. /* if it's 2 build the LUT, if it's higher just use the LUT */
  6129. if (idx >= 0 && fp_cache[idx].lru_count == 2) {
  6130. /* compute mp */
  6131. err = mp_montgomery_setup(modulus, &mp);
  6132. if (err == MP_OKAY) {
  6133. /* compute mu */
  6134. mpSetup = 1;
  6135. err = mp_montgomery_calc_normalization(&mu, modulus);
  6136. }
  6137. if (err == MP_OKAY)
  6138. /* build the LUT */
  6139. err = build_lut(idx, a, modulus, mp, &mu);
  6140. }
  6141. }
  6142. if (err == MP_OKAY) {
  6143. if (idx >= 0 && fp_cache[idx].lru_count >= 2) {
  6144. if (mpSetup == 0) {
  6145. /* compute mp */
  6146. err = mp_montgomery_setup(modulus, &mp);
  6147. }
  6148. if (err == MP_OKAY)
  6149. err = accel_fp_mul(idx, k, R, a, modulus, mp, map);
  6150. } else {
  6151. err = normal_ecc_mulmod(k, G, R, a, modulus, map, heap);
  6152. }
  6153. }
  6154. #ifndef HAVE_THREAD_LS
  6155. wc_UnLockMutex(&ecc_fp_lock);
  6156. #endif /* HAVE_THREAD_LS */
  6157. mp_clear(&mu);
  6158. return err;
  6159. }
  6160. /* helper function for freeing the cache ...
  6161. must be called with the cache mutex locked */
  6162. static void wc_ecc_fp_free_cache(void)
  6163. {
  6164. unsigned x, y;
  6165. for (x = 0; x < FP_ENTRIES; x++) {
  6166. if (fp_cache[x].g != NULL) {
  6167. for (y = 0; y < (1U<<FP_LUT); y++) {
  6168. wc_ecc_del_point(fp_cache[x].LUT[y]);
  6169. fp_cache[x].LUT[y] = NULL;
  6170. }
  6171. wc_ecc_del_point(fp_cache[x].g);
  6172. fp_cache[x].g = NULL;
  6173. mp_clear(&fp_cache[x].mu);
  6174. fp_cache[x].lru_count = 0;
  6175. fp_cache[x].lock = 0;
  6176. }
  6177. }
  6178. }
  6179. /** Free the Fixed Point cache */
  6180. void wc_ecc_fp_free(void)
  6181. {
  6182. #ifndef HAVE_THREAD_LS
  6183. if (initMutex == 0) {
  6184. wc_InitMutex(&ecc_fp_lock);
  6185. initMutex = 1;
  6186. }
  6187. if (wc_LockMutex(&ecc_fp_lock) == 0) {
  6188. #endif /* HAVE_THREAD_LS */
  6189. wc_ecc_fp_free_cache();
  6190. #ifndef HAVE_THREAD_LS
  6191. wc_UnLockMutex(&ecc_fp_lock);
  6192. wc_FreeMutex(&ecc_fp_lock);
  6193. initMutex = 0;
  6194. }
  6195. #endif /* HAVE_THREAD_LS */
  6196. }
  6197. #endif /* FP_ECC */
  6198. #ifdef HAVE_ECC_ENCRYPT
  6199. enum ecCliState {
  6200. ecCLI_INIT = 1,
  6201. ecCLI_SALT_GET = 2,
  6202. ecCLI_SALT_SET = 3,
  6203. ecCLI_SENT_REQ = 4,
  6204. ecCLI_RECV_RESP = 5,
  6205. ecCLI_BAD_STATE = 99
  6206. };
  6207. enum ecSrvState {
  6208. ecSRV_INIT = 1,
  6209. ecSRV_SALT_GET = 2,
  6210. ecSRV_SALT_SET = 3,
  6211. ecSRV_RECV_REQ = 4,
  6212. ecSRV_SENT_RESP = 5,
  6213. ecSRV_BAD_STATE = 99
  6214. };
  6215. struct ecEncCtx {
  6216. const byte* kdfSalt; /* optional salt for kdf */
  6217. const byte* kdfInfo; /* optional info for kdf */
  6218. const byte* macSalt; /* optional salt for mac */
  6219. word32 kdfSaltSz; /* size of kdfSalt */
  6220. word32 kdfInfoSz; /* size of kdfInfo */
  6221. word32 macSaltSz; /* size of macSalt */
  6222. void* heap; /* heap hint for memory used */
  6223. byte clientSalt[EXCHANGE_SALT_SZ]; /* for msg exchange */
  6224. byte serverSalt[EXCHANGE_SALT_SZ]; /* for msg exchange */
  6225. byte encAlgo; /* which encryption type */
  6226. byte kdfAlgo; /* which key derivation function type */
  6227. byte macAlgo; /* which mac function type */
  6228. byte protocol; /* are we REQ_RESP client or server ? */
  6229. byte cliSt; /* protocol state, for sanity checks */
  6230. byte srvSt; /* protocol state, for sanity checks */
  6231. };
  6232. const byte* wc_ecc_ctx_get_own_salt(ecEncCtx* ctx)
  6233. {
  6234. if (ctx == NULL || ctx->protocol == 0)
  6235. return NULL;
  6236. if (ctx->protocol == REQ_RESP_CLIENT) {
  6237. if (ctx->cliSt == ecCLI_INIT) {
  6238. ctx->cliSt = ecCLI_SALT_GET;
  6239. return ctx->clientSalt;
  6240. }
  6241. else {
  6242. ctx->cliSt = ecCLI_BAD_STATE;
  6243. return NULL;
  6244. }
  6245. }
  6246. else if (ctx->protocol == REQ_RESP_SERVER) {
  6247. if (ctx->srvSt == ecSRV_INIT) {
  6248. ctx->srvSt = ecSRV_SALT_GET;
  6249. return ctx->serverSalt;
  6250. }
  6251. else {
  6252. ctx->srvSt = ecSRV_BAD_STATE;
  6253. return NULL;
  6254. }
  6255. }
  6256. return NULL;
  6257. }
  6258. /* optional set info, can be called before or after set_peer_salt */
  6259. int wc_ecc_ctx_set_info(ecEncCtx* ctx, const byte* info, int sz)
  6260. {
  6261. if (ctx == NULL || info == 0 || sz < 0)
  6262. return BAD_FUNC_ARG;
  6263. ctx->kdfInfo = info;
  6264. ctx->kdfInfoSz = sz;
  6265. return 0;
  6266. }
  6267. static const char* exchange_info = "Secure Message Exchange";
  6268. int wc_ecc_ctx_set_peer_salt(ecEncCtx* ctx, const byte* salt)
  6269. {
  6270. byte tmp[EXCHANGE_SALT_SZ/2];
  6271. int halfSz = EXCHANGE_SALT_SZ/2;
  6272. if (ctx == NULL || ctx->protocol == 0 || salt == NULL)
  6273. return BAD_FUNC_ARG;
  6274. if (ctx->protocol == REQ_RESP_CLIENT) {
  6275. XMEMCPY(ctx->serverSalt, salt, EXCHANGE_SALT_SZ);
  6276. if (ctx->cliSt == ecCLI_SALT_GET)
  6277. ctx->cliSt = ecCLI_SALT_SET;
  6278. else {
  6279. ctx->cliSt = ecCLI_BAD_STATE;
  6280. return BAD_STATE_E;
  6281. }
  6282. }
  6283. else {
  6284. XMEMCPY(ctx->clientSalt, salt, EXCHANGE_SALT_SZ);
  6285. if (ctx->srvSt == ecSRV_SALT_GET)
  6286. ctx->srvSt = ecSRV_SALT_SET;
  6287. else {
  6288. ctx->srvSt = ecSRV_BAD_STATE;
  6289. return BAD_STATE_E;
  6290. }
  6291. }
  6292. /* mix half and half */
  6293. /* tmp stores 2nd half of client before overwrite */
  6294. XMEMCPY(tmp, ctx->clientSalt + halfSz, halfSz);
  6295. XMEMCPY(ctx->clientSalt + halfSz, ctx->serverSalt, halfSz);
  6296. XMEMCPY(ctx->serverSalt, tmp, halfSz);
  6297. ctx->kdfSalt = ctx->clientSalt;
  6298. ctx->kdfSaltSz = EXCHANGE_SALT_SZ;
  6299. ctx->macSalt = ctx->serverSalt;
  6300. ctx->macSaltSz = EXCHANGE_SALT_SZ;
  6301. if (ctx->kdfInfo == NULL) {
  6302. /* default info */
  6303. ctx->kdfInfo = (const byte*)exchange_info;
  6304. ctx->kdfInfoSz = EXCHANGE_INFO_SZ;
  6305. }
  6306. return 0;
  6307. }
  6308. static int ecc_ctx_set_salt(ecEncCtx* ctx, int flags, WC_RNG* rng)
  6309. {
  6310. byte* saltBuffer = NULL;
  6311. if (ctx == NULL || rng == NULL || flags == 0)
  6312. return BAD_FUNC_ARG;
  6313. saltBuffer = (flags == REQ_RESP_CLIENT) ? ctx->clientSalt : ctx->serverSalt;
  6314. return wc_RNG_GenerateBlock(rng, saltBuffer, EXCHANGE_SALT_SZ);
  6315. }
  6316. static void ecc_ctx_init(ecEncCtx* ctx, int flags)
  6317. {
  6318. if (ctx) {
  6319. XMEMSET(ctx, 0, sizeof(ecEncCtx));
  6320. ctx->encAlgo = ecAES_128_CBC;
  6321. ctx->kdfAlgo = ecHKDF_SHA256;
  6322. ctx->macAlgo = ecHMAC_SHA256;
  6323. ctx->protocol = (byte)flags;
  6324. if (flags == REQ_RESP_CLIENT)
  6325. ctx->cliSt = ecCLI_INIT;
  6326. if (flags == REQ_RESP_SERVER)
  6327. ctx->srvSt = ecSRV_INIT;
  6328. }
  6329. }
  6330. /* allow ecc context reset so user doesn't have to init/free for reuse */
  6331. int wc_ecc_ctx_reset(ecEncCtx* ctx, WC_RNG* rng)
  6332. {
  6333. if (ctx == NULL || rng == NULL)
  6334. return BAD_FUNC_ARG;
  6335. ecc_ctx_init(ctx, ctx->protocol);
  6336. return ecc_ctx_set_salt(ctx, ctx->protocol, rng);
  6337. }
  6338. ecEncCtx* wc_ecc_ctx_new_ex(int flags, WC_RNG* rng, void* heap)
  6339. {
  6340. int ret = 0;
  6341. ecEncCtx* ctx = (ecEncCtx*)XMALLOC(sizeof(ecEncCtx), heap,
  6342. DYNAMIC_TYPE_ECC);
  6343. if (ctx) {
  6344. ctx->protocol = (byte)flags;
  6345. ctx->heap = heap;
  6346. }
  6347. ret = wc_ecc_ctx_reset(ctx, rng);
  6348. if (ret != 0) {
  6349. wc_ecc_ctx_free(ctx);
  6350. ctx = NULL;
  6351. }
  6352. return ctx;
  6353. }
  6354. /* alloc/init and set defaults, return new Context */
  6355. ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng)
  6356. {
  6357. return wc_ecc_ctx_new_ex(flags, rng, NULL);
  6358. }
  6359. /* free any resources, clear any keys */
  6360. void wc_ecc_ctx_free(ecEncCtx* ctx)
  6361. {
  6362. if (ctx) {
  6363. ForceZero(ctx, sizeof(ecEncCtx));
  6364. XFREE(ctx, ctx->heap, DYNAMIC_TYPE_ECC);
  6365. }
  6366. }
  6367. static int ecc_get_key_sizes(ecEncCtx* ctx, int* encKeySz, int* ivSz,
  6368. int* keysLen, word32* digestSz, word32* blockSz)
  6369. {
  6370. if (ctx) {
  6371. switch (ctx->encAlgo) {
  6372. case ecAES_128_CBC:
  6373. *encKeySz = KEY_SIZE_128;
  6374. *ivSz = IV_SIZE_128;
  6375. *blockSz = AES_BLOCK_SIZE;
  6376. break;
  6377. default:
  6378. return BAD_FUNC_ARG;
  6379. }
  6380. switch (ctx->macAlgo) {
  6381. case ecHMAC_SHA256:
  6382. *digestSz = WC_SHA256_DIGEST_SIZE;
  6383. break;
  6384. default:
  6385. return BAD_FUNC_ARG;
  6386. }
  6387. } else
  6388. return BAD_FUNC_ARG;
  6389. *keysLen = *encKeySz + *ivSz + *digestSz;
  6390. return 0;
  6391. }
  6392. /* ecc encrypt with shared secret run through kdf
  6393. ctx holds non default algos and inputs
  6394. msgSz should be the right size for encAlgo, i.e., already padded
  6395. return 0 on success */
  6396. int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  6397. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx)
  6398. {
  6399. int ret = 0;
  6400. word32 blockSz;
  6401. word32 digestSz;
  6402. ecEncCtx localCtx;
  6403. #ifdef WOLFSSL_SMALL_STACK
  6404. byte* sharedSecret;
  6405. byte* keys;
  6406. #else
  6407. byte sharedSecret[ECC_MAXSIZE]; /* 521 max size */
  6408. byte keys[ECC_BUFSIZE]; /* max size */
  6409. #endif
  6410. word32 sharedSz = ECC_MAXSIZE;
  6411. int keysLen;
  6412. int encKeySz;
  6413. int ivSz;
  6414. int offset = 0; /* keys offset if doing msg exchange */
  6415. byte* encKey;
  6416. byte* encIv;
  6417. byte* macKey;
  6418. if (privKey == NULL || pubKey == NULL || msg == NULL || out == NULL ||
  6419. outSz == NULL)
  6420. return BAD_FUNC_ARG;
  6421. if (ctx == NULL) { /* use defaults */
  6422. ecc_ctx_init(&localCtx, 0);
  6423. ctx = &localCtx;
  6424. }
  6425. ret = ecc_get_key_sizes(ctx, &encKeySz, &ivSz, &keysLen, &digestSz,
  6426. &blockSz);
  6427. if (ret != 0)
  6428. return ret;
  6429. if (ctx->protocol == REQ_RESP_SERVER) {
  6430. offset = keysLen;
  6431. keysLen *= 2;
  6432. if (ctx->srvSt != ecSRV_RECV_REQ)
  6433. return BAD_STATE_E;
  6434. ctx->srvSt = ecSRV_BAD_STATE; /* we're done no more ops allowed */
  6435. }
  6436. else if (ctx->protocol == REQ_RESP_CLIENT) {
  6437. if (ctx->cliSt != ecCLI_SALT_SET)
  6438. return BAD_STATE_E;
  6439. ctx->cliSt = ecCLI_SENT_REQ; /* only do this once */
  6440. }
  6441. if (keysLen > ECC_BUFSIZE) /* keys size */
  6442. return BUFFER_E;
  6443. if ( (msgSz%blockSz) != 0)
  6444. return BAD_PADDING_E;
  6445. if (*outSz < (msgSz + digestSz))
  6446. return BUFFER_E;
  6447. #ifdef WOLFSSL_SMALL_STACK
  6448. sharedSecret = (byte*)XMALLOC(ECC_MAXSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6449. if (sharedSecret == NULL)
  6450. return MEMORY_E;
  6451. keys = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6452. if (keys == NULL) {
  6453. XFREE(sharedSecret, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6454. return MEMORY_E;
  6455. }
  6456. #endif
  6457. do {
  6458. #if defined(WOLFSSL_ASYNC_CRYPT)
  6459. ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6460. if (ret != 0)
  6461. break;
  6462. #endif
  6463. ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret, &sharedSz);
  6464. } while (ret == WC_PENDING_E);
  6465. if (ret == 0) {
  6466. switch (ctx->kdfAlgo) {
  6467. case ecHKDF_SHA256 :
  6468. ret = wc_HKDF(WC_SHA256, sharedSecret, sharedSz, ctx->kdfSalt,
  6469. ctx->kdfSaltSz, ctx->kdfInfo, ctx->kdfInfoSz,
  6470. keys, keysLen);
  6471. break;
  6472. default:
  6473. ret = BAD_FUNC_ARG;
  6474. break;
  6475. }
  6476. }
  6477. if (ret == 0) {
  6478. encKey = keys + offset;
  6479. encIv = encKey + encKeySz;
  6480. macKey = encKey + encKeySz + ivSz;
  6481. switch (ctx->encAlgo) {
  6482. case ecAES_128_CBC:
  6483. {
  6484. Aes aes;
  6485. ret = wc_AesSetKey(&aes, encKey, KEY_SIZE_128, encIv,
  6486. AES_ENCRYPTION);
  6487. if (ret != 0)
  6488. break;
  6489. ret = wc_AesCbcEncrypt(&aes, out, msg, msgSz);
  6490. #if defined(WOLFSSL_ASYNC_CRYPT)
  6491. ret = wc_AsyncWait(ret, &aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6492. #endif
  6493. }
  6494. break;
  6495. default:
  6496. ret = BAD_FUNC_ARG;
  6497. break;
  6498. }
  6499. }
  6500. if (ret == 0) {
  6501. switch (ctx->macAlgo) {
  6502. case ecHMAC_SHA256:
  6503. {
  6504. Hmac hmac;
  6505. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  6506. if (ret == 0) {
  6507. ret = wc_HmacSetKey(&hmac, WC_SHA256, macKey, WC_SHA256_DIGEST_SIZE);
  6508. if (ret == 0)
  6509. ret = wc_HmacUpdate(&hmac, out, msgSz);
  6510. if (ret == 0)
  6511. ret = wc_HmacUpdate(&hmac, ctx->macSalt, ctx->macSaltSz);
  6512. if (ret == 0)
  6513. ret = wc_HmacFinal(&hmac, out+msgSz);
  6514. wc_HmacFree(&hmac);
  6515. }
  6516. }
  6517. break;
  6518. default:
  6519. ret = BAD_FUNC_ARG;
  6520. break;
  6521. }
  6522. }
  6523. if (ret == 0)
  6524. *outSz = msgSz + digestSz;
  6525. #ifdef WOLFSSL_SMALL_STACK
  6526. XFREE(sharedSecret, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6527. XFREE(keys, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6528. #endif
  6529. return ret;
  6530. }
  6531. /* ecc decrypt with shared secret run through kdf
  6532. ctx holds non default algos and inputs
  6533. return 0 on success */
  6534. int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  6535. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx)
  6536. {
  6537. int ret = 0;
  6538. word32 blockSz;
  6539. word32 digestSz;
  6540. ecEncCtx localCtx;
  6541. #ifdef WOLFSSL_SMALL_STACK
  6542. byte* sharedSecret;
  6543. byte* keys;
  6544. #else
  6545. byte sharedSecret[ECC_MAXSIZE]; /* 521 max size */
  6546. byte keys[ECC_BUFSIZE]; /* max size */
  6547. #endif
  6548. word32 sharedSz = ECC_MAXSIZE;
  6549. int keysLen;
  6550. int encKeySz;
  6551. int ivSz;
  6552. int offset = 0; /* in case using msg exchange */
  6553. byte* encKey;
  6554. byte* encIv;
  6555. byte* macKey;
  6556. if (privKey == NULL || pubKey == NULL || msg == NULL || out == NULL ||
  6557. outSz == NULL)
  6558. return BAD_FUNC_ARG;
  6559. if (ctx == NULL) { /* use defaults */
  6560. ecc_ctx_init(&localCtx, 0);
  6561. ctx = &localCtx;
  6562. }
  6563. ret = ecc_get_key_sizes(ctx, &encKeySz, &ivSz, &keysLen, &digestSz,
  6564. &blockSz);
  6565. if (ret != 0)
  6566. return ret;
  6567. if (ctx->protocol == REQ_RESP_CLIENT) {
  6568. offset = keysLen;
  6569. keysLen *= 2;
  6570. if (ctx->cliSt != ecCLI_SENT_REQ)
  6571. return BAD_STATE_E;
  6572. ctx->cliSt = ecSRV_BAD_STATE; /* we're done no more ops allowed */
  6573. }
  6574. else if (ctx->protocol == REQ_RESP_SERVER) {
  6575. if (ctx->srvSt != ecSRV_SALT_SET)
  6576. return BAD_STATE_E;
  6577. ctx->srvSt = ecSRV_RECV_REQ; /* only do this once */
  6578. }
  6579. if (keysLen > ECC_BUFSIZE) /* keys size */
  6580. return BUFFER_E;
  6581. if ( ((msgSz-digestSz) % blockSz) != 0)
  6582. return BAD_PADDING_E;
  6583. if (*outSz < (msgSz - digestSz))
  6584. return BUFFER_E;
  6585. #ifdef WOLFSSL_SMALL_STACK
  6586. sharedSecret = (byte*)XMALLOC(ECC_MAXSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6587. if (sharedSecret == NULL)
  6588. return MEMORY_E;
  6589. keys = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6590. if (keys == NULL) {
  6591. XFREE(sharedSecret, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6592. return MEMORY_E;
  6593. }
  6594. #endif
  6595. do {
  6596. #if defined(WOLFSSL_ASYNC_CRYPT)
  6597. ret = wc_AsyncWait(ret, &privKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6598. if (ret != 0)
  6599. break;
  6600. #endif
  6601. ret = wc_ecc_shared_secret(privKey, pubKey, sharedSecret, &sharedSz);
  6602. } while (ret == WC_PENDING_E);
  6603. if (ret == 0) {
  6604. switch (ctx->kdfAlgo) {
  6605. case ecHKDF_SHA256 :
  6606. ret = wc_HKDF(WC_SHA256, sharedSecret, sharedSz, ctx->kdfSalt,
  6607. ctx->kdfSaltSz, ctx->kdfInfo, ctx->kdfInfoSz,
  6608. keys, keysLen);
  6609. break;
  6610. default:
  6611. ret = BAD_FUNC_ARG;
  6612. break;
  6613. }
  6614. }
  6615. if (ret == 0) {
  6616. encKey = keys + offset;
  6617. encIv = encKey + encKeySz;
  6618. macKey = encKey + encKeySz + ivSz;
  6619. switch (ctx->macAlgo) {
  6620. case ecHMAC_SHA256:
  6621. {
  6622. byte verify[WC_SHA256_DIGEST_SIZE];
  6623. Hmac hmac;
  6624. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  6625. if (ret == 0) {
  6626. ret = wc_HmacSetKey(&hmac, WC_SHA256, macKey, WC_SHA256_DIGEST_SIZE);
  6627. if (ret == 0)
  6628. ret = wc_HmacUpdate(&hmac, msg, msgSz-digestSz);
  6629. if (ret == 0)
  6630. ret = wc_HmacUpdate(&hmac, ctx->macSalt, ctx->macSaltSz);
  6631. if (ret == 0)
  6632. ret = wc_HmacFinal(&hmac, verify);
  6633. if (ret == 0) {
  6634. if (XMEMCMP(verify, msg + msgSz - digestSz, digestSz) != 0)
  6635. ret = -1;
  6636. }
  6637. wc_HmacFree(&hmac);
  6638. }
  6639. break;
  6640. }
  6641. default:
  6642. ret = BAD_FUNC_ARG;
  6643. break;
  6644. }
  6645. }
  6646. if (ret == 0) {
  6647. switch (ctx->encAlgo) {
  6648. #ifdef HAVE_AES_CBC
  6649. case ecAES_128_CBC:
  6650. {
  6651. Aes aes;
  6652. ret = wc_AesSetKey(&aes, encKey, KEY_SIZE_128, encIv,
  6653. AES_DECRYPTION);
  6654. if (ret != 0)
  6655. break;
  6656. ret = wc_AesCbcDecrypt(&aes, out, msg, msgSz-digestSz);
  6657. #if defined(WOLFSSL_ASYNC_CRYPT)
  6658. ret = wc_AsyncWait(ret, &aes.asyncDev, WC_ASYNC_FLAG_NONE);
  6659. #endif
  6660. }
  6661. break;
  6662. #endif
  6663. default:
  6664. ret = BAD_FUNC_ARG;
  6665. break;
  6666. }
  6667. }
  6668. if (ret == 0)
  6669. *outSz = msgSz - digestSz;
  6670. #ifdef WOLFSSL_SMALL_STACK
  6671. XFREE(sharedSecret, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6672. XFREE(keys, NULL, DYNAMIC_TYPE_ECC_BUFFER);
  6673. #endif
  6674. return ret;
  6675. }
  6676. #endif /* HAVE_ECC_ENCRYPT */
  6677. #ifdef HAVE_COMP_KEY
  6678. #ifndef WOLFSSL_ATECC508A
  6679. int do_mp_jacobi(mp_int* a, mp_int* n, int* c);
  6680. int do_mp_jacobi(mp_int* a, mp_int* n, int* c)
  6681. {
  6682. int k, s, res;
  6683. int r = 0; /* initialize to help static analysis out */
  6684. mp_digit residue;
  6685. /* if a < 0 return MP_VAL */
  6686. if (mp_isneg(a) == MP_YES) {
  6687. return MP_VAL;
  6688. }
  6689. /* if n <= 0 return MP_VAL */
  6690. if (mp_cmp_d(n, 0) != MP_GT) {
  6691. return MP_VAL;
  6692. }
  6693. /* step 1. handle case of a == 0 */
  6694. if (mp_iszero (a) == MP_YES) {
  6695. /* special case of a == 0 and n == 1 */
  6696. if (mp_cmp_d (n, 1) == MP_EQ) {
  6697. *c = 1;
  6698. } else {
  6699. *c = 0;
  6700. }
  6701. return MP_OKAY;
  6702. }
  6703. /* step 2. if a == 1, return 1 */
  6704. if (mp_cmp_d (a, 1) == MP_EQ) {
  6705. *c = 1;
  6706. return MP_OKAY;
  6707. }
  6708. /* default */
  6709. s = 0;
  6710. /* divide out larger power of two */
  6711. k = mp_cnt_lsb(a);
  6712. res = mp_div_2d(a, k, a, NULL);
  6713. if (res == MP_OKAY) {
  6714. /* step 4. if e is even set s=1 */
  6715. if ((k & 1) == 0) {
  6716. s = 1;
  6717. } else {
  6718. /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
  6719. residue = n->dp[0] & 7;
  6720. if (residue == 1 || residue == 7) {
  6721. s = 1;
  6722. } else if (residue == 3 || residue == 5) {
  6723. s = -1;
  6724. }
  6725. }
  6726. /* step 5. if p == 3 (mod 4) *and* a == 3 (mod 4) then s = -s */
  6727. if ( ((n->dp[0] & 3) == 3) && ((a->dp[0] & 3) == 3)) {
  6728. s = -s;
  6729. }
  6730. }
  6731. if (res == MP_OKAY) {
  6732. /* if a == 1 we're done */
  6733. if (mp_cmp_d(a, 1) == MP_EQ) {
  6734. *c = s;
  6735. } else {
  6736. /* n1 = n mod a */
  6737. res = mp_mod (n, a, n);
  6738. if (res == MP_OKAY)
  6739. res = do_mp_jacobi(n, a, &r);
  6740. if (res == MP_OKAY)
  6741. *c = s * r;
  6742. }
  6743. }
  6744. return res;
  6745. }
  6746. /* computes the jacobi c = (a | n) (or Legendre if n is prime)
  6747. * HAC pp. 73 Algorithm 2.149
  6748. * HAC is wrong here, as the special case of (0 | 1) is not
  6749. * handled correctly.
  6750. */
  6751. int mp_jacobi(mp_int* a, mp_int* n, int* c)
  6752. {
  6753. mp_int a1, n1;
  6754. int res;
  6755. /* step 3. write a = a1 * 2**k */
  6756. if ((res = mp_init_multi(&a1, &n1, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  6757. return res;
  6758. }
  6759. if ((res = mp_copy(a, &a1)) != MP_OKAY) {
  6760. goto done;
  6761. }
  6762. if ((res = mp_copy(n, &n1)) != MP_OKAY) {
  6763. goto done;
  6764. }
  6765. res = do_mp_jacobi(&a1, &n1, c);
  6766. done:
  6767. /* cleanup */
  6768. mp_clear(&n1);
  6769. mp_clear(&a1);
  6770. return res;
  6771. }
  6772. /* Solves the modular equation x^2 = n (mod p)
  6773. * where prime number is greater than 2 (odd prime).
  6774. * The result is returned in the third argument x
  6775. * the function returns MP_OKAY on success, MP_VAL or another error on failure
  6776. */
  6777. int mp_sqrtmod_prime(mp_int* n, mp_int* prime, mp_int* ret)
  6778. {
  6779. int res, legendre, done = 0;
  6780. mp_int t1, C, Q, S, Z, M, T, R, two;
  6781. mp_digit i;
  6782. /* first handle the simple cases n = 0 or n = 1 */
  6783. if (mp_cmp_d(n, 0) == MP_EQ) {
  6784. mp_zero(ret);
  6785. return MP_OKAY;
  6786. }
  6787. if (mp_cmp_d(n, 1) == MP_EQ) {
  6788. return mp_set(ret, 1);
  6789. }
  6790. /* prime must be odd */
  6791. if (mp_cmp_d(prime, 2) == MP_EQ) {
  6792. return MP_VAL;
  6793. }
  6794. /* is quadratic non-residue mod prime */
  6795. if ((res = mp_jacobi(n, prime, &legendre)) != MP_OKAY) {
  6796. return res;
  6797. }
  6798. if (legendre == -1) {
  6799. return MP_VAL;
  6800. }
  6801. if ((res = mp_init_multi(&t1, &C, &Q, &S, &Z, &M)) != MP_OKAY)
  6802. return res;
  6803. if ((res = mp_init_multi(&T, &R, &two, NULL, NULL, NULL))
  6804. != MP_OKAY) {
  6805. mp_clear(&t1); mp_clear(&C); mp_clear(&Q); mp_clear(&S); mp_clear(&Z);
  6806. mp_clear(&M);
  6807. return res;
  6808. }
  6809. /* SPECIAL CASE: if prime mod 4 == 3
  6810. * compute directly: res = n^(prime+1)/4 mod prime
  6811. * Handbook of Applied Cryptography algorithm 3.36
  6812. */
  6813. res = mp_mod_d(prime, 4, &i);
  6814. if (res == MP_OKAY && i == 3) {
  6815. res = mp_add_d(prime, 1, &t1);
  6816. if (res == MP_OKAY)
  6817. res = mp_div_2(&t1, &t1);
  6818. if (res == MP_OKAY)
  6819. res = mp_div_2(&t1, &t1);
  6820. if (res == MP_OKAY)
  6821. res = mp_exptmod(n, &t1, prime, ret);
  6822. done = 1;
  6823. }
  6824. /* NOW: TonelliShanks algorithm */
  6825. if (res == MP_OKAY && done == 0) {
  6826. /* factor out powers of 2 from prime-1, defining Q and S
  6827. * as: prime-1 = Q*2^S */
  6828. /* Q = prime - 1 */
  6829. res = mp_copy(prime, &Q);
  6830. if (res == MP_OKAY)
  6831. res = mp_sub_d(&Q, 1, &Q);
  6832. /* S = 0 */
  6833. if (res == MP_OKAY)
  6834. mp_zero(&S);
  6835. while (res == MP_OKAY && mp_iseven(&Q) == MP_YES) {
  6836. /* Q = Q / 2 */
  6837. res = mp_div_2(&Q, &Q);
  6838. /* S = S + 1 */
  6839. if (res == MP_OKAY)
  6840. res = mp_add_d(&S, 1, &S);
  6841. }
  6842. /* find a Z such that the Legendre symbol (Z|prime) == -1 */
  6843. /* Z = 2 */
  6844. if (res == MP_OKAY)
  6845. res = mp_set_int(&Z, 2);
  6846. while (res == MP_OKAY) {
  6847. res = mp_jacobi(&Z, prime, &legendre);
  6848. if (res == MP_OKAY && legendre == -1)
  6849. break;
  6850. /* Z = Z + 1 */
  6851. if (res == MP_OKAY)
  6852. res = mp_add_d(&Z, 1, &Z);
  6853. }
  6854. /* C = Z ^ Q mod prime */
  6855. if (res == MP_OKAY)
  6856. res = mp_exptmod(&Z, &Q, prime, &C);
  6857. /* t1 = (Q + 1) / 2 */
  6858. if (res == MP_OKAY)
  6859. res = mp_add_d(&Q, 1, &t1);
  6860. if (res == MP_OKAY)
  6861. res = mp_div_2(&t1, &t1);
  6862. /* R = n ^ ((Q + 1) / 2) mod prime */
  6863. if (res == MP_OKAY)
  6864. res = mp_exptmod(n, &t1, prime, &R);
  6865. /* T = n ^ Q mod prime */
  6866. if (res == MP_OKAY)
  6867. res = mp_exptmod(n, &Q, prime, &T);
  6868. /* M = S */
  6869. if (res == MP_OKAY)
  6870. res = mp_copy(&S, &M);
  6871. if (res == MP_OKAY)
  6872. res = mp_set_int(&two, 2);
  6873. while (res == MP_OKAY && done == 0) {
  6874. res = mp_copy(&T, &t1);
  6875. /* reduce to 1 and count */
  6876. i = 0;
  6877. while (res == MP_OKAY) {
  6878. if (mp_cmp_d(&t1, 1) == MP_EQ)
  6879. break;
  6880. res = mp_exptmod(&t1, &two, prime, &t1);
  6881. if (res == MP_OKAY)
  6882. i++;
  6883. }
  6884. if (res == MP_OKAY && i == 0) {
  6885. res = mp_copy(&R, ret);
  6886. done = 1;
  6887. }
  6888. if (done == 0) {
  6889. /* t1 = 2 ^ (M - i - 1) */
  6890. if (res == MP_OKAY)
  6891. res = mp_sub_d(&M, i, &t1);
  6892. if (res == MP_OKAY)
  6893. res = mp_sub_d(&t1, 1, &t1);
  6894. if (res == MP_OKAY)
  6895. res = mp_exptmod(&two, &t1, prime, &t1);
  6896. /* t1 = C ^ (2 ^ (M - i - 1)) mod prime */
  6897. if (res == MP_OKAY)
  6898. res = mp_exptmod(&C, &t1, prime, &t1);
  6899. /* C = (t1 * t1) mod prime */
  6900. if (res == MP_OKAY)
  6901. res = mp_sqrmod(&t1, prime, &C);
  6902. /* R = (R * t1) mod prime */
  6903. if (res == MP_OKAY)
  6904. res = mp_mulmod(&R, &t1, prime, &R);
  6905. /* T = (T * C) mod prime */
  6906. if (res == MP_OKAY)
  6907. res = mp_mulmod(&T, &C, prime, &T);
  6908. /* M = i */
  6909. if (res == MP_OKAY)
  6910. res = mp_set(&M, i);
  6911. }
  6912. }
  6913. }
  6914. /* done */
  6915. mp_clear(&t1);
  6916. mp_clear(&C);
  6917. mp_clear(&Q);
  6918. mp_clear(&S);
  6919. mp_clear(&Z);
  6920. mp_clear(&M);
  6921. mp_clear(&T);
  6922. mp_clear(&R);
  6923. mp_clear(&two);
  6924. return res;
  6925. }
  6926. #endif /* !WOLFSSL_ATECC508A */
  6927. /* export public ECC key in ANSI X9.63 format compressed */
  6928. static int wc_ecc_export_x963_compressed(ecc_key* key, byte* out, word32* outLen)
  6929. {
  6930. word32 numlen;
  6931. int ret = MP_OKAY;
  6932. if (key == NULL || out == NULL || outLen == NULL)
  6933. return BAD_FUNC_ARG;
  6934. if (wc_ecc_is_valid_idx(key->idx) == 0) {
  6935. return ECC_BAD_ARG_E;
  6936. }
  6937. numlen = key->dp->size;
  6938. if (*outLen < (1 + numlen)) {
  6939. *outLen = 1 + numlen;
  6940. return BUFFER_E;
  6941. }
  6942. #ifdef WOLFSSL_ATECC508A
  6943. /* TODO: Implement equiv call to ATECC508A */
  6944. ret = BAD_COND_E;
  6945. #else
  6946. /* store first byte */
  6947. out[0] = mp_isodd(key->pubkey.y) == MP_YES ? 0x03 : 0x02;
  6948. /* pad and store x */
  6949. XMEMSET(out+1, 0, numlen);
  6950. ret = mp_to_unsigned_bin(key->pubkey.x,
  6951. out+1 + (numlen - mp_unsigned_bin_size(key->pubkey.x)));
  6952. *outLen = 1 + numlen;
  6953. #endif /* WOLFSSL_ATECC508A */
  6954. return ret;
  6955. }
  6956. #endif /* HAVE_COMP_KEY */
  6957. int wc_ecc_get_oid(word32 oidSum, const byte** oid, word32* oidSz)
  6958. {
  6959. int x;
  6960. if (oidSum == 0) {
  6961. return BAD_FUNC_ARG;
  6962. }
  6963. /* find matching OID sum (based on encoded value) */
  6964. for (x = 0; ecc_sets[x].size != 0; x++) {
  6965. if (ecc_sets[x].oidSum == oidSum) {
  6966. int ret = 0;
  6967. #ifdef HAVE_OID_ENCODING
  6968. /* check cache */
  6969. oid_cache_t* o = &ecc_oid_cache[x];
  6970. if (o->oidSz == 0) {
  6971. o->oidSz = sizeof(o->oid);
  6972. ret = EncodeObjectId(ecc_sets[x].oid, ecc_sets[x].oidSz,
  6973. o->oid, &o->oidSz);
  6974. }
  6975. if (oidSz) {
  6976. *oidSz = o->oidSz;
  6977. }
  6978. if (oid) {
  6979. *oid = o->oid;
  6980. }
  6981. #else
  6982. if (oidSz) {
  6983. *oidSz = ecc_sets[x].oidSz;
  6984. }
  6985. if (oid) {
  6986. *oid = ecc_sets[x].oid;
  6987. }
  6988. #endif
  6989. /* on success return curve id */
  6990. if (ret == 0) {
  6991. ret = ecc_sets[x].id;
  6992. }
  6993. return ret;
  6994. }
  6995. }
  6996. return NOT_COMPILED_IN;
  6997. }
  6998. #ifdef WOLFSSL_CUSTOM_CURVES
  6999. int wc_ecc_set_custom_curve(ecc_key* key, const ecc_set_type* dp)
  7000. {
  7001. if (key == NULL || dp == NULL) {
  7002. return BAD_FUNC_ARG;
  7003. }
  7004. key->idx = ECC_CUSTOM_IDX;
  7005. key->dp = dp;
  7006. return 0;
  7007. }
  7008. #endif /* WOLFSSL_CUSTOM_CURVES */
  7009. #ifdef HAVE_X963_KDF
  7010. static INLINE void IncrementX963KdfCounter(byte* inOutCtr)
  7011. {
  7012. int i;
  7013. /* in network byte order so start at end and work back */
  7014. for (i = 3; i >= 0; i--) {
  7015. if (++inOutCtr[i]) /* we're done unless we overflow */
  7016. return;
  7017. }
  7018. }
  7019. /* ASN X9.63 Key Derivation Function (SEC1) */
  7020. int wc_X963_KDF(enum wc_HashType type, const byte* secret, word32 secretSz,
  7021. const byte* sinfo, word32 sinfoSz, byte* out, word32 outSz)
  7022. {
  7023. int ret, i;
  7024. int digestSz, copySz;
  7025. int remaining = outSz;
  7026. byte* outIdx;
  7027. byte counter[4];
  7028. byte tmp[WC_MAX_DIGEST_SIZE];
  7029. #ifdef WOLFSSL_SMALL_STACK
  7030. wc_HashAlg* hash;
  7031. #else
  7032. wc_HashAlg hash[1];
  7033. #endif
  7034. if (secret == NULL || secretSz == 0 || out == NULL)
  7035. return BAD_FUNC_ARG;
  7036. /* X9.63 allowed algos only */
  7037. if (type != WC_HASH_TYPE_SHA && type != WC_HASH_TYPE_SHA224 &&
  7038. type != WC_HASH_TYPE_SHA256 && type != WC_HASH_TYPE_SHA384 &&
  7039. type != WC_HASH_TYPE_SHA512)
  7040. return BAD_FUNC_ARG;
  7041. digestSz = wc_HashGetDigestSize(type);
  7042. if (digestSz < 0)
  7043. return digestSz;
  7044. #ifdef WOLFSSL_SMALL_STACK
  7045. hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), NULL,
  7046. DYNAMIC_TYPE_HASHES);
  7047. if (hash == NULL)
  7048. return MEMORY_E;
  7049. #endif
  7050. ret = wc_HashInit(hash, type);
  7051. if (ret != 0) {
  7052. #ifdef WOLFSSL_SMALL_STACK
  7053. XFREE(hash, NULL, DYNAMIC_TYPE_HASHES);
  7054. #endif
  7055. return ret;
  7056. }
  7057. outIdx = out;
  7058. XMEMSET(counter, 0, sizeof(counter));
  7059. for (i = 1; remaining > 0; i++) {
  7060. IncrementX963KdfCounter(counter);
  7061. ret = wc_HashUpdate(hash, type, secret, secretSz);
  7062. if (ret != 0) {
  7063. #ifdef WOLFSSL_SMALL_STACK
  7064. XFREE(hash, NULL, DYNAMIC_TYPE_HASHES);
  7065. #endif
  7066. return ret;
  7067. }
  7068. ret = wc_HashUpdate(hash, type, counter, sizeof(counter));
  7069. if (ret != 0) {
  7070. #ifdef WOLFSSL_SMALL_STACK
  7071. XFREE(hash, NULL, DYNAMIC_TYPE_HASHES);
  7072. #endif
  7073. return ret;
  7074. }
  7075. if (sinfo) {
  7076. ret = wc_HashUpdate(hash, type, sinfo, sinfoSz);
  7077. if (ret != 0) {
  7078. #ifdef WOLFSSL_SMALL_STACK
  7079. XFREE(hash, NULL, DYNAMIC_TYPE_HASHES);
  7080. #endif
  7081. return ret;
  7082. }
  7083. }
  7084. ret = wc_HashFinal(hash, type, tmp);
  7085. if (ret != 0) {
  7086. #ifdef WOLFSSL_SMALL_STACK
  7087. XFREE(hash, NULL, DYNAMIC_TYPE_HASHES);
  7088. #endif
  7089. return ret;
  7090. }
  7091. copySz = min(remaining, digestSz);
  7092. XMEMCPY(outIdx, tmp, copySz);
  7093. remaining -= copySz;
  7094. outIdx += copySz;
  7095. }
  7096. #ifdef WOLFSSL_SMALL_STACK
  7097. XFREE(hash, NULL, DYNAMIC_TYPE_HASHES);
  7098. #endif
  7099. return 0;
  7100. }
  7101. #endif /* HAVE_X963_KDF */
  7102. #endif /* HAVE_ECC */