tfm.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135
  1. /* tfm.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. * Based on public domain TomsFastMath 0.10 by Tom St Denis, tomstdenis@iahu.ca,
  23. * http://math.libtomcrypt.com
  24. */
  25. /**
  26. * Edited by Moises Guimaraes (moises@wolfssl.com)
  27. * to fit wolfSSL's needs.
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. #include <config.h>
  31. #endif
  32. /* in case user set USE_FAST_MATH there */
  33. #include <wolfssl/wolfcrypt/settings.h>
  34. #ifdef USE_FAST_MATH
  35. #ifdef NO_INLINE
  36. #include <wolfssl/wolfcrypt/misc.h>
  37. #else
  38. #define WOLFSSL_MISC_INCLUDED
  39. #include <wolfcrypt/src/misc.c>
  40. #endif
  41. #include <wolfssl/wolfcrypt/random.h>
  42. #include <wolfssl/wolfcrypt/tfm.h>
  43. #include <wolfcrypt/src/asm.c> /* will define asm MACROS or C ones */
  44. #include <wolfssl/wolfcrypt/wolfmath.h> /* common functions */
  45. #if defined(FREESCALE_LTC_TFM)
  46. #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
  47. #endif
  48. #ifdef WOLFSSL_DEBUG_MATH
  49. #include <stdio.h>
  50. #endif
  51. #if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. WOLFSSL_LOCAL int sp_ModExp_1024(mp_int* base, mp_int* exp, mp_int* mod,
  56. mp_int* res);
  57. WOLFSSL_LOCAL int sp_ModExp_1536(mp_int* base, mp_int* exp, mp_int* mod,
  58. mp_int* res);
  59. WOLFSSL_LOCAL int sp_ModExp_2048(mp_int* base, mp_int* exp, mp_int* mod,
  60. mp_int* res);
  61. WOLFSSL_LOCAL int sp_ModExp_3072(mp_int* base, mp_int* exp, mp_int* mod,
  62. mp_int* res);
  63. WOLFSSL_LOCAL int sp_ModExp_4096(mp_int* base, mp_int* exp, mp_int* mod,
  64. mp_int* res);
  65. #ifdef __cplusplus
  66. } /* extern "C" */
  67. #endif
  68. #endif
  69. #if !defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_SP_MATH_ALL)
  70. /* math settings check */
  71. word32 CheckRunTimeSettings(void)
  72. {
  73. return CTC_SETTINGS;
  74. }
  75. /* math settings size check */
  76. word32 CheckRunTimeFastMath(void)
  77. {
  78. return FP_SIZE;
  79. }
  80. #endif
  81. /* Functions */
  82. int fp_add(fp_int *a, fp_int *b, fp_int *c)
  83. {
  84. int sa, sb;
  85. int ret = FP_OKAY;
  86. /* get sign of both inputs */
  87. sa = a->sign;
  88. sb = b->sign;
  89. /* handle two cases, not four */
  90. if (sa == sb) {
  91. /* both positive or both negative */
  92. /* add their magnitudes, copy the sign */
  93. c->sign = sa;
  94. ret = s_fp_add (a, b, c);
  95. } else {
  96. /* one positive, the other negative */
  97. /* subtract the one with the greater magnitude from */
  98. /* the one of the lesser magnitude. The result gets */
  99. /* the sign of the one with the greater magnitude. */
  100. if (fp_cmp_mag (a, b) == FP_LT) {
  101. c->sign = sb;
  102. s_fp_sub (b, a, c);
  103. } else {
  104. c->sign = sa;
  105. s_fp_sub (a, b, c);
  106. }
  107. }
  108. return ret;
  109. }
  110. /* unsigned addition */
  111. int s_fp_add(fp_int *a, fp_int *b, fp_int *c)
  112. {
  113. int x, y, oldused;
  114. fp_word t;
  115. y = MAX(a->used, b->used);
  116. oldused = MIN(c->used, FP_SIZE); /* help static analysis w/ largest size */
  117. c->used = y;
  118. t = 0;
  119. for (x = 0; x < y; x++) {
  120. t += ((fp_word)a->dp[x]) + ((fp_word)b->dp[x]);
  121. c->dp[x] = (fp_digit)t;
  122. t >>= DIGIT_BIT;
  123. }
  124. if (t != 0) {
  125. if (x == FP_SIZE)
  126. return FP_VAL;
  127. c->dp[c->used++] = (fp_digit)t;
  128. ++x;
  129. }
  130. c->used = x;
  131. /* zero any excess digits on the destination that we didn't write to */
  132. for (; x < oldused; x++) {
  133. c->dp[x] = 0;
  134. }
  135. fp_clamp(c);
  136. return FP_OKAY;
  137. }
  138. /* c = a - b */
  139. int fp_sub(fp_int *a, fp_int *b, fp_int *c)
  140. {
  141. int sa, sb;
  142. int ret = FP_OKAY;
  143. sa = a->sign;
  144. sb = b->sign;
  145. if (sa != sb) {
  146. /* subtract a negative from a positive, OR */
  147. /* subtract a positive from a negative. */
  148. /* In either case, ADD their magnitudes, */
  149. /* and use the sign of the first number. */
  150. c->sign = sa;
  151. ret = s_fp_add (a, b, c);
  152. } else {
  153. /* subtract a positive from a positive, OR */
  154. /* subtract a negative from a negative. */
  155. /* First, take the difference between their */
  156. /* magnitudes, then... */
  157. if (fp_cmp_mag (a, b) != FP_LT) {
  158. /* Copy the sign from the first */
  159. c->sign = sa;
  160. /* The first has a larger or equal magnitude */
  161. s_fp_sub (a, b, c);
  162. } else {
  163. /* The result has the *opposite* sign from */
  164. /* the first number. */
  165. c->sign = (sa == FP_ZPOS) ? FP_NEG : FP_ZPOS;
  166. /* The second has a larger magnitude */
  167. s_fp_sub (b, a, c);
  168. }
  169. }
  170. return ret;
  171. }
  172. /* unsigned subtraction ||a|| >= ||b|| ALWAYS! */
  173. void s_fp_sub(fp_int *a, fp_int *b, fp_int *c)
  174. {
  175. int x, oldbused, oldused;
  176. fp_word t;
  177. oldused = c->used;
  178. oldbused = b->used;
  179. c->used = a->used;
  180. t = 0;
  181. for (x = 0; x < oldbused; x++) {
  182. t = ((fp_word)a->dp[x]) - (((fp_word)b->dp[x]) + t);
  183. c->dp[x] = (fp_digit)t;
  184. t = (t >> DIGIT_BIT)&1;
  185. }
  186. for (; x < a->used; x++) {
  187. t = ((fp_word)a->dp[x]) - t;
  188. c->dp[x] = (fp_digit)t;
  189. t = (t >> DIGIT_BIT)&1;
  190. }
  191. /* zero any excess digits on the destination that we didn't write to */
  192. for (; x < oldused; x++) {
  193. c->dp[x] = 0;
  194. }
  195. fp_clamp(c);
  196. }
  197. /* c = a * b */
  198. int fp_mul(fp_int *A, fp_int *B, fp_int *C)
  199. {
  200. int ret = 0;
  201. int y, yy, oldused;
  202. #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
  203. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
  204. ret = esp_mp_mul(A, B, C);
  205. if(ret != -2) return ret;
  206. #endif
  207. oldused = C->used;
  208. y = MAX(A->used, B->used);
  209. yy = MIN(A->used, B->used);
  210. /* fail if we are out of range */
  211. if (y + yy >= FP_SIZE) {
  212. ret = FP_VAL;
  213. goto clean;
  214. }
  215. /* pick a comba (unrolled 4/8/16/32 x or rolled) based on the size
  216. of the largest input. We also want to avoid doing excess mults if the
  217. inputs are not close to the next power of two. That is, for example,
  218. if say y=17 then we would do (32-17)^2 = 225 unneeded multiplications
  219. */
  220. #if defined(TFM_MUL3) && FP_SIZE >= 6
  221. if (y <= 3) {
  222. ret = fp_mul_comba3(A,B,C);
  223. goto clean;
  224. }
  225. #endif
  226. #if defined(TFM_MUL4) && FP_SIZE >= 8
  227. if (y == 4) {
  228. ret = fp_mul_comba4(A,B,C);
  229. goto clean;
  230. }
  231. #endif
  232. #if defined(TFM_MUL6) && FP_SIZE >= 12
  233. if (y <= 6) {
  234. ret = fp_mul_comba6(A,B,C);
  235. goto clean;
  236. }
  237. #endif
  238. #if defined(TFM_MUL7) && FP_SIZE >= 14
  239. if (y == 7) {
  240. ret = fp_mul_comba7(A,B,C);
  241. goto clean;
  242. }
  243. #endif
  244. #if defined(TFM_MUL8) && FP_SIZE >= 16
  245. if (y == 8) {
  246. ret = fp_mul_comba8(A,B,C);
  247. goto clean;
  248. }
  249. #endif
  250. #if defined(TFM_MUL9) && FP_SIZE >= 18
  251. if (y == 9) {
  252. ret = fp_mul_comba9(A,B,C);
  253. goto clean;
  254. }
  255. #endif
  256. #if defined(TFM_MUL12) && FP_SIZE >= 24
  257. if (y <= 12) {
  258. ret = fp_mul_comba12(A,B,C);
  259. goto clean;
  260. }
  261. #endif
  262. #if defined(TFM_MUL17) && FP_SIZE >= 34
  263. if (y <= 17) {
  264. ret = fp_mul_comba17(A,B,C);
  265. goto clean;
  266. }
  267. #endif
  268. #if defined(TFM_SMALL_SET) && FP_SIZE >= 32
  269. if (y <= 16) {
  270. ret = fp_mul_comba_small(A,B,C);
  271. goto clean;
  272. }
  273. #endif
  274. #if defined(TFM_MUL20) && FP_SIZE >= 40
  275. if (y <= 20) {
  276. ret = fp_mul_comba20(A,B,C);
  277. goto clean;
  278. }
  279. #endif
  280. #if defined(TFM_MUL24) && FP_SIZE >= 48
  281. if (yy >= 16 && y <= 24) {
  282. ret = fp_mul_comba24(A,B,C);
  283. goto clean;
  284. }
  285. #endif
  286. #if defined(TFM_MUL28) && FP_SIZE >= 56
  287. if (yy >= 20 && y <= 28) {
  288. ret = fp_mul_comba28(A,B,C);
  289. goto clean;
  290. }
  291. #endif
  292. #if defined(TFM_MUL32) && FP_SIZE >= 64
  293. if (yy >= 24 && y <= 32) {
  294. ret = fp_mul_comba32(A,B,C);
  295. goto clean;
  296. }
  297. #endif
  298. #if defined(TFM_MUL48) && FP_SIZE >= 96
  299. if (yy >= 40 && y <= 48) {
  300. ret = fp_mul_comba48(A,B,C);
  301. goto clean;
  302. }
  303. #endif
  304. #if defined(TFM_MUL64) && FP_SIZE >= 128
  305. if (yy >= 56 && y <= 64) {
  306. ret = fp_mul_comba64(A,B,C);
  307. goto clean;
  308. }
  309. #endif
  310. ret = fp_mul_comba(A,B,C);
  311. clean:
  312. /* zero any excess digits on the destination that we didn't write to */
  313. for (y = C->used; y >= 0 && y < oldused; y++) {
  314. C->dp[y] = 0;
  315. }
  316. return ret;
  317. }
  318. int fp_mul_2(fp_int * a, fp_int * b)
  319. {
  320. int x, oldused;
  321. /* Make sure value to double and result are in range. */
  322. if ((a->used > (FP_SIZE-1)) || ((a->used == (FP_SIZE - 1)) &&
  323. ((a->dp[FP_SIZE - 1] & ((fp_digit)1 << (DIGIT_BIT - 1))) != 0))) {
  324. return FP_VAL;
  325. }
  326. oldused = b->used;
  327. b->used = a->used;
  328. {
  329. fp_digit r, rr, *tmpa, *tmpb;
  330. /* alias for source */
  331. tmpa = a->dp;
  332. /* alias for dest */
  333. tmpb = b->dp;
  334. /* carry */
  335. r = 0;
  336. for (x = 0; x < a->used; x++) {
  337. /* get what will be the *next* carry bit from the
  338. * MSB of the current digit
  339. */
  340. rr = *tmpa >> ((fp_digit)(DIGIT_BIT - 1));
  341. /* now shift up this digit, add in the carry [from the previous] */
  342. *tmpb++ = ((*tmpa++ << ((fp_digit)1)) | r);
  343. /* copy the carry that would be from the source
  344. * digit into the next iteration
  345. */
  346. r = rr;
  347. }
  348. /* new leading digit? */
  349. if (r != 0) {
  350. /* add a MSB which is always 1 at this point */
  351. *tmpb = 1;
  352. ++(b->used);
  353. }
  354. /* zero any excess digits on the destination that we didn't write to */
  355. tmpb = b->dp + b->used;
  356. for (x = b->used; x < oldused; x++) {
  357. *tmpb++ = 0;
  358. }
  359. }
  360. b->sign = a->sign;
  361. return FP_OKAY;
  362. }
  363. /* c = a * b */
  364. int fp_mul_d(fp_int *a, fp_digit b, fp_int *c)
  365. {
  366. fp_word w;
  367. int x, oldused;
  368. oldused = c->used;
  369. c->used = a->used;
  370. c->sign = a->sign;
  371. w = 0;
  372. for (x = 0; x < a->used; x++) {
  373. w = ((fp_word)a->dp[x]) * ((fp_word)b) + w;
  374. c->dp[x] = (fp_digit)w;
  375. w = w >> DIGIT_BIT;
  376. }
  377. if (w != 0) {
  378. if (a->used == FP_SIZE)
  379. return FP_VAL;
  380. c->dp[c->used++] = (fp_digit) w;
  381. ++x;
  382. }
  383. /* zero any excess digits on the destination that we didn't write to */
  384. /* also checking FP_SIZE here for static analysis */
  385. for (; x < oldused && x < FP_SIZE; x++) {
  386. c->dp[x] = 0;
  387. }
  388. fp_clamp(c);
  389. return FP_OKAY;
  390. }
  391. /* c = a * 2**d */
  392. int fp_mul_2d(fp_int *a, int b, fp_int *c)
  393. {
  394. fp_digit carry, carrytmp, shift;
  395. int x;
  396. /* copy it */
  397. fp_copy(a, c);
  398. /* handle whole digits */
  399. if (b >= DIGIT_BIT) {
  400. int ret = fp_lshd(c, b/DIGIT_BIT);
  401. if (ret != FP_OKAY)
  402. return ret;
  403. }
  404. b %= DIGIT_BIT;
  405. /* shift the digits */
  406. if (b != 0) {
  407. carry = 0;
  408. shift = DIGIT_BIT - b;
  409. for (x = 0; x < c->used; x++) {
  410. carrytmp = c->dp[x] >> shift;
  411. c->dp[x] = (c->dp[x] << b) + carry;
  412. carry = carrytmp;
  413. }
  414. /* store last carry if room */
  415. if (carry && x < FP_SIZE) {
  416. c->dp[c->used++] = carry;
  417. }
  418. if (x == FP_SIZE)
  419. return FP_VAL;
  420. }
  421. fp_clamp(c);
  422. return FP_OKAY;
  423. }
  424. /* generic PxQ multiplier */
  425. #if defined(HAVE_INTEL_MULX)
  426. WC_INLINE static int fp_mul_comba_mulx(fp_int *A, fp_int *B, fp_int *C)
  427. {
  428. int ix, iy, iz, pa;
  429. fp_int *dst;
  430. #ifndef WOLFSSL_SMALL_STACK
  431. fp_int tmp[1];
  432. #else
  433. fp_int *tmp;
  434. #endif
  435. fp_digit carry;
  436. /* Variables used but not seen by cppcheck. */
  437. (void)ix; (void)iy; (void)iz;
  438. #ifdef WOLFSSL_SMALL_STACK
  439. tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  440. if (tmp == NULL)
  441. return FP_MEM;
  442. #endif
  443. /* get size of output and trim */
  444. pa = A->used + B->used;
  445. if (pa >= FP_SIZE) {
  446. pa = FP_SIZE-1;
  447. }
  448. /* Always take branch to use tmp variable. This avoids a cache attack for
  449. * determining if C equals A */
  450. if (1) {
  451. fp_init(tmp);
  452. dst = tmp;
  453. }
  454. TFM_INTEL_MUL_COMBA(A, B, carry, dst) ;
  455. dst->used = pa;
  456. dst->sign = A->sign ^ B->sign;
  457. fp_clamp(dst);
  458. fp_copy(dst, C);
  459. #ifdef WOLFSSL_SMALL_STACK
  460. XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
  461. #endif
  462. return FP_OKAY;
  463. }
  464. #endif
  465. int fp_mul_comba(fp_int *A, fp_int *B, fp_int *C)
  466. {
  467. int ret = 0;
  468. int ix, iy, iz, tx, ty, pa;
  469. fp_digit c0, c1, c2, *tmpx, *tmpy;
  470. fp_int *dst;
  471. #ifndef WOLFSSL_SMALL_STACK
  472. fp_int tmp[1];
  473. #else
  474. fp_int *tmp;
  475. #endif
  476. if (A->used + B->used >= FP_SIZE) return FP_VAL;
  477. IF_HAVE_INTEL_MULX(ret = fp_mul_comba_mulx(A, B, C), return ret) ;
  478. #ifdef WOLFSSL_SMALL_STACK
  479. tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  480. if (tmp == NULL)
  481. return FP_MEM;
  482. #endif
  483. COMBA_START;
  484. COMBA_CLEAR;
  485. /* get size of output and trim */
  486. pa = A->used + B->used;
  487. if (pa >= FP_SIZE) {
  488. pa = FP_SIZE-1;
  489. }
  490. /* Always take branch to use tmp variable. This avoids a cache attack for
  491. * determining if C equals A */
  492. if (1) {
  493. fp_init(tmp);
  494. dst = tmp;
  495. }
  496. for (ix = 0; ix < pa; ix++) {
  497. /* get offsets into the two bignums */
  498. ty = MIN(ix, (B->used > 0 ? B->used - 1 : 0));
  499. tx = ix - ty;
  500. /* setup temp aliases */
  501. tmpx = A->dp + tx;
  502. tmpy = B->dp + ty;
  503. /* this is the number of times the loop will iterate, essentially its
  504. while (tx++ < a->used && ty-- >= 0) { ... }
  505. */
  506. iy = MIN(A->used-tx, ty+1);
  507. /* execute loop */
  508. COMBA_FORWARD;
  509. for (iz = 0; iz < iy; ++iz) {
  510. fp_digit _tmpx = *tmpx++;
  511. fp_digit _tmpy = *tmpy--;
  512. MULADD(_tmpx, _tmpy);
  513. }
  514. /* store term */
  515. COMBA_STORE(dst->dp[ix]);
  516. }
  517. COMBA_FINI;
  518. dst->used = pa;
  519. dst->sign = A->sign ^ B->sign;
  520. fp_clamp(dst);
  521. fp_copy(dst, C);
  522. /* Variables used but not seen by cppcheck. */
  523. (void)c0; (void)c1; (void)c2;
  524. #ifdef WOLFSSL_SMALL_STACK
  525. XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
  526. #endif
  527. return ret;
  528. }
  529. /* a/b => cb + d == a */
  530. int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
  531. {
  532. int n, t, i, norm, neg;
  533. int ret;
  534. #ifndef WOLFSSL_SMALL_STACK
  535. fp_int q[1], x[1], y[1], t1[1], t2[1];
  536. #else
  537. fp_int *q, *x, *y, *t1, *t2;
  538. #endif
  539. /* is divisor zero ? */
  540. if (fp_iszero (b) == FP_YES) {
  541. return FP_VAL;
  542. }
  543. /* if a < b then q=0, r = a */
  544. if (fp_cmp_mag (a, b) == FP_LT)
  545. {
  546. if (d != NULL) {
  547. fp_copy (a, d);
  548. }
  549. if (c != NULL) {
  550. fp_zero (c);
  551. }
  552. return FP_OKAY;
  553. }
  554. #ifdef WOLFSSL_SMALL_STACK /* 0 1 2 3 4 */
  555. /* allocate 5 elements of fp_int for q, x, y, t1, t2 */
  556. q = (fp_int*)XMALLOC(sizeof(fp_int) * 5, NULL, DYNAMIC_TYPE_BIGINT);
  557. if (q == NULL) {
  558. return FP_MEM;
  559. }
  560. x = &q[1]; y = &q[2]; t1 = &q[3]; t2 = &q[4];
  561. #endif
  562. fp_init(q);
  563. /* qb + d = a, and b is an integer > 0, therefore q <= a */
  564. q->used = a->used;
  565. fp_init(t1);
  566. fp_init(t2);
  567. /* Init a copy (y) of the input (b) and
  568. ** Init a copy (x) of the input (a)
  569. **
  570. ** ALERT: Not calling fp_init_copy() as some compiler optimization settings
  571. ** such as -O2 will complain that (x) or (y) "may be used uninitialized".
  572. ** The fp_init() is here only to appease the compiler. */
  573. fp_init(x);
  574. fp_copy(a, x); /* copy (src = a) to (dst = x) */
  575. fp_init(y);
  576. fp_copy(b, y); /* copy (src = b) to (dst = y) */
  577. /* fix the sign */
  578. neg = (a->sign == b->sign) ? FP_ZPOS : FP_NEG;
  579. x->sign = y->sign = FP_ZPOS;
  580. /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
  581. norm = fp_count_bits(y) % DIGIT_BIT;
  582. if (norm < (int)(DIGIT_BIT-1)) {
  583. norm = (DIGIT_BIT-1) - norm;
  584. ret = fp_mul_2d (x, norm, x);
  585. if (ret != FP_OKAY) {
  586. #ifdef WOLFSSL_SMALL_STACK
  587. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  588. #endif
  589. return ret;
  590. }
  591. ret = fp_mul_2d (y, norm, y);
  592. if (ret != FP_OKAY) {
  593. #ifdef WOLFSSL_SMALL_STACK
  594. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  595. #endif
  596. return ret;
  597. }
  598. } else {
  599. norm = 0;
  600. }
  601. /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
  602. n = x->used - 1;
  603. t = y->used - 1;
  604. /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
  605. ret = fp_lshd (y, n - t); /* y = y*b**{n-t} */
  606. if (ret != FP_OKAY) {
  607. #ifdef WOLFSSL_SMALL_STACK
  608. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  609. #endif
  610. return ret;
  611. }
  612. while (fp_cmp (x, y) != FP_LT) {
  613. ++(q->dp[n - t]);
  614. ret = fp_sub (x, y, x);
  615. if (ret != FP_OKAY) {
  616. #ifdef WOLFSSL_SMALL_STACK
  617. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  618. #endif
  619. return ret;
  620. }
  621. }
  622. /* reset y by shifting it back down */
  623. fp_rshd (y, n - t);
  624. /* step 3. for i from n down to (t + 1) */
  625. for (i = n; i >= (t + 1); i--) {
  626. if (i > x->used) {
  627. continue;
  628. }
  629. /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
  630. * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
  631. if (x->dp[i] == y->dp[t]) {
  632. q->dp[i - t - 1] = (fp_digit) ((((fp_word)1) << DIGIT_BIT) - 1);
  633. } else {
  634. fp_word tmp;
  635. tmp = ((fp_word) x->dp[i]) << ((fp_word) DIGIT_BIT);
  636. tmp |= ((fp_word) x->dp[i - 1]);
  637. #ifdef WOLFSSL_LINUXKM
  638. /* Linux kernel macro for in-place 64 bit integer division. */
  639. do_div(tmp, (fp_word)y->dp[t]);
  640. #else
  641. tmp /= ((fp_word)y->dp[t]);
  642. #endif
  643. q->dp[i - t - 1] = (fp_digit) (tmp);
  644. }
  645. /* while (q{i-t-1} * (yt * b + y{t-1})) >
  646. xi * b**2 + xi-1 * b + xi-2
  647. do q{i-t-1} -= 1;
  648. */
  649. q->dp[i - t - 1] = (q->dp[i - t - 1] + 1);
  650. do {
  651. q->dp[i - t - 1] = (q->dp[i - t - 1] - 1);
  652. /* find left hand */
  653. fp_zero (t1);
  654. t1->dp[0] = (t - 1 < 0) ? 0 : y->dp[t - 1];
  655. t1->dp[1] = y->dp[t];
  656. t1->used = 2;
  657. ret = fp_mul_d (t1, q->dp[i - t - 1], t1);
  658. if (ret != FP_OKAY) {
  659. #ifdef WOLFSSL_SMALL_STACK
  660. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  661. #endif
  662. return ret;
  663. }
  664. /* find right hand */
  665. t2->dp[0] = (i - 2 < 0) ? 0 : x->dp[i - 2];
  666. t2->dp[1] = (i - 1 < 0) ? 0 : x->dp[i - 1];
  667. t2->dp[2] = x->dp[i];
  668. t2->used = 3;
  669. } while (fp_cmp_mag(t1, t2) == FP_GT);
  670. /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
  671. ret = fp_mul_d (y, q->dp[i - t - 1], t1);
  672. if (ret != FP_OKAY) {
  673. #ifdef WOLFSSL_SMALL_STACK
  674. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  675. #endif
  676. return ret;
  677. }
  678. ret = fp_lshd (t1, i - t - 1);
  679. if (ret != FP_OKAY) {
  680. #ifdef WOLFSSL_SMALL_STACK
  681. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  682. #endif
  683. return ret;
  684. }
  685. ret = fp_sub (x, t1, x);
  686. if (ret != FP_OKAY) {
  687. #ifdef WOLFSSL_SMALL_STACK
  688. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  689. #endif
  690. return ret;
  691. }
  692. /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
  693. if (x->sign == FP_NEG) {
  694. fp_copy (y, t1);
  695. ret = fp_lshd (t1, i - t - 1);
  696. if (ret != FP_OKAY) {
  697. #ifdef WOLFSSL_SMALL_STACK
  698. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  699. #endif
  700. return ret;
  701. }
  702. ret = fp_add (x, t1, x);
  703. if (ret != FP_OKAY) {
  704. #ifdef WOLFSSL_SMALL_STACK
  705. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  706. #endif
  707. return ret;
  708. }
  709. q->dp[i - t - 1] = q->dp[i - t - 1] - 1;
  710. }
  711. }
  712. /* now q is the quotient and x is the remainder
  713. * [which we have to normalize]
  714. */
  715. /* get sign before writing to c */
  716. x->sign = x->used == 0 ? FP_ZPOS : a->sign;
  717. if (c != NULL) {
  718. fp_clamp (q);
  719. fp_copy (q, c);
  720. c->sign = neg;
  721. }
  722. if (d != NULL) {
  723. fp_div_2d (x, norm, x, NULL);
  724. /* zero any excess digits on the destination that we didn't write to */
  725. for (i = b->used; i < x->used; i++) {
  726. x->dp[i] = 0;
  727. }
  728. fp_clamp(x);
  729. fp_copy (x, d);
  730. }
  731. #ifdef WOLFSSL_SMALL_STACK
  732. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  733. #endif
  734. return FP_OKAY;
  735. }
  736. /* b = a/2 */
  737. void fp_div_2(fp_int * a, fp_int * b)
  738. {
  739. int x, oldused;
  740. oldused = b->used;
  741. b->used = a->used;
  742. {
  743. fp_digit r, rr, *tmpa, *tmpb;
  744. /* source alias */
  745. tmpa = a->dp + b->used - 1;
  746. /* dest alias */
  747. tmpb = b->dp + b->used - 1;
  748. /* carry */
  749. r = 0;
  750. for (x = b->used - 1; x >= 0; x--) {
  751. /* get the carry for the next iteration */
  752. rr = *tmpa & 1;
  753. /* shift the current digit, add in carry and store */
  754. *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
  755. /* forward carry to next iteration */
  756. r = rr;
  757. }
  758. /* zero any excess digits on the destination that we didn't write to */
  759. tmpb = b->dp + b->used;
  760. for (x = b->used; x < oldused; x++) {
  761. *tmpb++ = 0;
  762. }
  763. }
  764. b->sign = a->sign;
  765. fp_clamp (b);
  766. }
  767. /* c = a / 2 (mod b) - constant time (a < b and positive) */
  768. int fp_div_2_mod_ct(fp_int *a, fp_int *b, fp_int *c)
  769. {
  770. fp_word w = 0;
  771. fp_digit mask;
  772. int i;
  773. mask = 0 - (a->dp[0] & 1);
  774. for (i = 0; i < b->used; i++) {
  775. fp_digit mask_a = 0 - (i < a->used);
  776. w += b->dp[i] & mask;
  777. w += a->dp[i] & mask_a;
  778. c->dp[i] = (fp_digit)w;
  779. w >>= DIGIT_BIT;
  780. }
  781. for (i = 0; i < b->used-1; i++) {
  782. c->dp[i] = (c->dp[i] >> 1) | (c->dp[i+1] << (DIGIT_BIT - 1));
  783. }
  784. c->dp[i] = (c->dp[i] >> 1) | ((fp_digit)w << (DIGIT_BIT - 1));
  785. c->used = i + 1;
  786. c->sign = FP_ZPOS;
  787. fp_clamp(c);
  788. return FP_OKAY;
  789. }
  790. /* c = a / 2**b */
  791. void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d)
  792. {
  793. int D;
  794. /* if the shift count is <= 0 then we do no work */
  795. if (b <= 0) {
  796. fp_copy (a, c);
  797. if (d != NULL) {
  798. fp_zero (d);
  799. }
  800. return;
  801. }
  802. /* get the remainder before a is changed in calculating c */
  803. if (a == c && d != NULL) {
  804. fp_mod_2d (a, b, d);
  805. }
  806. /* copy */
  807. fp_copy(a, c);
  808. /* shift by as many digits in the bit count */
  809. if (b >= (int)DIGIT_BIT) {
  810. fp_rshd (c, b / DIGIT_BIT);
  811. }
  812. /* shift any bit count < DIGIT_BIT */
  813. D = (b % DIGIT_BIT);
  814. if (D != 0) {
  815. fp_rshb(c, D);
  816. }
  817. /* get the remainder if a is not changed in calculating c */
  818. if (a != c && d != NULL) {
  819. fp_mod_2d (a, b, d);
  820. }
  821. fp_clamp (c);
  822. }
  823. /* c = a mod b, 0 <= c < b */
  824. int fp_mod(fp_int *a, fp_int *b, fp_int *c)
  825. {
  826. #ifndef WOLFSSL_SMALL_STACK
  827. fp_int t[1];
  828. #else
  829. fp_int *t;
  830. #endif
  831. int err;
  832. #ifdef WOLFSSL_SMALL_STACK
  833. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  834. if (t == NULL)
  835. return FP_MEM;
  836. #endif
  837. fp_init(t);
  838. err = fp_div(a, b, NULL, t);
  839. if (err == FP_OKAY) {
  840. if (!fp_iszero(t) && (t->sign != b->sign)) {
  841. err = fp_add(t, b, c);
  842. } else {
  843. fp_copy(t, c);
  844. }
  845. }
  846. #ifdef WOLFSSL_SMALL_STACK
  847. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  848. #endif
  849. return err;
  850. }
  851. /* c = a mod 2**d */
  852. void fp_mod_2d(fp_int *a, int b, fp_int *c)
  853. {
  854. unsigned int x;
  855. unsigned int bmax;
  856. /* zero if count less than or equal to zero */
  857. if (b <= 0) {
  858. fp_zero(c);
  859. return;
  860. }
  861. /* get copy of input */
  862. fp_copy(a, c);
  863. /* if 2**d is larger than we just return */
  864. if (c->sign == FP_ZPOS && b >= (DIGIT_BIT * a->used)) {
  865. return;
  866. }
  867. bmax = ((unsigned int)b + DIGIT_BIT - 1) / DIGIT_BIT;
  868. /* If a is negative and bmax is larger than FP_SIZE, then the
  869. * result can't fit within c. Just return. */
  870. if (c->sign == FP_NEG && bmax > FP_SIZE) {
  871. return;
  872. }
  873. /* zero digits above the last digit of the modulus */
  874. for (x = bmax; x < (unsigned int)c->used; x++) {
  875. c->dp[x] = 0;
  876. }
  877. if (c->sign == FP_NEG) {
  878. fp_digit carry = 0;
  879. /* negate value */
  880. for (x = 0; x < (unsigned int)c->used; x++) {
  881. fp_digit next = c->dp[x] > 0;
  882. c->dp[x] = (fp_digit)0 - c->dp[x] - carry;
  883. carry |= next;
  884. }
  885. for (; x < bmax; x++) {
  886. c->dp[x] = (fp_digit)0 - carry;
  887. }
  888. c->used = (int)bmax;
  889. c->sign = FP_ZPOS;
  890. }
  891. /* clear the digit that is not completely outside/inside the modulus */
  892. x = DIGIT_BIT - (b % DIGIT_BIT);
  893. if (x != DIGIT_BIT) {
  894. c->dp[bmax - 1] &= ~((fp_digit)0) >> x;
  895. }
  896. fp_clamp (c);
  897. }
  898. static int fp_invmod_slow (fp_int * a, fp_int * b, fp_int * c)
  899. {
  900. #ifndef WOLFSSL_SMALL_STACK
  901. fp_int x[1], y[1], u[1], v[1], A[1], B[1], C[1], D[1];
  902. #else
  903. fp_int *x, *y, *u, *v, *A, *B, *C, *D;
  904. #endif
  905. int err;
  906. /* b cannot be negative */
  907. if (b->sign == FP_NEG || fp_iszero(b) == FP_YES) {
  908. return FP_VAL;
  909. }
  910. if (fp_iszero(a) == FP_YES) {
  911. return FP_VAL;
  912. }
  913. #ifdef WOLFSSL_SMALL_STACK
  914. x = (fp_int*)XMALLOC(sizeof(fp_int) * 8, NULL, DYNAMIC_TYPE_BIGINT);
  915. if (x == NULL) {
  916. return FP_MEM;
  917. }
  918. y = &x[1]; u = &x[2]; v = &x[3]; A = &x[4]; B = &x[5]; C = &x[6]; D = &x[7];
  919. #endif
  920. /* init temps */
  921. fp_init(x); fp_init(y);
  922. fp_init(u); fp_init(v);
  923. fp_init(A); fp_init(B);
  924. fp_init(C); fp_init(D);
  925. /* x = a, y = b */
  926. if ((err = fp_mod(a, b, x)) != FP_OKAY) {
  927. #ifdef WOLFSSL_SMALL_STACK
  928. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  929. #endif
  930. return err;
  931. }
  932. fp_copy(b, y);
  933. if (fp_iszero(x) == FP_YES) {
  934. /* invmod doesn't exist for this a and b */
  935. #ifdef WOLFSSL_SMALL_STACK
  936. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  937. #endif
  938. return FP_VAL;
  939. }
  940. /* 2. [modified] if x,y are both even then return an error! */
  941. if (fp_iseven(x) == FP_YES && fp_iseven(y) == FP_YES) {
  942. #ifdef WOLFSSL_SMALL_STACK
  943. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  944. #endif
  945. return FP_VAL;
  946. }
  947. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  948. fp_copy (x, u);
  949. fp_copy (y, v);
  950. fp_set (A, 1);
  951. fp_set (D, 1);
  952. top:
  953. /* 4. while u is even do */
  954. while (fp_iseven (u) == FP_YES) {
  955. /* 4.1 u = u/2 */
  956. fp_div_2 (u, u);
  957. /* 4.2 if A or B is odd then */
  958. if (fp_isodd (A) == FP_YES || fp_isodd (B) == FP_YES) {
  959. /* A = (A+y)/2, B = (B-x)/2 */
  960. err = fp_add (A, y, A);
  961. if (err != FP_OKAY) {
  962. #ifdef WOLFSSL_SMALL_STACK
  963. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  964. #endif
  965. return err;
  966. }
  967. err = fp_sub (B, x, B);
  968. if (err != FP_OKAY) {
  969. #ifdef WOLFSSL_SMALL_STACK
  970. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  971. #endif
  972. return err;
  973. }
  974. }
  975. /* A = A/2, B = B/2 */
  976. fp_div_2 (A, A);
  977. fp_div_2 (B, B);
  978. }
  979. /* 5. while v is even do */
  980. while (fp_iseven (v) == FP_YES) {
  981. /* 5.1 v = v/2 */
  982. fp_div_2 (v, v);
  983. /* 5.2 if C or D is odd then */
  984. if (fp_isodd (C) == FP_YES || fp_isodd (D) == FP_YES) {
  985. /* C = (C+y)/2, D = (D-x)/2 */
  986. err = fp_add (C, y, C);
  987. if (err != FP_OKAY) {
  988. #ifdef WOLFSSL_SMALL_STACK
  989. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  990. #endif
  991. return err;
  992. }
  993. err = fp_sub (D, x, D);
  994. if (err != FP_OKAY) {
  995. #ifdef WOLFSSL_SMALL_STACK
  996. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  997. #endif
  998. return err;
  999. }
  1000. }
  1001. /* C = C/2, D = D/2 */
  1002. fp_div_2 (C, C);
  1003. fp_div_2 (D, D);
  1004. }
  1005. /* 6. if u >= v then */
  1006. if (fp_cmp (u, v) != FP_LT) {
  1007. /* u = u - v, A = A - C, B = B - D */
  1008. err = fp_sub (u, v, u);
  1009. if (err != FP_OKAY) {
  1010. #ifdef WOLFSSL_SMALL_STACK
  1011. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1012. #endif
  1013. return err;
  1014. }
  1015. err = fp_sub (A, C, A);
  1016. if (err != FP_OKAY) {
  1017. #ifdef WOLFSSL_SMALL_STACK
  1018. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1019. #endif
  1020. return err;
  1021. }
  1022. err = fp_sub (B, D, B);
  1023. if (err != FP_OKAY) {
  1024. #ifdef WOLFSSL_SMALL_STACK
  1025. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1026. #endif
  1027. return err;
  1028. }
  1029. } else {
  1030. /* v - v - u, C = C - A, D = D - B */
  1031. err = fp_sub (v, u, v);
  1032. if (err != FP_OKAY) {
  1033. #ifdef WOLFSSL_SMALL_STACK
  1034. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1035. #endif
  1036. return err;
  1037. }
  1038. err = fp_sub (C, A, C);
  1039. if (err != FP_OKAY) {
  1040. #ifdef WOLFSSL_SMALL_STACK
  1041. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1042. #endif
  1043. return err;
  1044. }
  1045. err = fp_sub (D, B, D);
  1046. if (err != FP_OKAY) {
  1047. #ifdef WOLFSSL_SMALL_STACK
  1048. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1049. #endif
  1050. return err;
  1051. }
  1052. }
  1053. /* if not zero goto step 4 */
  1054. if (fp_iszero (u) == FP_NO)
  1055. goto top;
  1056. /* now a = C, b = D, gcd == g*v */
  1057. /* if v != 1 then there is no inverse */
  1058. if (fp_cmp_d (v, 1) != FP_EQ) {
  1059. #ifdef WOLFSSL_SMALL_STACK
  1060. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1061. #endif
  1062. return FP_VAL;
  1063. }
  1064. /* if its too low */
  1065. while (fp_cmp_d(C, 0) == FP_LT) {
  1066. err = fp_add(C, b, C);
  1067. if (err != FP_OKAY) {
  1068. #ifdef WOLFSSL_SMALL_STACK
  1069. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1070. #endif
  1071. return err;
  1072. }
  1073. }
  1074. /* too big */
  1075. while (fp_cmp_mag(C, b) != FP_LT) {
  1076. err = fp_sub(C, b, C);
  1077. if (err != FP_OKAY) {
  1078. #ifdef WOLFSSL_SMALL_STACK
  1079. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1080. #endif
  1081. return err;
  1082. }
  1083. }
  1084. /* C is now the inverse */
  1085. fp_copy(C, c);
  1086. #ifdef WOLFSSL_SMALL_STACK
  1087. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1088. #endif
  1089. return FP_OKAY;
  1090. }
  1091. /* c = 1/a (mod b) for odd b only */
  1092. int fp_invmod(fp_int *a, fp_int *b, fp_int *c)
  1093. {
  1094. #ifndef WOLFSSL_SMALL_STACK
  1095. fp_int x[1], y[1], u[1], v[1], B[1], D[1];
  1096. #else
  1097. fp_int *x, *y, *u, *v, *B, *D;
  1098. #endif
  1099. int err;
  1100. if (b->sign == FP_NEG || fp_iszero(b) == FP_YES) {
  1101. return FP_VAL;
  1102. }
  1103. /* [modified] sanity check on "a" */
  1104. if (fp_iszero(a) == FP_YES) {
  1105. return FP_VAL; /* can not divide by 0 here */
  1106. }
  1107. /* 2. [modified] b must be odd */
  1108. if (fp_iseven(b) == FP_YES) {
  1109. return fp_invmod_slow(a,b,c);
  1110. }
  1111. #ifdef WOLFSSL_SMALL_STACK
  1112. x = (fp_int*)XMALLOC(sizeof(fp_int) * 6, NULL, DYNAMIC_TYPE_BIGINT);
  1113. if (x == NULL) {
  1114. return FP_MEM;
  1115. }
  1116. y = &x[1]; u = &x[2]; v = &x[3]; B = &x[4]; D = &x[5];
  1117. #endif
  1118. /* init all our temps */
  1119. fp_init(x); fp_init(y);
  1120. fp_init(u); fp_init(v);
  1121. fp_init(B); fp_init(D);
  1122. if (fp_iszero(a) == FP_YES) {
  1123. #ifdef WOLFSSL_SMALL_STACK
  1124. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1125. #endif
  1126. return FP_VAL;
  1127. }
  1128. /* x == modulus, y == value to invert */
  1129. fp_copy(b, x);
  1130. /* we need y = |a| */
  1131. if ((err = mp_mod(a, b, y)) != FP_OKAY) {
  1132. #ifdef WOLFSSL_SMALL_STACK
  1133. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1134. #endif
  1135. return err;
  1136. }
  1137. if (fp_iszero(y) == FP_YES) {
  1138. /* invmod doesn't exist for this a and b */
  1139. #ifdef WOLFSSL_SMALL_STACK
  1140. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1141. #endif
  1142. return FP_VAL;
  1143. }
  1144. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  1145. fp_copy(x, u);
  1146. fp_copy(y, v);
  1147. fp_set (D, 1);
  1148. top:
  1149. /* 4. while u is even do */
  1150. while (fp_iseven (u) == FP_YES) {
  1151. /* 4.1 u = u/2 */
  1152. fp_div_2 (u, u);
  1153. /* 4.2 if B is odd then */
  1154. if (fp_isodd (B) == FP_YES) {
  1155. err = fp_sub (B, x, B);
  1156. if (err != FP_OKAY) {
  1157. #ifdef WOLFSSL_SMALL_STACK
  1158. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1159. #endif
  1160. return err;
  1161. }
  1162. }
  1163. /* B = B/2 */
  1164. fp_div_2 (B, B);
  1165. }
  1166. /* 5. while v is even do */
  1167. while (fp_iseven (v) == FP_YES) {
  1168. /* 5.1 v = v/2 */
  1169. fp_div_2 (v, v);
  1170. /* 5.2 if D is odd then */
  1171. if (fp_isodd (D) == FP_YES) {
  1172. /* D = (D-x)/2 */
  1173. err = fp_sub (D, x, D);
  1174. if (err != FP_OKAY) {
  1175. #ifdef WOLFSSL_SMALL_STACK
  1176. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1177. #endif
  1178. return err;
  1179. }
  1180. }
  1181. /* D = D/2 */
  1182. fp_div_2 (D, D);
  1183. }
  1184. /* 6. if u >= v then */
  1185. if (fp_cmp (u, v) != FP_LT) {
  1186. /* u = u - v, B = B - D */
  1187. err = fp_sub (u, v, u);
  1188. if (err != FP_OKAY) {
  1189. #ifdef WOLFSSL_SMALL_STACK
  1190. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1191. #endif
  1192. return err;
  1193. }
  1194. err = fp_sub (B, D, B);
  1195. if (err != FP_OKAY) {
  1196. #ifdef WOLFSSL_SMALL_STACK
  1197. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1198. #endif
  1199. return err;
  1200. }
  1201. } else {
  1202. /* v - v - u, D = D - B */
  1203. err = fp_sub (v, u, v);
  1204. if (err != FP_OKAY) {
  1205. #ifdef WOLFSSL_SMALL_STACK
  1206. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1207. #endif
  1208. return err;
  1209. }
  1210. err = fp_sub (D, B, D);
  1211. if (err != FP_OKAY) {
  1212. #ifdef WOLFSSL_SMALL_STACK
  1213. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1214. #endif
  1215. return err;
  1216. }
  1217. }
  1218. /* if not zero goto step 4 */
  1219. if (fp_iszero (u) == FP_NO) {
  1220. goto top;
  1221. }
  1222. /* now a = C, b = D, gcd == g*v */
  1223. /* if v != 1 then there is no inverse */
  1224. if (fp_cmp_d (v, 1) != FP_EQ) {
  1225. #ifdef WOLFSSL_SMALL_STACK
  1226. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1227. #endif
  1228. return FP_VAL;
  1229. }
  1230. /* b is now the inverse */
  1231. while (D->sign == FP_NEG) {
  1232. err = fp_add (D, b, D);
  1233. if (err != FP_OKAY) {
  1234. #ifdef WOLFSSL_SMALL_STACK
  1235. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1236. #endif
  1237. return FP_OKAY;
  1238. }
  1239. }
  1240. /* too big */
  1241. while (fp_cmp_mag(D, b) != FP_LT) {
  1242. err = fp_sub(D, b, D);
  1243. if (err != FP_OKAY) {
  1244. #ifdef WOLFSSL_SMALL_STACK
  1245. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1246. #endif
  1247. return err;
  1248. }
  1249. }
  1250. fp_copy (D, c);
  1251. #ifdef WOLFSSL_SMALL_STACK
  1252. XFREE(x, NULL, DYNAMIC_TYPE_BIGINT);
  1253. #endif
  1254. return FP_OKAY;
  1255. }
  1256. #define CT_INV_MOD_PRE_CNT 8
  1257. /* modulus (b) must be greater than 2 and a prime */
  1258. int fp_invmod_mont_ct(fp_int *a, fp_int *b, fp_int *c, fp_digit mp)
  1259. {
  1260. int i, j, err = FP_OKAY;
  1261. #ifndef WOLFSSL_SMALL_STACK
  1262. fp_int t[1], e[1];
  1263. fp_int pre[CT_INV_MOD_PRE_CNT];
  1264. #else
  1265. fp_int* t;
  1266. fp_int* e;
  1267. fp_int* pre;
  1268. #endif
  1269. if ((a->used * 2 > FP_SIZE) || (b->used * 2 > FP_SIZE)) {
  1270. return FP_VAL;
  1271. }
  1272. #ifdef WOLFSSL_SMALL_STACK
  1273. t = (fp_int*)XMALLOC(sizeof(fp_int) * (2 + CT_INV_MOD_PRE_CNT), NULL,
  1274. DYNAMIC_TYPE_BIGINT);
  1275. if (t == NULL)
  1276. return FP_MEM;
  1277. e = t + 1;
  1278. pre = t + 2;
  1279. #endif
  1280. fp_init(t);
  1281. fp_init(e);
  1282. fp_init(&pre[0]);
  1283. fp_copy(a, &pre[0]);
  1284. for (i = 1; i < CT_INV_MOD_PRE_CNT; i++) {
  1285. fp_init(&pre[i]);
  1286. err |= fp_sqr(&pre[i-1], &pre[i]);
  1287. err |= fp_montgomery_reduce(&pre[i], b, mp);
  1288. err |= fp_mul(&pre[i], a, &pre[i]);
  1289. err |= fp_montgomery_reduce(&pre[i], b, mp);
  1290. }
  1291. err |= fp_sub_d(b, 2, e);
  1292. /* Highest bit is always set. */
  1293. j = 1;
  1294. for (i = fp_count_bits(e)-2; i >= 0; i--) {
  1295. if (!fp_is_bit_set(e, i) || j == CT_INV_MOD_PRE_CNT)
  1296. break;
  1297. j++;
  1298. }
  1299. fp_copy(&pre[j-1], t);
  1300. j = 0;
  1301. for (; i >= 0; i--) {
  1302. int set = fp_is_bit_set(e, i);
  1303. if ((j == CT_INV_MOD_PRE_CNT) || (!set && j > 0)) {
  1304. err |= fp_mul(t, &pre[j-1], t);
  1305. err |= fp_montgomery_reduce(t, b, mp);
  1306. j = 0;
  1307. }
  1308. err |= fp_sqr(t, t);
  1309. err |= fp_montgomery_reduce(t, b, mp);
  1310. j += set;
  1311. }
  1312. if (j > 0) {
  1313. err |= fp_mul(t, &pre[j-1], c);
  1314. err |= fp_montgomery_reduce(c, b, mp);
  1315. }
  1316. else
  1317. fp_copy(t, c);
  1318. #ifdef WOLFSSL_SMALL_STACK
  1319. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  1320. #endif
  1321. return err;
  1322. }
  1323. /* d = a * b (mod c) */
  1324. int fp_mulmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
  1325. {
  1326. int err;
  1327. #ifndef WOLFSSL_SMALL_STACK
  1328. fp_int t[1];
  1329. #else
  1330. fp_int *t;
  1331. #endif
  1332. #ifdef WOLFSSL_SMALL_STACK
  1333. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  1334. if (t == NULL)
  1335. return FP_MEM;
  1336. #endif
  1337. fp_init(t);
  1338. err = fp_mul(a, b, t);
  1339. if (err == FP_OKAY) {
  1340. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  1341. if (d->size < FP_SIZE) {
  1342. err = fp_mod(t, c, t);
  1343. fp_copy(t, d);
  1344. } else
  1345. #endif
  1346. {
  1347. err = fp_mod(t, c, d);
  1348. }
  1349. }
  1350. #ifdef WOLFSSL_SMALL_STACK
  1351. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  1352. #endif
  1353. return err;
  1354. }
  1355. /* d = a - b (mod c) */
  1356. int fp_submod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
  1357. {
  1358. int err;
  1359. #ifndef WOLFSSL_SMALL_STACK
  1360. fp_int t[1];
  1361. #else
  1362. fp_int *t;
  1363. #endif
  1364. #ifdef WOLFSSL_SMALL_STACK
  1365. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  1366. if (t == NULL)
  1367. return FP_MEM;
  1368. #endif
  1369. fp_init(t);
  1370. err = fp_sub(a, b, t);
  1371. if (err == FP_OKAY) {
  1372. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  1373. if (d->size < FP_SIZE) {
  1374. err = fp_mod(t, c, t);
  1375. fp_copy(t, d);
  1376. } else
  1377. #endif
  1378. {
  1379. err = fp_mod(t, c, d);
  1380. }
  1381. }
  1382. #ifdef WOLFSSL_SMALL_STACK
  1383. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  1384. #endif
  1385. return err;
  1386. }
  1387. /* d = a + b (mod c) */
  1388. int fp_addmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
  1389. {
  1390. int err;
  1391. #ifndef WOLFSSL_SMALL_STACK
  1392. fp_int t[1];
  1393. #else
  1394. fp_int *t;
  1395. #endif
  1396. #ifdef WOLFSSL_SMALL_STACK
  1397. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  1398. if (t == NULL)
  1399. return FP_MEM;
  1400. #endif
  1401. fp_init(t);
  1402. err = fp_add(a, b, t);
  1403. if (err == FP_OKAY) {
  1404. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  1405. if (d->size < FP_SIZE) {
  1406. err = fp_mod(t, c, t);
  1407. fp_copy(t, d);
  1408. } else
  1409. #endif
  1410. {
  1411. err = fp_mod(t, c, d);
  1412. }
  1413. }
  1414. #ifdef WOLFSSL_SMALL_STACK
  1415. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  1416. #endif
  1417. return err;
  1418. }
  1419. /* d = a - b (mod c) - constant time (a < c and b < c and all positive)
  1420. * c and d must not be the same pointers.
  1421. */
  1422. int fp_submod_ct(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
  1423. {
  1424. fp_sword w;
  1425. fp_digit mask;
  1426. int i;
  1427. if (c->used + 1 > FP_SIZE) {
  1428. return FP_VAL;
  1429. }
  1430. if (c == d) {
  1431. return FP_VAL;
  1432. }
  1433. /* In constant time, subtract b from a putting result in d. */
  1434. w = 0;
  1435. for (i = 0; i < c->used; i++) {
  1436. w += a->dp[i];
  1437. w -= b->dp[i];
  1438. d->dp[i] = (fp_digit)w;
  1439. w >>= DIGIT_BIT;
  1440. }
  1441. w += a->dp[i];
  1442. w -= b->dp[i];
  1443. w >>= DIGIT_BIT;
  1444. /* When w is negative then we need to add modulus to make result positive. */
  1445. mask = (fp_digit)0 - (w < 0);
  1446. /* Constant time, conditionally, add modulus to difference. */
  1447. w = 0;
  1448. for (i = 0; i < c->used; i++) {
  1449. w += d->dp[i];
  1450. w += c->dp[i] & mask;
  1451. d->dp[i] = (fp_digit)w;
  1452. w >>= DIGIT_BIT;
  1453. }
  1454. /* Result will always have digits equal to or less than those in modulus. */
  1455. d->used = i;
  1456. d->sign = FP_ZPOS;
  1457. fp_clamp(d);
  1458. return FP_OKAY;
  1459. }
  1460. /* d = a + b (mod c) - constant time (a < c and b < c and all positive)
  1461. * c and d must not be the same pointers.
  1462. */
  1463. int fp_addmod_ct(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
  1464. {
  1465. fp_word w;
  1466. fp_sword s;
  1467. fp_digit mask;
  1468. int i;
  1469. if (c == d) {
  1470. return FP_VAL;
  1471. }
  1472. /* Add a to b into d. Do the subtract of modulus but don't store result.
  1473. * When subtract result is negative, the overflow will be negative.
  1474. * Only need to subtract mod when result is positive - overflow is positive.
  1475. */
  1476. w = 0;
  1477. s = 0;
  1478. for (i = 0; i < c->used; i++) {
  1479. w += a->dp[i];
  1480. w += b->dp[i];
  1481. d->dp[i] = (fp_digit)w;
  1482. s += (fp_digit)w;
  1483. s -= c->dp[i];
  1484. w >>= DIGIT_BIT;
  1485. s >>= DIGIT_BIT;
  1486. }
  1487. s += (fp_digit)w;
  1488. /* s will be positive when subtracting modulus is needed. */
  1489. mask = (fp_digit)0 - (s >= 0);
  1490. /* Constant time, conditionally, subtract modulus from sum. */
  1491. w = 0;
  1492. for (i = 0; i < c->used; i++) {
  1493. w += c->dp[i] & mask;
  1494. w = d->dp[i] - w;
  1495. d->dp[i] = (fp_digit)w;
  1496. w = (w >> DIGIT_BIT)&1;
  1497. }
  1498. /* Result will always have digits equal to or less than those in modulus. */
  1499. d->used = i;
  1500. d->sign = FP_ZPOS;
  1501. fp_clamp(d);
  1502. return FP_OKAY;
  1503. }
  1504. #ifdef TFM_TIMING_RESISTANT
  1505. #ifdef WC_RSA_NONBLOCK
  1506. #ifdef WC_RSA_NONBLOCK_TIME
  1507. /* User can override the check-time at build-time using the
  1508. * FP_EXPTMOD_NB_CHECKTIME macro to define your own function */
  1509. #ifndef FP_EXPTMOD_NB_CHECKTIME
  1510. /* instruction count for each type of operation */
  1511. /* array lookup is using TFM_EXPTMOD_NB_* states */
  1512. static const word32 exptModNbInst[TFM_EXPTMOD_NB_COUNT] = {
  1513. #ifdef TFM_PPC32
  1514. #ifdef _DEBUG
  1515. 11098, 8701, 3971, 178394, 858093, 1040, 822, 178056, 181574, 90883, 184339, 236813
  1516. #else
  1517. 7050, 2554, 3187, 43178, 200422, 384, 275, 43024, 43550, 30450, 46270, 61376
  1518. #endif
  1519. #elif defined(TFM_X86_64)
  1520. #ifdef _DEBUG
  1521. 954, 2377, 858, 19027, 90840, 287, 407, 20140, 7874, 11385, 8005, 6151
  1522. #else
  1523. 765, 1007, 771, 5216, 34993, 248, 193, 4975, 4201, 3947, 4275, 3811
  1524. #endif
  1525. #else /* software only fast math */
  1526. #ifdef _DEBUG
  1527. 798, 2245, 802, 16657, 66920, 352, 186, 16997, 16145, 12789, 16742, 15006
  1528. #else
  1529. 775, 1084, 783, 4692, 37510, 207, 183, 4374, 4392, 3097, 4442, 4079
  1530. #endif
  1531. #endif
  1532. };
  1533. static int fp_exptmod_nb_checktime(exptModNb_t* nb)
  1534. {
  1535. word32 totalInst;
  1536. /* if no max time has been set then stop (do not block) */
  1537. if (nb->maxBlockInst == 0 || nb->state >= TFM_EXPTMOD_NB_COUNT) {
  1538. return TFM_EXPTMOD_NB_STOP;
  1539. }
  1540. /* if instruction table not set then use maxBlockInst as simple counter */
  1541. if (exptModNbInst[nb->state] == 0) {
  1542. if (++nb->totalInst < nb->maxBlockInst)
  1543. return TFM_EXPTMOD_NB_CONTINUE;
  1544. nb->totalInst = 0; /* reset counter */
  1545. return TFM_EXPTMOD_NB_STOP;
  1546. }
  1547. /* get total instruction count including next operation */
  1548. totalInst = nb->totalInst + exptModNbInst[nb->state];
  1549. /* if the next operation can completed within the maximum then continue */
  1550. if (totalInst <= nb->maxBlockInst) {
  1551. return TFM_EXPTMOD_NB_CONTINUE;
  1552. }
  1553. return TFM_EXPTMOD_NB_STOP;
  1554. }
  1555. #define FP_EXPTMOD_NB_CHECKTIME(nb) fp_exptmod_nb_checktime((nb))
  1556. #endif /* !FP_EXPTMOD_NB_CHECKTIME */
  1557. #endif /* WC_RSA_NONBLOCK_TIME */
  1558. /* non-blocking version of timing resistant fp_exptmod function */
  1559. /* supports cache resistance */
  1560. int fp_exptmod_nb(exptModNb_t* nb, fp_int* G, fp_int* X, fp_int* P, fp_int* Y)
  1561. {
  1562. int err, ret = FP_WOULDBLOCK;
  1563. if (nb == NULL)
  1564. return FP_VAL;
  1565. #ifdef WC_RSA_NONBLOCK_TIME
  1566. nb->totalInst = 0;
  1567. do {
  1568. nb->totalInst += exptModNbInst[nb->state];
  1569. #endif
  1570. switch (nb->state) {
  1571. case TFM_EXPTMOD_NB_INIT:
  1572. /* now setup montgomery */
  1573. if ((err = fp_montgomery_setup(P, &nb->mp)) != FP_OKAY) {
  1574. nb->state = TFM_EXPTMOD_NB_INIT;
  1575. return err;
  1576. }
  1577. /* init ints */
  1578. fp_init(&nb->R[0]);
  1579. fp_init(&nb->R[1]);
  1580. #ifndef WC_NO_CACHE_RESISTANT
  1581. fp_init(&nb->R[2]);
  1582. #endif
  1583. nb->state = TFM_EXPTMOD_NB_MONT;
  1584. break;
  1585. case TFM_EXPTMOD_NB_MONT:
  1586. /* mod m -> R[0] */
  1587. err = fp_montgomery_calc_normalization(&nb->R[0], P);
  1588. if (err != FP_OKAY) {
  1589. nb->state = TFM_EXPTMOD_NB_INIT;
  1590. return err;
  1591. }
  1592. nb->state = TFM_EXPTMOD_NB_MONT_RED;
  1593. break;
  1594. case TFM_EXPTMOD_NB_MONT_RED:
  1595. /* reduce G -> R[1] */
  1596. if (fp_cmp_mag(P, G) != FP_GT) {
  1597. /* G > P so we reduce it first */
  1598. err = fp_mod(G, P, &nb->R[1]);
  1599. if (err != FP_OKAY) {
  1600. nb->state = TFM_EXPTMOD_NB_INIT;
  1601. return err;
  1602. }
  1603. } else {
  1604. fp_copy(G, &nb->R[1]);
  1605. }
  1606. nb->state = TFM_EXPTMOD_NB_MONT_MUL;
  1607. break;
  1608. case TFM_EXPTMOD_NB_MONT_MUL:
  1609. /* G (R[1]) * m (R[0]) */
  1610. err = fp_mul(&nb->R[1], &nb->R[0], &nb->R[1]);
  1611. if (err != FP_OKAY) {
  1612. nb->state = TFM_EXPTMOD_NB_INIT;
  1613. return err;
  1614. }
  1615. nb->state = TFM_EXPTMOD_NB_MONT_MOD;
  1616. break;
  1617. case TFM_EXPTMOD_NB_MONT_MOD:
  1618. /* mod m */
  1619. err = fp_div(&nb->R[1], P, NULL, &nb->R[1]);
  1620. if (err != FP_OKAY) {
  1621. nb->state = TFM_EXPTMOD_NB_INIT;
  1622. return err;
  1623. }
  1624. nb->state = TFM_EXPTMOD_NB_MONT_MODCHK;
  1625. break;
  1626. case TFM_EXPTMOD_NB_MONT_MODCHK:
  1627. /* m matches sign of (G * R mod m) */
  1628. if (nb->R[1].sign != P->sign) {
  1629. err = fp_add(&nb->R[1], P, &nb->R[1]);
  1630. if (err != FP_OKAY) {
  1631. nb->state = TFM_EXPTMOD_NB_INIT;
  1632. return err;
  1633. }
  1634. }
  1635. /* set initial mode and bit cnt */
  1636. nb->bitcnt = 1;
  1637. nb->buf = 0;
  1638. nb->digidx = X->used - 1;
  1639. nb->state = TFM_EXPTMOD_NB_NEXT;
  1640. break;
  1641. case TFM_EXPTMOD_NB_NEXT:
  1642. /* grab next digit as required */
  1643. if (--nb->bitcnt == 0) {
  1644. /* if nb->digidx == -1 we are out of digits so break */
  1645. if (nb->digidx == -1) {
  1646. nb->state = TFM_EXPTMOD_NB_RED;
  1647. break;
  1648. }
  1649. /* read next digit and reset nb->bitcnt */
  1650. nb->buf = X->dp[nb->digidx--];
  1651. nb->bitcnt = (int)DIGIT_BIT;
  1652. }
  1653. /* grab the next msb from the exponent */
  1654. nb->y = (int)(nb->buf >> (DIGIT_BIT - 1)) & 1;
  1655. nb->buf <<= (fp_digit)1;
  1656. nb->state = TFM_EXPTMOD_NB_MUL;
  1657. FALL_THROUGH;
  1658. case TFM_EXPTMOD_NB_MUL:
  1659. fp_mul(&nb->R[0], &nb->R[1], &nb->R[nb->y^1]);
  1660. nb->state = TFM_EXPTMOD_NB_MUL_RED;
  1661. break;
  1662. case TFM_EXPTMOD_NB_MUL_RED:
  1663. err = fp_montgomery_reduce(&nb->R[nb->y^1], P, nb->mp);
  1664. if (err != FP_OKAY) {
  1665. nb->state = TFM_EXPTMOD_NB_INIT;
  1666. return err;
  1667. }
  1668. nb->state = TFM_EXPTMOD_NB_SQR;
  1669. break;
  1670. case TFM_EXPTMOD_NB_SQR:
  1671. #ifdef WC_NO_CACHE_RESISTANT
  1672. err = fp_sqr(&nb->R[nb->y], &nb->R[nb->y]);
  1673. #else
  1674. fp_copy((fp_int*) ( ((wc_ptr_t)&nb->R[0] & wc_off_on_addr[nb->y^1]) +
  1675. ((wc_ptr_t)&nb->R[1] & wc_off_on_addr[nb->y]) ),
  1676. &nb->R[2]);
  1677. err = fp_sqr(&nb->R[2], &nb->R[2]);
  1678. #endif /* WC_NO_CACHE_RESISTANT */
  1679. if (err != FP_OKAY) {
  1680. nb->state = TFM_EXPTMOD_NB_INIT;
  1681. return err;
  1682. }
  1683. nb->state = TFM_EXPTMOD_NB_SQR_RED;
  1684. break;
  1685. case TFM_EXPTMOD_NB_SQR_RED:
  1686. #ifdef WC_NO_CACHE_RESISTANT
  1687. err = fp_montgomery_reduce(&nb->R[nb->y], P, nb->mp);
  1688. #else
  1689. err = fp_montgomery_reduce(&nb->R[2], P, nb->mp);
  1690. fp_copy(&nb->R[2],
  1691. (fp_int*) ( ((wc_ptr_t)&nb->R[0] & wc_off_on_addr[nb->y^1]) +
  1692. ((wc_ptr_t)&nb->R[1] & wc_off_on_addr[nb->y]) ) );
  1693. #endif /* WC_NO_CACHE_RESISTANT */
  1694. if (err != FP_OKAY) {
  1695. nb->state = TFM_EXPTMOD_NB_INIT;
  1696. return err;
  1697. }
  1698. nb->state = TFM_EXPTMOD_NB_NEXT;
  1699. break;
  1700. case TFM_EXPTMOD_NB_RED:
  1701. /* final reduce */
  1702. err = fp_montgomery_reduce(&nb->R[0], P, nb->mp);
  1703. if (err != FP_OKAY) {
  1704. nb->state = TFM_EXPTMOD_NB_INIT;
  1705. return err;
  1706. }
  1707. fp_copy(&nb->R[0], Y);
  1708. nb->state = TFM_EXPTMOD_NB_INIT;
  1709. ret = FP_OKAY;
  1710. break;
  1711. } /* switch */
  1712. #ifdef WC_RSA_NONBLOCK_TIME
  1713. /* determine if maximum blocking time has been reached */
  1714. } while (ret == FP_WOULDBLOCK &&
  1715. FP_EXPTMOD_NB_CHECKTIME(nb) == TFM_EXPTMOD_NB_CONTINUE);
  1716. #endif
  1717. return ret;
  1718. }
  1719. #endif /* WC_RSA_NONBLOCK */
  1720. #ifndef WC_PROTECT_ENCRYPTED_MEM
  1721. /* timing resistant montgomery ladder based exptmod
  1722. Based on work by Marc Joye, Sung-Ming Yen, "The Montgomery Powering Ladder",
  1723. Cryptographic Hardware and Embedded Systems, CHES 2002
  1724. */
  1725. static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P,
  1726. fp_int * Y)
  1727. {
  1728. #ifndef WOLFSSL_SMALL_STACK
  1729. #ifdef WC_NO_CACHE_RESISTANT
  1730. fp_int R[2];
  1731. #else
  1732. fp_int R[3]; /* need a temp for cache resistance */
  1733. #endif
  1734. #else
  1735. fp_int *R;
  1736. #endif
  1737. fp_digit buf, mp;
  1738. int err, bitcnt, digidx, y;
  1739. /* now setup montgomery */
  1740. if ((err = fp_montgomery_setup (P, &mp)) != FP_OKAY) {
  1741. return err;
  1742. }
  1743. #ifdef WOLFSSL_SMALL_STACK
  1744. #ifndef WC_NO_CACHE_RESISTANT
  1745. R = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_BIGINT);
  1746. #else
  1747. R = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_BIGINT);
  1748. #endif
  1749. if (R == NULL)
  1750. return FP_MEM;
  1751. #endif
  1752. fp_init(&R[0]);
  1753. fp_init(&R[1]);
  1754. #ifndef WC_NO_CACHE_RESISTANT
  1755. fp_init(&R[2]);
  1756. #endif
  1757. /* now we need R mod m */
  1758. err = fp_montgomery_calc_normalization (&R[0], P);
  1759. if (err != FP_OKAY) {
  1760. #ifdef WOLFSSL_SMALL_STACK
  1761. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1762. #endif
  1763. return err;
  1764. }
  1765. /* now set R[0][1] to G * R mod m */
  1766. if (fp_cmp_mag(P, G) != FP_GT) {
  1767. /* G > P so we reduce it first */
  1768. err = fp_mod(G, P, &R[1]);
  1769. if (err != FP_OKAY) {
  1770. #ifdef WOLFSSL_SMALL_STACK
  1771. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1772. #endif
  1773. return err;
  1774. }
  1775. } else {
  1776. fp_copy(G, &R[1]);
  1777. }
  1778. err = fp_mulmod (&R[1], &R[0], P, &R[1]);
  1779. if (err != FP_OKAY) {
  1780. #ifdef WOLFSSL_SMALL_STACK
  1781. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1782. #endif
  1783. return err;
  1784. }
  1785. /* for j = t-1 downto 0 do
  1786. r_!k = R0*R1; r_k = r_k^2
  1787. */
  1788. /* set initial mode and bit cnt */
  1789. bitcnt = 1;
  1790. buf = 0;
  1791. digidx = digits - 1;
  1792. for (;;) {
  1793. /* grab next digit as required */
  1794. if (--bitcnt == 0) {
  1795. /* if digidx == -1 we are out of digits so break */
  1796. if (digidx == -1) {
  1797. break;
  1798. }
  1799. /* read next digit and reset bitcnt */
  1800. buf = X->dp[digidx--];
  1801. bitcnt = (int)DIGIT_BIT;
  1802. }
  1803. /* grab the next msb from the exponent */
  1804. y = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  1805. buf <<= (fp_digit)1;
  1806. #ifdef WC_NO_CACHE_RESISTANT
  1807. /* do ops */
  1808. err = fp_mul(&R[0], &R[1], &R[y^1]);
  1809. if (err != FP_OKAY) {
  1810. #ifdef WOLFSSL_SMALL_STACK
  1811. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1812. #endif
  1813. return err;
  1814. }
  1815. err = fp_montgomery_reduce(&R[y^1], P, mp);
  1816. if (err != FP_OKAY) {
  1817. #ifdef WOLFSSL_SMALL_STACK
  1818. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1819. #endif
  1820. return err;
  1821. }
  1822. err = fp_sqr(&R[y], &R[y]);
  1823. if (err != FP_OKAY) {
  1824. #ifdef WOLFSSL_SMALL_STACK
  1825. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1826. #endif
  1827. return err;
  1828. }
  1829. err = fp_montgomery_reduce(&R[y], P, mp);
  1830. if (err != FP_OKAY) {
  1831. #ifdef WOLFSSL_SMALL_STACK
  1832. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1833. #endif
  1834. return err;
  1835. }
  1836. #else
  1837. /* do ops */
  1838. err = fp_mul(&R[0], &R[1], &R[2]);
  1839. if (err != FP_OKAY) {
  1840. #ifdef WOLFSSL_SMALL_STACK
  1841. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1842. #endif
  1843. return err;
  1844. }
  1845. err = fp_montgomery_reduce(&R[2], P, mp);
  1846. if (err != FP_OKAY) {
  1847. #ifdef WOLFSSL_SMALL_STACK
  1848. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1849. #endif
  1850. return err;
  1851. }
  1852. /* instead of using R[y^1] for mul, which leaks key bit to cache monitor,
  1853. * use R[2] as temp, make sure address calc is constant, keep
  1854. * &R[0] and &R[1] in cache */
  1855. fp_copy(&R[2],
  1856. (fp_int*) ( ((wc_ptr_t)&R[0] & wc_off_on_addr[y]) +
  1857. ((wc_ptr_t)&R[1] & wc_off_on_addr[y^1]) ) );
  1858. /* instead of using R[y] for sqr, which leaks key bit to cache monitor,
  1859. * use R[2] as temp, make sure address calc is constant, keep
  1860. * &R[0] and &R[1] in cache */
  1861. fp_copy((fp_int*) ( ((wc_ptr_t)&R[0] & wc_off_on_addr[y^1]) +
  1862. ((wc_ptr_t)&R[1] & wc_off_on_addr[y]) ),
  1863. &R[2]);
  1864. err = fp_sqr(&R[2], &R[2]);
  1865. if (err != FP_OKAY) {
  1866. #ifdef WOLFSSL_SMALL_STACK
  1867. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1868. #endif
  1869. return err;
  1870. }
  1871. err = fp_montgomery_reduce(&R[2], P, mp);
  1872. if (err != FP_OKAY) {
  1873. #ifdef WOLFSSL_SMALL_STACK
  1874. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1875. #endif
  1876. return err;
  1877. }
  1878. fp_copy(&R[2],
  1879. (fp_int*) ( ((wc_ptr_t)&R[0] & wc_off_on_addr[y^1]) +
  1880. ((wc_ptr_t)&R[1] & wc_off_on_addr[y]) ) );
  1881. #endif /* WC_NO_CACHE_RESISTANT */
  1882. }
  1883. err = fp_montgomery_reduce(&R[0], P, mp);
  1884. fp_copy(&R[0], Y);
  1885. #ifdef WOLFSSL_SMALL_STACK
  1886. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1887. #endif
  1888. return err;
  1889. }
  1890. #else
  1891. /* Copy from a1 and a2 into r1 and r2 based on y in constant time.
  1892. * When y is 1, r1 = a1 and r2 = a2.
  1893. * When y is 0, r1 = a2 and r2 = a1.
  1894. * Always copy size digits as that is the maximum size for a1 and a2.
  1895. */
  1896. static void fp_copy_2_ct(fp_int* a1, fp_int* a2, fp_int* r1, fp_int* r2, int y,
  1897. int size)
  1898. {
  1899. int i;
  1900. /* Copy data - constant time. */
  1901. for (i = 0; i < size; i++) {
  1902. r1->dp[i] = (a1->dp[i] & ((fp_digit)wc_off_on_addr[y ])) +
  1903. (a2->dp[i] & ((fp_digit)wc_off_on_addr[y^1]));
  1904. r2->dp[i] = (a1->dp[i] & ((fp_digit)wc_off_on_addr[y^1])) +
  1905. (a2->dp[i] & ((fp_digit)wc_off_on_addr[y ]));
  1906. }
  1907. /* Copy used. */
  1908. r1->used = (a1->used & ((int)wc_off_on_addr[y ])) +
  1909. (a2->used & ((int)wc_off_on_addr[y^1]));
  1910. r2->used = (a1->used & ((int)wc_off_on_addr[y^1])) +
  1911. (a2->used & ((int)wc_off_on_addr[y ]));
  1912. /* Copy sign. */
  1913. r1->sign = (a1->sign & ((int)wc_off_on_addr[y ])) +
  1914. (a2->sign & ((int)wc_off_on_addr[y^1]));
  1915. r2->sign = (a1->sign & ((int)wc_off_on_addr[y^1])) +
  1916. (a2->sign & ((int)wc_off_on_addr[y ]));
  1917. }
  1918. /* timing resistant montgomery ladder based exptmod
  1919. Based on work by Marc Joye, Sung-Ming Yen, "The Montgomery Powering Ladder",
  1920. Cryptographic Hardware and Embedded Systems, CHES 2002
  1921. */
  1922. static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P,
  1923. fp_int * Y)
  1924. {
  1925. #ifndef WOLFSSL_SMALL_STACK
  1926. fp_int R[4]; /* need a temp for cache resistance */
  1927. #else
  1928. fp_int *R;
  1929. #endif
  1930. fp_digit buf, mp;
  1931. int err, bitcnt, digidx, y;
  1932. /* now setup montgomery */
  1933. if ((err = fp_montgomery_setup (P, &mp)) != FP_OKAY) {
  1934. return err;
  1935. }
  1936. #ifdef WOLFSSL_SMALL_STACK
  1937. R = (fp_int*)XMALLOC(sizeof(fp_int) * 4, NULL, DYNAMIC_TYPE_BIGINT);
  1938. if (R == NULL)
  1939. return FP_MEM;
  1940. #endif
  1941. fp_init(&R[0]);
  1942. fp_init(&R[1]);
  1943. fp_init(&R[2]);
  1944. fp_init(&R[3]);
  1945. /* now we need R mod m */
  1946. err = fp_montgomery_calc_normalization (&R[0], P);
  1947. if (err != FP_OKAY) {
  1948. #ifdef WOLFSSL_SMALL_STACK
  1949. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1950. #endif
  1951. return err;
  1952. }
  1953. /* now set R[0][1] to G * R mod m */
  1954. if (fp_cmp_mag(P, G) != FP_GT) {
  1955. /* G > P so we reduce it first */
  1956. err = fp_mod(G, P, &R[1]);
  1957. if (err != FP_OKAY) {
  1958. #ifdef WOLFSSL_SMALL_STACK
  1959. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1960. #endif
  1961. return err;
  1962. }
  1963. } else {
  1964. fp_copy(G, &R[1]);
  1965. }
  1966. err = fp_mulmod (&R[1], &R[0], P, &R[1]);
  1967. if (err != FP_OKAY) {
  1968. #ifdef WOLFSSL_SMALL_STACK
  1969. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1970. #endif
  1971. return err;
  1972. }
  1973. /* for j = t-1 downto 0 do
  1974. r_!k = R0*R1; r_k = r_k^2
  1975. */
  1976. /* set initial mode and bit cnt */
  1977. bitcnt = 1;
  1978. buf = 0;
  1979. digidx = digits - 1;
  1980. for (;;) {
  1981. /* grab next digit as required */
  1982. if (--bitcnt == 0) {
  1983. /* if digidx == -1 we are out of digits so break */
  1984. if (digidx == -1) {
  1985. break;
  1986. }
  1987. /* read next digit and reset bitcnt */
  1988. buf = X->dp[digidx--];
  1989. bitcnt = (int)DIGIT_BIT;
  1990. }
  1991. /* grab the next msb from the exponent */
  1992. y = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  1993. buf <<= (fp_digit)1;
  1994. /* do ops */
  1995. err = fp_mul(&R[0], &R[1], &R[2]);
  1996. if (err != FP_OKAY) {
  1997. #ifdef WOLFSSL_SMALL_STACK
  1998. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  1999. #endif
  2000. return err;
  2001. }
  2002. err = fp_montgomery_reduce(&R[2], P, mp);
  2003. if (err != FP_OKAY) {
  2004. #ifdef WOLFSSL_SMALL_STACK
  2005. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  2006. #endif
  2007. return err;
  2008. }
  2009. /* instead of using R[y] for sqr, which leaks key bit to cache monitor,
  2010. * use R[3] as temp, make sure address calc is constant, keep
  2011. * &R[0] and &R[1] in cache */
  2012. fp_copy((fp_int*) ( ((wc_ptr_t)&R[0] & wc_off_on_addr[y^1]) +
  2013. ((wc_ptr_t)&R[1] & wc_off_on_addr[y]) ),
  2014. &R[3]);
  2015. err = fp_sqr(&R[3], &R[3]);
  2016. if (err != FP_OKAY) {
  2017. #ifdef WOLFSSL_SMALL_STACK
  2018. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  2019. #endif
  2020. return err;
  2021. }
  2022. err = fp_montgomery_reduce(&R[3], P, mp);
  2023. if (err != FP_OKAY) {
  2024. #ifdef WOLFSSL_SMALL_STACK
  2025. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  2026. #endif
  2027. return err;
  2028. }
  2029. fp_copy_2_ct(&R[2], &R[3], &R[0], &R[1], y, P->used);
  2030. }
  2031. err = fp_montgomery_reduce(&R[0], P, mp);
  2032. fp_copy(&R[0], Y);
  2033. #ifdef WOLFSSL_SMALL_STACK
  2034. XFREE(R, NULL, DYNAMIC_TYPE_BIGINT);
  2035. #endif
  2036. return err;
  2037. }
  2038. #endif /* WC_PROTECT_ENCRYPTED_MEM */
  2039. #endif /* TFM_TIMING_RESISTANT */
  2040. /* y = g**x (mod b)
  2041. * Some restrictions... x must be positive and < b
  2042. */
  2043. static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
  2044. {
  2045. fp_int *res;
  2046. fp_digit buf, mp;
  2047. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  2048. #ifndef WOLFSSL_NO_MALLOC
  2049. fp_int *M;
  2050. #else
  2051. fp_int M[(1 << 6) + 1];
  2052. #endif
  2053. /* find window size */
  2054. x = fp_count_bits (X);
  2055. if (x <= 21) {
  2056. winsize = 1;
  2057. } else if (x <= 36) {
  2058. winsize = 3;
  2059. } else if (x <= 140) {
  2060. winsize = 4;
  2061. } else if (x <= 450) {
  2062. winsize = 5;
  2063. } else {
  2064. winsize = 6;
  2065. }
  2066. /* now setup montgomery */
  2067. if ((err = fp_montgomery_setup (P, &mp)) != FP_OKAY) {
  2068. return err;
  2069. }
  2070. #ifndef WOLFSSL_NO_MALLOC
  2071. /* only allocate space for what's needed for window plus res */
  2072. M = (fp_int*)XMALLOC(sizeof(fp_int)*((1 << winsize) + 1), NULL,
  2073. DYNAMIC_TYPE_BIGINT);
  2074. if (M == NULL) {
  2075. return FP_MEM;
  2076. }
  2077. #endif
  2078. res = &M[(word32)(1 << winsize)];
  2079. /* init M array */
  2080. for(x = 0; x < (1 << winsize); x++)
  2081. fp_init(&M[x]);
  2082. /* setup result */
  2083. fp_init(res);
  2084. /* create M table
  2085. *
  2086. * The M table contains powers of the input base, e.g. M[x] = G^x mod P
  2087. *
  2088. * The first half of the table is not computed though except for M[0] and M[1]
  2089. */
  2090. /* now we need R mod m */
  2091. err = fp_montgomery_calc_normalization (res, P);
  2092. if (err != FP_OKAY) {
  2093. #ifndef WOLFSSL_NO_MALLOC
  2094. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2095. #endif
  2096. return err;
  2097. }
  2098. /* now set M[1] to G * R mod m */
  2099. if (fp_cmp_mag(P, G) != FP_GT) {
  2100. /* G > P so we reduce it first */
  2101. err = fp_mod(G, P, &M[1]);
  2102. if (err != FP_OKAY) {
  2103. #ifndef WOLFSSL_NO_MALLOC
  2104. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2105. #endif
  2106. return err;
  2107. }
  2108. } else {
  2109. fp_copy(G, &M[1]);
  2110. }
  2111. err = fp_mulmod (&M[1], res, P, &M[1]);
  2112. if (err != FP_OKAY) {
  2113. #ifndef WOLFSSL_NO_MALLOC
  2114. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2115. #endif
  2116. return err;
  2117. }
  2118. /* compute the value at M[1<<(winsize-1)] by
  2119. * squaring M[1] (winsize-1) times */
  2120. fp_copy (&M[1], &M[(word32)(1 << (winsize - 1))]);
  2121. for (x = 0; x < (winsize - 1); x++) {
  2122. err = fp_sqr (&M[(word32)(1 << (winsize - 1))],
  2123. &M[(word32)(1 << (winsize - 1))]);
  2124. if (err != FP_OKAY) {
  2125. #ifndef WOLFSSL_NO_MALLOC
  2126. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2127. #endif
  2128. return err;
  2129. }
  2130. err = fp_montgomery_reduce_ex(&M[(word32)(1 << (winsize - 1))], P, mp, 0);
  2131. if (err != FP_OKAY) {
  2132. #ifndef WOLFSSL_NO_MALLOC
  2133. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2134. #endif
  2135. return err;
  2136. }
  2137. }
  2138. /* create upper table */
  2139. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  2140. err = fp_mul(&M[x - 1], &M[1], &M[x]);
  2141. if (err != FP_OKAY) {
  2142. #ifndef WOLFSSL_NO_MALLOC
  2143. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2144. #endif
  2145. return err;
  2146. }
  2147. err = fp_montgomery_reduce_ex(&M[x], P, mp, 0);
  2148. if (err != FP_OKAY) {
  2149. #ifndef WOLFSSL_NO_MALLOC
  2150. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2151. #endif
  2152. return err;
  2153. }
  2154. }
  2155. /* set initial mode and bit cnt */
  2156. mode = 0;
  2157. bitcnt = (x % DIGIT_BIT) + 1;
  2158. buf = 0;
  2159. digidx = X->used - 1;
  2160. bitcpy = 0;
  2161. bitbuf = 0;
  2162. for (;;) {
  2163. /* grab next digit as required */
  2164. if (--bitcnt == 0) {
  2165. /* if digidx == -1 we are out of digits so break */
  2166. if (digidx == -1) {
  2167. break;
  2168. }
  2169. /* read next digit and reset bitcnt */
  2170. buf = X->dp[digidx--];
  2171. bitcnt = (int)DIGIT_BIT;
  2172. }
  2173. /* grab the next msb from the exponent */
  2174. y = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  2175. buf <<= (fp_digit)1;
  2176. /* if the bit is zero and mode == 0 then we ignore it
  2177. * These represent the leading zero bits before the first 1 bit
  2178. * in the exponent. Technically this opt is not required but it
  2179. * does lower the # of trivial squaring/reductions used
  2180. */
  2181. if (mode == 0 && y == 0) {
  2182. continue;
  2183. }
  2184. /* if the bit is zero and mode == 1 then we square */
  2185. if (mode == 1 && y == 0) {
  2186. err = fp_sqr(res, res);
  2187. if (err != FP_OKAY) {
  2188. #ifndef WOLFSSL_NO_MALLOC
  2189. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2190. #endif
  2191. return err;
  2192. }
  2193. err = fp_montgomery_reduce_ex(res, P, mp, 0);
  2194. if (err != FP_OKAY) {
  2195. #ifndef WOLFSSL_NO_MALLOC
  2196. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2197. #endif
  2198. return err;
  2199. }
  2200. continue;
  2201. }
  2202. /* else we add it to the window */
  2203. bitbuf |= (y << (winsize - ++bitcpy));
  2204. mode = 2;
  2205. if (bitcpy == winsize) {
  2206. /* ok window is filled so square as required and multiply */
  2207. /* square first */
  2208. for (x = 0; x < winsize; x++) {
  2209. err = fp_sqr(res, res);
  2210. if (err != FP_OKAY) {
  2211. #ifndef WOLFSSL_NO_MALLOC
  2212. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2213. #endif
  2214. return err;
  2215. }
  2216. err = fp_montgomery_reduce_ex(res, P, mp, 0);
  2217. if (err != FP_OKAY) {
  2218. #ifndef WOLFSSL_NO_MALLOC
  2219. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2220. #endif
  2221. return err;
  2222. }
  2223. }
  2224. /* then multiply */
  2225. err = fp_mul(res, &M[bitbuf], res);
  2226. if (err != FP_OKAY) {
  2227. #ifndef WOLFSSL_NO_MALLOC
  2228. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2229. #endif
  2230. return err;
  2231. }
  2232. err = fp_montgomery_reduce_ex(res, P, mp, 0);
  2233. if (err != FP_OKAY) {
  2234. #ifndef WOLFSSL_NO_MALLOC
  2235. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2236. #endif
  2237. return err;
  2238. }
  2239. /* empty window and reset */
  2240. bitcpy = 0;
  2241. bitbuf = 0;
  2242. mode = 1;
  2243. }
  2244. }
  2245. /* if bits remain then square/multiply */
  2246. if (mode == 2 && bitcpy > 0) {
  2247. /* square then multiply if the bit is set */
  2248. for (x = 0; x < bitcpy; x++) {
  2249. err = fp_sqr(res, res);
  2250. if (err != FP_OKAY) {
  2251. #ifndef WOLFSSL_NO_MALLOC
  2252. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2253. #endif
  2254. return err;
  2255. }
  2256. err = fp_montgomery_reduce_ex(res, P, mp, 0);
  2257. if (err != FP_OKAY) {
  2258. #ifndef WOLFSSL_NO_MALLOC
  2259. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2260. #endif
  2261. return err;
  2262. }
  2263. /* get next bit of the window */
  2264. bitbuf <<= 1;
  2265. if ((bitbuf & (1 << winsize)) != 0) {
  2266. /* then multiply */
  2267. err = fp_mul(res, &M[1], res);
  2268. if (err != FP_OKAY) {
  2269. #ifndef WOLFSSL_NO_MALLOC
  2270. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2271. #endif
  2272. return err;
  2273. }
  2274. err = fp_montgomery_reduce_ex(res, P, mp, 0);
  2275. if (err != FP_OKAY) {
  2276. #ifndef WOLFSSL_NO_MALLOC
  2277. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2278. #endif
  2279. return err;
  2280. }
  2281. }
  2282. }
  2283. }
  2284. /* fixup result if Montgomery reduction is used
  2285. * recall that any value in a Montgomery system is
  2286. * actually multiplied by R mod n. So we have
  2287. * to reduce one more time to cancel out the factor
  2288. * of R.
  2289. */
  2290. err = fp_montgomery_reduce_ex(res, P, mp, 0);
  2291. /* swap res with Y */
  2292. fp_copy (res, Y);
  2293. #ifndef WOLFSSL_NO_MALLOC
  2294. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  2295. #endif
  2296. return err;
  2297. }
  2298. #ifdef TFM_TIMING_RESISTANT
  2299. #if DIGIT_BIT <= 16
  2300. #define WINSIZE 2
  2301. #define WINMASK 0x3
  2302. #elif DIGIT_BIT <= 32
  2303. #define WINSIZE 3
  2304. #define WINMASK 0x7
  2305. #elif DIGIT_BIT <= 64
  2306. #define WINSIZE 4
  2307. #define WINMASK 0xf
  2308. #elif DIGIT_BIT <= 128
  2309. #define WINSIZE 5
  2310. #define WINMASK 0x1f
  2311. #endif
  2312. /* y = 2**x (mod b)
  2313. * Some restrictions... x must be positive and < b
  2314. */
  2315. static int _fp_exptmod_base_2(fp_int * X, int digits, fp_int * P,
  2316. fp_int * Y)
  2317. {
  2318. fp_digit buf, mp;
  2319. int err, bitbuf, bitcpy, bitcnt, digidx, x, y;
  2320. #ifdef WOLFSSL_SMALL_STACK
  2321. fp_int *res;
  2322. fp_int *tmp;
  2323. #else
  2324. fp_int res[1];
  2325. fp_int tmp[1];
  2326. #endif
  2327. #ifdef WOLFSSL_SMALL_STACK
  2328. res = (fp_int*)XMALLOC(2*sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2329. if (res == NULL) {
  2330. return FP_MEM;
  2331. }
  2332. tmp = &res[1];
  2333. #endif
  2334. /* now setup montgomery */
  2335. if ((err = fp_montgomery_setup(P, &mp)) != FP_OKAY) {
  2336. #ifdef WOLFSSL_SMALL_STACK
  2337. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2338. #endif
  2339. return err;
  2340. }
  2341. /* setup result */
  2342. fp_init(res);
  2343. fp_init(tmp);
  2344. err = fp_mul_2d(P, 1 << WINSIZE, tmp);
  2345. if (err != FP_OKAY) {
  2346. #ifdef WOLFSSL_SMALL_STACK
  2347. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2348. #endif
  2349. return err;
  2350. }
  2351. /* now we need R mod m */
  2352. err = fp_montgomery_calc_normalization(res, P);
  2353. if (err != FP_OKAY) {
  2354. #ifdef WOLFSSL_SMALL_STACK
  2355. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2356. #endif
  2357. return err;
  2358. }
  2359. /* Get the top bits left over after taking WINSIZE bits starting at the
  2360. * least-significant.
  2361. */
  2362. digidx = digits - 1;
  2363. bitcpy = (digits * DIGIT_BIT) % WINSIZE;
  2364. if (bitcpy > 0) {
  2365. bitcnt = (int)DIGIT_BIT - bitcpy;
  2366. buf = X->dp[digidx--];
  2367. bitbuf = (int)(buf >> bitcnt);
  2368. /* Multiply montgomery representation of 1 by 2 ^ top */
  2369. err = fp_mul_2d(res, bitbuf, res);
  2370. if (err != FP_OKAY) {
  2371. #ifdef WOLFSSL_SMALL_STACK
  2372. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2373. #endif
  2374. return err;
  2375. }
  2376. err = fp_add(res, tmp, res);
  2377. if (err != FP_OKAY) {
  2378. #ifdef WOLFSSL_SMALL_STACK
  2379. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2380. #endif
  2381. return err;
  2382. }
  2383. err = fp_mod(res, P, res);
  2384. if (err != FP_OKAY) {
  2385. #ifdef WOLFSSL_SMALL_STACK
  2386. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2387. #endif
  2388. return err;
  2389. }
  2390. /* Move out bits used */
  2391. buf <<= bitcpy;
  2392. bitcnt++;
  2393. }
  2394. else {
  2395. bitcnt = 1;
  2396. buf = 0;
  2397. }
  2398. /* empty window and reset */
  2399. bitbuf = 0;
  2400. bitcpy = 0;
  2401. for (;;) {
  2402. /* grab next digit as required */
  2403. if (--bitcnt == 0) {
  2404. /* if digidx == -1 we are out of digits so break */
  2405. if (digidx == -1) {
  2406. break;
  2407. }
  2408. /* read next digit and reset bitcnt */
  2409. buf = X->dp[digidx--];
  2410. bitcnt = (int)DIGIT_BIT;
  2411. }
  2412. /* grab the next msb from the exponent */
  2413. y = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  2414. buf <<= (fp_digit)1;
  2415. /* add bit to the window */
  2416. #ifndef WC_PROTECT_ENCRYPTED_MEM
  2417. bitbuf |= (y << (WINSIZE - ++bitcpy));
  2418. #else
  2419. /* Ensure value changes even when y is zero. */
  2420. bitbuf += (WINMASK + 1) + (y << (WINSIZE - ++bitcpy));
  2421. #endif
  2422. if (bitcpy == WINSIZE) {
  2423. /* ok window is filled so square as required and multiply */
  2424. /* square first */
  2425. for (x = 0; x < WINSIZE; x++) {
  2426. err = fp_sqr(res, res);
  2427. if (err != FP_OKAY) {
  2428. #ifdef WOLFSSL_SMALL_STACK
  2429. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2430. #endif
  2431. return err;
  2432. }
  2433. err = fp_montgomery_reduce(res, P, mp);
  2434. if (err != FP_OKAY) {
  2435. #ifdef WOLFSSL_SMALL_STACK
  2436. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2437. #endif
  2438. return err;
  2439. }
  2440. }
  2441. /* then multiply by 2^bitbuf */
  2442. #ifndef WC_PROTECT_ENCRYPTED_MEM
  2443. err = fp_mul_2d(res, bitbuf, res);
  2444. #else
  2445. /* Get the window bits. */
  2446. err = fp_mul_2d(res, bitbuf & WINMASK, res);
  2447. #endif
  2448. if (err != FP_OKAY) {
  2449. #ifdef WOLFSSL_SMALL_STACK
  2450. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2451. #endif
  2452. return err;
  2453. }
  2454. /* Add in value to make mod operation take same time */
  2455. err = fp_add(res, tmp, res);
  2456. if (err != FP_OKAY) {
  2457. #ifdef WOLFSSL_SMALL_STACK
  2458. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2459. #endif
  2460. return err;
  2461. }
  2462. err = fp_mod(res, P, res);
  2463. if (err != FP_OKAY) {
  2464. #ifdef WOLFSSL_SMALL_STACK
  2465. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2466. #endif
  2467. return err;
  2468. }
  2469. /* empty window and reset */
  2470. bitcpy = 0;
  2471. #ifndef WC_PROTECT_ENCRYPTED_MEM
  2472. bitbuf = 0;
  2473. #else
  2474. /* Ensure value is new even when bottom bits are 0. */
  2475. bitbuf = (WINMASK + 1) + (bitbuf & ~WINMASK);
  2476. #endif
  2477. }
  2478. }
  2479. /* fixup result if Montgomery reduction is used
  2480. * recall that any value in a Montgomery system is
  2481. * actually multiplied by R mod n. So we have
  2482. * to reduce one more time to cancel out the factor
  2483. * of R.
  2484. */
  2485. err = fp_montgomery_reduce(res, P, mp);
  2486. /* swap res with Y */
  2487. fp_copy(res, Y);
  2488. #ifdef WOLFSSL_SMALL_STACK
  2489. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2490. #endif
  2491. return err;
  2492. }
  2493. #undef WINSIZE
  2494. #else
  2495. #if DIGIT_BIT < 16
  2496. #define WINSIZE 3
  2497. #elif DIGIT_BIT < 32
  2498. #define WINSIZE 4
  2499. #elif DIGIT_BIT < 64
  2500. #define WINSIZE 5
  2501. #elif DIGIT_BIT < 128
  2502. #define WINSIZE 6
  2503. #elif DIGIT_BIT == 128
  2504. #define WINSIZE 7
  2505. #endif
  2506. /* y = 2**x (mod b)
  2507. * Some restrictions... x must be positive and < b
  2508. */
  2509. static int _fp_exptmod_base_2(fp_int * X, int digits, fp_int * P,
  2510. fp_int * Y)
  2511. {
  2512. fp_digit buf, mp;
  2513. int err, bitbuf, bitcpy, bitcnt, digidx, x, y;
  2514. #ifdef WOLFSSL_SMALL_STACK
  2515. fp_int *res;
  2516. #else
  2517. fp_int res[1];
  2518. #endif
  2519. /* now setup montgomery */
  2520. if ((err = fp_montgomery_setup(P, &mp)) != FP_OKAY) {
  2521. return err;
  2522. }
  2523. #ifdef WOLFSSL_SMALL_STACK
  2524. res = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2525. if (res == NULL) {
  2526. return FP_MEM;
  2527. }
  2528. #endif
  2529. /* setup result */
  2530. fp_init(res);
  2531. /* now we need R mod m */
  2532. err = fp_montgomery_calc_normalization(res, P);
  2533. if (err != FP_OKAY) {
  2534. #ifdef WOLFSSL_SMALL_STACK
  2535. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2536. #endif
  2537. return err;
  2538. }
  2539. /* Get the top bits left over after taking WINSIZE bits starting at the
  2540. * least-significant.
  2541. */
  2542. digidx = digits - 1;
  2543. bitcpy = (digits * DIGIT_BIT) % WINSIZE;
  2544. if (bitcpy > 0) {
  2545. bitcnt = (int)DIGIT_BIT - bitcpy;
  2546. buf = X->dp[digidx--];
  2547. bitbuf = (int)(buf >> bitcnt);
  2548. /* Multiply montgomery representation of 1 by 2 ^ top */
  2549. err = fp_mul_2d(res, bitbuf, res);
  2550. if (err != FP_OKAY) {
  2551. #ifdef WOLFSSL_SMALL_STACK
  2552. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2553. #endif
  2554. return err;
  2555. }
  2556. err = fp_mod(res, P, res);
  2557. if (err != FP_OKAY) {
  2558. #ifdef WOLFSSL_SMALL_STACK
  2559. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2560. #endif
  2561. return err;
  2562. }
  2563. /* Move out bits used */
  2564. buf <<= bitcpy;
  2565. bitcnt++;
  2566. }
  2567. else {
  2568. bitcnt = 1;
  2569. buf = 0;
  2570. }
  2571. /* empty window and reset */
  2572. bitbuf = 0;
  2573. bitcpy = 0;
  2574. for (;;) {
  2575. /* grab next digit as required */
  2576. if (--bitcnt == 0) {
  2577. /* if digidx == -1 we are out of digits so break */
  2578. if (digidx == -1) {
  2579. break;
  2580. }
  2581. /* read next digit and reset bitcnt */
  2582. buf = X->dp[digidx--];
  2583. bitcnt = (int)DIGIT_BIT;
  2584. }
  2585. /* grab the next msb from the exponent */
  2586. y = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  2587. buf <<= (fp_digit)1;
  2588. /* add bit to the window */
  2589. bitbuf |= (y << (WINSIZE - ++bitcpy));
  2590. if (bitcpy == WINSIZE) {
  2591. /* ok window is filled so square as required and multiply */
  2592. /* square first */
  2593. for (x = 0; x < WINSIZE; x++) {
  2594. err = fp_sqr(res, res);
  2595. if (err != FP_OKAY) {
  2596. #ifdef WOLFSSL_SMALL_STACK
  2597. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2598. #endif
  2599. return err;
  2600. }
  2601. err = fp_montgomery_reduce(res, P, mp);
  2602. if (err != FP_OKAY) {
  2603. #ifdef WOLFSSL_SMALL_STACK
  2604. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2605. #endif
  2606. return err;
  2607. }
  2608. }
  2609. /* then multiply by 2^bitbuf */
  2610. err = fp_mul_2d(res, bitbuf, res);
  2611. if (err != FP_OKAY) {
  2612. #ifdef WOLFSSL_SMALL_STACK
  2613. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2614. #endif
  2615. return err;
  2616. }
  2617. err = fp_mod(res, P, res);
  2618. if (err != FP_OKAY) {
  2619. #ifdef WOLFSSL_SMALL_STACK
  2620. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2621. #endif
  2622. return err;
  2623. }
  2624. /* empty window and reset */
  2625. bitcpy = 0;
  2626. bitbuf = 0;
  2627. }
  2628. }
  2629. /* fixup result if Montgomery reduction is used
  2630. * recall that any value in a Montgomery system is
  2631. * actually multiplied by R mod n. So we have
  2632. * to reduce one more time to cancel out the factor
  2633. * of R.
  2634. */
  2635. err = fp_montgomery_reduce(res, P, mp);
  2636. /* swap res with Y */
  2637. fp_copy(res, Y);
  2638. #ifdef WOLFSSL_SMALL_STACK
  2639. XFREE(res, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2640. #endif
  2641. return err;
  2642. }
  2643. #undef WINSIZE
  2644. #endif
  2645. int fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
  2646. {
  2647. #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
  2648. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
  2649. int x = fp_count_bits (X);
  2650. #endif
  2651. /* handle modulus of zero and prevent overflows */
  2652. if (fp_iszero(P) || (P->used > (FP_SIZE/2))) {
  2653. return FP_VAL;
  2654. }
  2655. if (fp_isone(P)) {
  2656. fp_set(Y, 0);
  2657. return FP_OKAY;
  2658. }
  2659. if (fp_iszero(X)) {
  2660. fp_set(Y, 1);
  2661. return FP_OKAY;
  2662. }
  2663. if (fp_iszero(G)) {
  2664. fp_set(Y, 0);
  2665. return FP_OKAY;
  2666. }
  2667. #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
  2668. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
  2669. if(x > EPS_RSA_EXPT_XBTIS) {
  2670. return esp_mp_exptmod(G, X, x, P, Y);
  2671. }
  2672. #endif
  2673. if (X->sign == FP_NEG) {
  2674. #ifndef POSITIVE_EXP_ONLY /* reduce stack if assume no negatives */
  2675. int err;
  2676. #ifndef WOLFSSL_SMALL_STACK
  2677. fp_int tmp[2];
  2678. #else
  2679. fp_int *tmp;
  2680. #endif
  2681. #ifdef WOLFSSL_SMALL_STACK
  2682. tmp = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_BIGINT);
  2683. if (tmp == NULL)
  2684. return FP_MEM;
  2685. #endif
  2686. /* yes, copy G and invmod it */
  2687. fp_init_copy(&tmp[0], G);
  2688. fp_init_copy(&tmp[1], P);
  2689. tmp[1].sign = FP_ZPOS;
  2690. err = fp_invmod(&tmp[0], &tmp[1], &tmp[0]);
  2691. if (err == FP_OKAY) {
  2692. fp_copy(X, &tmp[1]);
  2693. tmp[1].sign = FP_ZPOS;
  2694. #ifdef TFM_TIMING_RESISTANT
  2695. err = _fp_exptmod_ct(&tmp[0], &tmp[1], tmp[1].used, P, Y);
  2696. #else
  2697. err = _fp_exptmod_nct(&tmp[0], &tmp[1], P, Y);
  2698. #endif
  2699. if ((err == 0) && (P->sign == FP_NEG)) {
  2700. err = fp_add(Y, P, Y);
  2701. }
  2702. }
  2703. #ifdef WOLFSSL_SMALL_STACK
  2704. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2705. #endif
  2706. return err;
  2707. #else
  2708. return FP_VAL;
  2709. #endif
  2710. }
  2711. else if (G->used == 1 && G->dp[0] == 2) {
  2712. return _fp_exptmod_base_2(X, X->used, P, Y);
  2713. }
  2714. else {
  2715. /* Positive exponent so just exptmod */
  2716. #ifdef TFM_TIMING_RESISTANT
  2717. return _fp_exptmod_ct(G, X, X->used, P, Y);
  2718. #else
  2719. return _fp_exptmod_nct(G, X, P, Y);
  2720. #endif
  2721. }
  2722. }
  2723. int fp_exptmod_ex(fp_int * G, fp_int * X, int digits, fp_int * P, fp_int * Y)
  2724. {
  2725. #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
  2726. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
  2727. int x = fp_count_bits (X);
  2728. #endif
  2729. /* handle modulus of zero and prevent overflows */
  2730. if (fp_iszero(P) || (P->used > (FP_SIZE/2))) {
  2731. return FP_VAL;
  2732. }
  2733. if (fp_isone(P)) {
  2734. fp_set(Y, 0);
  2735. return FP_OKAY;
  2736. }
  2737. if (fp_iszero(X)) {
  2738. fp_set(Y, 1);
  2739. return FP_OKAY;
  2740. }
  2741. if (fp_iszero(G)) {
  2742. fp_set(Y, 0);
  2743. return FP_OKAY;
  2744. }
  2745. #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
  2746. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
  2747. if(x > EPS_RSA_EXPT_XBTIS) {
  2748. return esp_mp_exptmod(G, X, x, P, Y);
  2749. }
  2750. #endif
  2751. if (X->sign == FP_NEG) {
  2752. #ifndef POSITIVE_EXP_ONLY /* reduce stack if assume no negatives */
  2753. int err;
  2754. #ifndef WOLFSSL_SMALL_STACK
  2755. fp_int tmp[2];
  2756. #else
  2757. fp_int *tmp;
  2758. #endif
  2759. #ifdef WOLFSSL_SMALL_STACK
  2760. tmp = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2761. if (tmp == NULL)
  2762. return FP_MEM;
  2763. #endif
  2764. /* yes, copy G and invmod it */
  2765. fp_init_copy(&tmp[0], G);
  2766. fp_init_copy(&tmp[1], P);
  2767. tmp[1].sign = FP_ZPOS;
  2768. err = fp_invmod(&tmp[0], &tmp[1], &tmp[0]);
  2769. if (err == FP_OKAY) {
  2770. X->sign = FP_ZPOS;
  2771. #ifdef TFM_TIMING_RESISTANT
  2772. err = _fp_exptmod_ct(&tmp[0], X, digits, P, Y);
  2773. #else
  2774. err = _fp_exptmod_nct(&tmp[0], X, P, Y);
  2775. (void)digits;
  2776. #endif
  2777. if (X != Y) {
  2778. X->sign = FP_NEG;
  2779. }
  2780. if ((err == 0) && (P->sign == FP_NEG)) {
  2781. err = fp_add(Y, P, Y);
  2782. }
  2783. }
  2784. #ifdef WOLFSSL_SMALL_STACK
  2785. XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
  2786. #endif
  2787. return err;
  2788. #else
  2789. return FP_VAL;
  2790. #endif
  2791. }
  2792. else {
  2793. /* Positive exponent so just exptmod */
  2794. #ifdef TFM_TIMING_RESISTANT
  2795. return _fp_exptmod_ct(G, X, digits, P, Y);
  2796. #else
  2797. return _fp_exptmod_nct(G, X, P, Y);
  2798. #endif
  2799. }
  2800. }
  2801. int fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
  2802. {
  2803. #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
  2804. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
  2805. int x = fp_count_bits (X);
  2806. #endif
  2807. if (fp_iszero(G)) {
  2808. fp_set(G, 0);
  2809. return FP_OKAY;
  2810. }
  2811. /* prevent overflows */
  2812. if (P->used > (FP_SIZE/2)) {
  2813. return FP_VAL;
  2814. }
  2815. #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
  2816. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
  2817. if(x > EPS_RSA_EXPT_XBTIS) {
  2818. return esp_mp_exptmod(G, X, x, P, Y);
  2819. }
  2820. #endif
  2821. if (X->sign == FP_NEG) {
  2822. #ifndef POSITIVE_EXP_ONLY /* reduce stack if assume no negatives */
  2823. int err;
  2824. #ifndef WOLFSSL_SMALL_STACK
  2825. fp_int tmp[2];
  2826. #else
  2827. fp_int *tmp;
  2828. #endif
  2829. #ifdef WOLFSSL_SMALL_STACK
  2830. tmp = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2831. if (tmp == NULL)
  2832. return FP_MEM;
  2833. #endif
  2834. /* yes, copy G and invmod it */
  2835. fp_init_copy(&tmp[0], G);
  2836. fp_init_copy(&tmp[1], P);
  2837. tmp[1].sign = FP_ZPOS;
  2838. err = fp_invmod(&tmp[0], &tmp[1], &tmp[0]);
  2839. if (err == FP_OKAY) {
  2840. X->sign = FP_ZPOS;
  2841. err = _fp_exptmod_nct(&tmp[0], X, P, Y);
  2842. if (X != Y) {
  2843. X->sign = FP_NEG;
  2844. }
  2845. if ((err == 0) && (P->sign == FP_NEG)) {
  2846. err = fp_add(Y, P, Y);
  2847. }
  2848. }
  2849. #ifdef WOLFSSL_SMALL_STACK
  2850. XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
  2851. #endif
  2852. return err;
  2853. #else
  2854. return FP_VAL;
  2855. #endif
  2856. }
  2857. else {
  2858. /* Positive exponent so just exptmod */
  2859. return _fp_exptmod_nct(G, X, P, Y);
  2860. }
  2861. }
  2862. /* computes a = 2**b */
  2863. void fp_2expt(fp_int *a, int b)
  2864. {
  2865. int z;
  2866. /* zero a as per default */
  2867. fp_zero (a);
  2868. if (b < 0) {
  2869. return;
  2870. }
  2871. z = b / DIGIT_BIT;
  2872. if (z >= FP_SIZE) {
  2873. return;
  2874. }
  2875. /* set the used count of where the bit will go */
  2876. a->used = z + 1;
  2877. /* put the single bit in its place */
  2878. a->dp[z] = ((fp_digit)1) << (b % DIGIT_BIT);
  2879. }
  2880. /* b = a*a */
  2881. int fp_sqr(fp_int *A, fp_int *B)
  2882. {
  2883. int err;
  2884. int y, oldused;
  2885. oldused = B->used;
  2886. y = A->used;
  2887. /* error if we're out of range */
  2888. if (y + y >= FP_SIZE) {
  2889. err = FP_VAL;
  2890. goto clean;
  2891. }
  2892. #if defined(TFM_SQR3) && FP_SIZE >= 6
  2893. if (y <= 3) {
  2894. err = fp_sqr_comba3(A,B);
  2895. goto clean;
  2896. }
  2897. #endif
  2898. #if defined(TFM_SQR4) && FP_SIZE >= 8
  2899. if (y == 4) {
  2900. err = fp_sqr_comba4(A,B);
  2901. goto clean;
  2902. }
  2903. #endif
  2904. #if defined(TFM_SQR6) && FP_SIZE >= 12
  2905. if (y <= 6) {
  2906. err = fp_sqr_comba6(A,B);
  2907. goto clean;
  2908. }
  2909. #endif
  2910. #if defined(TFM_SQR7) && FP_SIZE >= 14
  2911. if (y == 7) {
  2912. err = fp_sqr_comba7(A,B);
  2913. goto clean;
  2914. }
  2915. #endif
  2916. #if defined(TFM_SQR8) && FP_SIZE >= 16
  2917. if (y == 8) {
  2918. err = fp_sqr_comba8(A,B);
  2919. goto clean;
  2920. }
  2921. #endif
  2922. #if defined(TFM_SQR9) && FP_SIZE >= 18
  2923. if (y == 9) {
  2924. err = fp_sqr_comba9(A,B);
  2925. goto clean;
  2926. }
  2927. #endif
  2928. #if defined(TFM_SQR12) && FP_SIZE >= 24
  2929. if (y <= 12) {
  2930. err = fp_sqr_comba12(A,B);
  2931. goto clean;
  2932. }
  2933. #endif
  2934. #if defined(TFM_SQR17) && FP_SIZE >= 34
  2935. if (y <= 17) {
  2936. err = fp_sqr_comba17(A,B);
  2937. goto clean;
  2938. }
  2939. #endif
  2940. #if defined(TFM_SMALL_SET)
  2941. if (y <= 16) {
  2942. err = fp_sqr_comba_small(A,B);
  2943. goto clean;
  2944. }
  2945. #endif
  2946. #if defined(TFM_SQR20) && FP_SIZE >= 40
  2947. if (y <= 20) {
  2948. err = fp_sqr_comba20(A,B);
  2949. goto clean;
  2950. }
  2951. #endif
  2952. #if defined(TFM_SQR24) && FP_SIZE >= 48
  2953. if (y <= 24) {
  2954. err = fp_sqr_comba24(A,B);
  2955. goto clean;
  2956. }
  2957. #endif
  2958. #if defined(TFM_SQR28) && FP_SIZE >= 56
  2959. if (y <= 28) {
  2960. err = fp_sqr_comba28(A,B);
  2961. goto clean;
  2962. }
  2963. #endif
  2964. #if defined(TFM_SQR32) && FP_SIZE >= 64
  2965. if (y <= 32) {
  2966. err = fp_sqr_comba32(A,B);
  2967. goto clean;
  2968. }
  2969. #endif
  2970. #if defined(TFM_SQR48) && FP_SIZE >= 96
  2971. if (y <= 48) {
  2972. err = fp_sqr_comba48(A,B);
  2973. goto clean;
  2974. }
  2975. #endif
  2976. #if defined(TFM_SQR64) && FP_SIZE >= 128
  2977. if (y <= 64) {
  2978. err = fp_sqr_comba64(A,B);
  2979. goto clean;
  2980. }
  2981. #endif
  2982. err = fp_sqr_comba(A, B);
  2983. clean:
  2984. /* zero any excess digits on the destination that we didn't write to */
  2985. for (y = B->used; y >= 0 && y < oldused; y++) {
  2986. B->dp[y] = 0;
  2987. }
  2988. return err;
  2989. }
  2990. /* generic comba squarer */
  2991. int fp_sqr_comba(fp_int *A, fp_int *B)
  2992. {
  2993. int pa, ix, iz;
  2994. fp_digit c0, c1, c2;
  2995. #ifdef TFM_ISO
  2996. fp_word tt = 0;
  2997. #endif
  2998. fp_int *dst;
  2999. #ifndef WOLFSSL_SMALL_STACK
  3000. fp_int tmp[1];
  3001. #else
  3002. fp_int *tmp;
  3003. #endif
  3004. #ifdef WOLFSSL_SMALL_STACK
  3005. tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  3006. if (tmp == NULL)
  3007. return FP_MEM;
  3008. #endif
  3009. /* get size of output and trim */
  3010. pa = A->used + A->used;
  3011. if (pa >= FP_SIZE) {
  3012. pa = FP_SIZE-1;
  3013. }
  3014. /* number of output digits to produce */
  3015. COMBA_START;
  3016. COMBA_CLEAR;
  3017. if (A == B) {
  3018. fp_init(tmp);
  3019. dst = tmp;
  3020. } else {
  3021. fp_zero(B);
  3022. dst = B;
  3023. }
  3024. for (ix = 0; ix < pa; ix++) {
  3025. int tx, ty, iy;
  3026. fp_digit *tmpy, *tmpx;
  3027. /* get offsets into the two bignums */
  3028. ty = MIN(A->used-1, ix);
  3029. tx = ix - ty;
  3030. /* setup temp aliases */
  3031. tmpx = A->dp + tx;
  3032. tmpy = A->dp + ty;
  3033. /* this is the number of times the loop will iterate,
  3034. while (tx++ < a->used && ty-- >= 0) { ... }
  3035. */
  3036. iy = MIN(A->used-tx, ty+1);
  3037. /* now for squaring tx can never equal ty
  3038. * we halve the distance since they approach
  3039. * at a rate of 2x and we have to round because
  3040. * odd cases need to be executed
  3041. */
  3042. iy = MIN(iy, (ty-tx+1)>>1);
  3043. /* forward carries */
  3044. COMBA_FORWARD;
  3045. /* execute loop */
  3046. for (iz = 0; iz < iy; iz++) {
  3047. SQRADD2(*tmpx++, *tmpy--);
  3048. }
  3049. /* even columns have the square term in them */
  3050. if ((ix&1) == 0) {
  3051. /* TAO change COMBA_ADD back to SQRADD */
  3052. SQRADD(A->dp[ix>>1], A->dp[ix>>1]);
  3053. }
  3054. /* store it */
  3055. COMBA_STORE(dst->dp[ix]);
  3056. }
  3057. COMBA_FINI;
  3058. /* setup dest */
  3059. dst->used = pa;
  3060. fp_clamp (dst);
  3061. if (dst != B) {
  3062. fp_copy(dst, B);
  3063. }
  3064. /* Variables used but not seen by cppcheck. */
  3065. (void)c0; (void)c1; (void)c2;
  3066. #ifdef TFM_ISO
  3067. (void)tt;
  3068. #endif
  3069. #ifdef WOLFSSL_SMALL_STACK
  3070. XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
  3071. #endif
  3072. return FP_OKAY;
  3073. }
  3074. int fp_cmp(fp_int *a, fp_int *b)
  3075. {
  3076. if (a->sign == FP_NEG && b->sign == FP_ZPOS) {
  3077. return FP_LT;
  3078. } else if (a->sign == FP_ZPOS && b->sign == FP_NEG) {
  3079. return FP_GT;
  3080. } else {
  3081. /* compare digits */
  3082. if (a->sign == FP_NEG) {
  3083. /* if negative compare opposite direction */
  3084. return fp_cmp_mag(b, a);
  3085. } else {
  3086. return fp_cmp_mag(a, b);
  3087. }
  3088. }
  3089. }
  3090. /* compare against a single digit */
  3091. int fp_cmp_d(fp_int *a, fp_digit b)
  3092. {
  3093. /* special case for zero*/
  3094. if (a->used == 0 && b == 0)
  3095. return FP_EQ;
  3096. /* compare based on sign */
  3097. if ((b && a->used == 0) || a->sign == FP_NEG) {
  3098. return FP_LT;
  3099. }
  3100. /* compare based on magnitude */
  3101. if (a->used > 1) {
  3102. return FP_GT;
  3103. }
  3104. /* compare the only digit of a to b */
  3105. if (a->dp[0] > b) {
  3106. return FP_GT;
  3107. } else if (a->dp[0] < b) {
  3108. return FP_LT;
  3109. } else {
  3110. return FP_EQ;
  3111. }
  3112. }
  3113. int fp_cmp_mag(fp_int *a, fp_int *b)
  3114. {
  3115. int x;
  3116. if (a->used > b->used) {
  3117. return FP_GT;
  3118. } else if (a->used < b->used) {
  3119. return FP_LT;
  3120. } else {
  3121. for (x = a->used - 1; x >= 0; x--) {
  3122. if (a->dp[x] > b->dp[x]) {
  3123. return FP_GT;
  3124. } else if (a->dp[x] < b->dp[x]) {
  3125. return FP_LT;
  3126. }
  3127. }
  3128. }
  3129. return FP_EQ;
  3130. }
  3131. /* sets up the montgomery reduction */
  3132. int fp_montgomery_setup(fp_int *a, fp_digit *rho)
  3133. {
  3134. fp_digit x, b;
  3135. /* fast inversion mod 2**k
  3136. *
  3137. * Based on the fact that
  3138. *
  3139. * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
  3140. * => 2*X*A - X*X*A*A = 1
  3141. * => 2*(1) - (1) = 1
  3142. */
  3143. b = a->dp[0];
  3144. if ((b & 1) == 0) {
  3145. return FP_VAL;
  3146. }
  3147. x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
  3148. x *= 2 - b * x; /* here x*a==1 mod 2**8 */
  3149. x *= 2 - b * x; /* here x*a==1 mod 2**16 */
  3150. x *= 2 - b * x; /* here x*a==1 mod 2**32 */
  3151. #ifdef FP_64BIT
  3152. x *= 2 - b * x; /* here x*a==1 mod 2**64 */
  3153. #endif
  3154. /* rho = -1/m mod b */
  3155. *rho = (fp_digit) (((fp_word) 1 << ((fp_word) DIGIT_BIT)) - ((fp_word)x));
  3156. return FP_OKAY;
  3157. }
  3158. /* computes a = B**n mod b without division or multiplication useful for
  3159. * normalizing numbers in a Montgomery system.
  3160. */
  3161. int fp_montgomery_calc_normalization(fp_int *a, fp_int *b)
  3162. {
  3163. int x, bits;
  3164. /* how many bits of last digit does b use */
  3165. bits = fp_count_bits (b) % DIGIT_BIT;
  3166. if (!bits) bits = DIGIT_BIT;
  3167. /* compute A = B^(n-1) * 2^(bits-1) */
  3168. if (b->used > 1) {
  3169. fp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1);
  3170. } else {
  3171. fp_set(a, 1);
  3172. bits = 1;
  3173. }
  3174. /* now compute C = A * B mod b */
  3175. for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
  3176. int err = fp_mul_2 (a, a);
  3177. if (err != FP_OKAY) {
  3178. return err;
  3179. }
  3180. if (fp_cmp_mag (a, b) != FP_LT) {
  3181. s_fp_sub (a, b, a);
  3182. }
  3183. }
  3184. return FP_OKAY;
  3185. }
  3186. #ifdef TFM_SMALL_MONT_SET
  3187. #include "fp_mont_small.i"
  3188. #endif
  3189. #ifdef HAVE_INTEL_MULX
  3190. static WC_INLINE void innermul8_mulx(fp_digit *c_mulx, fp_digit *cy_mulx, fp_digit *tmpm, fp_digit mu)
  3191. {
  3192. fp_digit cy = *cy_mulx ;
  3193. INNERMUL8_MULX ;
  3194. *cy_mulx = cy ;
  3195. }
  3196. /* computes x/R == x (mod N) via Montgomery Reduction */
  3197. static int fp_montgomery_reduce_mulx(fp_int *a, fp_int *m, fp_digit mp, int ct)
  3198. {
  3199. #ifndef WOLFSSL_SMALL_STACK
  3200. fp_digit c[FP_SIZE+1];
  3201. #else
  3202. fp_digit *c;
  3203. #endif
  3204. fp_digit *_c, *tmpm, mu = 0;
  3205. int oldused, x, y, pa;
  3206. /* bail if too large */
  3207. if (m->used > (FP_SIZE/2)) {
  3208. (void)mu; /* shut up compiler */
  3209. return FP_VAL;
  3210. }
  3211. #ifdef TFM_SMALL_MONT_SET
  3212. if (m->used <= 16) {
  3213. return fp_montgomery_reduce_small(a, m, mp);
  3214. }
  3215. #endif
  3216. #ifdef WOLFSSL_SMALL_STACK
  3217. /* only allocate space for what's needed for window plus res */
  3218. c = (fp_digit*)XMALLOC(sizeof(fp_digit)*(FP_SIZE + 1), NULL, DYNAMIC_TYPE_BIGINT);
  3219. if (c == NULL) {
  3220. return FP_MEM;
  3221. }
  3222. #endif
  3223. /* now zero the buff */
  3224. XMEMSET(c, 0, sizeof(fp_digit)*(FP_SIZE + 1));
  3225. pa = m->used;
  3226. /* copy the input */
  3227. #ifdef TFM_TIMING_RESISTANT
  3228. if (a->used <= m->used) {
  3229. oldused = m->used;
  3230. }
  3231. else {
  3232. oldused = m->used * 2;
  3233. }
  3234. #else
  3235. oldused = a->used;
  3236. #endif
  3237. for (x = 0; x < oldused; x++) {
  3238. c[x] = a->dp[x];
  3239. }
  3240. MONT_START;
  3241. for (x = 0; x < pa; x++) {
  3242. fp_digit cy = 0;
  3243. /* get Mu for this round */
  3244. LOOP_START;
  3245. _c = c + x;
  3246. tmpm = m->dp;
  3247. y = 0;
  3248. for (; y < (pa & ~7); y += 8) {
  3249. innermul8_mulx(_c, &cy, tmpm, mu) ;
  3250. _c += 8;
  3251. tmpm += 8;
  3252. }
  3253. for (; y < pa; y++) {
  3254. INNERMUL;
  3255. ++_c;
  3256. }
  3257. LOOP_END;
  3258. while (cy) {
  3259. PROPCARRY;
  3260. ++_c;
  3261. }
  3262. }
  3263. /* now copy out */
  3264. _c = c + pa;
  3265. tmpm = a->dp;
  3266. for (x = 0; x < pa+1; x++) {
  3267. *tmpm++ = *_c++;
  3268. }
  3269. /* zero any excess digits on the destination that we didn't write to */
  3270. for (; x < oldused; x++) {
  3271. *tmpm++ = 0;
  3272. }
  3273. MONT_FINI;
  3274. a->used = pa+1;
  3275. fp_clamp(a);
  3276. #ifndef WOLFSSL_MONT_RED_CT
  3277. /* if A >= m then A = A - m */
  3278. if (fp_cmp_mag (a, m) != FP_LT) {
  3279. s_fp_sub (a, m, a);
  3280. }
  3281. (void)ct;
  3282. #else
  3283. if (ct) {
  3284. fp_submod_ct(a, m, m, a);
  3285. }
  3286. else if (fp_cmp_mag (a, m) != FP_LT) {
  3287. s_fp_sub (a, m, a);
  3288. }
  3289. #endif
  3290. #ifdef WOLFSSL_SMALL_STACK
  3291. XFREE(c, NULL, DYNAMIC_TYPE_BIGINT);
  3292. #endif
  3293. return FP_OKAY;
  3294. }
  3295. #endif
  3296. /* computes x/R == x (mod N) via Montgomery Reduction */
  3297. int fp_montgomery_reduce_ex(fp_int *a, fp_int *m, fp_digit mp, int ct)
  3298. {
  3299. #ifndef WOLFSSL_SMALL_STACK
  3300. fp_digit c[FP_SIZE+1];
  3301. #else
  3302. fp_digit *c;
  3303. #endif
  3304. fp_digit *_c, *tmpm, mu = 0;
  3305. int oldused, x, y, pa, err = 0;
  3306. IF_HAVE_INTEL_MULX(err=fp_montgomery_reduce_mulx(a, m, mp, ct), return err) ;
  3307. (void)err;
  3308. /* bail if too large */
  3309. if (m->used > (FP_SIZE/2)) {
  3310. (void)mu; /* shut up compiler */
  3311. return FP_VAL;
  3312. }
  3313. #ifdef TFM_SMALL_MONT_SET
  3314. if (m->used <= 16) {
  3315. return fp_montgomery_reduce_small(a, m, mp);
  3316. }
  3317. #endif
  3318. #ifdef WOLFSSL_SMALL_STACK
  3319. /* only allocate space for what's needed for window plus res */
  3320. c = (fp_digit*)XMALLOC(sizeof(fp_digit)*(FP_SIZE + 1), NULL, DYNAMIC_TYPE_BIGINT);
  3321. if (c == NULL) {
  3322. return FP_MEM;
  3323. }
  3324. #endif
  3325. /* now zero the buff */
  3326. XMEMSET(c, 0, sizeof(fp_digit)*(FP_SIZE + 1));
  3327. pa = m->used;
  3328. /* copy the input */
  3329. #ifdef TFM_TIMING_RESISTANT
  3330. if (a->used <= m->used) {
  3331. oldused = m->used;
  3332. }
  3333. else {
  3334. oldused = m->used * 2;
  3335. }
  3336. #else
  3337. oldused = a->used;
  3338. #endif
  3339. for (x = 0; x < oldused; x++) {
  3340. c[x] = a->dp[x];
  3341. }
  3342. MONT_START;
  3343. for (x = 0; x < pa; x++) {
  3344. fp_digit cy = 0;
  3345. /* get Mu for this round */
  3346. LOOP_START;
  3347. _c = c + x;
  3348. tmpm = m->dp;
  3349. y = 0;
  3350. #if defined(INNERMUL8)
  3351. for (; y < (pa & ~7); y += 8) {
  3352. INNERMUL8 ;
  3353. _c += 8;
  3354. tmpm += 8;
  3355. }
  3356. #endif
  3357. for (; y < pa; y++) {
  3358. INNERMUL;
  3359. ++_c;
  3360. }
  3361. LOOP_END;
  3362. while (cy) { /* //NOLINT(bugprone-infinite-loop) */ /* PROPCARRY is an asm macro */
  3363. PROPCARRY;
  3364. ++_c;
  3365. }
  3366. }
  3367. /* now copy out */
  3368. _c = c + pa;
  3369. tmpm = a->dp;
  3370. for (x = 0; x < pa+1; x++) {
  3371. *tmpm++ = *_c++;
  3372. }
  3373. /* zero any excess digits on the destination that we didn't write to */
  3374. for (; x < oldused; x++) {
  3375. *tmpm++ = 0;
  3376. }
  3377. MONT_FINI;
  3378. a->used = pa+1;
  3379. fp_clamp(a);
  3380. #ifndef WOLFSSL_MONT_RED_CT
  3381. /* if A >= m then A = A - m */
  3382. if (fp_cmp_mag (a, m) != FP_LT) {
  3383. s_fp_sub (a, m, a);
  3384. }
  3385. (void)ct;
  3386. #else
  3387. if (ct) {
  3388. fp_submod_ct(a, m, m, a);
  3389. }
  3390. else if (fp_cmp_mag (a, m) != FP_LT) {
  3391. s_fp_sub (a, m, a);
  3392. }
  3393. #endif
  3394. #ifdef WOLFSSL_SMALL_STACK
  3395. XFREE(c, NULL, DYNAMIC_TYPE_BIGINT);
  3396. #endif
  3397. return FP_OKAY;
  3398. }
  3399. int fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp)
  3400. {
  3401. return fp_montgomery_reduce_ex(a, m, mp, 1);
  3402. }
  3403. int fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c)
  3404. {
  3405. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  3406. const word32 maxC = (a->size * sizeof(fp_digit));
  3407. #else
  3408. const word32 maxC = (FP_SIZE * sizeof(fp_digit));
  3409. #endif
  3410. /* zero the int */
  3411. fp_zero (a);
  3412. if (c == 0) {
  3413. return FP_OKAY;
  3414. }
  3415. /* if input b excess max, then truncate */
  3416. if (c > 0 && (word32)c > maxC) {
  3417. int excess = (c - maxC);
  3418. c -= excess;
  3419. b += excess;
  3420. }
  3421. /* Not both endian simultaneously */
  3422. #if defined(LITTLE_ENDIAN_ORDER) && defined(BIG_ENDIAN_ORDER)
  3423. #error Both LITTLE_ENDIAN_ORDER and BIG_ENDIAN_ORDER defined.
  3424. #endif
  3425. #if (defined(LITTLE_ENDIAN_ORDER) || defined(BIG_ENDIAN_ORDER)) && \
  3426. (defined(FP_32BIT) || defined(FP_64BIT))
  3427. #ifdef FP_32BIT
  3428. /* If we know the endianness of this architecture, and we're using
  3429. 32-bit fp_digits, we can optimize this */
  3430. {
  3431. unsigned char *pd = (unsigned char *)a->dp;
  3432. a->used = (c + sizeof(fp_digit) - 1)/sizeof(fp_digit);
  3433. #ifdef BIG_ENDIAN_ORDER
  3434. {
  3435. /* Use Duff's device to unroll the loop. */
  3436. int idx = (c - 1) & ~3;
  3437. switch (c % 4) {
  3438. case 0: do { pd[idx+0] = *b++; FALL_THROUGH;
  3439. case 3: pd[idx+1] = *b++; FALL_THROUGH;
  3440. case 2: pd[idx+2] = *b++; FALL_THROUGH;
  3441. case 1: pd[idx+3] = *b++;
  3442. idx -= 4;
  3443. } while ((c -= 4) > 0);
  3444. }
  3445. }
  3446. #else
  3447. /* read the bytes in one at a time. */
  3448. for (c -= 1; c >= 0; c -= 1) {
  3449. pd[c] = *b++;
  3450. }
  3451. #endif
  3452. }
  3453. #elif defined(FP_64BIT)
  3454. /* If we know the endianness of this architecture, and we're using
  3455. 64-bit fp_digits, we can optimize this */
  3456. {
  3457. unsigned char *pd = (unsigned char *)a->dp;
  3458. a->used = (c + sizeof(fp_digit) - 1)/sizeof(fp_digit);
  3459. #ifdef BIG_ENDIAN_ORDER
  3460. {
  3461. /* Use Duff's device to unroll the loop. */
  3462. int idx = (c - 1) & ~7;
  3463. switch (c % 8) {
  3464. case 0: do { pd[idx+0] = *b++; FALL_THROUGH;
  3465. case 7: pd[idx+1] = *b++; FALL_THROUGH;
  3466. case 6: pd[idx+2] = *b++; FALL_THROUGH;
  3467. case 5: pd[idx+3] = *b++; FALL_THROUGH;
  3468. case 4: pd[idx+4] = *b++; FALL_THROUGH;
  3469. case 3: pd[idx+5] = *b++; FALL_THROUGH;
  3470. case 2: pd[idx+6] = *b++; FALL_THROUGH;
  3471. case 1: pd[idx+7] = *b++;
  3472. idx -= 8;
  3473. } while ((c -= 8) > 0);
  3474. }
  3475. }
  3476. #else
  3477. /* read the bytes in one at a time. */
  3478. for (c -= 1; c >= 0; c -= 1) {
  3479. pd[c] = *b++;
  3480. }
  3481. #endif
  3482. }
  3483. #endif
  3484. #else
  3485. /* read the bytes in one at a time - unknown number of bits in digit */
  3486. for (; c > 0; c--) {
  3487. int err = fp_mul_2d (a, 8, a);
  3488. if (err != FP_OKAY) {
  3489. return err;
  3490. }
  3491. a->dp[0] |= *b++;
  3492. if (a->used == 0) {
  3493. a->used = 1;
  3494. }
  3495. }
  3496. #endif
  3497. fp_clamp (a);
  3498. return FP_OKAY;
  3499. }
  3500. int fp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b)
  3501. {
  3502. #if DIGIT_BIT == 64 || DIGIT_BIT == 32
  3503. int i;
  3504. int j = 0;
  3505. fp_digit n;
  3506. for (i = 0; i < t->used-1; ) {
  3507. b[x++] = (unsigned char)(t->dp[i] >> j);
  3508. j += 8;
  3509. i += j == DIGIT_BIT;
  3510. j &= DIGIT_BIT - 1;
  3511. }
  3512. n = t->dp[i];
  3513. while (n != 0) {
  3514. b[x++] = (unsigned char)n;
  3515. n >>= 8;
  3516. }
  3517. return x;
  3518. #else
  3519. while (fp_iszero (t) == FP_NO) {
  3520. b[x++] = (unsigned char) (t->dp[0] & 255);
  3521. fp_div_2d (t, 8, t, NULL);
  3522. }
  3523. return x;
  3524. #endif
  3525. }
  3526. int fp_to_unsigned_bin(fp_int *a, unsigned char *b)
  3527. {
  3528. int x;
  3529. #ifndef WOLFSSL_SMALL_STACK
  3530. fp_int t[1];
  3531. #else
  3532. fp_int *t;
  3533. #endif
  3534. #ifdef WOLFSSL_SMALL_STACK
  3535. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  3536. if (t == NULL)
  3537. return FP_MEM;
  3538. #endif
  3539. fp_init_copy(t, a);
  3540. x = fp_to_unsigned_bin_at_pos(0, t, b);
  3541. mp_reverse (b, x);
  3542. #ifdef WOLFSSL_SMALL_STACK
  3543. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  3544. #endif
  3545. return FP_OKAY;
  3546. }
  3547. int fp_to_unsigned_bin_len(fp_int *a, unsigned char *b, int c)
  3548. {
  3549. #if DIGIT_BIT == 64 || DIGIT_BIT == 32 || DIGIT_BIT == 16
  3550. int i = 0;
  3551. int j = 0;
  3552. int x;
  3553. for (x=c-1; x >= 0 && i < a->used; x--) {
  3554. b[x] = (unsigned char)(a->dp[i] >> j);
  3555. j += 8;
  3556. i += j == DIGIT_BIT;
  3557. j &= DIGIT_BIT - 1;
  3558. }
  3559. for (; x >= 0; x--) {
  3560. b[x] = 0;
  3561. }
  3562. if (i < a->used - 1) {
  3563. return FP_VAL;
  3564. }
  3565. if ((i == a->used - 1) && ((a->dp[i] >> j) != 0)) {
  3566. return FP_VAL;
  3567. }
  3568. return FP_OKAY;
  3569. #else
  3570. int x;
  3571. #ifndef WOLFSSL_SMALL_STACK
  3572. fp_int t[1];
  3573. #else
  3574. fp_int *t;
  3575. #endif
  3576. #ifdef WOLFSSL_SMALL_STACK
  3577. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  3578. if (t == NULL)
  3579. return FP_MEM;
  3580. #endif
  3581. fp_init_copy(t, a);
  3582. for (x = 0; x < c; x++) {
  3583. b[x] = (unsigned char) (t->dp[0] & 255);
  3584. fp_div_2d (t, 8, t, NULL);
  3585. }
  3586. mp_reverse (b, x);
  3587. #ifdef WOLFSSL_SMALL_STACK
  3588. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  3589. #endif
  3590. if (!fp_iszero(t)) {
  3591. return FP_VAL;
  3592. }
  3593. return FP_OKAY;
  3594. #endif
  3595. }
  3596. int fp_unsigned_bin_size(const fp_int *a)
  3597. {
  3598. int size = fp_count_bits (a);
  3599. return (size / 8 + ((size & 7) != 0 ? 1 : 0));
  3600. }
  3601. void fp_set(fp_int *a, fp_digit b)
  3602. {
  3603. fp_zero(a);
  3604. a->dp[0] = b;
  3605. a->used = a->dp[0] ? 1 : 0;
  3606. }
  3607. #ifndef MP_SET_CHUNK_BITS
  3608. #define MP_SET_CHUNK_BITS 4
  3609. #endif
  3610. int fp_set_int(fp_int *a, unsigned long b)
  3611. {
  3612. /* use direct fp_set if b is less than fp_digit max
  3613. * If input max value of b down shift by 1 less than full range
  3614. * fp_digit, then condition is always true. */
  3615. #if ((ULONG_MAX >> (DIGIT_BIT-1)) > 0)
  3616. int x;
  3617. if (b < FP_DIGIT_MAX)
  3618. {
  3619. fp_set (a, (fp_digit)b);
  3620. return FP_OKAY;
  3621. }
  3622. fp_zero (a);
  3623. /* set chunk bits at a time */
  3624. for (x = 0; x < (int)(sizeof(b) * 8) / MP_SET_CHUNK_BITS; x++) {
  3625. int err = fp_mul_2d (a, MP_SET_CHUNK_BITS, a);
  3626. if (err != FP_OKAY)
  3627. return err;
  3628. /* OR in the top bits of the source */
  3629. a->dp[0] |= (b >> ((sizeof(b) * 8) - MP_SET_CHUNK_BITS)) &
  3630. ((1 << MP_SET_CHUNK_BITS) - 1);
  3631. /* shift the source up to the next chunk bits */
  3632. b <<= MP_SET_CHUNK_BITS;
  3633. /* ensure that digits are not clamped off */
  3634. a->used += 1;
  3635. }
  3636. /* clamp digits */
  3637. fp_clamp(a);
  3638. #else
  3639. fp_set (a, (fp_digit)b);
  3640. #endif
  3641. return FP_OKAY;
  3642. }
  3643. /* check if a bit is set */
  3644. int fp_is_bit_set (fp_int *a, fp_digit b)
  3645. {
  3646. fp_digit i;
  3647. if (b > FP_MAX_BITS)
  3648. return FP_VAL;
  3649. i = b/DIGIT_BIT;
  3650. if ((fp_digit)a->used < i)
  3651. return 0;
  3652. return (int)((a->dp[i] >> b%DIGIT_BIT) & (fp_digit)1);
  3653. }
  3654. /* set the b bit of a */
  3655. int fp_set_bit (fp_int * a, fp_digit b)
  3656. {
  3657. fp_digit i;
  3658. if (b > FP_MAX_BITS)
  3659. return FP_VAL;
  3660. i = b/DIGIT_BIT;
  3661. /* set the used count of where the bit will go if required */
  3662. if (a->used < (int)(i+1))
  3663. a->used = (int)(i+1);
  3664. /* put the single bit in its place */
  3665. a->dp[i] |= ((fp_digit)1) << (b % DIGIT_BIT);
  3666. return MP_OKAY;
  3667. }
  3668. int fp_count_bits (const fp_int * a)
  3669. {
  3670. int r;
  3671. fp_digit q;
  3672. /* shortcut */
  3673. if (a->used == 0) {
  3674. return 0;
  3675. }
  3676. /* get number of digits and add that */
  3677. r = (a->used - 1) * DIGIT_BIT;
  3678. /* take the last digit and count the bits in it */
  3679. q = a->dp[a->used - 1];
  3680. while (q > ((fp_digit) 0)) {
  3681. ++r;
  3682. q >>= ((fp_digit) 1);
  3683. }
  3684. return r;
  3685. }
  3686. int fp_leading_bit(fp_int *a)
  3687. {
  3688. int bit = 0;
  3689. if (a->used != 0) {
  3690. fp_digit q = a->dp[a->used - 1];
  3691. int qSz = sizeof(fp_digit);
  3692. while (qSz > 0) {
  3693. if ((unsigned char)q != 0)
  3694. bit = (q & 0x80) != 0;
  3695. q >>= 8;
  3696. qSz--;
  3697. }
  3698. }
  3699. return bit;
  3700. }
  3701. int fp_lshd(fp_int *a, int x)
  3702. {
  3703. int y;
  3704. if (a->used + x > FP_SIZE) return FP_VAL;
  3705. y = a->used + x - 1;
  3706. /* store new size */
  3707. a->used = y + 1;
  3708. /* move digits */
  3709. for (; y >= x; y--) {
  3710. a->dp[y] = a->dp[y-x];
  3711. }
  3712. /* zero lower digits */
  3713. for (; y >= 0; y--) {
  3714. a->dp[y] = 0;
  3715. }
  3716. /* clamp digits */
  3717. fp_clamp(a);
  3718. return FP_OKAY;
  3719. }
  3720. /* right shift by bit count */
  3721. void fp_rshb(fp_int *c, int x)
  3722. {
  3723. fp_digit *tmpc, mask, shift;
  3724. fp_digit r, rr;
  3725. fp_digit D = x;
  3726. /* shifting by a negative number not supported, and shifting by
  3727. * zero changes nothing.
  3728. */
  3729. if (x <= 0) return;
  3730. /* shift digits first if needed */
  3731. if (x >= DIGIT_BIT) {
  3732. fp_rshd(c, x / DIGIT_BIT);
  3733. /* recalculate number of bits to shift */
  3734. D = x % DIGIT_BIT;
  3735. /* check if any more shifting needed */
  3736. if (D == 0) return;
  3737. }
  3738. /* zero shifted is always zero */
  3739. if (fp_iszero(c)) return;
  3740. /* mask */
  3741. mask = (((fp_digit)1) << D) - 1;
  3742. /* shift for lsb */
  3743. shift = DIGIT_BIT - D;
  3744. /* alias */
  3745. tmpc = c->dp + (c->used - 1);
  3746. /* carry */
  3747. r = 0;
  3748. for (x = c->used - 1; x >= 0; x--) {
  3749. /* get the lower bits of this word in a temp */
  3750. rr = *tmpc & mask;
  3751. /* shift the current word and mix in the carry bits from previous word */
  3752. *tmpc = (*tmpc >> D) | (r << shift);
  3753. --tmpc;
  3754. /* set the carry to the carry bits of the current word found above */
  3755. r = rr;
  3756. }
  3757. /* clamp digits */
  3758. fp_clamp(c);
  3759. }
  3760. void fp_rshd(fp_int *a, int x)
  3761. {
  3762. int y;
  3763. /* too many digits just zero and return */
  3764. if (x >= a->used) {
  3765. fp_zero(a);
  3766. return;
  3767. }
  3768. /* shift */
  3769. for (y = 0; y < a->used - x; y++) {
  3770. a->dp[y] = a->dp[y+x];
  3771. }
  3772. /* zero rest */
  3773. for (; y < a->used; y++) {
  3774. a->dp[y] = 0;
  3775. }
  3776. /* decrement count */
  3777. a->used -= x;
  3778. fp_clamp(a);
  3779. }
  3780. /* c = a - b */
  3781. int fp_sub_d(fp_int *a, fp_digit b, fp_int *c)
  3782. {
  3783. #ifndef WOLFSSL_SMALL_STACK
  3784. fp_int tmp[1];
  3785. #else
  3786. fp_int *tmp;
  3787. #endif
  3788. int err = FP_OKAY;
  3789. #ifdef WOLFSSL_SMALL_STACK
  3790. tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  3791. if (tmp == NULL)
  3792. return FP_MEM;
  3793. #endif
  3794. fp_init(tmp);
  3795. fp_set(tmp, b);
  3796. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  3797. if (c->size < FP_SIZE) {
  3798. err = fp_sub(a, tmp, tmp);
  3799. fp_copy(tmp, c);
  3800. }
  3801. else
  3802. #endif
  3803. {
  3804. err = fp_sub(a, tmp, c);
  3805. }
  3806. #ifdef WOLFSSL_SMALL_STACK
  3807. XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
  3808. #endif
  3809. return err;
  3810. }
  3811. /* wolfSSL callers from normal lib */
  3812. /* init a new mp_int */
  3813. int mp_init (mp_int * a)
  3814. {
  3815. if (a)
  3816. fp_init(a);
  3817. return MP_OKAY;
  3818. }
  3819. void fp_init(fp_int *a)
  3820. {
  3821. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  3822. a->size = FP_SIZE;
  3823. #endif
  3824. #ifdef HAVE_WOLF_BIGINT
  3825. wc_bigint_init(&a->raw);
  3826. #endif
  3827. fp_zero(a);
  3828. }
  3829. void fp_zero(fp_int *a)
  3830. {
  3831. int size;
  3832. a->used = 0;
  3833. a->sign = FP_ZPOS;
  3834. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  3835. size = a->size;
  3836. #else
  3837. size = FP_SIZE;
  3838. #endif
  3839. XMEMSET(a->dp, 0, size * sizeof(fp_digit));
  3840. }
  3841. void fp_clear(fp_int *a)
  3842. {
  3843. int size;
  3844. a->used = 0;
  3845. a->sign = FP_ZPOS;
  3846. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  3847. size = a->size;
  3848. #else
  3849. size = FP_SIZE;
  3850. #endif
  3851. XMEMSET(a->dp, 0, size * sizeof(fp_digit));
  3852. fp_free(a);
  3853. }
  3854. void fp_forcezero (mp_int * a)
  3855. {
  3856. int size;
  3857. a->used = 0;
  3858. a->sign = FP_ZPOS;
  3859. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  3860. size = a->size;
  3861. #else
  3862. size = FP_SIZE;
  3863. #endif
  3864. ForceZero(a->dp, size * sizeof(fp_digit));
  3865. #ifdef HAVE_WOLF_BIGINT
  3866. wc_bigint_zero(&a->raw);
  3867. #endif
  3868. fp_free(a);
  3869. }
  3870. void mp_forcezero (mp_int * a)
  3871. {
  3872. fp_forcezero(a);
  3873. }
  3874. void fp_free(fp_int* a)
  3875. {
  3876. #ifdef HAVE_WOLF_BIGINT
  3877. wc_bigint_free(&a->raw);
  3878. #else
  3879. (void)a;
  3880. #endif
  3881. }
  3882. /* clear one (frees) */
  3883. void mp_clear (mp_int * a)
  3884. {
  3885. if (a == NULL)
  3886. return;
  3887. fp_clear(a);
  3888. }
  3889. void mp_free(mp_int* a)
  3890. {
  3891. fp_free(a);
  3892. }
  3893. /* handle up to 6 inits */
  3894. int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d,
  3895. mp_int* e, mp_int* f)
  3896. {
  3897. if (a)
  3898. fp_init(a);
  3899. if (b)
  3900. fp_init(b);
  3901. if (c)
  3902. fp_init(c);
  3903. if (d)
  3904. fp_init(d);
  3905. if (e)
  3906. fp_init(e);
  3907. if (f)
  3908. fp_init(f);
  3909. return MP_OKAY;
  3910. }
  3911. /* high level addition (handles signs) */
  3912. int mp_add (mp_int * a, mp_int * b, mp_int * c)
  3913. {
  3914. return fp_add(a, b, c);
  3915. }
  3916. /* high level subtraction (handles signs) */
  3917. int mp_sub (mp_int * a, mp_int * b, mp_int * c)
  3918. {
  3919. return fp_sub(a, b, c);
  3920. }
  3921. /* high level multiplication (handles sign) */
  3922. #if defined(FREESCALE_LTC_TFM)
  3923. int wolfcrypt_mp_mul(mp_int * a, mp_int * b, mp_int * c)
  3924. #else
  3925. int mp_mul (mp_int * a, mp_int * b, mp_int * c)
  3926. #endif
  3927. {
  3928. return fp_mul(a, b, c);
  3929. }
  3930. int mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
  3931. {
  3932. return fp_mul_d(a, b, c);
  3933. }
  3934. /* d = a * b (mod c) */
  3935. #if defined(FREESCALE_LTC_TFM)
  3936. int wolfcrypt_mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  3937. #else
  3938. int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  3939. #endif
  3940. {
  3941. #if defined(WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI) && \
  3942. !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_RSA_PRI)
  3943. int A = fp_count_bits (a);
  3944. int B = fp_count_bits (b);
  3945. if( A >= ESP_RSA_MULM_BITS && B >= ESP_RSA_MULM_BITS)
  3946. return esp_mp_mulmod(a, b, c, d);
  3947. else
  3948. #endif
  3949. return fp_mulmod(a, b, c, d);
  3950. }
  3951. /* d = a - b (mod c) */
  3952. int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
  3953. {
  3954. return fp_submod(a, b, c, d);
  3955. }
  3956. /* d = a + b (mod c) */
  3957. int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
  3958. {
  3959. return fp_addmod(a, b, c, d);
  3960. }
  3961. /* d = a - b (mod c) - constant time (a < c and b < c) */
  3962. int mp_submod_ct(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
  3963. {
  3964. return fp_submod_ct(a, b, c, d);
  3965. }
  3966. /* d = a + b (mod c) - constant time (a < c and b < c) */
  3967. int mp_addmod_ct(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
  3968. {
  3969. return fp_addmod_ct(a, b, c, d);
  3970. }
  3971. /* c = a mod b, 0 <= c < b */
  3972. #if defined(FREESCALE_LTC_TFM)
  3973. int wolfcrypt_mp_mod (mp_int * a, mp_int * b, mp_int * c)
  3974. #else
  3975. int mp_mod (mp_int * a, mp_int * b, mp_int * c)
  3976. #endif
  3977. {
  3978. return fp_mod (a, b, c);
  3979. }
  3980. /* hac 14.61, pp608 */
  3981. #if defined(FREESCALE_LTC_TFM)
  3982. int wolfcrypt_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
  3983. #else
  3984. int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
  3985. #endif
  3986. {
  3987. return fp_invmod(a, b, c);
  3988. }
  3989. /* hac 14.61, pp608 */
  3990. int mp_invmod_mont_ct (mp_int * a, mp_int * b, mp_int * c, mp_digit mp)
  3991. {
  3992. return fp_invmod_mont_ct(a, b, c, mp);
  3993. }
  3994. /* this is a shell function that calls either the normal or Montgomery
  3995. * exptmod functions. Originally the call to the montgomery code was
  3996. * embedded in the normal function but that wasted a lot of stack space
  3997. * for nothing (since 99% of the time the Montgomery code would be called)
  3998. */
  3999. #if defined(FREESCALE_LTC_TFM)
  4000. int wolfcrypt_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
  4001. #else
  4002. int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
  4003. #endif
  4004. {
  4005. return fp_exptmod(G, X, P, Y);
  4006. }
  4007. int mp_exptmod_ex (mp_int * G, mp_int * X, int digits, mp_int * P, mp_int * Y)
  4008. {
  4009. return fp_exptmod_ex(G, X, digits, P, Y);
  4010. }
  4011. #if defined(FREESCALE_LTC_TFM)
  4012. int wolfcrypt_mp_exptmod_nct (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
  4013. #else
  4014. int mp_exptmod_nct (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
  4015. #endif
  4016. {
  4017. return fp_exptmod_nct(G, X, P, Y);
  4018. }
  4019. /* compare two ints (signed)*/
  4020. int mp_cmp (mp_int * a, mp_int * b)
  4021. {
  4022. return fp_cmp(a, b);
  4023. }
  4024. /* compare a digit */
  4025. int mp_cmp_d(mp_int * a, mp_digit b)
  4026. {
  4027. return fp_cmp_d(a, b);
  4028. }
  4029. /* get the size for an unsigned equivalent */
  4030. int mp_unsigned_bin_size (const mp_int * a)
  4031. {
  4032. return fp_unsigned_bin_size(a);
  4033. }
  4034. int mp_to_unsigned_bin_at_pos(int x, fp_int *t, unsigned char *b)
  4035. {
  4036. return fp_to_unsigned_bin_at_pos(x, t, b);
  4037. }
  4038. /* store in unsigned [big endian] format */
  4039. int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
  4040. {
  4041. return fp_to_unsigned_bin(a,b);
  4042. }
  4043. int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c)
  4044. {
  4045. return fp_to_unsigned_bin_len(a, b, c);
  4046. }
  4047. /* reads a unsigned char array, assumes the msb is stored first [big endian] */
  4048. int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
  4049. {
  4050. return fp_read_unsigned_bin(a, b, c);
  4051. }
  4052. int mp_sub_d(fp_int *a, fp_digit b, fp_int *c)
  4053. {
  4054. return fp_sub_d(a, b, c);
  4055. }
  4056. int mp_mul_2d(fp_int *a, int b, fp_int *c)
  4057. {
  4058. return fp_mul_2d(a, b, c);
  4059. }
  4060. int mp_2expt(fp_int* a, int b)
  4061. {
  4062. fp_2expt(a, b);
  4063. return MP_OKAY;
  4064. }
  4065. int mp_div(fp_int * a, fp_int * b, fp_int * c, fp_int * d)
  4066. {
  4067. return fp_div(a, b, c, d);
  4068. }
  4069. int mp_div_2d(fp_int* a, int b, fp_int* c, fp_int* d)
  4070. {
  4071. fp_div_2d(a, b, c, d);
  4072. return MP_OKAY;
  4073. }
  4074. /* copy (src = a) to (dst = b) */
  4075. void fp_copy(const fp_int *a, fp_int *b)
  4076. {
  4077. /* if source and destination are different */
  4078. if (a != b) {
  4079. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  4080. /* verify a will fit in b */
  4081. if (b->size >= a->used) {
  4082. int x, oldused;
  4083. oldused = b->used;
  4084. b->used = a->used;
  4085. b->sign = a->sign;
  4086. XMEMCPY(b->dp, a->dp, a->used * sizeof(fp_digit));
  4087. /* zero any excess digits on the destination that we didn't write to */
  4088. for (x = b->used; x >= 0 && x < oldused; x++) {
  4089. b->dp[x] = 0;
  4090. }
  4091. }
  4092. else {
  4093. /* TODO: Handle error case */
  4094. }
  4095. #else
  4096. /* all dp's are same size, so do straight copy */
  4097. b->used = a->used;
  4098. b->sign = a->sign;
  4099. XMEMCPY(b->dp, a->dp, FP_SIZE * sizeof(fp_digit));
  4100. #endif
  4101. }
  4102. }
  4103. int mp_init_copy(fp_int * a, fp_int * b)
  4104. {
  4105. fp_init_copy(a, b);
  4106. return MP_OKAY;
  4107. }
  4108. /* Copy (dst = a) from (src = b) */
  4109. void fp_init_copy(fp_int *a, fp_int* b)
  4110. {
  4111. if (a != b) {
  4112. fp_init(a);
  4113. /* Note reversed parameter order! */
  4114. fp_copy(b, a); /* copy (src = b) to (dst = a) */
  4115. }
  4116. }
  4117. /* fast math wrappers */
  4118. int mp_copy(const fp_int* a, fp_int* b)
  4119. {
  4120. fp_copy(a, b);
  4121. return MP_OKAY;
  4122. }
  4123. int mp_isodd(mp_int* a)
  4124. {
  4125. return fp_isodd(a);
  4126. }
  4127. int mp_iszero(mp_int* a)
  4128. {
  4129. return fp_iszero(a);
  4130. }
  4131. int mp_count_bits (const mp_int* a)
  4132. {
  4133. return fp_count_bits(a);
  4134. }
  4135. int mp_leading_bit (mp_int* a)
  4136. {
  4137. return fp_leading_bit(a);
  4138. }
  4139. void mp_rshb (mp_int* a, int x)
  4140. {
  4141. fp_rshb(a, x);
  4142. }
  4143. void mp_rshd (mp_int* a, int x)
  4144. {
  4145. fp_rshd(a, x);
  4146. }
  4147. int mp_set_int(mp_int *a, unsigned long b)
  4148. {
  4149. return fp_set_int(a, b);
  4150. }
  4151. int mp_is_bit_set (mp_int *a, mp_digit b)
  4152. {
  4153. return fp_is_bit_set(a, b);
  4154. }
  4155. int mp_set_bit(mp_int *a, mp_digit b)
  4156. {
  4157. return fp_set_bit(a, b);
  4158. }
  4159. #if defined(WOLFSSL_KEY_GEN) || defined (HAVE_ECC) || !defined(NO_DH) || \
  4160. !defined(NO_DSA) || !defined(NO_RSA)
  4161. /* c = a * a (mod b) */
  4162. int fp_sqrmod(fp_int *a, fp_int *b, fp_int *c)
  4163. {
  4164. int err;
  4165. #ifndef WOLFSSL_SMALL_STACK
  4166. fp_int t[1];
  4167. #else
  4168. fp_int *t;
  4169. #endif
  4170. #ifdef WOLFSSL_SMALL_STACK
  4171. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  4172. if (t == NULL)
  4173. return FP_MEM;
  4174. #endif
  4175. fp_init(t);
  4176. err = fp_sqr(a, t);
  4177. if (err == FP_OKAY) {
  4178. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  4179. if (c->size < FP_SIZE) {
  4180. err = fp_mod(t, b, t);
  4181. fp_copy(t, c);
  4182. }
  4183. else
  4184. #endif
  4185. {
  4186. err = fp_mod(t, b, c);
  4187. }
  4188. }
  4189. #ifdef WOLFSSL_SMALL_STACK
  4190. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  4191. #endif
  4192. return err;
  4193. }
  4194. /* fast math conversion */
  4195. int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c)
  4196. {
  4197. return fp_sqrmod(a, b, c);
  4198. }
  4199. /* fast math conversion */
  4200. int mp_montgomery_calc_normalization(mp_int *a, mp_int *b)
  4201. {
  4202. return fp_montgomery_calc_normalization(a, b);
  4203. }
  4204. #endif /* WOLFSSL_KEY_GEN || HAVE_ECC */
  4205. static int fp_cond_swap_ct_ex(mp_int* a, mp_int* b, int c, int m, mp_int* t)
  4206. {
  4207. int i;
  4208. mp_digit mask = (mp_digit)0 - m;
  4209. t->used = (a->used ^ b->used) & mask;
  4210. for (i = 0; i < c; i++) {
  4211. t->dp[i] = (a->dp[i] ^ b->dp[i]) & mask;
  4212. }
  4213. a->used ^= t->used;
  4214. for (i = 0; i < c; i++) {
  4215. a->dp[i] ^= t->dp[i];
  4216. }
  4217. b->used ^= t->used;
  4218. for (i = 0; i < c; i++) {
  4219. b->dp[i] ^= t->dp[i];
  4220. }
  4221. return FP_OKAY;
  4222. }
  4223. static int fp_cond_swap_ct(mp_int* a, mp_int* b, int c, int m)
  4224. {
  4225. #ifndef WOLFSSL_SMALL_STACK
  4226. fp_int t[1];
  4227. #else
  4228. fp_int* t;
  4229. #endif
  4230. #ifdef WOLFSSL_SMALL_STACK
  4231. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  4232. if (t == NULL)
  4233. return FP_MEM;
  4234. #endif
  4235. fp_cond_swap_ct_ex(a, b, c, m, t);
  4236. #ifdef WOLFSSL_SMALL_STACK
  4237. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  4238. #endif
  4239. return FP_OKAY;
  4240. }
  4241. #if defined(WC_MP_TO_RADIX) || !defined(NO_DH) || !defined(NO_DSA) || \
  4242. !defined(NO_RSA)
  4243. #ifdef WOLFSSL_KEY_GEN
  4244. /* swap the elements of two integers, for cases where you can't simply swap the
  4245. * mp_int pointers around
  4246. */
  4247. static int fp_exch (fp_int * a, fp_int * b)
  4248. {
  4249. #ifndef WOLFSSL_SMALL_STACK
  4250. fp_int t[1];
  4251. #else
  4252. fp_int *t;
  4253. #endif
  4254. #ifdef WOLFSSL_SMALL_STACK
  4255. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  4256. if (t == NULL)
  4257. return FP_MEM;
  4258. #endif
  4259. *t = *a;
  4260. *a = *b;
  4261. *b = *t;
  4262. #ifdef WOLFSSL_SMALL_STACK
  4263. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  4264. #endif
  4265. return FP_OKAY;
  4266. }
  4267. #endif
  4268. static const int lnz[16] = {
  4269. 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  4270. };
  4271. /* Counts the number of lsbs which are zero before the first zero bit */
  4272. int fp_cnt_lsb(fp_int *a)
  4273. {
  4274. int x;
  4275. fp_digit q, qq;
  4276. /* easy out */
  4277. if (fp_iszero(a) == FP_YES) {
  4278. return 0;
  4279. }
  4280. /* scan lower digits until non-zero */
  4281. for (x = 0; x < a->used && a->dp[x] == 0; x++) {}
  4282. q = a->dp[x];
  4283. x *= DIGIT_BIT;
  4284. /* now scan this digit until a 1 is found */
  4285. if ((q & 1) == 0) {
  4286. do {
  4287. qq = q & 15;
  4288. x += lnz[qq];
  4289. q >>= 4;
  4290. } while (qq == 0);
  4291. }
  4292. return x;
  4293. }
  4294. static int s_is_power_of_two(fp_digit b, int *p)
  4295. {
  4296. int x;
  4297. /* fast return if no power of two */
  4298. if ((b==0) || (b & (b-1))) {
  4299. return FP_NO;
  4300. }
  4301. for (x = 0; x < DIGIT_BIT; x++) {
  4302. if (b == (((fp_digit)1)<<x)) {
  4303. *p = x;
  4304. return FP_YES;
  4305. }
  4306. }
  4307. return FP_NO;
  4308. }
  4309. /* a/b => cb + d == a */
  4310. static int fp_div_d(fp_int *a, fp_digit b, fp_int *c, fp_digit *d)
  4311. {
  4312. #ifndef WOLFSSL_SMALL_STACK
  4313. fp_int q[1];
  4314. #else
  4315. fp_int *q;
  4316. #endif
  4317. fp_word w;
  4318. fp_digit t;
  4319. int ix;
  4320. /* cannot divide by zero */
  4321. if (b == 0) {
  4322. return FP_VAL;
  4323. }
  4324. /* quick outs */
  4325. if (b == 1 || fp_iszero(a) == FP_YES) {
  4326. if (d != NULL) {
  4327. *d = 0;
  4328. }
  4329. if (c != NULL) {
  4330. fp_copy(a, c);
  4331. }
  4332. return FP_OKAY;
  4333. }
  4334. /* power of two ? */
  4335. if (s_is_power_of_two(b, &ix) == FP_YES) {
  4336. if (d != NULL) {
  4337. *d = a->dp[0] & ((((fp_digit)1)<<ix) - 1);
  4338. }
  4339. if (c != NULL) {
  4340. fp_div_2d(a, ix, c, NULL);
  4341. }
  4342. return FP_OKAY;
  4343. }
  4344. #ifdef WOLFSSL_SMALL_STACK
  4345. q = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  4346. if (q == NULL)
  4347. return FP_MEM;
  4348. #endif
  4349. fp_init(q);
  4350. if (c != NULL) {
  4351. q->used = a->used;
  4352. q->sign = a->sign;
  4353. }
  4354. w = 0;
  4355. for (ix = a->used - 1; ix >= 0; ix--) {
  4356. w = (w << ((fp_word)DIGIT_BIT)) | ((fp_word)a->dp[ix]);
  4357. if (w >= b) {
  4358. #ifdef WOLFSSL_LINUXKM
  4359. t = (fp_digit)w;
  4360. /* Linux kernel macro for in-place 64 bit integer division. */
  4361. do_div(t, b);
  4362. #else
  4363. t = (fp_digit)(w / b);
  4364. #endif
  4365. w -= ((fp_word)t) * ((fp_word)b);
  4366. } else {
  4367. t = 0;
  4368. }
  4369. if (c != NULL)
  4370. q->dp[ix] = (fp_digit)t;
  4371. }
  4372. if (d != NULL) {
  4373. *d = (fp_digit)w;
  4374. }
  4375. if (c != NULL) {
  4376. fp_clamp(q);
  4377. fp_copy(q, c);
  4378. }
  4379. #ifdef WOLFSSL_SMALL_STACK
  4380. XFREE(q, NULL, DYNAMIC_TYPE_BIGINT);
  4381. #endif
  4382. return FP_OKAY;
  4383. }
  4384. /* c = a mod b, 0 <= c < b */
  4385. static int fp_mod_d(fp_int *a, fp_digit b, fp_digit *c)
  4386. {
  4387. return fp_div_d(a, b, NULL, c);
  4388. }
  4389. int mp_mod_d(fp_int *a, fp_digit b, fp_digit *c)
  4390. {
  4391. return fp_mod_d(a, b, c);
  4392. }
  4393. #endif /* WC_MP_TO_RADIX || !NO_DH || !NO_DSA || !NO_RSA */
  4394. #if !defined(NO_DH) || !defined(NO_DSA) || !defined(NO_RSA) || \
  4395. defined(WOLFSSL_KEY_GEN)
  4396. static int fp_isprime_ex(fp_int *a, int t, int* result);
  4397. #if defined(FREESCALE_LTC_TFM)
  4398. int wolfcrypt_mp_prime_is_prime(mp_int* a, int t, int* result)
  4399. #else
  4400. int mp_prime_is_prime(mp_int* a, int t, int* result)
  4401. #endif
  4402. {
  4403. return fp_isprime_ex(a, t, result);
  4404. }
  4405. /* Miller-Rabin test of "a" to the base of "b" as described in
  4406. * HAC pp. 139 Algorithm 4.24
  4407. *
  4408. * Sets result to 0 if definitely composite or 1 if probably prime.
  4409. * Randomly the chance of error is no more than 1/4 and often
  4410. * very much lower.
  4411. */
  4412. static int fp_prime_miller_rabin_ex(fp_int * a, fp_int * b, int *result,
  4413. fp_int *n1, fp_int *y, fp_int *r)
  4414. {
  4415. int s, j;
  4416. int err;
  4417. /* default */
  4418. *result = FP_NO;
  4419. /* ensure b > 1 */
  4420. if (fp_cmp_d(b, 1) != FP_GT) {
  4421. return FP_OKAY;
  4422. }
  4423. /* get n1 = a - 1 */
  4424. fp_copy(a, n1);
  4425. err = fp_sub_d(n1, 1, n1);
  4426. if (err != FP_OKAY) {
  4427. return err;
  4428. }
  4429. /* set 2**s * r = n1 */
  4430. fp_copy(n1, r);
  4431. /* count the number of least significant bits
  4432. * which are zero
  4433. */
  4434. s = fp_cnt_lsb(r);
  4435. /* now divide n - 1 by 2**s */
  4436. fp_div_2d (r, s, r, NULL);
  4437. /* compute y = b**r mod a */
  4438. fp_zero(y);
  4439. #if (defined(WOLFSSL_HAVE_SP_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)) || \
  4440. defined(WOLFSSL_HAVE_SP_DH)
  4441. #ifndef WOLFSSL_SP_NO_2048
  4442. if (fp_count_bits(a) == 1024 && fp_isodd(a))
  4443. err = sp_ModExp_1024(b, r, a, y);
  4444. else if (fp_count_bits(a) == 2048 && fp_isodd(a))
  4445. err = sp_ModExp_2048(b, r, a, y);
  4446. else
  4447. #endif
  4448. #ifndef WOLFSSL_SP_NO_3072
  4449. if (fp_count_bits(a) == 1536 && fp_isodd(a))
  4450. err = sp_ModExp_1536(b, r, a, y);
  4451. else if (fp_count_bits(a) == 3072 && fp_isodd(a))
  4452. err = sp_ModExp_3072(b, r, a, y);
  4453. else
  4454. #endif
  4455. #ifdef WOLFSSL_SP_4096
  4456. if (fp_count_bits(a) == 4096 && fp_isodd(a))
  4457. err = sp_ModExp_4096(b, r, a, y);
  4458. else
  4459. #endif
  4460. #endif
  4461. err = fp_exptmod(b, r, a, y);
  4462. if (err != FP_OKAY) {
  4463. return err;
  4464. }
  4465. /* if y != 1 and y != n1 do */
  4466. if (fp_cmp_d (y, 1) != FP_EQ && fp_cmp (y, n1) != FP_EQ) {
  4467. j = 1;
  4468. /* while j <= s-1 and y != n1 */
  4469. while ((j <= (s - 1)) && fp_cmp (y, n1) != FP_EQ) {
  4470. err = fp_sqrmod (y, a, y);
  4471. if (err != FP_OKAY)
  4472. return err;
  4473. /* if y == 1 then composite */
  4474. if (fp_cmp_d (y, 1) == FP_EQ) {
  4475. return FP_OKAY;
  4476. }
  4477. ++j;
  4478. }
  4479. /* if y != n1 then composite */
  4480. if (fp_cmp (y, n1) != FP_EQ) {
  4481. return FP_OKAY;
  4482. }
  4483. }
  4484. /* probably prime now */
  4485. *result = FP_YES;
  4486. return FP_OKAY;
  4487. }
  4488. static int fp_prime_miller_rabin(fp_int * a, fp_int * b, int *result)
  4489. {
  4490. int err;
  4491. #ifndef WOLFSSL_SMALL_STACK
  4492. fp_int n1[1], y[1], r[1];
  4493. #else
  4494. fp_int *n1, *y, *r;
  4495. #endif
  4496. #ifdef WOLFSSL_SMALL_STACK
  4497. n1 = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_BIGINT);
  4498. if (n1 == NULL) {
  4499. return FP_MEM;
  4500. }
  4501. y = &n1[1]; r = &n1[2];
  4502. #endif
  4503. fp_init(n1);
  4504. fp_init(y);
  4505. fp_init(r);
  4506. err = fp_prime_miller_rabin_ex(a, b, result, n1, y, r);
  4507. fp_clear(n1);
  4508. fp_clear(y);
  4509. fp_clear(r);
  4510. #ifdef WOLFSSL_SMALL_STACK
  4511. XFREE(n1, NULL, DYNAMIC_TYPE_BIGINT);
  4512. #endif
  4513. return err;
  4514. }
  4515. /* a few primes */
  4516. static const fp_digit primes[FP_PRIME_SIZE] = {
  4517. 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
  4518. 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
  4519. 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
  4520. 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F, 0x0083,
  4521. 0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD,
  4522. 0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF,
  4523. 0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107,
  4524. 0x010D, 0x010F, 0x0115, 0x0119, 0x011B, 0x0125, 0x0133, 0x0137,
  4525. 0x0139, 0x013D, 0x014B, 0x0151, 0x015B, 0x015D, 0x0161, 0x0167,
  4526. 0x016F, 0x0175, 0x017B, 0x017F, 0x0185, 0x018D, 0x0191, 0x0199,
  4527. 0x01A3, 0x01A5, 0x01AF, 0x01B1, 0x01B7, 0x01BB, 0x01C1, 0x01C9,
  4528. 0x01CD, 0x01CF, 0x01D3, 0x01DF, 0x01E7, 0x01EB, 0x01F3, 0x01F7,
  4529. 0x01FD, 0x0209, 0x020B, 0x021D, 0x0223, 0x022D, 0x0233, 0x0239,
  4530. 0x023B, 0x0241, 0x024B, 0x0251, 0x0257, 0x0259, 0x025F, 0x0265,
  4531. 0x0269, 0x026B, 0x0277, 0x0281, 0x0283, 0x0287, 0x028D, 0x0293,
  4532. 0x0295, 0x02A1, 0x02A5, 0x02AB, 0x02B3, 0x02BD, 0x02C5, 0x02CF,
  4533. 0x02D7, 0x02DD, 0x02E3, 0x02E7, 0x02EF, 0x02F5, 0x02F9, 0x0301,
  4534. 0x0305, 0x0313, 0x031D, 0x0329, 0x032B, 0x0335, 0x0337, 0x033B,
  4535. 0x033D, 0x0347, 0x0355, 0x0359, 0x035B, 0x035F, 0x036D, 0x0371,
  4536. 0x0373, 0x0377, 0x038B, 0x038F, 0x0397, 0x03A1, 0x03A9, 0x03AD,
  4537. 0x03B3, 0x03B9, 0x03C7, 0x03CB, 0x03D1, 0x03D7, 0x03DF, 0x03E5,
  4538. 0x03F1, 0x03F5, 0x03FB, 0x03FD, 0x0407, 0x0409, 0x040F, 0x0419,
  4539. 0x041B, 0x0425, 0x0427, 0x042D, 0x043F, 0x0443, 0x0445, 0x0449,
  4540. 0x044F, 0x0455, 0x045D, 0x0463, 0x0469, 0x047F, 0x0481, 0x048B,
  4541. 0x0493, 0x049D, 0x04A3, 0x04A9, 0x04B1, 0x04BD, 0x04C1, 0x04C7,
  4542. 0x04CD, 0x04CF, 0x04D5, 0x04E1, 0x04EB, 0x04FD, 0x04FF, 0x0503,
  4543. 0x0509, 0x050B, 0x0511, 0x0515, 0x0517, 0x051B, 0x0527, 0x0529,
  4544. 0x052F, 0x0551, 0x0557, 0x055D, 0x0565, 0x0577, 0x0581, 0x058F,
  4545. 0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
  4546. 0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
  4547. 0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
  4548. 0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
  4549. };
  4550. int fp_isprime_ex(fp_int *a, int t, int* result)
  4551. {
  4552. #ifndef WOLFSSL_SMALL_STACK
  4553. fp_int b[1];
  4554. #else
  4555. fp_int *b;
  4556. #endif
  4557. fp_digit d;
  4558. int r, res;
  4559. int err;
  4560. if (t <= 0 || t > FP_PRIME_SIZE) {
  4561. *result = FP_NO;
  4562. return FP_VAL;
  4563. }
  4564. if (fp_isone(a)) {
  4565. *result = FP_NO;
  4566. return FP_OKAY;
  4567. }
  4568. /* check against primes table */
  4569. for (r = 0; r < FP_PRIME_SIZE; r++) {
  4570. if (fp_cmp_d(a, primes[r]) == FP_EQ) {
  4571. *result = FP_YES;
  4572. return FP_OKAY;
  4573. }
  4574. }
  4575. /* do trial division */
  4576. for (r = 0; r < FP_PRIME_SIZE; r++) {
  4577. res = fp_mod_d(a, primes[r], &d);
  4578. if (res != MP_OKAY || d == 0) {
  4579. *result = FP_NO;
  4580. return res;
  4581. }
  4582. }
  4583. #ifdef WOLFSSL_SMALL_STACK
  4584. b = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  4585. if (b == NULL)
  4586. return FP_MEM;
  4587. #endif
  4588. /* now do 't' miller rabins */
  4589. fp_init(b);
  4590. for (r = 0; r < t; r++) {
  4591. fp_set(b, primes[r]);
  4592. err = fp_prime_miller_rabin(a, b, &res);
  4593. if ((err != FP_OKAY) || (res == FP_NO)) {
  4594. *result = res;
  4595. #ifdef WOLFSSL_SMALL_STACK
  4596. XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
  4597. #endif
  4598. return err;
  4599. }
  4600. }
  4601. *result = FP_YES;
  4602. #ifdef WOLFSSL_SMALL_STACK
  4603. XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
  4604. #endif
  4605. return FP_OKAY;
  4606. }
  4607. #if defined(FREESCALE_LTC_TFM)
  4608. int wolfcrypt_mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng)
  4609. #else
  4610. int mp_prime_is_prime_ex(mp_int* a, int t, int* result, WC_RNG* rng)
  4611. #endif
  4612. {
  4613. int ret = FP_YES;
  4614. fp_digit d;
  4615. int i;
  4616. if (a == NULL || result == NULL || rng == NULL)
  4617. return FP_VAL;
  4618. if (a->sign == FP_NEG)
  4619. return FP_VAL;
  4620. if (t <= 0 || t > FP_PRIME_SIZE)
  4621. return FP_VAL;
  4622. if (fp_isone(a)) {
  4623. *result = FP_NO;
  4624. return FP_OKAY;
  4625. }
  4626. /* check against primes table */
  4627. for (i = 0; i < FP_PRIME_SIZE; i++) {
  4628. if (fp_cmp_d(a, primes[i]) == FP_EQ) {
  4629. *result = FP_YES;
  4630. return FP_OKAY;
  4631. }
  4632. }
  4633. /* do trial division */
  4634. for (i = 0; i < FP_PRIME_SIZE; i++) {
  4635. if (fp_mod_d(a, primes[i], &d) == MP_OKAY) {
  4636. if (d == 0) {
  4637. *result = FP_NO;
  4638. return FP_OKAY;
  4639. }
  4640. }
  4641. else
  4642. return FP_VAL;
  4643. }
  4644. #ifndef WC_NO_RNG
  4645. /* now do a miller rabin with up to t random numbers, this should
  4646. * give a (1/4)^t chance of a false prime. */
  4647. {
  4648. #ifndef WOLFSSL_SMALL_STACK
  4649. fp_int b[1], c[1], n1[1], y[1], r[1];
  4650. byte base[FP_MAX_PRIME_SIZE];
  4651. #else
  4652. fp_int *b, *c, *n1, *y, *r;
  4653. byte* base;
  4654. #endif
  4655. word32 baseSz;
  4656. word32 bitSz;
  4657. int err;
  4658. bitSz = fp_count_bits(a);
  4659. /* The base size is the number of bits / 8. One is added if the number
  4660. * of bits isn't an even 8. */
  4661. baseSz = (bitSz / 8) + ((bitSz % 8) ? 1 : 0);
  4662. bitSz %= 8;
  4663. #ifndef WOLFSSL_SMALL_STACK
  4664. if (baseSz > sizeof(base))
  4665. return FP_MEM;
  4666. #else
  4667. base = (byte*)XMALLOC(baseSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4668. if (base == NULL)
  4669. return FP_MEM;
  4670. b = (fp_int*)XMALLOC(sizeof(fp_int) * 5, NULL, DYNAMIC_TYPE_BIGINT);
  4671. if (b == NULL) {
  4672. XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4673. return FP_MEM;
  4674. }
  4675. c = &b[1]; n1 = &b[2]; y= &b[3]; r = &b[4];
  4676. #endif
  4677. fp_init(b);
  4678. fp_init(c);
  4679. fp_init(n1);
  4680. fp_init(y);
  4681. fp_init(r);
  4682. err = fp_sub_d(a, 2, c);
  4683. if (err != FP_OKAY) {
  4684. #ifdef WOLFSSL_SMALL_STACK
  4685. XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
  4686. XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4687. #endif
  4688. return err;
  4689. }
  4690. while (t > 0) {
  4691. if ((err = wc_RNG_GenerateBlock(rng, base, baseSz)) != 0) {
  4692. #ifdef WOLFSSL_SMALL_STACK
  4693. XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
  4694. XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4695. #endif
  4696. return err;
  4697. }
  4698. if (bitSz != 0)
  4699. base[0] &= (1 << bitSz) - 1;
  4700. err = fp_read_unsigned_bin(b, base, baseSz);
  4701. if (err != FP_OKAY) {
  4702. #ifdef WOLFSSL_SMALL_STACK
  4703. XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
  4704. XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4705. #endif
  4706. return err;
  4707. }
  4708. if (fp_cmp_d(b, 2) != FP_GT || fp_cmp(b, c) != FP_LT) {
  4709. continue;
  4710. }
  4711. err = fp_prime_miller_rabin_ex(a, b, &ret, n1, y, r);
  4712. if (err != FP_OKAY) {
  4713. #ifdef WOLFSSL_SMALL_STACK
  4714. XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
  4715. XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4716. #endif
  4717. return err;
  4718. }
  4719. if (ret == FP_NO)
  4720. break;
  4721. fp_zero(b);
  4722. t--;
  4723. }
  4724. fp_clear(n1);
  4725. fp_clear(y);
  4726. fp_clear(r);
  4727. fp_clear(b);
  4728. fp_clear(c);
  4729. #ifdef WOLFSSL_SMALL_STACK
  4730. XFREE(b, NULL, DYNAMIC_TYPE_BIGINT);
  4731. XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4732. #endif
  4733. }
  4734. #else
  4735. (void)t;
  4736. #endif /* !WC_NO_RNG */
  4737. *result = ret;
  4738. return FP_OKAY;
  4739. }
  4740. #endif /* !NO_RSA || !NO_DSA || !NO_DH || WOLFSSL_KEY_GEN */
  4741. int mp_cond_swap_ct_ex(mp_int* a, mp_int* b, int c, int m, mp_int* t)
  4742. {
  4743. return fp_cond_swap_ct_ex(a, b, c, m, t);
  4744. }
  4745. int mp_cond_swap_ct(mp_int* a, mp_int* b, int c, int m)
  4746. {
  4747. return fp_cond_swap_ct(a, b, c, m);
  4748. }
  4749. #ifdef WOLFSSL_KEY_GEN
  4750. static int fp_gcd(fp_int *a, fp_int *b, fp_int *c);
  4751. static int fp_lcm(fp_int *a, fp_int *b, fp_int *c);
  4752. static int fp_randprime(fp_int* a, int len, WC_RNG* rng, void* heap);
  4753. int mp_gcd(fp_int *a, fp_int *b, fp_int *c)
  4754. {
  4755. return fp_gcd(a, b, c);
  4756. }
  4757. int mp_lcm(fp_int *a, fp_int *b, fp_int *c)
  4758. {
  4759. return fp_lcm(a, b, c);
  4760. }
  4761. int mp_rand_prime(mp_int* a, int len, WC_RNG* rng, void* heap)
  4762. {
  4763. int err;
  4764. err = fp_randprime(a, len, rng, heap);
  4765. switch(err) {
  4766. case FP_VAL:
  4767. return MP_VAL;
  4768. case FP_MEM:
  4769. return MP_MEM;
  4770. default:
  4771. break;
  4772. }
  4773. return MP_OKAY;
  4774. }
  4775. int mp_exch (mp_int * a, mp_int * b)
  4776. {
  4777. return fp_exch(a, b);
  4778. }
  4779. int fp_randprime(fp_int* a, int len, WC_RNG* rng, void* heap)
  4780. {
  4781. static const int USE_BBS = 1;
  4782. int err, type;
  4783. int isPrime = FP_YES;
  4784. /* Assume the candidate is probably prime and then test until
  4785. * it is proven composite. */
  4786. byte* buf;
  4787. (void)heap;
  4788. /* get type */
  4789. if (len < 0) {
  4790. type = USE_BBS;
  4791. len = -len;
  4792. } else {
  4793. type = 0;
  4794. }
  4795. /* allow sizes between 2 and 512 bytes for a prime size */
  4796. if (len < 2 || len > 512) {
  4797. return FP_VAL;
  4798. }
  4799. /* allocate buffer to work with */
  4800. buf = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TMP_BUFFER);
  4801. if (buf == NULL) {
  4802. return FP_MEM;
  4803. }
  4804. XMEMSET(buf, 0, len);
  4805. do {
  4806. #ifdef SHOW_GEN
  4807. printf(".");
  4808. fflush(stdout);
  4809. #endif
  4810. /* generate value */
  4811. err = wc_RNG_GenerateBlock(rng, buf, len);
  4812. if (err != 0) {
  4813. XFREE(buf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  4814. return FP_VAL;
  4815. }
  4816. /* munge bits */
  4817. buf[0] |= 0x80 | 0x40;
  4818. buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00);
  4819. /* load value */
  4820. err = fp_read_unsigned_bin(a, buf, len);
  4821. if (err != 0) {
  4822. XFREE(buf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  4823. return err;
  4824. }
  4825. /* test */
  4826. /* Running Miller-Rabin up to 3 times gives us a 2^{-80} chance
  4827. * of a 1024-bit candidate being a false positive, when it is our
  4828. * prime candidate. (Note 4.49 of Handbook of Applied Cryptography.)
  4829. * Using 8 because we've always used 8 */
  4830. mp_prime_is_prime_ex(a, 8, &isPrime, rng);
  4831. } while (isPrime == FP_NO);
  4832. XMEMSET(buf, 0, len);
  4833. XFREE(buf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  4834. return FP_OKAY;
  4835. }
  4836. /* c = [a, b] */
  4837. int fp_lcm(fp_int *a, fp_int *b, fp_int *c)
  4838. {
  4839. int err;
  4840. #ifndef WOLFSSL_SMALL_STACK
  4841. fp_int t[2];
  4842. #else
  4843. fp_int *t;
  4844. #endif
  4845. /* LCM of 0 and any number is undefined as 0 is not in the set of values
  4846. * being used. */
  4847. if (fp_iszero(a) == FP_YES || fp_iszero(b) == FP_YES) {
  4848. return FP_VAL;
  4849. }
  4850. #ifdef WOLFSSL_SMALL_STACK
  4851. t = (fp_int*)XMALLOC(sizeof(fp_int) * 2, NULL, DYNAMIC_TYPE_BIGINT);
  4852. if (t == NULL) {
  4853. return FP_MEM;
  4854. }
  4855. #endif
  4856. fp_init(&t[0]);
  4857. fp_init(&t[1]);
  4858. err = fp_gcd(a, b, &t[0]);
  4859. if (err == FP_OKAY) {
  4860. if (fp_cmp_mag(a, b) == FP_GT) {
  4861. err = fp_div(a, &t[0], &t[1], NULL);
  4862. if (err == FP_OKAY)
  4863. err = fp_mul(b, &t[1], c);
  4864. } else {
  4865. err = fp_div(b, &t[0], &t[1], NULL);
  4866. if (err == FP_OKAY)
  4867. err = fp_mul(a, &t[1], c);
  4868. }
  4869. }
  4870. #ifdef WOLFSSL_SMALL_STACK
  4871. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  4872. #endif
  4873. return err;
  4874. }
  4875. /* c = (a, b) */
  4876. int fp_gcd(fp_int *a, fp_int *b, fp_int *c)
  4877. {
  4878. #ifndef WOLFSSL_SMALL_STACK
  4879. fp_int u[1], v[1], r[1];
  4880. #else
  4881. fp_int *u, *v, *r;
  4882. #endif
  4883. /* GCD of 0 and 0 is undefined as all integers divide 0. */
  4884. if (fp_iszero(a) == FP_YES && fp_iszero(b) == FP_YES) {
  4885. return FP_VAL;
  4886. }
  4887. /* either zero than gcd is the largest */
  4888. if (fp_iszero (a) == FP_YES && fp_iszero (b) == FP_NO) {
  4889. fp_abs (b, c);
  4890. return FP_OKAY;
  4891. }
  4892. if (fp_iszero (a) == FP_NO && fp_iszero (b) == FP_YES) {
  4893. fp_abs (a, c);
  4894. return FP_OKAY;
  4895. }
  4896. /* optimized. At this point if a == 0 then
  4897. * b must equal zero too
  4898. */
  4899. if (fp_iszero (a) == FP_YES) {
  4900. fp_zero(c);
  4901. return FP_OKAY;
  4902. }
  4903. #ifdef WOLFSSL_SMALL_STACK
  4904. u = (fp_int*)XMALLOC(sizeof(fp_int) * 3, NULL, DYNAMIC_TYPE_BIGINT);
  4905. if (u == NULL) {
  4906. return FP_MEM;
  4907. }
  4908. v = &u[1]; r = &u[2];
  4909. #endif
  4910. /* sort inputs */
  4911. if (fp_cmp_mag(a, b) != FP_LT) {
  4912. fp_init_copy(u, a);
  4913. fp_init_copy(v, b);
  4914. } else {
  4915. fp_init_copy(u, b);
  4916. fp_init_copy(v, a);
  4917. }
  4918. u->sign = FP_ZPOS;
  4919. v->sign = FP_ZPOS;
  4920. fp_init(r);
  4921. while (fp_iszero(v) == FP_NO) {
  4922. int err = fp_mod(u, v, r);
  4923. if (err != MP_OKAY) {
  4924. #ifdef WOLFSSL_SMALL_STACK
  4925. XFREE(u, NULL, DYNAMIC_TYPE_BIGINT);
  4926. #endif
  4927. return err;
  4928. }
  4929. fp_copy(v, u);
  4930. fp_copy(r, v);
  4931. }
  4932. fp_copy(u, c);
  4933. #ifdef WOLFSSL_SMALL_STACK
  4934. XFREE(u, NULL, DYNAMIC_TYPE_BIGINT);
  4935. #endif
  4936. return FP_OKAY;
  4937. }
  4938. #endif /* WOLFSSL_KEY_GEN */
  4939. #if defined(HAVE_ECC) || !defined(NO_PWDBASED) || defined(OPENSSL_EXTRA) || \
  4940. defined(WC_RSA_BLINDING) || !defined(NO_DSA) || \
  4941. (!defined(NO_RSA) && !defined(NO_RSA_BOUNDS_CHECK))
  4942. /* c = a + b */
  4943. int fp_add_d(fp_int *a, fp_digit b, fp_int *c)
  4944. {
  4945. #ifndef WOLFSSL_SMALL_STACK
  4946. fp_int tmp[1];
  4947. #else
  4948. fp_int* tmp;
  4949. #endif
  4950. int err;
  4951. #ifdef WOLFSSL_SMALL_STACK
  4952. tmp = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  4953. if (tmp == NULL)
  4954. return FP_MEM;
  4955. #endif
  4956. fp_init(tmp);
  4957. fp_set(tmp, b);
  4958. err = fp_add(a, tmp, c);
  4959. #ifdef WOLFSSL_SMALL_STACK
  4960. XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
  4961. #endif
  4962. return err;
  4963. }
  4964. /* external compatibility */
  4965. int mp_add_d(fp_int *a, fp_digit b, fp_int *c)
  4966. {
  4967. return fp_add_d(a, b, c);
  4968. }
  4969. #endif /* HAVE_ECC || !NO_PWDBASED || OPENSSL_EXTRA || WC_RSA_BLINDING ||
  4970. !NO_DSA || (!NO_RSA && !NO_RSA_BOUNDS_CHECK) */
  4971. #if !defined(NO_DSA) || defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || \
  4972. defined(HAVE_COMP_KEY) || defined(WOLFSSL_DEBUG_MATH) || \
  4973. defined(DEBUG_WOLFSSL) || defined(OPENSSL_EXTRA) || defined(WC_MP_TO_RADIX)
  4974. /* chars used in radix conversions */
  4975. static wcchar fp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  4976. "abcdefghijklmnopqrstuvwxyz+/";
  4977. #endif
  4978. #if defined(OPENSSL_EXTRA) || !defined(NO_DSA) || defined(HAVE_ECC)
  4979. #if DIGIT_BIT == 64 || DIGIT_BIT == 32
  4980. static int fp_read_radix_16(fp_int *a, const char *str)
  4981. {
  4982. int i, j, k, neg;
  4983. int ch;
  4984. /* if the leading digit is a
  4985. * minus set the sign to negative.
  4986. */
  4987. if (*str == '-') {
  4988. ++str;
  4989. neg = FP_NEG;
  4990. } else {
  4991. neg = FP_ZPOS;
  4992. }
  4993. j = 0;
  4994. k = 0;
  4995. for (i = (int)(XSTRLEN(str) - 1); i >= 0; i--) {
  4996. ch = (int)HexCharToByte(str[i]);
  4997. if (ch < 0) {
  4998. return FP_VAL;
  4999. }
  5000. k += j == DIGIT_BIT;
  5001. j &= DIGIT_BIT - 1;
  5002. if (k >= FP_SIZE)
  5003. return FP_VAL;
  5004. a->dp[k] |= ((fp_digit)ch) << j;
  5005. j += 4;
  5006. }
  5007. a->used = k + 1;
  5008. fp_clamp(a);
  5009. /* set the sign only if a != 0 */
  5010. if (fp_iszero(a) != FP_YES) {
  5011. a->sign = neg;
  5012. }
  5013. return FP_OKAY;
  5014. }
  5015. #endif
  5016. static int fp_read_radix(fp_int *a, const char *str, int radix)
  5017. {
  5018. int y, neg;
  5019. char ch;
  5020. /* set the integer to the default of zero */
  5021. fp_zero (a);
  5022. #if DIGIT_BIT == 64 || DIGIT_BIT == 32
  5023. if (radix == 16)
  5024. return fp_read_radix_16(a, str);
  5025. #endif
  5026. /* make sure the radix is ok */
  5027. if (radix < 2 || radix > 64) {
  5028. return FP_VAL;
  5029. }
  5030. /* if the leading digit is a
  5031. * minus set the sign to negative.
  5032. */
  5033. if (*str == '-') {
  5034. ++str;
  5035. neg = FP_NEG;
  5036. } else {
  5037. neg = FP_ZPOS;
  5038. }
  5039. /* process each digit of the string */
  5040. while (*str) {
  5041. /* if the radix <= 36 the conversion is case insensitive
  5042. * this allows numbers like 1AB and 1ab to represent the same value
  5043. * [e.g. in hex]
  5044. */
  5045. ch = (char)((radix <= 36) ? XTOUPPER((unsigned char)*str) : *str);
  5046. for (y = 0; y < 64; y++) {
  5047. if (ch == fp_s_rmap[y]) {
  5048. break;
  5049. }
  5050. }
  5051. if (y >= radix) {
  5052. return FP_VAL;
  5053. }
  5054. /* if the char was found in the map
  5055. * and is less than the given radix add it
  5056. * to the number, otherwise exit the loop.
  5057. */
  5058. if (y < radix) {
  5059. int ret = fp_mul_d (a, (fp_digit) radix, a);
  5060. if (ret != FP_OKAY)
  5061. return ret;
  5062. ret = fp_add_d (a, (fp_digit) y, a);
  5063. if (ret != FP_OKAY)
  5064. return ret;
  5065. } else {
  5066. break;
  5067. }
  5068. ++str;
  5069. }
  5070. /* set the sign only if a != 0 */
  5071. if (fp_iszero(a) != FP_YES) {
  5072. a->sign = neg;
  5073. }
  5074. return FP_OKAY;
  5075. }
  5076. /* fast math conversion */
  5077. int mp_read_radix(mp_int *a, const char *str, int radix)
  5078. {
  5079. return fp_read_radix(a, str, radix);
  5080. }
  5081. #endif /* !defined(NO_DSA) || defined(HAVE_ECC) */
  5082. #ifdef HAVE_ECC
  5083. /* fast math conversion */
  5084. int mp_sqr(fp_int *A, fp_int *B)
  5085. {
  5086. return fp_sqr(A, B);
  5087. }
  5088. /* fast math conversion */
  5089. int mp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp)
  5090. {
  5091. return fp_montgomery_reduce(a, m, mp);
  5092. }
  5093. int mp_montgomery_reduce_ex(fp_int *a, fp_int *m, fp_digit mp, int ct)
  5094. {
  5095. return fp_montgomery_reduce_ex(a, m, mp, ct);
  5096. }
  5097. /* fast math conversion */
  5098. int mp_montgomery_setup(fp_int *a, fp_digit *rho)
  5099. {
  5100. return fp_montgomery_setup(a, rho);
  5101. }
  5102. int mp_div_2(fp_int * a, fp_int * b)
  5103. {
  5104. fp_div_2(a, b);
  5105. return MP_OKAY;
  5106. }
  5107. /* c = a / 2 (mod b) - constant time (a < b and positive) */
  5108. int mp_div_2_mod_ct(mp_int *a, mp_int *b, mp_int *c)
  5109. {
  5110. return fp_div_2_mod_ct(a, b, c);
  5111. }
  5112. #ifdef HAVE_COMP_KEY
  5113. int mp_cnt_lsb(fp_int* a)
  5114. {
  5115. return fp_cnt_lsb(a);
  5116. }
  5117. #endif /* HAVE_COMP_KEY */
  5118. #endif /* HAVE_ECC */
  5119. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DSA) || \
  5120. defined(WOLFSSL_KEY_GEN)
  5121. /* fast math conversion */
  5122. int mp_set(fp_int *a, fp_digit b)
  5123. {
  5124. fp_set(a,b);
  5125. return MP_OKAY;
  5126. }
  5127. #endif
  5128. #ifdef WC_MP_TO_RADIX
  5129. /* returns size of ASCII representation */
  5130. int mp_radix_size (mp_int *a, int radix, int *size)
  5131. {
  5132. int res, digs;
  5133. fp_digit d;
  5134. #ifndef WOLFSSL_SMALL_STACK
  5135. fp_int t[1];
  5136. #else
  5137. fp_int *t;
  5138. #endif
  5139. *size = 0;
  5140. /* special case for binary */
  5141. if (radix == 2) {
  5142. *size = fp_count_bits(a);
  5143. if (*size == 0)
  5144. *size = 1;
  5145. *size += (a->sign == FP_NEG ? 1 : 0) + 1; /* "-" sign + null term */
  5146. return FP_OKAY;
  5147. }
  5148. /* make sure the radix is in range */
  5149. if (radix < 2 || radix > 64) {
  5150. return FP_VAL;
  5151. }
  5152. if (fp_iszero(a) == MP_YES) {
  5153. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  5154. if (radix == 16)
  5155. *size = 3;
  5156. else
  5157. #endif
  5158. *size = 2;
  5159. return FP_OKAY;
  5160. }
  5161. /* digs is the digit count */
  5162. digs = 0;
  5163. #ifdef WOLFSSL_SMALL_STACK
  5164. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  5165. if (t == NULL)
  5166. return FP_MEM;
  5167. #endif
  5168. /* Init a copy (t) of the input (a)
  5169. **
  5170. ** ALERT: Not calling fp_init_copy() as some compiler optimization settings
  5171. ** such as -O2 will complain that (t) "may be used uninitialized"
  5172. ** The fp_init() is here only to appease the compiler. */
  5173. fp_init(t);
  5174. fp_copy(a, t); /* copy (src = a) to (dst = t)*/
  5175. /* force temp to positive */
  5176. t->sign = FP_ZPOS;
  5177. /* fetch out all of the digits */
  5178. while (fp_iszero (t) == FP_NO) {
  5179. if ((res = fp_div_d (t, (mp_digit) radix, t, &d)) != FP_OKAY) {
  5180. fp_zero (t);
  5181. #ifdef WOLFSSL_SMALL_STACK
  5182. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  5183. #endif
  5184. return res;
  5185. }
  5186. ++digs;
  5187. }
  5188. fp_zero (t);
  5189. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  5190. /* For hexadecimal output, add zero padding when number of digits is odd */
  5191. if ((digs & 1) && (radix == 16)) {
  5192. ++digs;
  5193. }
  5194. #endif
  5195. /* if it's negative add one for the sign */
  5196. if (a->sign == FP_NEG) {
  5197. ++digs;
  5198. }
  5199. /* return digs + 1, the 1 is for the NULL byte that would be required. */
  5200. *size = digs + 1;
  5201. #ifdef WOLFSSL_SMALL_STACK
  5202. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  5203. #endif
  5204. return FP_OKAY;
  5205. }
  5206. /* stores a bignum as a ASCII string in a given radix (2..64) */
  5207. int mp_toradix (mp_int *a, char *str, int radix)
  5208. {
  5209. int res, digs;
  5210. fp_digit d;
  5211. char *_s = str;
  5212. #ifndef WOLFSSL_SMALL_STACK
  5213. fp_int t[1];
  5214. #else
  5215. fp_int *t;
  5216. #endif
  5217. /* check range of the radix */
  5218. if (radix < 2 || radix > 64) {
  5219. return FP_VAL;
  5220. }
  5221. /* quick out if its zero */
  5222. if (fp_iszero(a) == FP_YES) {
  5223. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  5224. if (radix == 16)
  5225. *str++ = '0';
  5226. #endif
  5227. *str++ = '0';
  5228. *str = '\0';
  5229. return FP_OKAY;
  5230. }
  5231. #ifdef WOLFSSL_SMALL_STACK
  5232. t = (fp_int*)XMALLOC(sizeof(fp_int), NULL, DYNAMIC_TYPE_BIGINT);
  5233. if (t == NULL)
  5234. return FP_MEM;
  5235. #endif
  5236. /* Init a copy (t) of the input (a)
  5237. **
  5238. ** ALERT: Not calling fp_init_copy() as some compiler optimization settings
  5239. ** such as -O2 will complain that (t) "may be used uninitialized"
  5240. ** The fp_init() is here only to appease the compiler. */
  5241. fp_init(t);
  5242. fp_copy(a, t); /* copy (src = a) to (dst = t) */
  5243. /* if it is negative output a - */
  5244. if (t->sign == FP_NEG) {
  5245. ++_s;
  5246. *str++ = '-';
  5247. t->sign = FP_ZPOS;
  5248. }
  5249. digs = 0;
  5250. while (fp_iszero (t) == FP_NO) {
  5251. if ((res = fp_div_d (t, (fp_digit) radix, t, &d)) != FP_OKAY) {
  5252. fp_zero (t);
  5253. #ifdef WOLFSSL_SMALL_STACK
  5254. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  5255. #endif
  5256. return res;
  5257. }
  5258. *str++ = fp_s_rmap[d];
  5259. ++digs;
  5260. }
  5261. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  5262. /* For hexadecimal output, add zero padding when number of digits is odd */
  5263. if ((digs & 1) && (radix == 16)) {
  5264. *str++ = fp_s_rmap[0];
  5265. ++digs;
  5266. }
  5267. #endif
  5268. /* reverse the digits of the string. In this case _s points
  5269. * to the first digit [excluding the sign] of the number]
  5270. */
  5271. mp_reverse ((unsigned char *)_s, digs);
  5272. /* append a NULL so the string is properly terminated */
  5273. *str = '\0';
  5274. fp_zero (t);
  5275. #ifdef WOLFSSL_SMALL_STACK
  5276. XFREE(t, NULL, DYNAMIC_TYPE_BIGINT);
  5277. #endif
  5278. return FP_OKAY;
  5279. }
  5280. #ifdef WOLFSSL_DEBUG_MATH
  5281. void mp_dump(const char* desc, mp_int* a, byte verbose)
  5282. {
  5283. char buffer[FP_SIZE * sizeof(fp_digit) * 2];
  5284. int size;
  5285. #if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT)
  5286. size = a->size;
  5287. #else
  5288. size = FP_SIZE;
  5289. #endif
  5290. printf("%s: ptr=%p, used=%d, sign=%d, size=%d, fpd=%d\n",
  5291. desc, a, a->used, a->sign, size, (int)sizeof(fp_digit));
  5292. mp_tohex(a, buffer);
  5293. printf(" %s\n ", buffer);
  5294. if (verbose) {
  5295. int i;
  5296. for(i=0; i<size * (int)sizeof(fp_digit); i++) {
  5297. printf("%x ", *(((byte*)a->dp) + i));
  5298. }
  5299. printf("\n");
  5300. }
  5301. }
  5302. #endif /* WOLFSSL_DEBUG_MATH */
  5303. #endif /* WC_MP_TO_RADIX */
  5304. int mp_abs(mp_int* a, mp_int* b)
  5305. {
  5306. fp_abs(a, b);
  5307. return FP_OKAY;
  5308. }
  5309. int mp_lshd (mp_int * a, int b)
  5310. {
  5311. return fp_lshd(a, b);
  5312. }
  5313. #ifdef WOLFSSL_CHECK_MEM_ZERO
  5314. void mp_memzero_add(const char* name, mp_int* a)
  5315. {
  5316. wc_MemZero_Add(name, a->dp, sizeof(a->dp));
  5317. }
  5318. void mp_memzero_check(mp_int* a)
  5319. {
  5320. wc_MemZero_Check(a->dp, sizeof(a->dp));
  5321. }
  5322. #endif /* WOLFSSL_CHECK_MEM_ZERO */
  5323. #endif /* USE_FAST_MATH */