integer.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511
  1. /* integer.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 LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca,
  23. * http://math.libtomcrypt.com
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. /* in case user set USE_FAST_MATH there */
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #ifndef NO_BIG_INT
  31. #if !defined(USE_FAST_MATH) && defined(USE_INTEGER_HEAP_MATH)
  32. #ifndef WOLFSSL_SP_MATH
  33. #ifdef NO_INLINE
  34. #include <wolfssl/wolfcrypt/misc.h>
  35. #else
  36. #define WOLFSSL_MISC_INCLUDED
  37. #include <wolfcrypt/src/misc.c>
  38. #endif
  39. #include <wolfssl/wolfcrypt/wolfmath.h>
  40. #if defined(FREESCALE_LTC_TFM)
  41. #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
  42. #endif
  43. #ifdef WOLFSSL_DEBUG_MATH
  44. #include <stdio.h>
  45. #endif
  46. #ifdef SHOW_GEN
  47. #ifndef NO_STDIO_FILESYSTEM
  48. #include <stdio.h>
  49. #endif
  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. /* reverse an array, used for radix code */
  70. static void
  71. bn_reverse (unsigned char *s, int len)
  72. {
  73. int ix, iy;
  74. unsigned char t;
  75. ix = 0;
  76. iy = len - 1;
  77. while (ix < iy) {
  78. t = s[ix];
  79. s[ix] = s[iy];
  80. s[iy] = t;
  81. ++ix;
  82. --iy;
  83. }
  84. }
  85. /* math settings check */
  86. word32 CheckRunTimeSettings(void)
  87. {
  88. return CTC_SETTINGS;
  89. }
  90. /* handle up to 6 inits */
  91. int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
  92. mp_int* f)
  93. {
  94. int res = MP_OKAY;
  95. if (a) XMEMSET(a, 0, sizeof(mp_int));
  96. if (b) XMEMSET(b, 0, sizeof(mp_int));
  97. if (c) XMEMSET(c, 0, sizeof(mp_int));
  98. if (d) XMEMSET(d, 0, sizeof(mp_int));
  99. if (e) XMEMSET(e, 0, sizeof(mp_int));
  100. if (f) XMEMSET(f, 0, sizeof(mp_int));
  101. if (a && ((res = mp_init(a)) != MP_OKAY))
  102. return res;
  103. if (b && ((res = mp_init(b)) != MP_OKAY)) {
  104. mp_clear(a);
  105. return res;
  106. }
  107. if (c && ((res = mp_init(c)) != MP_OKAY)) {
  108. mp_clear(a); mp_clear(b);
  109. return res;
  110. }
  111. if (d && ((res = mp_init(d)) != MP_OKAY)) {
  112. mp_clear(a); mp_clear(b); mp_clear(c);
  113. return res;
  114. }
  115. if (e && ((res = mp_init(e)) != MP_OKAY)) {
  116. mp_clear(a); mp_clear(b); mp_clear(c); mp_clear(d);
  117. return res;
  118. }
  119. if (f && ((res = mp_init(f)) != MP_OKAY)) {
  120. mp_clear(a); mp_clear(b); mp_clear(c); mp_clear(d); mp_clear(e);
  121. return res;
  122. }
  123. return res;
  124. }
  125. /* init a new mp_int */
  126. int mp_init (mp_int * a)
  127. {
  128. /* Safeguard against passing in a null pointer */
  129. if (a == NULL)
  130. return MP_VAL;
  131. /* defer allocation until mp_grow */
  132. a->dp = NULL;
  133. /* set the used to zero, allocated digits to the default precision
  134. * and sign to positive */
  135. a->used = 0;
  136. a->alloc = 0;
  137. a->sign = MP_ZPOS;
  138. #ifdef HAVE_WOLF_BIGINT
  139. wc_bigint_init(&a->raw);
  140. #endif
  141. return MP_OKAY;
  142. }
  143. /* clear one (frees) */
  144. void mp_clear (mp_int * a)
  145. {
  146. int i;
  147. if (a == NULL)
  148. return;
  149. /* only do anything if a hasn't been freed previously */
  150. #ifndef HAVE_WOLF_BIGINT
  151. /* When HAVE_WOLF_BIGINT then mp_free -> wc_bigint_free needs to be called
  152. * because a->raw->buf may be allocated even when a->dp == NULL. This is the
  153. * case for when a zero is loaded into the mp_int. */
  154. if (a->dp != NULL)
  155. #endif
  156. {
  157. /* first zero the digits */
  158. for (i = 0; i < a->used; i++) {
  159. a->dp[i] = 0;
  160. }
  161. /* free ram */
  162. mp_free(a);
  163. /* reset members to make debugging easier */
  164. a->alloc = a->used = 0;
  165. a->sign = MP_ZPOS;
  166. }
  167. }
  168. void mp_free (mp_int * a)
  169. {
  170. /* only do anything if a hasn't been freed previously */
  171. if (a->dp != NULL) {
  172. /* free ram */
  173. XFREE(a->dp, 0, DYNAMIC_TYPE_BIGINT);
  174. a->dp = NULL;
  175. }
  176. #ifdef HAVE_WOLF_BIGINT
  177. wc_bigint_free(&a->raw);
  178. #endif
  179. }
  180. void mp_forcezero(mp_int * a)
  181. {
  182. if (a == NULL)
  183. return;
  184. /* only do anything if a hasn't been freed previously */
  185. if (a->dp != NULL) {
  186. /* force zero the used digits */
  187. ForceZero(a->dp, a->used * sizeof(mp_digit));
  188. #ifdef HAVE_WOLF_BIGINT
  189. wc_bigint_zero(&a->raw);
  190. #endif
  191. /* free ram */
  192. mp_free(a);
  193. /* reset members to make debugging easier */
  194. a->alloc = a->used = 0;
  195. a->sign = MP_ZPOS;
  196. }
  197. a->sign = MP_ZPOS;
  198. a->used = 0;
  199. }
  200. /* get the size for an unsigned equivalent */
  201. int mp_unsigned_bin_size (const mp_int * a)
  202. {
  203. int size = mp_count_bits (a);
  204. return (size / 8 + ((size & 7) != 0 ? 1 : 0));
  205. }
  206. /* returns the number of bits in an int */
  207. int mp_count_bits (const mp_int * a)
  208. {
  209. int r;
  210. mp_digit q;
  211. /* shortcut */
  212. if (a->used == 0) {
  213. return 0;
  214. }
  215. /* get number of digits and add that */
  216. r = (a->used - 1) * DIGIT_BIT;
  217. /* take the last digit and count the bits in it */
  218. q = a->dp[a->used - 1];
  219. while (q > ((mp_digit) 0)) {
  220. ++r;
  221. q >>= ((mp_digit) 1);
  222. }
  223. return r;
  224. }
  225. int mp_leading_bit (mp_int * a)
  226. {
  227. int c = mp_count_bits(a);
  228. if (c == 0) return 0;
  229. return (c % 8) == 0;
  230. }
  231. int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b)
  232. {
  233. int res = 0;
  234. while (mp_iszero(t) == MP_NO) {
  235. #ifndef MP_8BIT
  236. b[x++] = (unsigned char) (t->dp[0] & 255);
  237. #else
  238. b[x++] = (unsigned char) (t->dp[0] | ((t->dp[1] & 0x01) << 7));
  239. #endif
  240. if ((res = mp_div_2d (t, 8, t, NULL)) != MP_OKAY) {
  241. return res;
  242. }
  243. res = x;
  244. }
  245. return res;
  246. }
  247. /* store in unsigned [big endian] format */
  248. int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
  249. {
  250. int x, res;
  251. mp_int t;
  252. if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
  253. return res;
  254. }
  255. x = mp_to_unsigned_bin_at_pos(0, &t, b);
  256. if (x < 0) {
  257. mp_clear(&t);
  258. return x;
  259. }
  260. bn_reverse (b, x);
  261. mp_clear (&t);
  262. return res;
  263. }
  264. int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c)
  265. {
  266. int i, len;
  267. len = mp_unsigned_bin_size(a);
  268. if (len > c) {
  269. return MP_VAL;
  270. }
  271. /* pad front w/ zeros to match length */
  272. for (i = 0; i < c - len; i++) {
  273. b[i] = 0x00;
  274. }
  275. return mp_to_unsigned_bin(a, b + i);
  276. }
  277. /* creates "a" then copies b into it */
  278. int mp_init_copy (mp_int * a, mp_int * b)
  279. {
  280. int res;
  281. if ((res = mp_init_size (a, b->used)) != MP_OKAY) {
  282. return res;
  283. }
  284. if((res = mp_copy (b, a)) != MP_OKAY) {
  285. mp_clear(a);
  286. }
  287. return res;
  288. }
  289. /* copy, b = a */
  290. int mp_copy (const mp_int * a, mp_int * b)
  291. {
  292. int res, n;
  293. /* Safeguard against passing in a null pointer */
  294. if (a == NULL || b == NULL)
  295. return MP_VAL;
  296. /* if dst == src do nothing */
  297. if (a == b) {
  298. return MP_OKAY;
  299. }
  300. /* grow dest */
  301. if (b->alloc < a->used || b->alloc == 0) {
  302. if ((res = mp_grow (b, a->used)) != MP_OKAY) {
  303. return res;
  304. }
  305. }
  306. /* zero b and copy the parameters over */
  307. {
  308. mp_digit *tmpa, *tmpb;
  309. /* pointer aliases */
  310. /* source */
  311. tmpa = a->dp;
  312. /* destination */
  313. tmpb = b->dp;
  314. /* copy all the digits */
  315. for (n = 0; n < a->used; n++) {
  316. *tmpb++ = *tmpa++;
  317. }
  318. /* clear high digits */
  319. for (; n < b->used && b->dp; n++) {
  320. *tmpb++ = 0;
  321. }
  322. }
  323. /* copy used count and sign */
  324. b->used = a->used;
  325. b->sign = a->sign;
  326. return MP_OKAY;
  327. }
  328. /* grow as required */
  329. int mp_grow (mp_int * a, int size)
  330. {
  331. int i;
  332. mp_digit *tmp;
  333. /* if the alloc size is smaller alloc more ram */
  334. if (a->alloc < size || size == 0) {
  335. /* ensure there are always at least MP_PREC digits extra on top */
  336. size += (MP_PREC * 2) - (size % MP_PREC);
  337. /* reallocate the array a->dp
  338. *
  339. * We store the return in a temporary variable
  340. * in case the operation failed we don't want
  341. * to overwrite the dp member of a.
  342. */
  343. tmp = (mp_digit *)XREALLOC (a->dp, sizeof (mp_digit) * size, NULL,
  344. DYNAMIC_TYPE_BIGINT);
  345. if (tmp == NULL) {
  346. /* reallocation failed but "a" is still valid [can be freed] */
  347. return MP_MEM;
  348. }
  349. /* reallocation succeeded so set a->dp */
  350. a->dp = tmp;
  351. /* zero excess digits */
  352. i = a->alloc;
  353. a->alloc = size;
  354. for (; i < a->alloc; i++) {
  355. a->dp[i] = 0;
  356. }
  357. }
  358. return MP_OKAY;
  359. }
  360. /* shift right by a certain bit count (store quotient in c, optional
  361. remainder in d) */
  362. int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
  363. {
  364. int D, res;
  365. mp_int t;
  366. /* if the shift count is <= 0 then we do no work */
  367. if (b <= 0) {
  368. res = mp_copy (a, c);
  369. if (d != NULL) {
  370. mp_zero (d);
  371. }
  372. return res;
  373. }
  374. if ((res = mp_init (&t)) != MP_OKAY) {
  375. return res;
  376. }
  377. /* get the remainder */
  378. if (d != NULL) {
  379. if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) {
  380. mp_clear (&t);
  381. return res;
  382. }
  383. }
  384. /* copy */
  385. if ((res = mp_copy (a, c)) != MP_OKAY) {
  386. mp_clear (&t);
  387. return res;
  388. }
  389. /* shift by as many digits in the bit count */
  390. if (b >= (int)DIGIT_BIT) {
  391. mp_rshd (c, b / DIGIT_BIT);
  392. }
  393. /* shift any bit count < DIGIT_BIT */
  394. D = (b % DIGIT_BIT);
  395. if (D != 0) {
  396. mp_rshb(c, D);
  397. }
  398. mp_clamp (c);
  399. if (d != NULL) {
  400. mp_exch (&t, d);
  401. }
  402. mp_clear (&t);
  403. return MP_OKAY;
  404. }
  405. /* set to zero */
  406. void mp_zero (mp_int * a)
  407. {
  408. int n;
  409. mp_digit *tmp;
  410. if (a == NULL)
  411. return;
  412. a->sign = MP_ZPOS;
  413. a->used = 0;
  414. tmp = a->dp;
  415. for (n = 0; tmp != NULL && n < a->alloc; n++) {
  416. *tmp++ = 0;
  417. }
  418. }
  419. /* trim unused digits
  420. *
  421. * This is used to ensure that leading zero digits are
  422. * trimmed and the leading "used" digit will be non-zero
  423. * Typically very fast. Also fixes the sign if there
  424. * are no more leading digits
  425. */
  426. void mp_clamp (mp_int * a)
  427. {
  428. /* decrease used while the most significant digit is
  429. * zero.
  430. */
  431. while (a->used > 0 && a->dp[a->used - 1] == 0) {
  432. --(a->used);
  433. }
  434. /* reset the sign flag if used == 0 */
  435. if (a->used == 0) {
  436. a->sign = MP_ZPOS;
  437. }
  438. }
  439. /* swap the elements of two integers, for cases where you can't simply swap the
  440. * mp_int pointers around
  441. */
  442. int mp_exch (mp_int * a, mp_int * b)
  443. {
  444. mp_int t;
  445. t = *a;
  446. *a = *b;
  447. *b = t;
  448. return MP_OKAY;
  449. }
  450. int mp_cond_swap_ct_ex (mp_int * a, mp_int * b, int c, int m, mp_int * t)
  451. {
  452. (void)c;
  453. (void)t;
  454. if (m == 1)
  455. mp_exch(a, b);
  456. return MP_OKAY;
  457. }
  458. int mp_cond_swap_ct (mp_int * a, mp_int * b, int c, int m)
  459. {
  460. (void)c;
  461. if (m == 1)
  462. mp_exch(a, b);
  463. return MP_OKAY;
  464. }
  465. /* shift right a certain number of bits */
  466. void mp_rshb (mp_int *c, int x)
  467. {
  468. mp_digit *tmpc, mask, shift;
  469. mp_digit r, rr;
  470. mp_digit D = x;
  471. /* shifting by a negative number not supported, and shifting by
  472. * zero changes nothing.
  473. */
  474. if (x <= 0) return;
  475. /* shift digits first if needed */
  476. if (x >= DIGIT_BIT) {
  477. mp_rshd(c, x / DIGIT_BIT);
  478. /* recalculate number of bits to shift */
  479. D = x % DIGIT_BIT;
  480. /* check if any more shifting needed */
  481. if (D == 0) return;
  482. }
  483. /* zero shifted is always zero */
  484. if (mp_iszero(c)) return;
  485. /* mask */
  486. mask = (((mp_digit)1) << D) - 1;
  487. /* shift for lsb */
  488. shift = DIGIT_BIT - D;
  489. /* alias */
  490. tmpc = c->dp + (c->used - 1);
  491. /* carry */
  492. r = 0;
  493. for (x = c->used - 1; x >= 0; x--) {
  494. /* get the lower bits of this word in a temp */
  495. rr = *tmpc & mask;
  496. /* shift the current word and mix in the carry bits from previous word */
  497. *tmpc = (*tmpc >> D) | (r << shift);
  498. --tmpc;
  499. /* set the carry to the carry bits of the current word found above */
  500. r = rr;
  501. }
  502. mp_clamp(c);
  503. }
  504. /* shift right a certain amount of digits */
  505. void mp_rshd (mp_int * a, int b)
  506. {
  507. int x;
  508. /* if b <= 0 then ignore it */
  509. if (b <= 0) {
  510. return;
  511. }
  512. /* if b > used then simply zero it and return */
  513. if (a->used <= b) {
  514. mp_zero (a);
  515. return;
  516. }
  517. {
  518. mp_digit *bottom, *top;
  519. /* shift the digits down */
  520. /* bottom */
  521. bottom = a->dp;
  522. /* top [offset into digits] */
  523. top = a->dp + b;
  524. /* this is implemented as a sliding window where
  525. * the window is b-digits long and digits from
  526. * the top of the window are copied to the bottom
  527. *
  528. * e.g.
  529. b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
  530. /\ | ---->
  531. \-------------------/ ---->
  532. */
  533. for (x = 0; x < (a->used - b); x++) {
  534. *bottom++ = *top++;
  535. }
  536. /* zero the top digits */
  537. for (; x < a->used; x++) {
  538. *bottom++ = 0;
  539. }
  540. }
  541. /* remove excess digits */
  542. a->used -= b;
  543. }
  544. /* calc a value mod 2**b */
  545. int mp_mod_2d (mp_int * a, int b, mp_int * c)
  546. {
  547. int x, res, bmax;
  548. /* if b is <= 0 then zero the int */
  549. if (b <= 0) {
  550. mp_zero (c);
  551. return MP_OKAY;
  552. }
  553. /* if the modulus is larger than the value than return */
  554. if (a->sign == MP_ZPOS && b >= (int) (a->used * DIGIT_BIT)) {
  555. res = mp_copy (a, c);
  556. return res;
  557. }
  558. /* copy */
  559. if ((res = mp_copy (a, c)) != MP_OKAY) {
  560. return res;
  561. }
  562. /* calculate number of digits in mod value */
  563. bmax = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1);
  564. /* zero digits above the last digit of the modulus */
  565. for (x = bmax; x < c->used; x++) {
  566. c->dp[x] = 0;
  567. }
  568. if (c->sign == MP_NEG) {
  569. mp_digit carry = 0;
  570. /* grow result to size of modulus */
  571. if ((res = mp_grow(c, bmax)) != MP_OKAY) {
  572. return res;
  573. }
  574. /* negate value */
  575. for (x = 0; x < c->used; x++) {
  576. mp_digit next = c->dp[x] > 0;
  577. c->dp[x] = ((mp_digit)0 - c->dp[x] - carry) & MP_MASK;
  578. carry |= next;
  579. }
  580. for (; x < bmax; x++) {
  581. c->dp[x] = ((mp_digit)0 - carry) & MP_MASK;
  582. }
  583. c->used = bmax;
  584. c->sign = MP_ZPOS;
  585. }
  586. /* clear the digit that is not completely outside/inside the modulus */
  587. x = DIGIT_BIT - (b % DIGIT_BIT);
  588. if (x != DIGIT_BIT) {
  589. c->dp[bmax - 1] &=
  590. ((mp_digit)~((mp_digit)0)) >> (x + ((sizeof(mp_digit)*8) - DIGIT_BIT));
  591. }
  592. mp_clamp (c);
  593. return MP_OKAY;
  594. }
  595. /* reads a unsigned char array, assumes the msb is stored first [big endian] */
  596. int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
  597. {
  598. int res;
  599. int digits_needed;
  600. while (c > 0 && b[0] == 0) {
  601. c--;
  602. b++;
  603. }
  604. digits_needed = ((c * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT;
  605. /* make sure there are enough digits available */
  606. if (a->alloc < digits_needed) {
  607. if ((res = mp_grow(a, digits_needed)) != MP_OKAY) {
  608. return res;
  609. }
  610. }
  611. /* zero the int */
  612. mp_zero (a);
  613. /* read the bytes in */
  614. while (c-- > 0) {
  615. if ((res = mp_mul_2d (a, 8, a)) != MP_OKAY) {
  616. return res;
  617. }
  618. #ifndef MP_8BIT
  619. a->dp[0] |= *b++;
  620. if (a->used == 0)
  621. a->used = 1;
  622. #else
  623. a->dp[0] = (*b & MP_MASK);
  624. a->dp[1] |= ((*b++ >> 7U) & 1);
  625. if (a->used == 0)
  626. a->used = 2;
  627. #endif
  628. }
  629. mp_clamp (a);
  630. return MP_OKAY;
  631. }
  632. /* shift left by a certain bit count */
  633. int mp_mul_2d (mp_int * a, int b, mp_int * c)
  634. {
  635. mp_digit d;
  636. int res;
  637. /* copy */
  638. if (a != c) {
  639. if ((res = mp_copy (a, c)) != MP_OKAY) {
  640. return res;
  641. }
  642. }
  643. if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) {
  644. if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) {
  645. return res;
  646. }
  647. }
  648. /* shift by as many digits in the bit count */
  649. if (b >= (int)DIGIT_BIT) {
  650. if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
  651. return res;
  652. }
  653. }
  654. /* shift any bit count < DIGIT_BIT */
  655. d = (mp_digit) (b % DIGIT_BIT);
  656. if (d != 0) {
  657. mp_digit *tmpc, shift, mask, r, rr;
  658. int x;
  659. /* bitmask for carries */
  660. mask = (((mp_digit)1) << d) - 1;
  661. /* shift for msbs */
  662. shift = DIGIT_BIT - d;
  663. /* alias */
  664. tmpc = c->dp;
  665. /* carry */
  666. r = 0;
  667. for (x = 0; x < c->used; x++) {
  668. /* get the higher bits of the current word */
  669. rr = (*tmpc >> shift) & mask;
  670. /* shift the current word and OR in the carry */
  671. *tmpc = (mp_digit)(((*tmpc << d) | r) & MP_MASK);
  672. ++tmpc;
  673. /* set the carry to the carry bits of the current word */
  674. r = rr;
  675. }
  676. /* set final carry */
  677. if (r != 0) {
  678. c->dp[(c->used)++] = r;
  679. }
  680. }
  681. mp_clamp (c);
  682. return MP_OKAY;
  683. }
  684. /* shift left a certain amount of digits */
  685. int mp_lshd (mp_int * a, int b)
  686. {
  687. int x, res;
  688. /* if its less than zero return */
  689. if (b <= 0) {
  690. return MP_OKAY;
  691. }
  692. /* grow to fit the new digits */
  693. if (a->alloc < a->used + b) {
  694. if ((res = mp_grow (a, a->used + b)) != MP_OKAY) {
  695. return res;
  696. }
  697. }
  698. {
  699. mp_digit *top, *bottom;
  700. /* increment the used by the shift amount then copy upwards */
  701. a->used += b;
  702. /* top */
  703. top = a->dp + a->used - 1;
  704. /* base */
  705. bottom = a->dp + a->used - 1 - b;
  706. /* much like mp_rshd this is implemented using a sliding window
  707. * except the window goes the other way around. Copying from
  708. * the bottom to the top. see bn_mp_rshd.c for more info.
  709. */
  710. for (x = a->used - 1; x >= b; x--) {
  711. *top-- = *bottom--;
  712. }
  713. /* zero the lower digits */
  714. top = a->dp;
  715. for (x = 0; x < b; x++) {
  716. *top++ = 0;
  717. }
  718. }
  719. return MP_OKAY;
  720. }
  721. /* this is a shell function that calls either the normal or Montgomery
  722. * exptmod functions. Originally the call to the montgomery code was
  723. * embedded in the normal function but that wasted a lot of stack space
  724. * for nothing (since 99% of the time the Montgomery code would be called)
  725. */
  726. #if defined(FREESCALE_LTC_TFM)
  727. int wolfcrypt_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
  728. #else
  729. int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) /* //NOLINT(misc-no-recursion) */
  730. #endif
  731. {
  732. int dr;
  733. /* modulus P must be positive */
  734. if (mp_iszero(P) || P->sign == MP_NEG) {
  735. return MP_VAL;
  736. }
  737. if (mp_isone(P)) {
  738. return mp_set(Y, 0);
  739. }
  740. if (mp_iszero(X)) {
  741. return mp_set(Y, 1);
  742. }
  743. if (mp_iszero(G)) {
  744. return mp_set(Y, 0);
  745. }
  746. /* if exponent X is negative we have to recurse */
  747. if (X->sign == MP_NEG) {
  748. #ifdef BN_MP_INVMOD_C
  749. mp_int tmpG, tmpX;
  750. int err;
  751. /* first compute 1/G mod P */
  752. if ((err = mp_init(&tmpG)) != MP_OKAY) {
  753. return err;
  754. }
  755. if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) {
  756. mp_clear(&tmpG);
  757. return err;
  758. }
  759. /* now get |X| */
  760. if ((err = mp_init(&tmpX)) != MP_OKAY) {
  761. mp_clear(&tmpG);
  762. return err;
  763. }
  764. if ((err = mp_abs(X, &tmpX)) != MP_OKAY) {
  765. mp_clear(&tmpG);
  766. mp_clear(&tmpX);
  767. return err;
  768. }
  769. /* and now compute (1/G)**|X| instead of G**X [X < 0] */
  770. err = mp_exptmod(&tmpG, &tmpX, P, Y);
  771. mp_clear(&tmpG);
  772. mp_clear(&tmpX);
  773. return err;
  774. #else
  775. /* no invmod */
  776. return MP_VAL;
  777. #endif
  778. }
  779. #ifdef BN_MP_EXPTMOD_BASE_2
  780. if (G->used == 1 && G->dp[0] == 2 && mp_isodd(P) == MP_YES) {
  781. return mp_exptmod_base_2(X, P, Y);
  782. }
  783. #endif
  784. /* modified diminished radix reduction */
  785. #if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && \
  786. defined(BN_S_MP_EXPTMOD_C)
  787. if (mp_reduce_is_2k_l(P) == MP_YES) {
  788. return s_mp_exptmod(G, X, P, Y, 1);
  789. }
  790. #endif
  791. #ifdef BN_MP_DR_IS_MODULUS_C
  792. /* is it a DR modulus? */
  793. dr = mp_dr_is_modulus(P);
  794. #else
  795. /* default to no */
  796. dr = 0;
  797. #endif
  798. (void)dr;
  799. #ifdef BN_MP_REDUCE_IS_2K_C
  800. /* if not, is it a unrestricted DR modulus? */
  801. if (dr == 0) {
  802. dr = mp_reduce_is_2k(P) << 1;
  803. }
  804. #endif
  805. /* if the modulus is odd use the montgomery method, or use other known */
  806. #ifdef BN_MP_EXPTMOD_FAST_C
  807. if (mp_isodd (P) == MP_YES || dr != 0) {
  808. return mp_exptmod_fast (G, X, P, Y, dr);
  809. } else {
  810. #endif
  811. #ifdef BN_S_MP_EXPTMOD_C
  812. /* otherwise use the generic Barrett reduction technique */
  813. return s_mp_exptmod (G, X, P, Y, 0);
  814. #else
  815. /* no exptmod for evens */
  816. return MP_VAL;
  817. #endif
  818. #ifdef BN_MP_EXPTMOD_FAST_C
  819. }
  820. #endif
  821. }
  822. int mp_exptmod_ex (mp_int * G, mp_int * X, int digits, mp_int * P, mp_int * Y)
  823. {
  824. (void)digits;
  825. return mp_exptmod(G, X, P, Y);
  826. }
  827. /* b = |a|
  828. *
  829. * Simple function copies the input and fixes the sign to positive
  830. */
  831. int mp_abs (mp_int * a, mp_int * b)
  832. {
  833. int res;
  834. /* copy a to b */
  835. if (a != b) {
  836. if ((res = mp_copy (a, b)) != MP_OKAY) {
  837. return res;
  838. }
  839. }
  840. /* force the sign of b to positive */
  841. b->sign = MP_ZPOS;
  842. return MP_OKAY;
  843. }
  844. /* hac 14.61, pp608 */
  845. #if defined(FREESCALE_LTC_TFM)
  846. int wolfcrypt_mp_invmod(mp_int * a, mp_int * b, mp_int * c)
  847. #else
  848. int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
  849. #endif
  850. {
  851. /* b cannot be negative or zero, and can not divide by 0 (1/a mod b) */
  852. if (b->sign == MP_NEG || mp_iszero(b) == MP_YES || mp_iszero(a) == MP_YES) {
  853. return MP_VAL;
  854. }
  855. #ifdef BN_FAST_MP_INVMOD_C
  856. /* if the modulus is odd we can use a faster routine instead */
  857. if ((mp_isodd(b) == MP_YES) && (mp_cmp_d(b, 1) != MP_EQ)) {
  858. return fast_mp_invmod (a, b, c);
  859. }
  860. #endif
  861. #ifdef BN_MP_INVMOD_SLOW_C
  862. return mp_invmod_slow(a, b, c);
  863. #else
  864. return MP_VAL;
  865. #endif
  866. }
  867. /* computes the modular inverse via binary extended euclidean algorithm,
  868. * that is c = 1/a mod b
  869. *
  870. * Based on slow invmod except this is optimized for the case where b is
  871. * odd as per HAC Note 14.64 on pp. 610
  872. */
  873. int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
  874. {
  875. mp_int x, y, u, v, B, D;
  876. int res, loop_check = 0;
  877. /* 2. [modified] b must be odd */
  878. if (mp_iseven (b) == MP_YES) {
  879. return MP_VAL;
  880. }
  881. /* init all our temps */
  882. if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D)) != MP_OKAY) {
  883. return res;
  884. }
  885. /* x == modulus, y == value to invert */
  886. if ((res = mp_copy (b, &x)) != MP_OKAY) {
  887. goto LBL_ERR;
  888. }
  889. /* we need y = |a| */
  890. if ((res = mp_mod (a, b, &y)) != MP_OKAY) {
  891. goto LBL_ERR;
  892. }
  893. if (mp_iszero (&y) == MP_YES) {
  894. /* invmod doesn't exist for this a and b */
  895. res = MP_VAL;
  896. goto LBL_ERR;
  897. }
  898. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  899. if ((res = mp_copy (&x, &u)) != MP_OKAY) {
  900. goto LBL_ERR;
  901. }
  902. if ((res = mp_copy (&y, &v)) != MP_OKAY) {
  903. goto LBL_ERR;
  904. }
  905. if ((res = mp_set (&D, 1)) != MP_OKAY) {
  906. goto LBL_ERR;
  907. }
  908. top:
  909. /* 4. while u is even do */
  910. while (mp_iseven (&u) == MP_YES) {
  911. /* 4.1 u = u/2 */
  912. if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
  913. goto LBL_ERR;
  914. }
  915. /* 4.2 if B is odd then */
  916. if (mp_isodd (&B) == MP_YES) {
  917. if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
  918. goto LBL_ERR;
  919. }
  920. }
  921. /* B = B/2 */
  922. if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
  923. goto LBL_ERR;
  924. }
  925. }
  926. /* 5. while v is even do */
  927. while (mp_iseven (&v) == MP_YES) {
  928. /* 5.1 v = v/2 */
  929. if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
  930. goto LBL_ERR;
  931. }
  932. /* 5.2 if D is odd then */
  933. if (mp_isodd (&D) == MP_YES) {
  934. /* D = (D-x)/2 */
  935. if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
  936. goto LBL_ERR;
  937. }
  938. }
  939. /* D = D/2 */
  940. if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
  941. goto LBL_ERR;
  942. }
  943. }
  944. /* 6. if u >= v then */
  945. if (mp_cmp (&u, &v) != MP_LT) {
  946. /* u = u - v, B = B - D */
  947. if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
  948. goto LBL_ERR;
  949. }
  950. if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
  951. goto LBL_ERR;
  952. }
  953. } else {
  954. /* v - v - u, D = D - B */
  955. if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
  956. goto LBL_ERR;
  957. }
  958. if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
  959. goto LBL_ERR;
  960. }
  961. }
  962. /* if not zero goto step 4 */
  963. if (mp_iszero (&u) == MP_NO) {
  964. if (++loop_check > MAX_INVMOD_SZ) {
  965. res = MP_VAL;
  966. goto LBL_ERR;
  967. }
  968. goto top;
  969. }
  970. /* now a = C, b = D, gcd == g*v */
  971. /* if v != 1 then there is no inverse */
  972. if (mp_cmp_d (&v, 1) != MP_EQ) {
  973. res = MP_VAL;
  974. goto LBL_ERR;
  975. }
  976. /* b is now the inverse */
  977. while (D.sign == MP_NEG) {
  978. if ((res = mp_add (&D, b, &D)) != MP_OKAY) {
  979. goto LBL_ERR;
  980. }
  981. }
  982. /* too big */
  983. while (mp_cmp_mag(&D, b) != MP_LT) {
  984. if ((res = mp_sub(&D, b, &D)) != MP_OKAY) {
  985. goto LBL_ERR;
  986. }
  987. }
  988. mp_exch (&D, c);
  989. res = MP_OKAY;
  990. LBL_ERR:mp_clear(&x);
  991. mp_clear(&y);
  992. mp_clear(&u);
  993. mp_clear(&v);
  994. mp_clear(&B);
  995. mp_clear(&D);
  996. return res;
  997. }
  998. /* hac 14.61, pp608 */
  999. int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
  1000. {
  1001. mp_int x, y, u, v, A, B, C, D;
  1002. int res;
  1003. /* b cannot be negative */
  1004. if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) {
  1005. return MP_VAL;
  1006. }
  1007. /* init temps */
  1008. if ((res = mp_init_multi(&x, &y, &u, &v,
  1009. &A, &B)) != MP_OKAY) {
  1010. return res;
  1011. }
  1012. /* init rest of tmps temps */
  1013. if ((res = mp_init_multi(&C, &D, 0, 0, 0, 0)) != MP_OKAY) {
  1014. mp_clear(&x);
  1015. mp_clear(&y);
  1016. mp_clear(&u);
  1017. mp_clear(&v);
  1018. mp_clear(&A);
  1019. mp_clear(&B);
  1020. return res;
  1021. }
  1022. /* x = a, y = b */
  1023. if ((res = mp_mod(a, b, &x)) != MP_OKAY) {
  1024. goto LBL_ERR;
  1025. }
  1026. if (mp_iszero (&x) == MP_YES) {
  1027. /* invmod doesn't exist for this a and b */
  1028. res = MP_VAL;
  1029. goto LBL_ERR;
  1030. }
  1031. if (mp_isone(&x)) {
  1032. res = mp_set(c, 1);
  1033. goto LBL_ERR;
  1034. }
  1035. if ((res = mp_copy (b, &y)) != MP_OKAY) {
  1036. goto LBL_ERR;
  1037. }
  1038. /* 2. [modified] if x,y are both even then return an error! */
  1039. if (mp_iseven (&x) == MP_YES && mp_iseven (&y) == MP_YES) {
  1040. res = MP_VAL;
  1041. goto LBL_ERR;
  1042. }
  1043. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  1044. if ((res = mp_copy (&x, &u)) != MP_OKAY) {
  1045. goto LBL_ERR;
  1046. }
  1047. if ((res = mp_copy (&y, &v)) != MP_OKAY) {
  1048. goto LBL_ERR;
  1049. }
  1050. if ((res = mp_set (&A, 1)) != MP_OKAY) {
  1051. goto LBL_ERR;
  1052. }
  1053. if ((res = mp_set (&D, 1)) != MP_OKAY) {
  1054. goto LBL_ERR;
  1055. }
  1056. top:
  1057. /* 4. while u is even do */
  1058. while (mp_iseven (&u) == MP_YES) {
  1059. /* 4.1 u = u/2 */
  1060. if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
  1061. goto LBL_ERR;
  1062. }
  1063. /* 4.2 if A or B is odd then */
  1064. if (mp_isodd (&A) == MP_YES || mp_isodd (&B) == MP_YES) {
  1065. /* A = (A+y)/2, B = (B-x)/2 */
  1066. if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
  1067. goto LBL_ERR;
  1068. }
  1069. if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
  1070. goto LBL_ERR;
  1071. }
  1072. }
  1073. /* A = A/2, B = B/2 */
  1074. if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
  1075. goto LBL_ERR;
  1076. }
  1077. if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
  1078. goto LBL_ERR;
  1079. }
  1080. }
  1081. /* 5. while v is even do */
  1082. while (mp_iseven (&v) == MP_YES) {
  1083. /* 5.1 v = v/2 */
  1084. if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
  1085. goto LBL_ERR;
  1086. }
  1087. /* 5.2 if C or D is odd then */
  1088. if (mp_isodd (&C) == MP_YES || mp_isodd (&D) == MP_YES) {
  1089. /* C = (C+y)/2, D = (D-x)/2 */
  1090. if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
  1091. goto LBL_ERR;
  1092. }
  1093. if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
  1094. goto LBL_ERR;
  1095. }
  1096. }
  1097. /* C = C/2, D = D/2 */
  1098. if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
  1099. goto LBL_ERR;
  1100. }
  1101. if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
  1102. goto LBL_ERR;
  1103. }
  1104. }
  1105. /* 6. if u >= v then */
  1106. if (mp_cmp (&u, &v) != MP_LT) {
  1107. /* u = u - v, A = A - C, B = B - D */
  1108. if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
  1109. goto LBL_ERR;
  1110. }
  1111. if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
  1112. goto LBL_ERR;
  1113. }
  1114. if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
  1115. goto LBL_ERR;
  1116. }
  1117. } else {
  1118. /* v - v - u, C = C - A, D = D - B */
  1119. if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
  1120. goto LBL_ERR;
  1121. }
  1122. if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
  1123. goto LBL_ERR;
  1124. }
  1125. if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
  1126. goto LBL_ERR;
  1127. }
  1128. }
  1129. /* if not zero goto step 4 */
  1130. if (mp_iszero (&u) == MP_NO)
  1131. goto top;
  1132. /* now a = C, b = D, gcd == g*v */
  1133. /* if v != 1 then there is no inverse */
  1134. if (mp_cmp_d (&v, 1) != MP_EQ) {
  1135. res = MP_VAL;
  1136. goto LBL_ERR;
  1137. }
  1138. /* if its too low */
  1139. while (mp_cmp_d(&C, 0) == MP_LT) {
  1140. if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
  1141. goto LBL_ERR;
  1142. }
  1143. }
  1144. /* too big */
  1145. while (mp_cmp_mag(&C, b) != MP_LT) {
  1146. if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
  1147. goto LBL_ERR;
  1148. }
  1149. }
  1150. /* C is now the inverse */
  1151. mp_exch (&C, c);
  1152. res = MP_OKAY;
  1153. LBL_ERR:mp_clear(&x);
  1154. mp_clear(&y);
  1155. mp_clear(&u);
  1156. mp_clear(&v);
  1157. mp_clear(&A);
  1158. mp_clear(&B);
  1159. mp_clear(&C);
  1160. mp_clear(&D);
  1161. return res;
  1162. }
  1163. /* compare magnitude of two ints (unsigned) */
  1164. int mp_cmp_mag (mp_int * a, mp_int * b)
  1165. {
  1166. int n;
  1167. mp_digit *tmpa, *tmpb;
  1168. /* compare based on # of non-zero digits */
  1169. if (a->used > b->used) {
  1170. return MP_GT;
  1171. }
  1172. if (a->used < b->used) {
  1173. return MP_LT;
  1174. }
  1175. if (a->used == 0)
  1176. return MP_EQ;
  1177. /* alias for a */
  1178. tmpa = a->dp + (a->used - 1);
  1179. /* alias for b */
  1180. tmpb = b->dp + (a->used - 1);
  1181. /* compare based on digits */
  1182. for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
  1183. if (*tmpa > *tmpb) {
  1184. return MP_GT;
  1185. }
  1186. if (*tmpa < *tmpb) {
  1187. return MP_LT;
  1188. }
  1189. }
  1190. return MP_EQ;
  1191. }
  1192. /* compare two ints (signed)*/
  1193. int mp_cmp (mp_int * a, mp_int * b)
  1194. {
  1195. /* compare based on sign */
  1196. if (a->sign != b->sign) {
  1197. if (a->sign == MP_NEG) {
  1198. return MP_LT;
  1199. } else {
  1200. return MP_GT;
  1201. }
  1202. }
  1203. /* compare digits */
  1204. if (a->sign == MP_NEG) {
  1205. /* if negative compare opposite direction */
  1206. return mp_cmp_mag(b, a);
  1207. } else {
  1208. return mp_cmp_mag(a, b);
  1209. }
  1210. }
  1211. /* compare a digit */
  1212. int mp_cmp_d(mp_int * a, mp_digit b)
  1213. {
  1214. /* special case for zero*/
  1215. if (a->used == 0 && b == 0)
  1216. return MP_EQ;
  1217. /* compare based on sign */
  1218. if ((b && a->used == 0) || a->sign == MP_NEG) {
  1219. return MP_LT;
  1220. }
  1221. /* compare based on magnitude */
  1222. if (a->used > 1) {
  1223. return MP_GT;
  1224. }
  1225. /* compare the only digit of a to b */
  1226. if (a->dp[0] > b) {
  1227. return MP_GT;
  1228. } else if (a->dp[0] < b) {
  1229. return MP_LT;
  1230. } else {
  1231. return MP_EQ;
  1232. }
  1233. }
  1234. /* set to a digit */
  1235. int mp_set (mp_int * a, mp_digit b)
  1236. {
  1237. int res;
  1238. mp_zero (a);
  1239. res = mp_grow (a, 1);
  1240. if (res == MP_OKAY) {
  1241. a->dp[0] = (mp_digit)(b & MP_MASK);
  1242. a->used = (a->dp[0] != 0) ? 1 : 0;
  1243. }
  1244. return res;
  1245. }
  1246. /* check if a bit is set */
  1247. int mp_is_bit_set (mp_int *a, mp_digit b)
  1248. {
  1249. mp_digit i = b / DIGIT_BIT; /* word index */
  1250. mp_digit s = b % DIGIT_BIT; /* bit index */
  1251. if ((mp_digit)a->used <= i) {
  1252. /* no words available at that bit count */
  1253. return 0;
  1254. }
  1255. /* get word and shift bit to check down to index 0 */
  1256. return (int)((a->dp[i] >> s) & (mp_digit)1);
  1257. }
  1258. /* c = a mod b, 0 <= c < b */
  1259. #if defined(FREESCALE_LTC_TFM)
  1260. int wolfcrypt_mp_mod(mp_int * a, mp_int * b, mp_int * c)
  1261. #else
  1262. int mp_mod (mp_int * a, mp_int * b, mp_int * c)
  1263. #endif
  1264. {
  1265. mp_int t;
  1266. int res;
  1267. if ((res = mp_init_size (&t, b->used)) != MP_OKAY) {
  1268. return res;
  1269. }
  1270. if ((res = mp_div (a, b, NULL, &t)) != MP_OKAY) {
  1271. mp_clear (&t);
  1272. return res;
  1273. }
  1274. if ((mp_iszero(&t) != MP_NO) || (t.sign == b->sign)) {
  1275. res = MP_OKAY;
  1276. mp_exch (&t, c);
  1277. } else {
  1278. res = mp_add (b, &t, c);
  1279. }
  1280. mp_clear (&t);
  1281. return res;
  1282. }
  1283. /* slower bit-bang division... also smaller */
  1284. int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  1285. {
  1286. mp_int ta, tb, tq, q;
  1287. int res, n, n2;
  1288. /* is divisor zero ? */
  1289. if (mp_iszero (b) == MP_YES) {
  1290. return MP_VAL;
  1291. }
  1292. /* if a < b then q=0, r = a */
  1293. if (mp_cmp_mag (a, b) == MP_LT) {
  1294. if (d != NULL) {
  1295. res = mp_copy (a, d);
  1296. } else {
  1297. res = MP_OKAY;
  1298. }
  1299. if (c != NULL) {
  1300. mp_zero (c);
  1301. }
  1302. return res;
  1303. }
  1304. /* init our temps */
  1305. if ((res = mp_init_multi(&ta, &tb, &tq, &q, 0, 0)) != MP_OKAY) {
  1306. return res;
  1307. }
  1308. if ((res = mp_set(&tq, 1)) != MP_OKAY) {
  1309. return res;
  1310. }
  1311. n = mp_count_bits(a) - mp_count_bits(b);
  1312. if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
  1313. ((res = mp_abs(b, &tb)) != MP_OKAY) ||
  1314. ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
  1315. ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
  1316. goto LBL_ERR;
  1317. }
  1318. while (n-- >= 0) {
  1319. if (mp_cmp(&tb, &ta) != MP_GT) {
  1320. if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
  1321. ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) {
  1322. goto LBL_ERR;
  1323. }
  1324. }
  1325. if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
  1326. ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) {
  1327. goto LBL_ERR;
  1328. }
  1329. }
  1330. /* now q == quotient and ta == remainder */
  1331. n = a->sign;
  1332. n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
  1333. if (c != NULL) {
  1334. mp_exch(c, &q);
  1335. c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
  1336. }
  1337. if (d != NULL) {
  1338. mp_exch(d, &ta);
  1339. d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n;
  1340. }
  1341. LBL_ERR:
  1342. mp_clear(&ta);
  1343. mp_clear(&tb);
  1344. mp_clear(&tq);
  1345. mp_clear(&q);
  1346. return res;
  1347. }
  1348. /* b = a/2 */
  1349. int mp_div_2(mp_int * a, mp_int * b)
  1350. {
  1351. int x, res, oldused;
  1352. /* copy */
  1353. if (b->alloc < a->used) {
  1354. if ((res = mp_grow (b, a->used)) != MP_OKAY) {
  1355. return res;
  1356. }
  1357. }
  1358. oldused = b->used;
  1359. b->used = a->used;
  1360. {
  1361. mp_digit r, rr, *tmpa, *tmpb;
  1362. /* source alias */
  1363. tmpa = a->dp + b->used - 1;
  1364. /* dest alias */
  1365. tmpb = b->dp + b->used - 1;
  1366. /* carry */
  1367. r = 0;
  1368. for (x = b->used - 1; x >= 0; x--) {
  1369. /* get the carry for the next iteration */
  1370. rr = *tmpa & 1;
  1371. /* shift the current digit, add in carry and store */
  1372. *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
  1373. /* forward carry to next iteration */
  1374. r = rr;
  1375. }
  1376. /* zero excess digits */
  1377. tmpb = b->dp + b->used;
  1378. for (x = b->used; x < oldused; x++) {
  1379. *tmpb++ = 0;
  1380. }
  1381. }
  1382. b->sign = a->sign;
  1383. mp_clamp (b);
  1384. return MP_OKAY;
  1385. }
  1386. /* c = a / 2 (mod b) - constant time (a < b and positive) */
  1387. int mp_div_2_mod_ct(mp_int *a, mp_int *b, mp_int *c)
  1388. {
  1389. int res;
  1390. if (mp_isodd(a)) {
  1391. res = mp_add(a, b, c);
  1392. if (res == MP_OKAY) {
  1393. res = mp_div_2(c, c);
  1394. }
  1395. }
  1396. else {
  1397. res = mp_div_2(a, c);
  1398. }
  1399. return res;
  1400. }
  1401. /* high level addition (handles signs) */
  1402. int mp_add (mp_int * a, mp_int * b, mp_int * c)
  1403. {
  1404. int sa, sb, res;
  1405. /* get sign of both inputs */
  1406. sa = a->sign;
  1407. sb = b->sign;
  1408. /* handle two cases, not four */
  1409. if (sa == sb) {
  1410. /* both positive or both negative */
  1411. /* add their magnitudes, copy the sign */
  1412. c->sign = sa;
  1413. res = s_mp_add (a, b, c);
  1414. } else {
  1415. /* one positive, the other negative */
  1416. /* subtract the one with the greater magnitude from */
  1417. /* the one of the lesser magnitude. The result gets */
  1418. /* the sign of the one with the greater magnitude. */
  1419. if (mp_cmp_mag (a, b) == MP_LT) {
  1420. c->sign = sb;
  1421. res = s_mp_sub (b, a, c);
  1422. } else {
  1423. c->sign = sa;
  1424. res = s_mp_sub (a, b, c);
  1425. }
  1426. }
  1427. return res;
  1428. }
  1429. /* low level addition, based on HAC pp.594, Algorithm 14.7 */
  1430. int s_mp_add (mp_int * a, mp_int * b, mp_int * c)
  1431. {
  1432. mp_int *x;
  1433. int olduse, res, min_ab, max_ab;
  1434. /* find sizes, we let |a| <= |b| which means we have to sort
  1435. * them. "x" will point to the input with the most digits
  1436. */
  1437. if (a->used > b->used) {
  1438. min_ab = b->used;
  1439. max_ab = a->used;
  1440. x = a;
  1441. } else {
  1442. min_ab = a->used;
  1443. max_ab = b->used;
  1444. x = b;
  1445. }
  1446. /* init result */
  1447. if (c->dp == NULL || c->alloc < max_ab + 1) {
  1448. if ((res = mp_grow (c, max_ab + 1)) != MP_OKAY) {
  1449. return res;
  1450. }
  1451. }
  1452. /* get old used digit count and set new one */
  1453. olduse = c->used;
  1454. c->used = max_ab + 1;
  1455. {
  1456. mp_digit u, *tmpa, *tmpb, *tmpc;
  1457. int i;
  1458. /* alias for digit pointers */
  1459. /* first input */
  1460. tmpa = a->dp;
  1461. /* second input */
  1462. tmpb = b->dp;
  1463. /* destination */
  1464. tmpc = c->dp;
  1465. /* zero the carry */
  1466. u = 0;
  1467. for (i = 0; i < min_ab; i++) {
  1468. /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
  1469. *tmpc = *tmpa++ + *tmpb++ + u;
  1470. /* U = carry bit of T[i] */
  1471. u = *tmpc >> ((mp_digit)DIGIT_BIT);
  1472. /* take away carry bit from T[i] */
  1473. *tmpc++ &= MP_MASK;
  1474. }
  1475. /* now copy higher words if any, that is in A+B
  1476. * if A or B has more digits add those in
  1477. */
  1478. if (min_ab != max_ab) {
  1479. for (; i < max_ab; i++) {
  1480. /* T[i] = X[i] + U */
  1481. *tmpc = x->dp[i] + u;
  1482. /* U = carry bit of T[i] */
  1483. u = *tmpc >> ((mp_digit)DIGIT_BIT);
  1484. /* take away carry bit from T[i] */
  1485. *tmpc++ &= MP_MASK;
  1486. }
  1487. }
  1488. /* add carry */
  1489. *tmpc++ = u;
  1490. /* clear digits above olduse */
  1491. for (i = c->used; i < olduse; i++) {
  1492. *tmpc++ = 0;
  1493. }
  1494. }
  1495. mp_clamp (c);
  1496. return MP_OKAY;
  1497. }
  1498. /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
  1499. int s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
  1500. {
  1501. int olduse, res, min_b, max_a;
  1502. /* find sizes */
  1503. min_b = b->used;
  1504. max_a = a->used;
  1505. /* init result */
  1506. if (c->alloc < max_a) {
  1507. if ((res = mp_grow (c, max_a)) != MP_OKAY) {
  1508. return res;
  1509. }
  1510. }
  1511. /* sanity check on destination */
  1512. if (c->dp == NULL)
  1513. return MP_VAL;
  1514. olduse = c->used;
  1515. c->used = max_a;
  1516. {
  1517. mp_digit u, *tmpa, *tmpb, *tmpc;
  1518. int i;
  1519. /* alias for digit pointers */
  1520. tmpa = a->dp;
  1521. tmpb = b->dp;
  1522. tmpc = c->dp;
  1523. /* set carry to zero */
  1524. u = 0;
  1525. for (i = 0; i < min_b; i++) {
  1526. /* T[i] = A[i] - B[i] - U */
  1527. *tmpc = *tmpa++ - *tmpb++ - u;
  1528. /* U = carry bit of T[i]
  1529. * Note this saves performing an AND operation since
  1530. * if a carry does occur it will propagate all the way to the
  1531. * MSB. As a result a single shift is enough to get the carry
  1532. */
  1533. u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
  1534. /* Clear carry from T[i] */
  1535. *tmpc++ &= MP_MASK;
  1536. }
  1537. /* now copy higher words if any, e.g. if A has more digits than B */
  1538. for (; i < max_a; i++) {
  1539. /* T[i] = A[i] - U */
  1540. *tmpc = *tmpa++ - u;
  1541. /* U = carry bit of T[i] */
  1542. u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
  1543. /* Clear carry from T[i] */
  1544. *tmpc++ &= MP_MASK;
  1545. }
  1546. /* clear digits above used (since we may not have grown result above) */
  1547. for (i = c->used; i < olduse; i++) {
  1548. *tmpc++ = 0;
  1549. }
  1550. }
  1551. mp_clamp (c);
  1552. return MP_OKAY;
  1553. }
  1554. /* high level subtraction (handles signs) */
  1555. int mp_sub (mp_int * a, mp_int * b, mp_int * c)
  1556. {
  1557. int sa, sb, res;
  1558. sa = a->sign;
  1559. sb = b->sign;
  1560. if (sa != sb) {
  1561. /* subtract a negative from a positive, OR */
  1562. /* subtract a positive from a negative. */
  1563. /* In either case, ADD their magnitudes, */
  1564. /* and use the sign of the first number. */
  1565. c->sign = sa;
  1566. res = s_mp_add (a, b, c);
  1567. } else {
  1568. /* subtract a positive from a positive, OR */
  1569. /* subtract a negative from a negative. */
  1570. /* First, take the difference between their */
  1571. /* magnitudes, then... */
  1572. if (mp_cmp_mag (a, b) != MP_LT) {
  1573. /* Copy the sign from the first */
  1574. c->sign = sa;
  1575. /* The first has a larger or equal magnitude */
  1576. res = s_mp_sub (a, b, c);
  1577. } else {
  1578. /* The result has the *opposite* sign from */
  1579. /* the first number. */
  1580. c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS;
  1581. /* The second has a larger magnitude */
  1582. res = s_mp_sub (b, a, c);
  1583. }
  1584. }
  1585. return res;
  1586. }
  1587. /* determines if reduce_2k_l can be used */
  1588. int mp_reduce_is_2k_l(mp_int *a)
  1589. {
  1590. int ix, iy;
  1591. if (a->used == 0) {
  1592. return MP_NO;
  1593. } else if (a->used == 1) {
  1594. return MP_YES;
  1595. } else if (a->used > 1) {
  1596. /* if more than half of the digits are -1 we're sold */
  1597. for (iy = ix = 0; ix < a->used; ix++) {
  1598. if (a->dp[ix] == MP_MASK) {
  1599. ++iy;
  1600. }
  1601. }
  1602. return (iy >= (a->used/2)) ? MP_YES : MP_NO;
  1603. }
  1604. return MP_NO;
  1605. }
  1606. /* determines if mp_reduce_2k can be used */
  1607. int mp_reduce_is_2k(mp_int *a)
  1608. {
  1609. int ix, iy, iw;
  1610. mp_digit iz;
  1611. if (a->used == 0) {
  1612. return MP_NO;
  1613. } else if (a->used == 1) {
  1614. return MP_YES;
  1615. } else if (a->used > 1) {
  1616. iy = mp_count_bits(a);
  1617. iz = 1;
  1618. iw = 1;
  1619. /* Test every bit from the second digit up, must be 1 */
  1620. for (ix = DIGIT_BIT; ix < iy; ix++) {
  1621. if ((a->dp[iw] & iz) == 0) {
  1622. return MP_NO;
  1623. }
  1624. iz <<= 1;
  1625. if (iz > (mp_digit)MP_MASK) {
  1626. ++iw;
  1627. iz = 1;
  1628. }
  1629. }
  1630. }
  1631. return MP_YES;
  1632. }
  1633. /* determines if a number is a valid DR modulus */
  1634. int mp_dr_is_modulus(mp_int *a)
  1635. {
  1636. int ix;
  1637. /* must be at least two digits */
  1638. if (a->used < 2) {
  1639. return 0;
  1640. }
  1641. /* must be of the form b**k - a [a <= b] so all
  1642. * but the first digit must be equal to -1 (mod b).
  1643. */
  1644. for (ix = 1; ix < a->used; ix++) {
  1645. if (a->dp[ix] != MP_MASK) {
  1646. return 0;
  1647. }
  1648. }
  1649. return 1;
  1650. }
  1651. /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
  1652. *
  1653. * Uses a left-to-right k-ary sliding window to compute the modular
  1654. * exponentiation.
  1655. * The value of k changes based on the size of the exponent.
  1656. *
  1657. * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
  1658. */
  1659. #ifdef MP_LOW_MEM
  1660. #define TAB_SIZE 32
  1661. #else
  1662. #define TAB_SIZE 256
  1663. #endif
  1664. int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
  1665. int redmode)
  1666. {
  1667. mp_int res;
  1668. mp_digit buf, mp;
  1669. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  1670. #ifdef WOLFSSL_SMALL_STACK
  1671. mp_int* M;
  1672. #else
  1673. mp_int M[TAB_SIZE];
  1674. #endif
  1675. /* use a pointer to the reduction algorithm. This allows us to use
  1676. * one of many reduction algorithms without modding the guts of
  1677. * the code with if statements everywhere.
  1678. */
  1679. int (*redux)(mp_int*,mp_int*,mp_digit) = NULL;
  1680. #ifdef WOLFSSL_SMALL_STACK
  1681. M = (mp_int*) XMALLOC(sizeof(mp_int) * TAB_SIZE, NULL,
  1682. DYNAMIC_TYPE_BIGINT);
  1683. if (M == NULL)
  1684. return MP_MEM;
  1685. #endif
  1686. /* find window size */
  1687. x = mp_count_bits (X);
  1688. if (x <= 7) {
  1689. winsize = 2;
  1690. } else if (x <= 36) {
  1691. winsize = 3;
  1692. } else if (x <= 140) {
  1693. winsize = 4;
  1694. } else if (x <= 450) {
  1695. winsize = 5;
  1696. } else if (x <= 1303) {
  1697. winsize = 6;
  1698. } else if (x <= 3529) {
  1699. winsize = 7;
  1700. } else {
  1701. winsize = 8;
  1702. }
  1703. #ifdef MP_LOW_MEM
  1704. if (winsize > 5) {
  1705. winsize = 5;
  1706. }
  1707. #endif
  1708. /* init M array */
  1709. /* init first cell */
  1710. if ((err = mp_init_size(&M[1], P->alloc)) != MP_OKAY) {
  1711. #ifdef WOLFSSL_SMALL_STACK
  1712. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  1713. #endif
  1714. return err;
  1715. }
  1716. /* now init the second half of the array */
  1717. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  1718. if ((err = mp_init_size(&M[x], P->alloc)) != MP_OKAY) {
  1719. for (y = 1<<(winsize-1); y < x; y++) {
  1720. mp_clear (&M[y]);
  1721. }
  1722. mp_clear(&M[1]);
  1723. #ifdef WOLFSSL_SMALL_STACK
  1724. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  1725. #endif
  1726. return err;
  1727. }
  1728. }
  1729. /* determine and setup reduction code */
  1730. if (redmode == 0) {
  1731. #ifdef BN_MP_MONTGOMERY_SETUP_C
  1732. /* now setup montgomery */
  1733. if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
  1734. goto LBL_M;
  1735. }
  1736. #else
  1737. err = MP_VAL;
  1738. goto LBL_M;
  1739. #endif
  1740. /* automatically pick the comba one if available (saves quite a few
  1741. calls/ifs) */
  1742. #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
  1743. if (((P->used * 2 + 1) < (int)MP_WARRAY) &&
  1744. P->used < (1L << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  1745. redux = fast_mp_montgomery_reduce;
  1746. } else
  1747. #endif
  1748. {
  1749. #ifdef BN_MP_MONTGOMERY_REDUCE_C
  1750. /* use slower baseline Montgomery method */
  1751. redux = mp_montgomery_reduce;
  1752. #endif
  1753. }
  1754. } else if (redmode == 1) {
  1755. #if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C)
  1756. /* setup DR reduction for moduli of the form B**k - b */
  1757. mp_dr_setup(P, &mp);
  1758. redux = mp_dr_reduce;
  1759. #endif
  1760. } else {
  1761. #if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
  1762. /* setup DR reduction for moduli of the form 2**k - b */
  1763. if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
  1764. goto LBL_M;
  1765. }
  1766. /* mp of zero is not usable */
  1767. if (mp != 0) {
  1768. redux = mp_reduce_2k;
  1769. }
  1770. #endif
  1771. }
  1772. if (redux == NULL) {
  1773. err = MP_VAL;
  1774. goto LBL_M;
  1775. }
  1776. /* setup result */
  1777. if ((err = mp_init_size (&res, P->alloc)) != MP_OKAY) {
  1778. goto LBL_M;
  1779. }
  1780. /* create M table
  1781. *
  1782. *
  1783. * The first half of the table is not computed though accept for M[0] and M[1]
  1784. */
  1785. if (redmode == 0) {
  1786. #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  1787. /* now we need R mod m */
  1788. if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
  1789. goto LBL_RES;
  1790. }
  1791. /* now set M[1] to G * R mod m */
  1792. if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
  1793. goto LBL_RES;
  1794. }
  1795. #else
  1796. err = MP_VAL;
  1797. goto LBL_RES;
  1798. #endif
  1799. } else {
  1800. if ((err = mp_set(&res, 1)) != MP_OKAY) {
  1801. goto LBL_RES;
  1802. }
  1803. if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
  1804. goto LBL_RES;
  1805. }
  1806. }
  1807. /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times*/
  1808. if ((err = mp_copy (&M[1], &M[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
  1809. goto LBL_RES;
  1810. }
  1811. for (x = 0; x < (winsize - 1); x++) {
  1812. if ((err = mp_sqr (&M[(mp_digit)(1 << (winsize - 1))],
  1813. &M[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
  1814. goto LBL_RES;
  1815. }
  1816. if ((err = redux (&M[(mp_digit)(1 << (winsize - 1))], P, mp)) != MP_OKAY) {
  1817. goto LBL_RES;
  1818. }
  1819. }
  1820. /* create upper table */
  1821. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  1822. if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
  1823. goto LBL_RES;
  1824. }
  1825. if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
  1826. goto LBL_RES;
  1827. }
  1828. }
  1829. /* set initial mode and bit cnt */
  1830. mode = 0;
  1831. bitcnt = 1;
  1832. buf = 0;
  1833. digidx = X->used - 1;
  1834. bitcpy = 0;
  1835. bitbuf = 0;
  1836. for (;;) {
  1837. /* grab next digit as required */
  1838. if (--bitcnt == 0) {
  1839. /* if digidx == -1 we are out of digits so break */
  1840. if (digidx == -1) {
  1841. break;
  1842. }
  1843. /* read next digit and reset bitcnt */
  1844. buf = X->dp[digidx--];
  1845. bitcnt = (int)DIGIT_BIT;
  1846. }
  1847. /* grab the next msb from the exponent */
  1848. y = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  1849. buf <<= (mp_digit)1;
  1850. /* if the bit is zero and mode == 0 then we ignore it
  1851. * These represent the leading zero bits before the first 1 bit
  1852. * in the exponent. Technically this opt is not required but it
  1853. * does lower the # of trivial squaring/reductions used
  1854. */
  1855. if (mode == 0 && y == 0) {
  1856. continue;
  1857. }
  1858. /* if the bit is zero and mode == 1 then we square */
  1859. if (mode == 1 && y == 0) {
  1860. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  1861. goto LBL_RES;
  1862. }
  1863. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  1864. goto LBL_RES;
  1865. }
  1866. continue;
  1867. }
  1868. /* else we add it to the window */
  1869. bitbuf |= (y << (winsize - ++bitcpy));
  1870. mode = 2;
  1871. if (bitcpy == winsize) {
  1872. /* ok window is filled so square as required and multiply */
  1873. /* square first */
  1874. for (x = 0; x < winsize; x++) {
  1875. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  1876. goto LBL_RES;
  1877. }
  1878. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  1879. goto LBL_RES;
  1880. }
  1881. }
  1882. /* then multiply */
  1883. if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
  1884. goto LBL_RES;
  1885. }
  1886. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  1887. goto LBL_RES;
  1888. }
  1889. /* empty window and reset */
  1890. bitcpy = 0;
  1891. bitbuf = 0;
  1892. mode = 1;
  1893. }
  1894. }
  1895. /* if bits remain then square/multiply */
  1896. if (mode == 2 && bitcpy > 0) {
  1897. /* square then multiply if the bit is set */
  1898. for (x = 0; x < bitcpy; x++) {
  1899. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  1900. goto LBL_RES;
  1901. }
  1902. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  1903. goto LBL_RES;
  1904. }
  1905. /* get next bit of the window */
  1906. bitbuf <<= 1;
  1907. if ((bitbuf & (1 << winsize)) != 0) {
  1908. /* then multiply */
  1909. if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
  1910. goto LBL_RES;
  1911. }
  1912. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  1913. goto LBL_RES;
  1914. }
  1915. }
  1916. }
  1917. }
  1918. if (redmode == 0) {
  1919. /* fixup result if Montgomery reduction is used
  1920. * recall that any value in a Montgomery system is
  1921. * actually multiplied by R mod n. So we have
  1922. * to reduce one more time to cancel out the factor
  1923. * of R.
  1924. */
  1925. if ((err = redux(&res, P, mp)) != MP_OKAY) {
  1926. goto LBL_RES;
  1927. }
  1928. }
  1929. /* swap res with Y */
  1930. mp_exch (&res, Y);
  1931. err = MP_OKAY;
  1932. LBL_RES:mp_clear (&res);
  1933. LBL_M:
  1934. mp_clear(&M[1]);
  1935. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  1936. mp_clear (&M[x]);
  1937. }
  1938. #ifdef WOLFSSL_SMALL_STACK
  1939. XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
  1940. #endif
  1941. return err;
  1942. }
  1943. #ifdef BN_MP_EXPTMOD_BASE_2
  1944. #if DIGIT_BIT < 16
  1945. #define WINSIZE 3
  1946. #elif DIGIT_BIT < 32
  1947. #define WINSIZE 4
  1948. #elif DIGIT_BIT < 64
  1949. #define WINSIZE 5
  1950. #elif DIGIT_BIT < 128
  1951. #define WINSIZE 6
  1952. #endif
  1953. int mp_exptmod_base_2(mp_int * X, mp_int * P, mp_int * Y)
  1954. {
  1955. mp_digit buf, mp;
  1956. int err = MP_OKAY, bitbuf, bitcpy, bitcnt, digidx, x, y;
  1957. mp_int res[1];
  1958. int (*redux)(mp_int*,mp_int*,mp_digit) = NULL;
  1959. /* automatically pick the comba one if available (saves quite a few
  1960. calls/ifs) */
  1961. #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
  1962. if (((P->used * 2 + 1) < (int)MP_WARRAY) &&
  1963. P->used < (1L << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  1964. redux = fast_mp_montgomery_reduce;
  1965. } else
  1966. #endif
  1967. #ifdef BN_MP_MONTGOMERY_REDUCE_C
  1968. {
  1969. /* use slower baseline Montgomery method */
  1970. redux = mp_montgomery_reduce;
  1971. }
  1972. #endif
  1973. if (redux == NULL) {
  1974. return MP_VAL;
  1975. }
  1976. /* now setup montgomery */
  1977. if ((err = mp_montgomery_setup(P, &mp)) != MP_OKAY) {
  1978. goto LBL_M;
  1979. }
  1980. /* setup result */
  1981. if ((err = mp_init(res)) != MP_OKAY) {
  1982. goto LBL_M;
  1983. }
  1984. /* now we need R mod m */
  1985. if ((err = mp_montgomery_calc_normalization(res, P)) != MP_OKAY) {
  1986. goto LBL_RES;
  1987. }
  1988. /* Get the top bits left over after taking WINSIZE bits starting at the
  1989. * least-significant.
  1990. */
  1991. digidx = X->used - 1;
  1992. bitcpy = (X->used * DIGIT_BIT) % WINSIZE;
  1993. if (bitcpy > 0) {
  1994. bitcnt = (int)DIGIT_BIT - bitcpy;
  1995. buf = X->dp[digidx--];
  1996. bitbuf = (int)(buf >> bitcnt);
  1997. /* Multiply montgomery representation of 1 by 2 ^ top */
  1998. err = mp_mul_2d(res, bitbuf, res);
  1999. if (err != MP_OKAY) {
  2000. goto LBL_RES;
  2001. }
  2002. err = mp_mod(res, P, res);
  2003. if (err != MP_OKAY) {
  2004. goto LBL_RES;
  2005. }
  2006. /* Move out bits used */
  2007. buf <<= bitcpy;
  2008. bitcnt++;
  2009. }
  2010. else {
  2011. bitcnt = 1;
  2012. buf = 0;
  2013. }
  2014. /* empty window and reset */
  2015. bitbuf = 0;
  2016. bitcpy = 0;
  2017. for (;;) {
  2018. /* grab next digit as required */
  2019. if (--bitcnt == 0) {
  2020. /* if digidx == -1 we are out of digits so break */
  2021. if (digidx == -1) {
  2022. break;
  2023. }
  2024. /* read next digit and reset bitcnt */
  2025. buf = X->dp[digidx--];
  2026. bitcnt = (int)DIGIT_BIT;
  2027. }
  2028. /* grab the next msb from the exponent */
  2029. y = (int)(buf >> (DIGIT_BIT - 1)) & 1;
  2030. buf <<= (mp_digit)1;
  2031. /* add bit to the window */
  2032. bitbuf |= (y << (WINSIZE - ++bitcpy));
  2033. if (bitcpy == WINSIZE) {
  2034. /* ok window is filled so square as required and multiply */
  2035. /* square first */
  2036. for (x = 0; x < WINSIZE; x++) {
  2037. err = mp_sqr(res, res);
  2038. if (err != MP_OKAY) {
  2039. goto LBL_RES;
  2040. }
  2041. err = (*redux)(res, P, mp);
  2042. if (err != MP_OKAY) {
  2043. goto LBL_RES;
  2044. }
  2045. }
  2046. /* then multiply by 2^bitbuf */
  2047. err = mp_mul_2d(res, bitbuf, res);
  2048. if (err != MP_OKAY) {
  2049. goto LBL_RES;
  2050. }
  2051. err = mp_mod(res, P, res);
  2052. if (err != MP_OKAY) {
  2053. goto LBL_RES;
  2054. }
  2055. /* empty window and reset */
  2056. bitcpy = 0;
  2057. bitbuf = 0;
  2058. }
  2059. }
  2060. /* fixup result if Montgomery reduction is used
  2061. * recall that any value in a Montgomery system is
  2062. * actually multiplied by R mod n. So we have
  2063. * to reduce one more time to cancel out the factor
  2064. * of R.
  2065. */
  2066. err = (*redux)(res, P, mp);
  2067. if (err != MP_OKAY) {
  2068. goto LBL_RES;
  2069. }
  2070. /* swap res with Y */
  2071. err = mp_copy(res, Y);
  2072. LBL_RES:mp_clear (res);
  2073. LBL_M:
  2074. return err;
  2075. }
  2076. #undef WINSIZE
  2077. #endif /* BN_MP_EXPTMOD_BASE_2 */
  2078. /* setups the montgomery reduction stuff */
  2079. int mp_montgomery_setup (mp_int * n, mp_digit * rho)
  2080. {
  2081. mp_digit x, b;
  2082. /* fast inversion mod 2**k
  2083. *
  2084. * Based on the fact that
  2085. *
  2086. * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
  2087. * => 2*X*A - X*X*A*A = 1
  2088. * => 2*(1) - (1) = 1
  2089. */
  2090. b = n->dp[0];
  2091. if ((b & 1) == 0) {
  2092. return MP_VAL;
  2093. }
  2094. x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
  2095. x *= 2 - b * x; /* here x*a==1 mod 2**8 */
  2096. #if !defined(MP_8BIT)
  2097. x *= 2 - b * x; /* here x*a==1 mod 2**16 */
  2098. #endif
  2099. #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
  2100. x *= 2 - b * x; /* here x*a==1 mod 2**32 */
  2101. #endif
  2102. #ifdef MP_64BIT
  2103. x *= 2 - b * x; /* here x*a==1 mod 2**64 */
  2104. #endif
  2105. /* rho = -1/m mod b */
  2106. /* TAO, switched mp_word casts to mp_digit to shut up compiler */
  2107. *rho = (mp_digit)((((mp_digit)1 << ((mp_digit) DIGIT_BIT)) - x) & MP_MASK);
  2108. return MP_OKAY;
  2109. }
  2110. /* computes xR**-1 == x (mod N) via Montgomery Reduction
  2111. *
  2112. * This is an optimized implementation of montgomery_reduce
  2113. * which uses the comba method to quickly calculate the columns of the
  2114. * reduction.
  2115. *
  2116. * Based on Algorithm 14.32 on pp.601 of HAC.
  2117. */
  2118. int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
  2119. {
  2120. int ix, res, olduse;
  2121. #ifdef WOLFSSL_SMALL_STACK
  2122. mp_word* W; /* uses dynamic memory and slower */
  2123. #else
  2124. mp_word W[MP_WARRAY];
  2125. #endif
  2126. /* get old used count */
  2127. olduse = x->used;
  2128. /* grow a as required */
  2129. if (x->alloc < n->used + 1) {
  2130. if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) {
  2131. return res;
  2132. }
  2133. }
  2134. #ifdef WOLFSSL_SMALL_STACK
  2135. W = (mp_word*)XMALLOC(sizeof(mp_word) * (n->used * 2 + 2), NULL,
  2136. DYNAMIC_TYPE_BIGINT);
  2137. if (W == NULL)
  2138. return MP_MEM;
  2139. #endif
  2140. XMEMSET(W, 0, sizeof(mp_word) * (n->used * 2 + 2));
  2141. /* first we have to get the digits of the input into
  2142. * an array of double precision words W[...]
  2143. */
  2144. {
  2145. mp_word *_W;
  2146. mp_digit *tmpx;
  2147. /* alias for the W[] array */
  2148. _W = W;
  2149. /* alias for the digits of x*/
  2150. tmpx = x->dp;
  2151. /* copy the digits of a into W[0..a->used-1] */
  2152. for (ix = 0; ix < x->used; ix++) {
  2153. *_W++ = *tmpx++;
  2154. }
  2155. }
  2156. /* now we proceed to zero successive digits
  2157. * from the least significant upwards
  2158. */
  2159. for (ix = 0; ix < n->used; ix++) {
  2160. /* mu = ai * m' mod b
  2161. *
  2162. * We avoid a double precision multiplication (which isn't required)
  2163. * by casting the value down to a mp_digit. Note this requires
  2164. * that W[ix-1] have the carry cleared (see after the inner loop)
  2165. */
  2166. mp_digit mu;
  2167. mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK);
  2168. /* a = a + mu * m * b**i
  2169. *
  2170. * This is computed in place and on the fly. The multiplication
  2171. * by b**i is handled by offsetting which columns the results
  2172. * are added to.
  2173. *
  2174. * Note the comba method normally doesn't handle carries in the
  2175. * inner loop In this case we fix the carry from the previous
  2176. * column since the Montgomery reduction requires digits of the
  2177. * result (so far) [see above] to work. This is
  2178. * handled by fixing up one carry after the inner loop. The
  2179. * carry fixups are done in order so after these loops the
  2180. * first m->used words of W[] have the carries fixed
  2181. */
  2182. {
  2183. int iy;
  2184. mp_digit *tmpn;
  2185. mp_word *_W;
  2186. /* alias for the digits of the modulus */
  2187. tmpn = n->dp;
  2188. /* Alias for the columns set by an offset of ix */
  2189. _W = W + ix;
  2190. /* inner loop */
  2191. for (iy = 0; iy < n->used; iy++) {
  2192. *_W++ += ((mp_word)mu) * ((mp_word)*tmpn++);
  2193. }
  2194. }
  2195. /* now fix carry for next digit, W[ix+1] */
  2196. W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
  2197. }
  2198. /* now we have to propagate the carries and
  2199. * shift the words downward [all those least
  2200. * significant digits we zeroed].
  2201. */
  2202. {
  2203. mp_digit *tmpx;
  2204. mp_word *_W, *_W1;
  2205. /* nox fix rest of carries */
  2206. /* alias for current word */
  2207. _W1 = W + ix;
  2208. /* alias for next word, where the carry goes */
  2209. _W = W + ++ix;
  2210. for (; ix <= n->used * 2 + 1; ix++) {
  2211. *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
  2212. }
  2213. /* copy out, A = A/b**n
  2214. *
  2215. * The result is A/b**n but instead of converting from an
  2216. * array of mp_word to mp_digit than calling mp_rshd
  2217. * we just copy them in the right order
  2218. */
  2219. /* alias for destination word */
  2220. tmpx = x->dp;
  2221. /* alias for shifted double precision result */
  2222. _W = W + n->used;
  2223. for (ix = 0; ix < n->used + 1; ix++) {
  2224. *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
  2225. }
  2226. /* zero olduse digits, if the input a was larger than
  2227. * m->used+1 we'll have to clear the digits
  2228. */
  2229. for (; ix < olduse; ix++) {
  2230. *tmpx++ = 0;
  2231. }
  2232. }
  2233. /* set the max used and clamp */
  2234. x->used = n->used + 1;
  2235. mp_clamp (x);
  2236. #ifdef WOLFSSL_SMALL_STACK
  2237. XFREE(W, NULL, DYNAMIC_TYPE_BIGINT);
  2238. #endif
  2239. /* if A >= m then A = A - m */
  2240. if (mp_cmp_mag (x, n) != MP_LT) {
  2241. return s_mp_sub (x, n, x);
  2242. }
  2243. return MP_OKAY;
  2244. }
  2245. /* computes xR**-1 == x (mod N) via Montgomery Reduction */
  2246. int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
  2247. {
  2248. int ix, res, digs;
  2249. mp_digit mu;
  2250. /* can the fast reduction [comba] method be used?
  2251. *
  2252. * Note that unlike in mul you're safely allowed *less*
  2253. * than the available columns [255 per default] since carries
  2254. * are fixed up in the inner loop.
  2255. */
  2256. digs = n->used * 2 + 1;
  2257. if ((digs < (int)MP_WARRAY) &&
  2258. n->used <
  2259. (1L << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  2260. return fast_mp_montgomery_reduce (x, n, rho);
  2261. }
  2262. /* grow the input as required */
  2263. if (x->alloc < digs) {
  2264. if ((res = mp_grow (x, digs)) != MP_OKAY) {
  2265. return res;
  2266. }
  2267. }
  2268. x->used = digs;
  2269. for (ix = 0; ix < n->used; ix++) {
  2270. /* mu = ai * rho mod b
  2271. *
  2272. * The value of rho must be precalculated via
  2273. * montgomery_setup() such that
  2274. * it equals -1/n0 mod b this allows the
  2275. * following inner loop to reduce the
  2276. * input one digit at a time
  2277. */
  2278. mu = (mp_digit) (((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK);
  2279. /* a = a + mu * m * b**i */
  2280. {
  2281. int iy;
  2282. mp_digit *tmpn, *tmpx, u;
  2283. mp_word r;
  2284. /* alias for digits of the modulus */
  2285. tmpn = n->dp;
  2286. /* alias for the digits of x [the input] */
  2287. tmpx = x->dp + ix;
  2288. /* set the carry to zero */
  2289. u = 0;
  2290. /* Multiply and add in place */
  2291. for (iy = 0; iy < n->used; iy++) {
  2292. /* compute product and sum */
  2293. r = ((mp_word)mu) * ((mp_word)*tmpn++) +
  2294. ((mp_word) u) + ((mp_word) * tmpx);
  2295. /* get carry */
  2296. u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
  2297. /* fix digit */
  2298. *tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));
  2299. }
  2300. /* At this point the ix'th digit of x should be zero */
  2301. /* propagate carries upwards as required*/
  2302. while (u) {
  2303. *tmpx += u;
  2304. u = *tmpx >> DIGIT_BIT;
  2305. *tmpx++ &= MP_MASK;
  2306. }
  2307. }
  2308. }
  2309. /* at this point the n.used'th least
  2310. * significant digits of x are all zero
  2311. * which means we can shift x to the
  2312. * right by n.used digits and the
  2313. * residue is unchanged.
  2314. */
  2315. /* x = x/b**n.used */
  2316. mp_clamp(x);
  2317. mp_rshd (x, n->used);
  2318. /* if x >= n then x = x - n */
  2319. if (mp_cmp_mag (x, n) != MP_LT) {
  2320. return s_mp_sub (x, n, x);
  2321. }
  2322. return MP_OKAY;
  2323. }
  2324. /* determines the setup value */
  2325. void mp_dr_setup(mp_int *a, mp_digit *d)
  2326. {
  2327. /* the casts are required if DIGIT_BIT is one less than
  2328. * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
  2329. */
  2330. *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -
  2331. ((mp_word)a->dp[0]));
  2332. }
  2333. /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
  2334. *
  2335. * Based on algorithm from the paper
  2336. *
  2337. * "Generating Efficient Primes for Discrete Log Cryptosystems"
  2338. * Chae Hoon Lim, Pil Joong Lee,
  2339. * POSTECH Information Research Laboratories
  2340. *
  2341. * The modulus must be of a special format [see manual]
  2342. *
  2343. * Has been modified to use algorithm 7.10 from the LTM book instead
  2344. *
  2345. * Input x must be in the range 0 <= x <= (n-1)**2
  2346. */
  2347. int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
  2348. {
  2349. int err, i, m;
  2350. mp_word r;
  2351. mp_digit mu, *tmpx1, *tmpx2;
  2352. /* m = digits in modulus */
  2353. m = n->used;
  2354. /* ensure that "x" has at least 2m digits */
  2355. if (x->alloc < m + m) {
  2356. if ((err = mp_grow (x, m + m)) != MP_OKAY) {
  2357. return err;
  2358. }
  2359. }
  2360. /* top of loop, this is where the code resumes if
  2361. * another reduction pass is required.
  2362. */
  2363. top:
  2364. /* aliases for digits */
  2365. /* alias for lower half of x */
  2366. tmpx1 = x->dp;
  2367. /* alias for upper half of x, or x/B**m */
  2368. tmpx2 = x->dp + m;
  2369. /* set carry to zero */
  2370. mu = 0;
  2371. /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
  2372. for (i = 0; i < m; i++) {
  2373. r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu;
  2374. *tmpx1++ = (mp_digit)(r & MP_MASK);
  2375. mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
  2376. }
  2377. /* set final carry */
  2378. *tmpx1++ = mu;
  2379. /* zero words above m */
  2380. for (i = m + 1; i < x->used; i++) {
  2381. *tmpx1++ = 0;
  2382. }
  2383. /* clamp, sub and return */
  2384. mp_clamp (x);
  2385. /* if x >= n then subtract and reduce again
  2386. * Each successive "recursion" makes the input smaller and smaller.
  2387. */
  2388. if (mp_cmp_mag (x, n) != MP_LT) {
  2389. if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
  2390. return err;
  2391. }
  2392. goto top;
  2393. }
  2394. return MP_OKAY;
  2395. }
  2396. /* reduces a modulo n where n is of the form 2**p - d */
  2397. int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
  2398. {
  2399. mp_int q;
  2400. int p, res;
  2401. if ((res = mp_init(&q)) != MP_OKAY) {
  2402. return res;
  2403. }
  2404. p = mp_count_bits(n);
  2405. top:
  2406. /* q = a/2**p, a = a mod 2**p */
  2407. if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
  2408. goto ERR;
  2409. }
  2410. if (d != 1) {
  2411. /* q = q * d */
  2412. if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
  2413. goto ERR;
  2414. }
  2415. }
  2416. /* a = a + q */
  2417. if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
  2418. goto ERR;
  2419. }
  2420. if (mp_cmp_mag(a, n) != MP_LT) {
  2421. if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
  2422. goto ERR;
  2423. }
  2424. goto top;
  2425. }
  2426. ERR:
  2427. mp_clear(&q);
  2428. return res;
  2429. }
  2430. /* determines the setup value */
  2431. int mp_reduce_2k_setup(mp_int *a, mp_digit *d)
  2432. {
  2433. int res, p;
  2434. mp_int tmp;
  2435. if ((res = mp_init(&tmp)) != MP_OKAY) {
  2436. return res;
  2437. }
  2438. p = mp_count_bits(a);
  2439. if ((res = mp_2expt(&tmp, p)) != MP_OKAY) {
  2440. mp_clear(&tmp);
  2441. return res;
  2442. }
  2443. if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) {
  2444. mp_clear(&tmp);
  2445. return res;
  2446. }
  2447. *d = tmp.dp[0];
  2448. mp_clear(&tmp);
  2449. return MP_OKAY;
  2450. }
  2451. /* set the b bit of a */
  2452. int mp_set_bit (mp_int * a, int b)
  2453. {
  2454. int i = b / DIGIT_BIT, res;
  2455. /*
  2456. * Require:
  2457. * bit index b >= 0
  2458. * a->alloc == a->used == 0 if a->dp == NULL
  2459. */
  2460. if (b < 0 || (a->dp == NULL && (a->alloc != 0 || a->used != 0)))
  2461. return MP_VAL;
  2462. if (a->dp == NULL || a->used < (int)(i + 1)) {
  2463. /* grow a to accommodate the single bit */
  2464. if ((res = mp_grow (a, i + 1)) != MP_OKAY) {
  2465. return res;
  2466. }
  2467. /* set the used count of where the bit will go */
  2468. a->used = (int)(i + 1);
  2469. }
  2470. /* put the single bit in its place */
  2471. a->dp[i] |= ((mp_digit)1) << (b % DIGIT_BIT);
  2472. return MP_OKAY;
  2473. }
  2474. /* computes a = 2**b
  2475. *
  2476. * Simple algorithm which zeros the int, set the required bit
  2477. */
  2478. int mp_2expt (mp_int * a, int b)
  2479. {
  2480. /* zero a as per default */
  2481. mp_zero (a);
  2482. return mp_set_bit(a, b);
  2483. }
  2484. /* multiply by a digit */
  2485. int mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
  2486. {
  2487. mp_digit u, *tmpa, *tmpc;
  2488. mp_word r;
  2489. int ix, res, olduse;
  2490. /* make sure c is big enough to hold a*b */
  2491. if (c->dp == NULL || c->alloc < a->used + 1) {
  2492. if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
  2493. return res;
  2494. }
  2495. }
  2496. /* get the original destinations used count */
  2497. olduse = c->used;
  2498. /* set the sign */
  2499. c->sign = a->sign;
  2500. /* alias for a->dp [source] */
  2501. tmpa = a->dp;
  2502. /* alias for c->dp [dest] */
  2503. tmpc = c->dp;
  2504. /* zero carry */
  2505. u = 0;
  2506. /* compute columns */
  2507. for (ix = 0; ix < a->used; ix++) {
  2508. /* compute product and carry sum for this term */
  2509. r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
  2510. /* mask off higher bits to get a single digit */
  2511. *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
  2512. /* send carry into next iteration */
  2513. u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
  2514. }
  2515. /* store final carry [if any] and increment ix offset */
  2516. *tmpc++ = u;
  2517. ++ix;
  2518. /* now zero digits above the top */
  2519. while (ix++ < olduse) {
  2520. *tmpc++ = 0;
  2521. }
  2522. /* set used count */
  2523. c->used = a->used + 1;
  2524. mp_clamp(c);
  2525. return MP_OKAY;
  2526. }
  2527. /* d = a * b (mod c) */
  2528. #if defined(FREESCALE_LTC_TFM)
  2529. int wolfcrypt_mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
  2530. #else
  2531. int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  2532. #endif
  2533. {
  2534. int res;
  2535. mp_int t;
  2536. if ((res = mp_init_size (&t, c->used)) != MP_OKAY) {
  2537. return res;
  2538. }
  2539. res = mp_mul (a, b, &t);
  2540. if (res == MP_OKAY) {
  2541. res = mp_mod (&t, c, d);
  2542. }
  2543. mp_clear (&t);
  2544. return res;
  2545. }
  2546. /* d = a - b (mod c) */
  2547. int mp_submod(mp_int* a, mp_int* b, mp_int* c, mp_int* d)
  2548. {
  2549. int res;
  2550. mp_int t;
  2551. if ((res = mp_init (&t)) != MP_OKAY) {
  2552. return res;
  2553. }
  2554. res = mp_sub (a, b, &t);
  2555. if (res == MP_OKAY) {
  2556. res = mp_mod (&t, c, d);
  2557. }
  2558. mp_clear (&t);
  2559. return res;
  2560. }
  2561. /* d = a + b (mod c) */
  2562. int mp_addmod(mp_int* a, mp_int* b, mp_int* c, mp_int* d)
  2563. {
  2564. int res;
  2565. mp_int t;
  2566. if ((res = mp_init (&t)) != MP_OKAY) {
  2567. return res;
  2568. }
  2569. res = mp_add (a, b, &t);
  2570. if (res == MP_OKAY) {
  2571. res = mp_mod (&t, c, d);
  2572. }
  2573. mp_clear (&t);
  2574. return res;
  2575. }
  2576. /* d = a - b (mod c) - a < c and b < c and positive */
  2577. int mp_submod_ct(mp_int* a, mp_int* b, mp_int* c, mp_int* d)
  2578. {
  2579. int res;
  2580. res = mp_sub(a, b, d);
  2581. if (res == MP_OKAY && mp_isneg(d)) {
  2582. res = mp_add(d, c, d);
  2583. }
  2584. return res;
  2585. }
  2586. /* d = a + b (mod c) - a < c and b < c and positive */
  2587. int mp_addmod_ct(mp_int* a, mp_int* b, mp_int* c, mp_int* d)
  2588. {
  2589. int res;
  2590. res = mp_add(a, b, d);
  2591. if (res == MP_OKAY && mp_cmp(d, c) != MP_LT) {
  2592. res = mp_sub(d, c, d);
  2593. }
  2594. return res;
  2595. }
  2596. /* computes b = a*a */
  2597. int mp_sqr (mp_int * a, mp_int * b)
  2598. {
  2599. int res;
  2600. {
  2601. #ifdef BN_FAST_S_MP_SQR_C
  2602. /* can we use the fast comba multiplier? */
  2603. if ((a->used * 2 + 1) < (int)MP_WARRAY &&
  2604. a->used <
  2605. (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
  2606. res = fast_s_mp_sqr (a, b);
  2607. } else
  2608. #endif
  2609. #ifdef BN_S_MP_SQR_C
  2610. res = s_mp_sqr (a, b);
  2611. #else
  2612. res = MP_VAL;
  2613. #endif
  2614. }
  2615. b->sign = MP_ZPOS;
  2616. return res;
  2617. }
  2618. /* high level multiplication (handles sign) */
  2619. #if defined(FREESCALE_LTC_TFM)
  2620. int wolfcrypt_mp_mul(mp_int *a, mp_int *b, mp_int *c)
  2621. #else
  2622. int mp_mul (mp_int * a, mp_int * b, mp_int * c)
  2623. #endif
  2624. {
  2625. int res, neg;
  2626. neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
  2627. {
  2628. #ifdef BN_FAST_S_MP_MUL_DIGS_C
  2629. /* can we use the fast multiplier?
  2630. *
  2631. * The fast multiplier can be used if the output will
  2632. * have less than MP_WARRAY digits and the number of
  2633. * digits won't affect carry propagation
  2634. */
  2635. int digs = a->used + b->used + 1;
  2636. if ((digs < (int)MP_WARRAY) &&
  2637. MIN(a->used, b->used) <=
  2638. (1L << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  2639. res = fast_s_mp_mul_digs (a, b, c, digs);
  2640. } else
  2641. #endif
  2642. #ifdef BN_S_MP_MUL_DIGS_C
  2643. res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
  2644. #else
  2645. res = MP_VAL;
  2646. #endif
  2647. }
  2648. c->sign = (c->used > 0) ? neg : MP_ZPOS;
  2649. return res;
  2650. }
  2651. /* b = a*2 */
  2652. int mp_mul_2(mp_int * a, mp_int * b)
  2653. {
  2654. int x, res, oldused;
  2655. /* grow to accommodate result */
  2656. if (b->alloc < a->used + 1) {
  2657. if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
  2658. return res;
  2659. }
  2660. }
  2661. oldused = b->used;
  2662. b->used = a->used;
  2663. {
  2664. mp_digit r, rr, *tmpa, *tmpb;
  2665. /* alias for source */
  2666. tmpa = a->dp;
  2667. /* alias for dest */
  2668. tmpb = b->dp;
  2669. /* carry */
  2670. r = 0;
  2671. for (x = 0; x < a->used; x++) {
  2672. /* get what will be the *next* carry bit from the
  2673. * MSB of the current digit
  2674. */
  2675. rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
  2676. /* now shift up this digit, add in the carry [from the previous] */
  2677. *tmpb++ = (mp_digit)(((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK);
  2678. /* copy the carry that would be from the source
  2679. * digit into the next iteration
  2680. */
  2681. r = rr;
  2682. }
  2683. /* new leading digit? */
  2684. if (r != 0) {
  2685. /* add a MSB which is always 1 at this point */
  2686. *tmpb = 1;
  2687. ++(b->used);
  2688. }
  2689. /* now zero any excess digits on the destination
  2690. * that we didn't write to
  2691. */
  2692. tmpb = b->dp + b->used;
  2693. for (x = b->used; x < oldused; x++) {
  2694. *tmpb++ = 0;
  2695. }
  2696. }
  2697. b->sign = a->sign;
  2698. return MP_OKAY;
  2699. }
  2700. /* divide by three (based on routine from MPI and the GMP manual) */
  2701. int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
  2702. {
  2703. mp_int q;
  2704. mp_word w, t;
  2705. mp_digit b;
  2706. int res, ix;
  2707. /* b = 2**DIGIT_BIT / 3 */
  2708. b = (mp_digit) ( (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3) );
  2709. if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
  2710. return res;
  2711. }
  2712. q.used = a->used;
  2713. q.sign = a->sign;
  2714. w = 0;
  2715. for (ix = a->used - 1; ix >= 0; ix--) {
  2716. w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
  2717. if (w >= 3) {
  2718. /* multiply w by [1/3] */
  2719. t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
  2720. /* now subtract 3 * [w/3] from w, to get the remainder */
  2721. w -= t+t+t;
  2722. /* fixup the remainder as required since
  2723. * the optimization is not exact.
  2724. */
  2725. while (w >= 3) {
  2726. t += 1;
  2727. w -= 3;
  2728. }
  2729. } else {
  2730. t = 0;
  2731. }
  2732. q.dp[ix] = (mp_digit)t;
  2733. }
  2734. /* [optional] store the remainder */
  2735. if (d != NULL) {
  2736. *d = (mp_digit)w;
  2737. }
  2738. /* [optional] store the quotient */
  2739. if (c != NULL) {
  2740. mp_clamp(&q);
  2741. mp_exch(&q, c);
  2742. }
  2743. mp_clear(&q);
  2744. return res;
  2745. }
  2746. /* init an mp_init for a given size */
  2747. int mp_init_size (mp_int * a, int size)
  2748. {
  2749. int x;
  2750. /* pad size so there are always extra digits */
  2751. size += (MP_PREC * 2) - (size % MP_PREC);
  2752. /* alloc mem */
  2753. a->dp = (mp_digit *)XMALLOC (sizeof (mp_digit) * size, NULL,
  2754. DYNAMIC_TYPE_BIGINT);
  2755. if (a->dp == NULL) {
  2756. return MP_MEM;
  2757. }
  2758. /* set the members */
  2759. a->used = 0;
  2760. a->alloc = size;
  2761. a->sign = MP_ZPOS;
  2762. #ifdef HAVE_WOLF_BIGINT
  2763. wc_bigint_init(&a->raw);
  2764. #endif
  2765. /* zero the digits */
  2766. for (x = 0; x < size; x++) {
  2767. a->dp[x] = 0;
  2768. }
  2769. return MP_OKAY;
  2770. }
  2771. /* the list of squaring...
  2772. * you do like mult except the offset of the tmpx [one that
  2773. * starts closer to zero] can't equal the offset of tmpy.
  2774. * So basically you set up iy like before then you min it with
  2775. * (ty-tx) so that it never happens. You double all those
  2776. * you add in the inner loop
  2777. After that loop you do the squares and add them in.
  2778. */
  2779. int fast_s_mp_sqr (mp_int * a, mp_int * b)
  2780. {
  2781. int olduse, res, pa, ix, iz;
  2782. #ifdef WOLFSSL_SMALL_STACK
  2783. mp_digit* W; /* uses dynamic memory and slower */
  2784. #else
  2785. mp_digit W[MP_WARRAY];
  2786. #endif
  2787. mp_digit *tmpx;
  2788. mp_word W1;
  2789. /* grow the destination as required */
  2790. pa = a->used + a->used;
  2791. if (b->alloc < pa) {
  2792. if ((res = mp_grow (b, pa)) != MP_OKAY) {
  2793. return res;
  2794. }
  2795. }
  2796. if (pa > (int)MP_WARRAY)
  2797. return MP_RANGE; /* TAO range check */
  2798. if (pa == 0) {
  2799. /* Nothing to do. Zero result and return. */
  2800. mp_zero(b);
  2801. return MP_OKAY;
  2802. }
  2803. #ifdef WOLFSSL_SMALL_STACK
  2804. W = (mp_digit*)XMALLOC(sizeof(mp_digit) * pa, NULL, DYNAMIC_TYPE_BIGINT);
  2805. if (W == NULL)
  2806. return MP_MEM;
  2807. #endif
  2808. /* number of output digits to produce */
  2809. W1 = 0;
  2810. for (ix = 0; ix < pa; ix++) {
  2811. int tx, ty, iy;
  2812. mp_word _W;
  2813. mp_digit *tmpy;
  2814. /* clear counter */
  2815. _W = 0;
  2816. /* get offsets into the two bignums */
  2817. ty = MIN(a->used-1, ix);
  2818. tx = ix - ty;
  2819. /* setup temp aliases */
  2820. tmpx = a->dp + tx;
  2821. tmpy = a->dp + ty;
  2822. /* this is the number of times the loop will iterate, essentially
  2823. while (tx++ < a->used && ty-- >= 0) { ... }
  2824. */
  2825. iy = MIN(a->used-tx, ty+1);
  2826. /* now for squaring tx can never equal ty
  2827. * we halve the distance since they approach at a rate of 2x
  2828. * and we have to round because odd cases need to be executed
  2829. */
  2830. iy = MIN(iy, (ty-tx+1)>>1);
  2831. /* execute loop */
  2832. for (iz = 0; iz < iy; iz++) {
  2833. _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
  2834. }
  2835. /* double the inner product and add carry */
  2836. _W = _W + _W + W1;
  2837. /* even columns have the square term in them */
  2838. if ((ix&1) == 0) {
  2839. _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
  2840. }
  2841. /* store it */
  2842. W[ix] = (mp_digit)(_W & MP_MASK);
  2843. /* make next carry */
  2844. W1 = _W >> ((mp_word)DIGIT_BIT);
  2845. }
  2846. /* setup dest */
  2847. olduse = b->used;
  2848. b->used = a->used+a->used;
  2849. {
  2850. mp_digit *tmpb;
  2851. tmpb = b->dp;
  2852. for (ix = 0; ix < pa; ix++) {
  2853. *tmpb++ = (mp_digit)(W[ix] & MP_MASK);
  2854. }
  2855. /* clear unused digits [that existed in the old copy of c] */
  2856. for (; ix < olduse; ix++) {
  2857. *tmpb++ = 0;
  2858. }
  2859. }
  2860. mp_clamp (b);
  2861. #ifdef WOLFSSL_SMALL_STACK
  2862. XFREE(W, NULL, DYNAMIC_TYPE_BIGINT);
  2863. #endif
  2864. return MP_OKAY;
  2865. }
  2866. /* Fast (comba) multiplier
  2867. *
  2868. * This is the fast column-array [comba] multiplier. It is
  2869. * designed to compute the columns of the product first
  2870. * then handle the carries afterwards. This has the effect
  2871. * of making the nested loops that compute the columns very
  2872. * simple and schedulable on super-scalar processors.
  2873. *
  2874. * This has been modified to produce a variable number of
  2875. * digits of output so if say only a half-product is required
  2876. * you don't have to compute the upper half (a feature
  2877. * required for fast Barrett reduction).
  2878. *
  2879. * Based on Algorithm 14.12 on pp.595 of HAC.
  2880. *
  2881. */
  2882. int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
  2883. {
  2884. int olduse, res, pa, ix, iz;
  2885. #ifdef WOLFSSL_SMALL_STACK
  2886. mp_digit* W; /* uses dynamic memory and slower */
  2887. #else
  2888. mp_digit W[MP_WARRAY];
  2889. #endif
  2890. mp_word _W;
  2891. /* grow the destination as required */
  2892. if (c->alloc < digs) {
  2893. if ((res = mp_grow (c, digs)) != MP_OKAY) {
  2894. return res;
  2895. }
  2896. }
  2897. /* number of output digits to produce */
  2898. pa = MIN(digs, a->used + b->used);
  2899. if (pa > (int)MP_WARRAY)
  2900. return MP_RANGE; /* TAO range check */
  2901. if (pa == 0) {
  2902. /* Nothing to do. Zero result and return. */
  2903. mp_zero(c);
  2904. return MP_OKAY;
  2905. }
  2906. #ifdef WOLFSSL_SMALL_STACK
  2907. W = (mp_digit*)XMALLOC(sizeof(mp_digit) * pa, NULL, DYNAMIC_TYPE_BIGINT);
  2908. if (W == NULL)
  2909. return MP_MEM;
  2910. #endif
  2911. /* clear the carry */
  2912. _W = 0;
  2913. for (ix = 0; ix < pa; ix++) {
  2914. int tx, ty;
  2915. int iy;
  2916. mp_digit *tmpx, *tmpy;
  2917. if ((a->used > 0) && (b->used > 0)) {
  2918. /* get offsets into the two bignums */
  2919. ty = MIN(b->used-1, ix);
  2920. tx = ix - ty;
  2921. /* setup temp aliases */
  2922. tmpx = a->dp + tx;
  2923. tmpy = b->dp + ty;
  2924. /* this is the number of times the loop will iterate, essentially
  2925. while (tx++ < a->used && ty-- >= 0) { ... }
  2926. */
  2927. iy = MIN(a->used-tx, ty+1);
  2928. /* execute loop */
  2929. for (iz = 0; iz < iy; ++iz) {
  2930. _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
  2931. }
  2932. }
  2933. /* store term */
  2934. W[ix] = (mp_digit)(((mp_digit)_W) & MP_MASK);
  2935. /* make next carry */
  2936. _W = _W >> ((mp_word)DIGIT_BIT);
  2937. }
  2938. /* setup dest */
  2939. olduse = c->used;
  2940. c->used = pa;
  2941. {
  2942. mp_digit *tmpc;
  2943. tmpc = c->dp;
  2944. for (ix = 0; ix < pa; ix++) { /* JRB, +1 could read uninitialized data */
  2945. /* now extract the previous digit [below the carry] */
  2946. *tmpc++ = W[ix];
  2947. }
  2948. /* clear unused digits [that existed in the old copy of c] */
  2949. for (; ix < olduse; ix++) {
  2950. *tmpc++ = 0;
  2951. }
  2952. }
  2953. mp_clamp (c);
  2954. #ifdef WOLFSSL_SMALL_STACK
  2955. XFREE(W, NULL, DYNAMIC_TYPE_BIGINT);
  2956. #endif
  2957. return MP_OKAY;
  2958. }
  2959. /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
  2960. int s_mp_sqr (mp_int * a, mp_int * b)
  2961. {
  2962. mp_int t;
  2963. int res, ix, iy, pa;
  2964. mp_word r;
  2965. mp_digit u, tmpx, *tmpt;
  2966. pa = a->used;
  2967. if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) {
  2968. return res;
  2969. }
  2970. /* default used is maximum possible size */
  2971. t.used = 2*pa + 1;
  2972. for (ix = 0; ix < pa; ix++) {
  2973. /* first calculate the digit at 2*ix */
  2974. /* calculate double precision result */
  2975. r = ((mp_word) t.dp[2*ix]) +
  2976. ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]);
  2977. /* store lower part in result */
  2978. t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
  2979. /* get the carry */
  2980. u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
  2981. /* left hand side of A[ix] * A[iy] */
  2982. tmpx = a->dp[ix];
  2983. /* alias for where to store the results */
  2984. tmpt = t.dp + (2*ix + 1);
  2985. for (iy = ix + 1; iy < pa; iy++) {
  2986. /* first calculate the product */
  2987. r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
  2988. /* now calculate the double precision result, note we use
  2989. * addition instead of *2 since it's easier to optimize
  2990. */
  2991. r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
  2992. /* store lower part */
  2993. *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
  2994. /* get carry */
  2995. u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
  2996. }
  2997. /* propagate upwards */
  2998. while (u != ((mp_digit) 0)) {
  2999. r = ((mp_word) *tmpt) + ((mp_word) u);
  3000. *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
  3001. u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
  3002. }
  3003. }
  3004. mp_clamp (&t);
  3005. mp_exch (&t, b);
  3006. mp_clear (&t);
  3007. return MP_OKAY;
  3008. }
  3009. /* multiplies |a| * |b| and only computes up to digs digits of result
  3010. * HAC pp. 595, Algorithm 14.12 Modified so you can control how
  3011. * many digits of output are created.
  3012. */
  3013. int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
  3014. {
  3015. mp_int t;
  3016. int res, pa, pb, ix, iy;
  3017. mp_digit u;
  3018. mp_word r;
  3019. mp_digit tmpx, *tmpt, *tmpy;
  3020. /* can we use the fast multiplier? */
  3021. if ((digs < (int)MP_WARRAY) &&
  3022. MIN (a->used, b->used) <
  3023. (1L << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  3024. return fast_s_mp_mul_digs (a, b, c, digs);
  3025. }
  3026. if ((res = mp_init_size (&t, digs)) != MP_OKAY) {
  3027. return res;
  3028. }
  3029. t.used = digs;
  3030. /* compute the digits of the product directly */
  3031. pa = a->used;
  3032. for (ix = 0; ix < pa; ix++) {
  3033. /* set the carry to zero */
  3034. u = 0;
  3035. /* limit ourselves to making digs digits of output */
  3036. pb = MIN (b->used, digs - ix);
  3037. /* setup some aliases */
  3038. /* copy of the digit from a used within the nested loop */
  3039. tmpx = a->dp[ix];
  3040. /* an alias for the destination shifted ix places */
  3041. tmpt = t.dp + ix;
  3042. /* an alias for the digits of b */
  3043. tmpy = b->dp;
  3044. /* compute the columns of the output and propagate the carry */
  3045. for (iy = 0; iy < pb; iy++) {
  3046. /* compute the column as a mp_word */
  3047. r = ((mp_word)*tmpt) +
  3048. ((mp_word)tmpx) * ((mp_word)*tmpy++) +
  3049. ((mp_word) u);
  3050. /* the new column is the lower part of the result */
  3051. *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
  3052. /* get the carry word from the result */
  3053. u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
  3054. }
  3055. /* set carry if it is placed below digs */
  3056. if (ix + iy < digs) {
  3057. *tmpt = u;
  3058. }
  3059. }
  3060. mp_clamp (&t);
  3061. mp_exch (&t, c);
  3062. mp_clear (&t);
  3063. return MP_OKAY;
  3064. }
  3065. /*
  3066. * shifts with subtractions when the result is greater than b.
  3067. *
  3068. * The method is slightly modified to shift B unconditionally up to just under
  3069. * the leading bit of b. This saves a lot of multiple precision shifting.
  3070. */
  3071. int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
  3072. {
  3073. int x, bits, res;
  3074. /* how many bits of last digit does b use */
  3075. bits = mp_count_bits (b) % DIGIT_BIT;
  3076. if (b->used > 1) {
  3077. if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1))
  3078. != MP_OKAY) {
  3079. return res;
  3080. }
  3081. } else {
  3082. if ((res = mp_set(a, 1)) != MP_OKAY) {
  3083. return res;
  3084. }
  3085. bits = 1;
  3086. }
  3087. /* now compute C = A * B mod b */
  3088. for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
  3089. if ((res = mp_mul_2 (a, a)) != MP_OKAY) {
  3090. return res;
  3091. }
  3092. if (mp_cmp_mag (a, b) != MP_LT) {
  3093. if ((res = s_mp_sub (a, b, a)) != MP_OKAY) {
  3094. return res;
  3095. }
  3096. }
  3097. }
  3098. return MP_OKAY;
  3099. }
  3100. #ifdef MP_LOW_MEM
  3101. #define TAB_SIZE 32
  3102. #else
  3103. #define TAB_SIZE 256
  3104. #endif
  3105. int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
  3106. {
  3107. mp_int M[TAB_SIZE], res, mu;
  3108. mp_digit buf;
  3109. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  3110. int (*redux)(mp_int*,mp_int*,mp_int*);
  3111. /* find window size */
  3112. x = mp_count_bits (X);
  3113. if (x <= 7) {
  3114. winsize = 2;
  3115. } else if (x <= 36) {
  3116. winsize = 3;
  3117. } else if (x <= 140) {
  3118. winsize = 4;
  3119. } else if (x <= 450) {
  3120. winsize = 5;
  3121. } else if (x <= 1303) {
  3122. winsize = 6;
  3123. } else if (x <= 3529) {
  3124. winsize = 7;
  3125. } else {
  3126. winsize = 8;
  3127. }
  3128. #ifdef MP_LOW_MEM
  3129. if (winsize > 5) {
  3130. winsize = 5;
  3131. }
  3132. #endif
  3133. /* init M array */
  3134. /* init first cell */
  3135. if ((err = mp_init(&M[1])) != MP_OKAY) {
  3136. return err;
  3137. }
  3138. /* now init the second half of the array */
  3139. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  3140. if ((err = mp_init(&M[x])) != MP_OKAY) {
  3141. for (y = 1<<(winsize-1); y < x; y++) {
  3142. mp_clear (&M[y]);
  3143. }
  3144. mp_clear(&M[1]);
  3145. return err;
  3146. }
  3147. }
  3148. /* create mu, used for Barrett reduction */
  3149. if ((err = mp_init (&mu)) != MP_OKAY) {
  3150. goto LBL_M;
  3151. }
  3152. if (redmode == 0) {
  3153. if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
  3154. goto LBL_MU;
  3155. }
  3156. redux = mp_reduce;
  3157. } else {
  3158. if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
  3159. goto LBL_MU;
  3160. }
  3161. redux = mp_reduce_2k_l;
  3162. }
  3163. /* create M table
  3164. *
  3165. * The M table contains powers of the base,
  3166. * e.g. M[x] = G**x mod P
  3167. *
  3168. * The first half of the table is not
  3169. * computed though accept for M[0] and M[1]
  3170. */
  3171. if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
  3172. goto LBL_MU;
  3173. }
  3174. /* compute the value at M[1<<(winsize-1)] by squaring
  3175. * M[1] (winsize-1) times
  3176. */
  3177. if ((err = mp_copy (&M[1], &M[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
  3178. goto LBL_MU;
  3179. }
  3180. for (x = 0; x < (winsize - 1); x++) {
  3181. /* square it */
  3182. if ((err = mp_sqr (&M[(mp_digit)(1 << (winsize - 1))],
  3183. &M[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
  3184. goto LBL_MU;
  3185. }
  3186. /* reduce modulo P */
  3187. if ((err = redux (&M[(mp_digit)(1 << (winsize - 1))], P, &mu)) != MP_OKAY) {
  3188. goto LBL_MU;
  3189. }
  3190. }
  3191. /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
  3192. * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
  3193. */
  3194. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  3195. if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
  3196. goto LBL_MU;
  3197. }
  3198. if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
  3199. goto LBL_MU;
  3200. }
  3201. }
  3202. /* setup result */
  3203. if ((err = mp_init (&res)) != MP_OKAY) {
  3204. goto LBL_MU;
  3205. }
  3206. if ((err = mp_set (&res, 1)) != MP_OKAY) {
  3207. goto LBL_MU;
  3208. }
  3209. /* set initial mode and bit cnt */
  3210. mode = 0;
  3211. bitcnt = 1;
  3212. buf = 0;
  3213. digidx = X->used - 1;
  3214. bitcpy = 0;
  3215. bitbuf = 0;
  3216. for (;;) {
  3217. /* grab next digit as required */
  3218. if (--bitcnt == 0) {
  3219. /* if digidx == -1 we are out of digits */
  3220. if (digidx == -1) {
  3221. break;
  3222. }
  3223. /* read next digit and reset the bitcnt */
  3224. buf = X->dp[digidx--];
  3225. bitcnt = (int) DIGIT_BIT;
  3226. }
  3227. /* grab the next msb from the exponent */
  3228. y = (int)(buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
  3229. buf <<= (mp_digit)1;
  3230. /* if the bit is zero and mode == 0 then we ignore it
  3231. * These represent the leading zero bits before the first 1 bit
  3232. * in the exponent. Technically this opt is not required but it
  3233. * does lower the # of trivial squaring/reductions used
  3234. */
  3235. if (mode == 0 && y == 0) {
  3236. continue;
  3237. }
  3238. /* if the bit is zero and mode == 1 then we square */
  3239. if (mode == 1 && y == 0) {
  3240. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  3241. goto LBL_RES;
  3242. }
  3243. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  3244. goto LBL_RES;
  3245. }
  3246. continue;
  3247. }
  3248. /* else we add it to the window */
  3249. bitbuf |= (y << (winsize - ++bitcpy));
  3250. mode = 2;
  3251. if (bitcpy == winsize) {
  3252. /* ok window is filled so square as required and multiply */
  3253. /* square first */
  3254. for (x = 0; x < winsize; x++) {
  3255. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  3256. goto LBL_RES;
  3257. }
  3258. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  3259. goto LBL_RES;
  3260. }
  3261. }
  3262. /* then multiply */
  3263. if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
  3264. goto LBL_RES;
  3265. }
  3266. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  3267. goto LBL_RES;
  3268. }
  3269. /* empty window and reset */
  3270. bitcpy = 0;
  3271. bitbuf = 0;
  3272. mode = 1;
  3273. }
  3274. }
  3275. /* if bits remain then square/multiply */
  3276. if (mode == 2 && bitcpy > 0) {
  3277. /* square then multiply if the bit is set */
  3278. for (x = 0; x < bitcpy; x++) {
  3279. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  3280. goto LBL_RES;
  3281. }
  3282. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  3283. goto LBL_RES;
  3284. }
  3285. bitbuf <<= 1;
  3286. if ((bitbuf & (1 << winsize)) != 0) {
  3287. /* then multiply */
  3288. if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
  3289. goto LBL_RES;
  3290. }
  3291. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  3292. goto LBL_RES;
  3293. }
  3294. }
  3295. }
  3296. }
  3297. mp_exch (&res, Y);
  3298. err = MP_OKAY;
  3299. LBL_RES:mp_clear (&res);
  3300. LBL_MU:mp_clear (&mu);
  3301. LBL_M:
  3302. mp_clear(&M[1]);
  3303. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  3304. mp_clear (&M[x]);
  3305. }
  3306. return err;
  3307. }
  3308. /* pre-calculate the value required for Barrett reduction
  3309. * For a given modulus "b" it calculates the value required in "a"
  3310. */
  3311. int mp_reduce_setup (mp_int * a, mp_int * b)
  3312. {
  3313. int res;
  3314. if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
  3315. return res;
  3316. }
  3317. return mp_div (a, b, a, NULL);
  3318. }
  3319. /* reduces x mod m, assumes 0 < x < m**2, mu is
  3320. * precomputed via mp_reduce_setup.
  3321. * From HAC pp.604 Algorithm 14.42
  3322. */
  3323. int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
  3324. {
  3325. mp_int q;
  3326. int res, um = m->used;
  3327. /* q = x */
  3328. if ((res = mp_init_copy (&q, x)) != MP_OKAY) {
  3329. return res;
  3330. }
  3331. /* q1 = x / b**(k-1) */
  3332. mp_rshd (&q, um - 1);
  3333. /* according to HAC this optimization is ok */
  3334. if (((mp_word) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
  3335. if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) {
  3336. goto CLEANUP;
  3337. }
  3338. } else {
  3339. #ifdef BN_S_MP_MUL_HIGH_DIGS_C
  3340. if ((res = s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
  3341. goto CLEANUP;
  3342. }
  3343. #elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
  3344. if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
  3345. goto CLEANUP;
  3346. }
  3347. #else
  3348. {
  3349. res = MP_VAL;
  3350. goto CLEANUP;
  3351. }
  3352. #endif
  3353. }
  3354. /* q3 = q2 / b**(k+1) */
  3355. mp_rshd (&q, um + 1);
  3356. /* x = x mod b**(k+1), quick (no division) */
  3357. if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
  3358. goto CLEANUP;
  3359. }
  3360. /* q = q * m mod b**(k+1), quick (no division) */
  3361. if ((res = s_mp_mul_digs (&q, m, &q, um + 1)) != MP_OKAY) {
  3362. goto CLEANUP;
  3363. }
  3364. /* x = x - q */
  3365. if ((res = mp_sub (x, &q, x)) != MP_OKAY) {
  3366. goto CLEANUP;
  3367. }
  3368. /* If x < 0, add b**(k+1) to it */
  3369. if (mp_cmp_d (x, 0) == MP_LT) {
  3370. if ((res = mp_set (&q, 1)) != MP_OKAY)
  3371. goto CLEANUP;
  3372. if ((res = mp_lshd (&q, um + 1)) != MP_OKAY)
  3373. goto CLEANUP;
  3374. if ((res = mp_add (x, &q, x)) != MP_OKAY)
  3375. goto CLEANUP;
  3376. }
  3377. /* Back off if it's too big */
  3378. while (mp_cmp (x, m) != MP_LT) {
  3379. if ((res = s_mp_sub (x, m, x)) != MP_OKAY) {
  3380. goto CLEANUP;
  3381. }
  3382. }
  3383. CLEANUP:
  3384. mp_clear (&q);
  3385. return res;
  3386. }
  3387. /* reduces a modulo n where n is of the form 2**p - d
  3388. This differs from reduce_2k since "d" can be larger
  3389. than a single digit.
  3390. */
  3391. int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
  3392. {
  3393. mp_int q;
  3394. int p, res;
  3395. if ((res = mp_init(&q)) != MP_OKAY) {
  3396. return res;
  3397. }
  3398. p = mp_count_bits(n);
  3399. top:
  3400. /* q = a/2**p, a = a mod 2**p */
  3401. if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
  3402. goto ERR;
  3403. }
  3404. /* q = q * d */
  3405. if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
  3406. goto ERR;
  3407. }
  3408. /* a = a + q */
  3409. if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
  3410. goto ERR;
  3411. }
  3412. if (mp_cmp_mag(a, n) != MP_LT) {
  3413. if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
  3414. goto ERR;
  3415. }
  3416. goto top;
  3417. }
  3418. ERR:
  3419. mp_clear(&q);
  3420. return res;
  3421. }
  3422. /* determines the setup value */
  3423. int mp_reduce_2k_setup_l(mp_int *a, mp_int *d)
  3424. {
  3425. int res;
  3426. mp_int tmp;
  3427. if ((res = mp_init(&tmp)) != MP_OKAY) {
  3428. return res;
  3429. }
  3430. if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
  3431. goto ERR;
  3432. }
  3433. if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
  3434. goto ERR;
  3435. }
  3436. ERR:
  3437. mp_clear(&tmp);
  3438. return res;
  3439. }
  3440. /* multiplies |a| * |b| and does not compute the lower digs digits
  3441. * [meant to get the higher part of the product]
  3442. */
  3443. int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
  3444. {
  3445. mp_int t;
  3446. int res, pa, pb, ix, iy;
  3447. mp_digit u;
  3448. mp_word r;
  3449. mp_digit tmpx, *tmpt, *tmpy;
  3450. /* can we use the fast multiplier? */
  3451. #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
  3452. if (((a->used + b->used + 1) < (int)MP_WARRAY)
  3453. && MIN (a->used, b->used) <
  3454. (1L << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  3455. return fast_s_mp_mul_high_digs (a, b, c, digs);
  3456. }
  3457. #endif
  3458. if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) {
  3459. return res;
  3460. }
  3461. t.used = a->used + b->used + 1;
  3462. pa = a->used;
  3463. pb = b->used;
  3464. for (ix = 0; ix < pa && a->dp; ix++) {
  3465. /* clear the carry */
  3466. u = 0;
  3467. /* left hand side of A[ix] * B[iy] */
  3468. tmpx = a->dp[ix];
  3469. /* alias to the address of where the digits will be stored */
  3470. tmpt = &(t.dp[digs]);
  3471. /* alias for where to read the right hand side from */
  3472. tmpy = b->dp + (digs - ix);
  3473. for (iy = digs - ix; iy < pb; iy++) {
  3474. /* calculate the double precision result */
  3475. r = ((mp_word)*tmpt) +
  3476. ((mp_word)tmpx) * ((mp_word)*tmpy++) +
  3477. ((mp_word) u);
  3478. /* get the lower part */
  3479. *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
  3480. /* carry the carry */
  3481. u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
  3482. }
  3483. *tmpt = u;
  3484. }
  3485. mp_clamp (&t);
  3486. mp_exch (&t, c);
  3487. mp_clear (&t);
  3488. return MP_OKAY;
  3489. }
  3490. /* this is a modified version of fast_s_mul_digs that only produces
  3491. * output digits *above* digs. See the comments for fast_s_mul_digs
  3492. * to see how it works.
  3493. *
  3494. * This is used in the Barrett reduction since for one of the multiplications
  3495. * only the higher digits were needed. This essentially halves the work.
  3496. *
  3497. * Based on Algorithm 14.12 on pp.595 of HAC.
  3498. */
  3499. int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
  3500. {
  3501. int olduse, res, pa, ix, iz;
  3502. #ifdef WOLFSSL_SMALL_STACK
  3503. mp_digit* W; /* uses dynamic memory and slower */
  3504. #else
  3505. mp_digit W[MP_WARRAY];
  3506. #endif
  3507. mp_word _W;
  3508. if (a->dp == NULL) { /* JRB, avoid reading uninitialized values */
  3509. return MP_VAL;
  3510. }
  3511. /* grow the destination as required */
  3512. pa = a->used + b->used;
  3513. if (c->alloc < pa) {
  3514. if ((res = mp_grow (c, pa)) != MP_OKAY) {
  3515. return res;
  3516. }
  3517. }
  3518. if (pa > (int)MP_WARRAY)
  3519. return MP_RANGE; /* TAO range check */
  3520. #ifdef WOLFSSL_SMALL_STACK
  3521. W = (mp_digit*)XMALLOC(sizeof(mp_digit) * pa, NULL, DYNAMIC_TYPE_BIGINT);
  3522. if (W == NULL)
  3523. return MP_MEM;
  3524. #endif
  3525. /* number of output digits to produce */
  3526. _W = 0;
  3527. for (ix = digs; ix < pa; ix++) { /* JRB, have a->dp check at top of function*/
  3528. int tx, ty, iy;
  3529. mp_digit *tmpx, *tmpy;
  3530. /* get offsets into the two bignums */
  3531. ty = MIN(b->used-1, ix);
  3532. tx = ix - ty;
  3533. /* setup temp aliases */
  3534. tmpx = a->dp + tx;
  3535. tmpy = b->dp + ty;
  3536. /* this is the number of times the loop will iterate, essentially its
  3537. while (tx++ < a->used && ty-- >= 0) { ... }
  3538. */
  3539. iy = MIN(a->used-tx, ty+1);
  3540. /* execute loop */
  3541. for (iz = 0; iz < iy; iz++) {
  3542. _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
  3543. }
  3544. /* store term */
  3545. W[ix] = (mp_digit)(((mp_digit)_W) & MP_MASK);
  3546. /* make next carry */
  3547. _W = _W >> ((mp_word)DIGIT_BIT);
  3548. }
  3549. /* setup dest */
  3550. olduse = c->used;
  3551. c->used = pa;
  3552. {
  3553. mp_digit *tmpc;
  3554. tmpc = c->dp + digs;
  3555. for (ix = digs; ix < pa; ix++) { /* TAO, <= could potentially overwrite */
  3556. /* now extract the previous digit [below the carry] */
  3557. *tmpc++ = W[ix];
  3558. }
  3559. /* clear unused digits [that existed in the old copy of c] */
  3560. for (; ix < olduse; ix++) {
  3561. *tmpc++ = 0;
  3562. }
  3563. }
  3564. mp_clamp (c);
  3565. #ifdef WOLFSSL_SMALL_STACK
  3566. XFREE(W, NULL, DYNAMIC_TYPE_BIGINT);
  3567. #endif
  3568. return MP_OKAY;
  3569. }
  3570. #ifndef MP_SET_CHUNK_BITS
  3571. #define MP_SET_CHUNK_BITS 4
  3572. #endif
  3573. int mp_set_int (mp_int * a, unsigned long b)
  3574. {
  3575. int x, res;
  3576. /* use direct mp_set if b is less than mp_digit max */
  3577. if (b < MP_DIGIT_MAX) {
  3578. return mp_set (a, (mp_digit)b);
  3579. }
  3580. mp_zero (a);
  3581. /* set chunk bits at a time */
  3582. for (x = 0; x < (int)(sizeof(b) * 8) / MP_SET_CHUNK_BITS; x++) {
  3583. /* shift the number up chunk bits */
  3584. if ((res = mp_mul_2d (a, MP_SET_CHUNK_BITS, a)) != MP_OKAY) {
  3585. return res;
  3586. }
  3587. /* OR in the top bits of the source */
  3588. a->dp[0] |= (b >> ((sizeof(b) * 8) - MP_SET_CHUNK_BITS)) &
  3589. ((1 << MP_SET_CHUNK_BITS) - 1);
  3590. /* shift the source up to the next chunk bits */
  3591. b <<= MP_SET_CHUNK_BITS;
  3592. /* ensure that digits are not clamped off */
  3593. a->used += 1;
  3594. }
  3595. mp_clamp (a);
  3596. return MP_OKAY;
  3597. }
  3598. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_ECC) || !defined(NO_RSA) || \
  3599. !defined(NO_DSA) | !defined(NO_DH)
  3600. /* c = a * a (mod b) */
  3601. int mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
  3602. {
  3603. int res;
  3604. mp_int t;
  3605. if ((res = mp_init (&t)) != MP_OKAY) {
  3606. return res;
  3607. }
  3608. if ((res = mp_sqr (a, &t)) != MP_OKAY) {
  3609. mp_clear (&t);
  3610. return res;
  3611. }
  3612. res = mp_mod (&t, b, c);
  3613. mp_clear (&t);
  3614. return res;
  3615. }
  3616. #endif
  3617. #if defined(HAVE_ECC) || !defined(NO_PWDBASED) || defined(WOLFSSL_SNIFFER) || \
  3618. defined(WOLFSSL_HAVE_WOLFSCEP) || defined(WOLFSSL_KEY_GEN) || \
  3619. defined(OPENSSL_EXTRA) || defined(WC_RSA_BLINDING) || \
  3620. (!defined(NO_RSA) && !defined(NO_RSA_BOUNDS_CHECK))
  3621. /* single digit addition */
  3622. int mp_add_d (mp_int* a, mp_digit b, mp_int* c) /* //NOLINT(misc-no-recursion) */
  3623. {
  3624. int res, ix, oldused;
  3625. mp_digit *tmpa, *tmpc, mu;
  3626. if (b > MP_DIGIT_MAX) return MP_VAL;
  3627. /* grow c as required */
  3628. if (c->alloc < a->used + 1) {
  3629. if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
  3630. return res;
  3631. }
  3632. }
  3633. /* if a is negative and |a| >= b, call c = |a| - b */
  3634. if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) {
  3635. /* temporarily fix sign of a */
  3636. a->sign = MP_ZPOS;
  3637. /* c = |a| - b */
  3638. res = mp_sub_d(a, b, c);
  3639. /* fix sign */
  3640. a->sign = c->sign = MP_NEG;
  3641. /* clamp */
  3642. mp_clamp(c);
  3643. return res;
  3644. }
  3645. /* old number of used digits in c */
  3646. oldused = c->used;
  3647. /* sign always positive */
  3648. c->sign = MP_ZPOS;
  3649. /* source alias */
  3650. tmpa = a->dp;
  3651. /* destination alias */
  3652. tmpc = c->dp;
  3653. if (tmpa == NULL || tmpc == NULL) {
  3654. return MP_MEM;
  3655. }
  3656. /* if a is positive */
  3657. if (a->sign == MP_ZPOS) {
  3658. /* add digit, after this we're propagating
  3659. * the carry.
  3660. */
  3661. *tmpc = *tmpa++ + b;
  3662. mu = *tmpc >> DIGIT_BIT;
  3663. *tmpc++ &= MP_MASK;
  3664. /* now handle rest of the digits */
  3665. for (ix = 1; ix < a->used; ix++) {
  3666. *tmpc = *tmpa++ + mu;
  3667. mu = *tmpc >> DIGIT_BIT;
  3668. *tmpc++ &= MP_MASK;
  3669. }
  3670. /* set final carry */
  3671. if (ix < c->alloc) {
  3672. ix++;
  3673. *tmpc++ = mu;
  3674. }
  3675. /* setup size */
  3676. c->used = a->used + 1;
  3677. } else {
  3678. /* a was negative and |a| < b */
  3679. c->used = 1;
  3680. /* the result is a single digit */
  3681. if (a->used == 1) {
  3682. *tmpc++ = b - a->dp[0];
  3683. } else {
  3684. *tmpc++ = b;
  3685. }
  3686. /* setup count so the clearing of oldused
  3687. * can fall through correctly
  3688. */
  3689. ix = 1;
  3690. }
  3691. /* now zero to oldused */
  3692. while (ix++ < oldused) {
  3693. *tmpc++ = 0;
  3694. }
  3695. mp_clamp(c);
  3696. return MP_OKAY;
  3697. }
  3698. /* single digit subtraction */
  3699. int mp_sub_d (mp_int * a, mp_digit b, mp_int * c) /* //NOLINT(misc-no-recursion) */
  3700. {
  3701. mp_digit *tmpa, *tmpc, mu;
  3702. int res, ix, oldused;
  3703. if (b > MP_MASK) return MP_VAL;
  3704. /* grow c as required */
  3705. if (c->alloc < a->used + 1) {
  3706. if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
  3707. return res;
  3708. }
  3709. }
  3710. /* if a is negative just do an unsigned
  3711. * addition [with fudged signs]
  3712. */
  3713. if (a->sign == MP_NEG) {
  3714. a->sign = MP_ZPOS;
  3715. res = mp_add_d(a, b, c);
  3716. a->sign = c->sign = MP_NEG;
  3717. /* clamp */
  3718. mp_clamp(c);
  3719. return res;
  3720. }
  3721. /* setup regs */
  3722. oldused = c->used;
  3723. tmpa = a->dp;
  3724. tmpc = c->dp;
  3725. if (tmpa == NULL || tmpc == NULL) {
  3726. return MP_MEM;
  3727. }
  3728. /* if a <= b simply fix the single digit */
  3729. if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) {
  3730. if (a->used == 1) {
  3731. *tmpc++ = b - *tmpa;
  3732. } else {
  3733. *tmpc++ = b;
  3734. }
  3735. ix = 1;
  3736. /* negative/1digit */
  3737. c->sign = MP_NEG;
  3738. c->used = 1;
  3739. } else {
  3740. /* positive/size */
  3741. c->sign = MP_ZPOS;
  3742. c->used = a->used;
  3743. /* subtract first digit */
  3744. *tmpc = *tmpa++ - b;
  3745. mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1);
  3746. *tmpc++ &= MP_MASK;
  3747. /* handle rest of the digits */
  3748. for (ix = 1; ix < a->used; ix++) {
  3749. *tmpc = *tmpa++ - mu;
  3750. mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1);
  3751. *tmpc++ &= MP_MASK;
  3752. }
  3753. }
  3754. /* zero excess digits */
  3755. while (ix++ < oldused) {
  3756. *tmpc++ = 0;
  3757. }
  3758. mp_clamp(c);
  3759. return MP_OKAY;
  3760. }
  3761. #endif /* defined(HAVE_ECC) || !defined(NO_PWDBASED) */
  3762. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || defined(HAVE_ECC) || \
  3763. defined(DEBUG_WOLFSSL) || !defined(NO_RSA) || !defined(NO_DSA) || \
  3764. !defined(NO_DH) || defined(WC_MP_TO_RADIX)
  3765. static const int lnz[16] = {
  3766. 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  3767. };
  3768. /* Counts the number of lsbs which are zero before the first zero bit */
  3769. int mp_cnt_lsb(mp_int *a)
  3770. {
  3771. int x;
  3772. mp_digit q = 0, qq;
  3773. /* easy out */
  3774. if (mp_iszero(a) == MP_YES) {
  3775. return 0;
  3776. }
  3777. /* scan lower digits until non-zero */
  3778. for (x = 0; x < a->used && a->dp[x] == 0; x++) {}
  3779. if (a->dp)
  3780. q = a->dp[x];
  3781. x *= DIGIT_BIT;
  3782. /* now scan this digit until a 1 is found */
  3783. if ((q & 1) == 0) {
  3784. do {
  3785. qq = q & 15;
  3786. x += lnz[qq];
  3787. q >>= 4;
  3788. } while (qq == 0);
  3789. }
  3790. return x;
  3791. }
  3792. static int s_is_power_of_two(mp_digit b, int *p)
  3793. {
  3794. int x;
  3795. /* fast return if no power of two */
  3796. if ((b==0) || (b & (b-1))) {
  3797. return 0;
  3798. }
  3799. for (x = 0; x < DIGIT_BIT; x++) {
  3800. if (b == (((mp_digit)1)<<x)) {
  3801. *p = x;
  3802. return 1;
  3803. }
  3804. }
  3805. return 0;
  3806. }
  3807. /* single digit division (based on routine from MPI) */
  3808. static int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
  3809. {
  3810. mp_int q;
  3811. mp_word w;
  3812. mp_digit t;
  3813. int res = MP_OKAY, ix;
  3814. /* cannot divide by zero */
  3815. if (b == 0) {
  3816. return MP_VAL;
  3817. }
  3818. /* quick outs */
  3819. if (b == 1 || mp_iszero(a) == MP_YES) {
  3820. if (d != NULL) {
  3821. *d = 0;
  3822. }
  3823. if (c != NULL) {
  3824. return mp_copy(a, c);
  3825. }
  3826. return MP_OKAY;
  3827. }
  3828. /* power of two ? */
  3829. if (s_is_power_of_two(b, &ix) == 1) {
  3830. if (d != NULL) {
  3831. *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
  3832. }
  3833. if (c != NULL) {
  3834. return mp_div_2d(a, ix, c, NULL);
  3835. }
  3836. return MP_OKAY;
  3837. }
  3838. #ifdef BN_MP_DIV_3_C
  3839. /* three? */
  3840. if (b == 3) {
  3841. return mp_div_3(a, c, d);
  3842. }
  3843. #endif
  3844. /* no easy answer [c'est la vie]. Just division */
  3845. if (c != NULL) {
  3846. if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
  3847. return res;
  3848. }
  3849. q.used = a->used;
  3850. q.sign = a->sign;
  3851. }
  3852. else {
  3853. if ((res = mp_init(&q)) != MP_OKAY) {
  3854. return res;
  3855. }
  3856. }
  3857. w = 0;
  3858. for (ix = a->used - 1; ix >= 0; ix--) {
  3859. w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
  3860. if (w >= b) {
  3861. #ifdef WOLFSSL_LINUXKM
  3862. t = (mp_digit)w;
  3863. /* Linux kernel macro for in-place 64 bit integer division. */
  3864. do_div(t, b);
  3865. #else
  3866. t = (mp_digit)(w / b);
  3867. #endif
  3868. w -= ((mp_word)t) * ((mp_word)b);
  3869. } else {
  3870. t = 0;
  3871. }
  3872. if (c != NULL)
  3873. q.dp[ix] = (mp_digit)t;
  3874. }
  3875. if (d != NULL) {
  3876. *d = (mp_digit)w;
  3877. }
  3878. if (c != NULL) {
  3879. mp_clamp(&q);
  3880. mp_exch(&q, c);
  3881. }
  3882. mp_clear(&q);
  3883. return res;
  3884. }
  3885. int mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
  3886. {
  3887. return mp_div_d(a, b, NULL, c);
  3888. }
  3889. #endif /* WOLFSSL_KEY_GEN || HAVE_COMP_KEY || HAVE_ECC || DEBUG_WOLFSSL */
  3890. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)) || !defined(NO_DH) || !defined(NO_DSA)
  3891. const FLASH_QUALIFIER mp_digit ltm_prime_tab[PRIME_SIZE] = {
  3892. 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
  3893. 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
  3894. 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
  3895. 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F,
  3896. #ifndef MP_8BIT
  3897. 0x0083,
  3898. 0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD,
  3899. 0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF,
  3900. 0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107,
  3901. 0x010D, 0x010F, 0x0115, 0x0119, 0x011B, 0x0125, 0x0133, 0x0137,
  3902. 0x0139, 0x013D, 0x014B, 0x0151, 0x015B, 0x015D, 0x0161, 0x0167,
  3903. 0x016F, 0x0175, 0x017B, 0x017F, 0x0185, 0x018D, 0x0191, 0x0199,
  3904. 0x01A3, 0x01A5, 0x01AF, 0x01B1, 0x01B7, 0x01BB, 0x01C1, 0x01C9,
  3905. 0x01CD, 0x01CF, 0x01D3, 0x01DF, 0x01E7, 0x01EB, 0x01F3, 0x01F7,
  3906. 0x01FD, 0x0209, 0x020B, 0x021D, 0x0223, 0x022D, 0x0233, 0x0239,
  3907. 0x023B, 0x0241, 0x024B, 0x0251, 0x0257, 0x0259, 0x025F, 0x0265,
  3908. 0x0269, 0x026B, 0x0277, 0x0281, 0x0283, 0x0287, 0x028D, 0x0293,
  3909. 0x0295, 0x02A1, 0x02A5, 0x02AB, 0x02B3, 0x02BD, 0x02C5, 0x02CF,
  3910. 0x02D7, 0x02DD, 0x02E3, 0x02E7, 0x02EF, 0x02F5, 0x02F9, 0x0301,
  3911. 0x0305, 0x0313, 0x031D, 0x0329, 0x032B, 0x0335, 0x0337, 0x033B,
  3912. 0x033D, 0x0347, 0x0355, 0x0359, 0x035B, 0x035F, 0x036D, 0x0371,
  3913. 0x0373, 0x0377, 0x038B, 0x038F, 0x0397, 0x03A1, 0x03A9, 0x03AD,
  3914. 0x03B3, 0x03B9, 0x03C7, 0x03CB, 0x03D1, 0x03D7, 0x03DF, 0x03E5,
  3915. 0x03F1, 0x03F5, 0x03FB, 0x03FD, 0x0407, 0x0409, 0x040F, 0x0419,
  3916. 0x041B, 0x0425, 0x0427, 0x042D, 0x043F, 0x0443, 0x0445, 0x0449,
  3917. 0x044F, 0x0455, 0x045D, 0x0463, 0x0469, 0x047F, 0x0481, 0x048B,
  3918. 0x0493, 0x049D, 0x04A3, 0x04A9, 0x04B1, 0x04BD, 0x04C1, 0x04C7,
  3919. 0x04CD, 0x04CF, 0x04D5, 0x04E1, 0x04EB, 0x04FD, 0x04FF, 0x0503,
  3920. 0x0509, 0x050B, 0x0511, 0x0515, 0x0517, 0x051B, 0x0527, 0x0529,
  3921. 0x052F, 0x0551, 0x0557, 0x055D, 0x0565, 0x0577, 0x0581, 0x058F,
  3922. 0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
  3923. 0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
  3924. 0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
  3925. 0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
  3926. #endif
  3927. };
  3928. /* Miller-Rabin test of "a" to the base of "b" as described in
  3929. * HAC pp. 139 Algorithm 4.24
  3930. *
  3931. * Sets result to 0 if definitely composite or 1 if probably prime.
  3932. * Randomly the chance of error is no more than 1/4 and often
  3933. * very much lower.
  3934. */
  3935. static int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
  3936. {
  3937. mp_int n1, y, r;
  3938. int s, j, err;
  3939. /* default */
  3940. *result = MP_NO;
  3941. /* ensure b > 1 */
  3942. if (mp_cmp_d(b, 1) != MP_GT) {
  3943. return MP_VAL;
  3944. }
  3945. /* get n1 = a - 1 */
  3946. if ((err = mp_init_copy (&n1, a)) != MP_OKAY) {
  3947. return err;
  3948. }
  3949. if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) {
  3950. goto LBL_N1;
  3951. }
  3952. /* set 2**s * r = n1 */
  3953. if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) {
  3954. goto LBL_N1;
  3955. }
  3956. /* count the number of least significant bits
  3957. * which are zero
  3958. */
  3959. s = mp_cnt_lsb(&r);
  3960. /* now divide n - 1 by 2**s */
  3961. if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) {
  3962. goto LBL_R;
  3963. }
  3964. /* compute y = b**r mod a */
  3965. if ((err = mp_init (&y)) != MP_OKAY) {
  3966. goto LBL_R;
  3967. }
  3968. #if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
  3969. #ifndef WOLFSSL_SP_NO_2048
  3970. if (mp_count_bits(a) == 1024 && mp_isodd(a))
  3971. err = sp_ModExp_1024(b, &r, a, &y);
  3972. else if (mp_count_bits(a) == 2048 && mp_isodd(a))
  3973. err = sp_ModExp_2048(b, &r, a, &y);
  3974. else
  3975. #endif
  3976. #ifndef WOLFSSL_SP_NO_3072
  3977. if (mp_count_bits(a) == 1536 && mp_isodd(a))
  3978. err = sp_ModExp_1536(b, &r, a, &y);
  3979. else if (mp_count_bits(a) == 3072 && mp_isodd(a))
  3980. err = sp_ModExp_3072(b, &r, a, &y);
  3981. else
  3982. #endif
  3983. #ifdef WOLFSSL_SP_4096
  3984. if (mp_count_bits(a) == 4096 && mp_isodd(a))
  3985. err = sp_ModExp_4096(b, &r, a, &y);
  3986. else
  3987. #endif
  3988. #endif
  3989. err = mp_exptmod (b, &r, a, &y);
  3990. if (err != MP_OKAY)
  3991. goto LBL_Y;
  3992. /* if y != 1 and y != n1 do */
  3993. if (mp_cmp_d (&y, 1) != MP_EQ && mp_cmp (&y, &n1) != MP_EQ) {
  3994. j = 1;
  3995. /* while j <= s-1 and y != n1 */
  3996. while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) {
  3997. if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) {
  3998. goto LBL_Y;
  3999. }
  4000. /* if y == 1 then composite */
  4001. if (mp_cmp_d (&y, 1) == MP_EQ) {
  4002. goto LBL_Y;
  4003. }
  4004. ++j;
  4005. }
  4006. /* if y != n1 then composite */
  4007. if (mp_cmp (&y, &n1) != MP_EQ) {
  4008. goto LBL_Y;
  4009. }
  4010. }
  4011. /* probably prime now */
  4012. *result = MP_YES;
  4013. LBL_Y:mp_clear (&y);
  4014. LBL_R:mp_clear (&r);
  4015. LBL_N1:mp_clear (&n1);
  4016. return err;
  4017. }
  4018. /* determines if an integers is divisible by one
  4019. * of the first PRIME_SIZE primes or not
  4020. *
  4021. * sets result to 0 if not, 1 if yes
  4022. */
  4023. static int mp_prime_is_divisible (mp_int * a, int *result)
  4024. {
  4025. int err, ix;
  4026. mp_digit res;
  4027. /* default to not */
  4028. *result = MP_NO;
  4029. for (ix = 0; ix < PRIME_SIZE; ix++) {
  4030. /* what is a mod LBL_prime_tab[ix] */
  4031. if ((err = mp_mod_d (a, ltm_prime_tab[ix], &res)) != MP_OKAY) {
  4032. return err;
  4033. }
  4034. /* is the residue zero? */
  4035. if (res == 0) {
  4036. *result = MP_YES;
  4037. return MP_OKAY;
  4038. }
  4039. }
  4040. return MP_OKAY;
  4041. }
  4042. /*
  4043. * Sets result to 1 if probably prime, 0 otherwise
  4044. */
  4045. int mp_prime_is_prime (mp_int * a, int t, int *result)
  4046. {
  4047. mp_int b;
  4048. int ix, err, res;
  4049. /* default to no */
  4050. *result = MP_NO;
  4051. /* valid value of t? */
  4052. if (t <= 0 || t > PRIME_SIZE) {
  4053. return MP_VAL;
  4054. }
  4055. if (mp_isone(a)) {
  4056. *result = MP_NO;
  4057. return MP_OKAY;
  4058. }
  4059. /* is the input equal to one of the primes in the table? */
  4060. for (ix = 0; ix < PRIME_SIZE; ix++) {
  4061. if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {
  4062. *result = MP_YES;
  4063. return MP_OKAY;
  4064. }
  4065. }
  4066. /* first perform trial division */
  4067. if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) {
  4068. return err;
  4069. }
  4070. /* return if it was trivially divisible */
  4071. if (res == MP_YES) {
  4072. return MP_OKAY;
  4073. }
  4074. /* now perform the miller-rabin rounds */
  4075. if ((err = mp_init (&b)) != MP_OKAY) {
  4076. return err;
  4077. }
  4078. for (ix = 0; ix < t; ix++) {
  4079. /* set the prime */
  4080. if ((err = mp_set (&b, ltm_prime_tab[ix])) != MP_OKAY) {
  4081. goto LBL_B;
  4082. }
  4083. if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
  4084. goto LBL_B;
  4085. }
  4086. if (res == MP_NO) {
  4087. goto LBL_B;
  4088. }
  4089. }
  4090. /* passed the test */
  4091. *result = MP_YES;
  4092. LBL_B:mp_clear (&b);
  4093. return err;
  4094. }
  4095. /*
  4096. * Sets result to 1 if probably prime, 0 otherwise
  4097. */
  4098. int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG *rng)
  4099. {
  4100. mp_int b, c;
  4101. int ix, err, res;
  4102. byte* base = NULL;
  4103. word32 bitSz = 0;
  4104. word32 baseSz = 0;
  4105. /* default to no */
  4106. *result = MP_NO;
  4107. /* valid value of t? */
  4108. if (t <= 0 || t > PRIME_SIZE) {
  4109. return MP_VAL;
  4110. }
  4111. if (a->sign == MP_NEG) {
  4112. return MP_VAL;
  4113. }
  4114. if (mp_isone(a)) {
  4115. *result = MP_NO;
  4116. return MP_OKAY;
  4117. }
  4118. /* is the input equal to one of the primes in the table? */
  4119. for (ix = 0; ix < PRIME_SIZE; ix++) {
  4120. if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) {
  4121. *result = MP_YES;
  4122. return MP_OKAY;
  4123. }
  4124. }
  4125. /* first perform trial division */
  4126. if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) {
  4127. return err;
  4128. }
  4129. /* return if it was trivially divisible */
  4130. if (res == MP_YES) {
  4131. return MP_OKAY;
  4132. }
  4133. /* now perform the miller-rabin rounds */
  4134. if ((err = mp_init (&b)) != MP_OKAY) {
  4135. return err;
  4136. }
  4137. if ((err = mp_init (&c)) != MP_OKAY) {
  4138. mp_clear(&b);
  4139. return err;
  4140. }
  4141. bitSz = mp_count_bits(a);
  4142. baseSz = (bitSz / 8) + ((bitSz % 8) ? 1 : 0);
  4143. bitSz %= 8;
  4144. base = (byte*)XMALLOC(baseSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4145. if (base == NULL) {
  4146. err = MP_MEM;
  4147. goto LBL_B;
  4148. }
  4149. if ((err = mp_sub_d(a, 2, &c)) != MP_OKAY) {
  4150. goto LBL_B;
  4151. }
  4152. /* now do a miller rabin with up to t random numbers, this should
  4153. * give a (1/4)^t chance of a false prime. */
  4154. for (ix = 0; ix < t; ix++) {
  4155. /* Set a test candidate. */
  4156. if ((err = wc_RNG_GenerateBlock(rng, base, baseSz)) != 0) {
  4157. goto LBL_B;
  4158. }
  4159. /* Clear bits higher than those in a. */
  4160. if (bitSz > 0) {
  4161. base[0] &= (1 << bitSz) - 1;
  4162. }
  4163. if ((err = mp_read_unsigned_bin(&b, base, baseSz)) != MP_OKAY) {
  4164. goto LBL_B;
  4165. }
  4166. if (mp_cmp_d(&b, 2) != MP_GT || mp_cmp(&b, &c) != MP_LT) {
  4167. ix--;
  4168. continue;
  4169. }
  4170. if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
  4171. goto LBL_B;
  4172. }
  4173. if (res == MP_NO) {
  4174. goto LBL_B;
  4175. }
  4176. }
  4177. /* passed the test */
  4178. *result = MP_YES;
  4179. LBL_B:mp_clear (&b);
  4180. mp_clear (&c);
  4181. XFREE(base, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4182. return err;
  4183. }
  4184. #endif /* (WOLFSSL_KEY_GEN && !NO_RSA) || !NO_DH || !NO_DSA */
  4185. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_DH) || !defined(NO_DSA))
  4186. static const int USE_BBS = 1;
  4187. int mp_rand_prime(mp_int* a, int len, WC_RNG* rng, void* heap)
  4188. {
  4189. int err, res, type;
  4190. byte* buf;
  4191. if (a == NULL || rng == NULL)
  4192. return MP_VAL;
  4193. /* get type */
  4194. if (len < 0) {
  4195. type = USE_BBS;
  4196. len = -len;
  4197. } else {
  4198. type = 0;
  4199. }
  4200. /* allow sizes between 2 and 512 bytes for a prime size */
  4201. if (len < 2 || len > 512) {
  4202. return MP_VAL;
  4203. }
  4204. /* allocate buffer to work with */
  4205. buf = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_RSA);
  4206. if (buf == NULL) {
  4207. return MP_MEM;
  4208. }
  4209. XMEMSET(buf, 0, len);
  4210. do {
  4211. #ifdef SHOW_GEN
  4212. printf(".");
  4213. fflush(stdout);
  4214. #endif
  4215. /* generate value */
  4216. err = wc_RNG_GenerateBlock(rng, buf, len);
  4217. if (err != 0) {
  4218. XFREE(buf, heap, DYNAMIC_TYPE_RSA);
  4219. return err;
  4220. }
  4221. /* munge bits */
  4222. buf[0] |= 0x80 | 0x40;
  4223. buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00);
  4224. /* load value */
  4225. if ((err = mp_read_unsigned_bin(a, buf, len)) != MP_OKAY) {
  4226. XFREE(buf, heap, DYNAMIC_TYPE_RSA);
  4227. return err;
  4228. }
  4229. /* test */
  4230. /* Running Miller-Rabin up to 3 times gives us a 2^{-80} chance
  4231. * of a 1024-bit candidate being a false positive, when it is our
  4232. * prime candidate. (Note 4.49 of Handbook of Applied Cryptography.)
  4233. * Using 8 because we've always used 8. */
  4234. if ((err = mp_prime_is_prime_ex(a, 8, &res, rng)) != MP_OKAY) {
  4235. XFREE(buf, heap, DYNAMIC_TYPE_RSA);
  4236. return err;
  4237. }
  4238. } while (res == MP_NO);
  4239. XMEMSET(buf, 0, len);
  4240. XFREE(buf, heap, DYNAMIC_TYPE_RSA);
  4241. return MP_OKAY;
  4242. }
  4243. #endif
  4244. #if defined(WOLFSSL_KEY_GEN)
  4245. /* computes least common multiple as |a*b|/(a, b) */
  4246. int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
  4247. {
  4248. int res;
  4249. mp_int t1, t2;
  4250. /* LCM of 0 and any number is undefined as 0 is not in the set of values
  4251. * being used. */
  4252. if (mp_iszero (a) == MP_YES || mp_iszero (b) == MP_YES) {
  4253. return MP_VAL;
  4254. }
  4255. if ((res = mp_init_multi (&t1, &t2, NULL, NULL, NULL, NULL)) != MP_OKAY) {
  4256. return res;
  4257. }
  4258. /* t1 = get the GCD of the two inputs */
  4259. if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) {
  4260. goto LBL_T;
  4261. }
  4262. /* divide the smallest by the GCD */
  4263. if (mp_cmp_mag(a, b) == MP_LT) {
  4264. /* store quotient in t2 such that t2 * b is the LCM */
  4265. if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) {
  4266. goto LBL_T;
  4267. }
  4268. res = mp_mul(b, &t2, c);
  4269. } else {
  4270. /* store quotient in t2 such that t2 * a is the LCM */
  4271. if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) {
  4272. goto LBL_T;
  4273. }
  4274. res = mp_mul(a, &t2, c);
  4275. }
  4276. /* fix the sign to positive */
  4277. c->sign = MP_ZPOS;
  4278. LBL_T:
  4279. mp_clear(&t1);
  4280. mp_clear(&t2);
  4281. return res;
  4282. }
  4283. /* Greatest Common Divisor using the binary method */
  4284. int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
  4285. {
  4286. mp_int u, v;
  4287. int k, u_lsb, v_lsb, res;
  4288. /* either zero than gcd is the largest */
  4289. if (mp_iszero (a) == MP_YES) {
  4290. /* GCD of 0 and 0 is undefined as all integers divide 0. */
  4291. if (mp_iszero (b) == MP_YES) {
  4292. return MP_VAL;
  4293. }
  4294. return mp_abs (b, c);
  4295. }
  4296. if (mp_iszero (b) == MP_YES) {
  4297. return mp_abs (a, c);
  4298. }
  4299. /* get copies of a and b we can modify */
  4300. if ((res = mp_init_copy (&u, a)) != MP_OKAY) {
  4301. return res;
  4302. }
  4303. if ((res = mp_init_copy (&v, b)) != MP_OKAY) {
  4304. goto LBL_U;
  4305. }
  4306. /* must be positive for the remainder of the algorithm */
  4307. u.sign = v.sign = MP_ZPOS;
  4308. /* B1. Find the common power of two for u and v */
  4309. u_lsb = mp_cnt_lsb(&u);
  4310. v_lsb = mp_cnt_lsb(&v);
  4311. k = MIN(u_lsb, v_lsb);
  4312. if (k > 0) {
  4313. /* divide the power of two out */
  4314. if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) {
  4315. goto LBL_V;
  4316. }
  4317. if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) {
  4318. goto LBL_V;
  4319. }
  4320. }
  4321. /* divide any remaining factors of two out */
  4322. if (u_lsb != k) {
  4323. if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) {
  4324. goto LBL_V;
  4325. }
  4326. }
  4327. if (v_lsb != k) {
  4328. if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) {
  4329. goto LBL_V;
  4330. }
  4331. }
  4332. while (mp_iszero(&v) == MP_NO) {
  4333. /* make sure v is the largest */
  4334. if (mp_cmp_mag(&u, &v) == MP_GT) {
  4335. /* swap u and v to make sure v is >= u */
  4336. mp_exch(&u, &v);
  4337. }
  4338. /* subtract smallest from largest */
  4339. if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) {
  4340. goto LBL_V;
  4341. }
  4342. /* Divide out all factors of two */
  4343. if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) {
  4344. goto LBL_V;
  4345. }
  4346. }
  4347. /* multiply by 2**k which we divided out at the beginning */
  4348. if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) {
  4349. goto LBL_V;
  4350. }
  4351. c->sign = MP_ZPOS;
  4352. res = MP_OKAY;
  4353. LBL_V:mp_clear (&v);
  4354. LBL_U:mp_clear (&u);
  4355. return res;
  4356. }
  4357. #endif /* WOLFSSL_KEY_GEN */
  4358. #if !defined(NO_DSA) || defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || \
  4359. defined(HAVE_COMP_KEY) || defined(WOLFSSL_DEBUG_MATH) || \
  4360. defined(DEBUG_WOLFSSL) || defined(OPENSSL_EXTRA) || defined(WC_MP_TO_RADIX)
  4361. /* chars used in radix conversions */
  4362. const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  4363. "abcdefghijklmnopqrstuvwxyz+/";
  4364. #endif
  4365. #if !defined(NO_DSA) || defined(HAVE_ECC) || defined(OPENSSL_EXTRA)
  4366. /* read a string [ASCII] in a given radix */
  4367. int mp_read_radix (mp_int * a, const char *str, int radix)
  4368. {
  4369. int y, res, neg;
  4370. char ch;
  4371. /* zero the digit bignum */
  4372. mp_zero(a);
  4373. /* make sure the radix is ok */
  4374. if (radix < MP_RADIX_BIN || radix > MP_RADIX_MAX) {
  4375. return MP_VAL;
  4376. }
  4377. /* if the leading digit is a
  4378. * minus set the sign to negative.
  4379. */
  4380. if (*str == '-') {
  4381. ++str;
  4382. neg = MP_NEG;
  4383. } else {
  4384. neg = MP_ZPOS;
  4385. }
  4386. /* set the integer to the default of zero */
  4387. mp_zero (a);
  4388. /* process each digit of the string */
  4389. while (*str != '\0') {
  4390. /* if the radix <= 36 the conversion is case insensitive
  4391. * this allows numbers like 1AB and 1ab to represent the same value
  4392. * [e.g. in hex]
  4393. */
  4394. ch = (radix <= 36) ? (char)XTOUPPER((unsigned char)*str) : *str;
  4395. for (y = 0; y < 64; y++) {
  4396. if (ch == mp_s_rmap[y]) {
  4397. break;
  4398. }
  4399. }
  4400. /* if the char was found in the map
  4401. * and is less than the given radix add it
  4402. * to the number, otherwise exit the loop.
  4403. */
  4404. if (y < radix) {
  4405. if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) {
  4406. mp_zero(a);
  4407. return res;
  4408. }
  4409. if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) {
  4410. mp_zero(a);
  4411. return res;
  4412. }
  4413. } else {
  4414. break;
  4415. }
  4416. ++str;
  4417. }
  4418. /* if digit in isn't null term, then invalid character was found */
  4419. if (*str != '\0') {
  4420. mp_zero (a);
  4421. return MP_VAL;
  4422. }
  4423. /* set the sign only if a != 0 */
  4424. if (mp_iszero(a) != MP_YES) {
  4425. a->sign = neg;
  4426. }
  4427. return MP_OKAY;
  4428. }
  4429. #endif /* !defined(NO_DSA) || defined(HAVE_ECC) */
  4430. #ifdef WC_MP_TO_RADIX
  4431. /* returns size of ASCII representation */
  4432. int mp_radix_size (mp_int *a, int radix, int *size)
  4433. {
  4434. int res, digs;
  4435. mp_int t;
  4436. mp_digit d;
  4437. *size = 0;
  4438. /* special case for binary */
  4439. if (radix == MP_RADIX_BIN) {
  4440. *size = mp_count_bits(a);
  4441. if (*size == 0)
  4442. *size = 1;
  4443. *size += (a->sign == MP_NEG ? 1 : 0) + 1; /* "-" sign + null term */
  4444. return MP_OKAY;
  4445. }
  4446. /* make sure the radix is in range */
  4447. if (radix < MP_RADIX_BIN || radix > MP_RADIX_MAX) {
  4448. return MP_VAL;
  4449. }
  4450. if (mp_iszero(a) == MP_YES) {
  4451. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  4452. if (radix == 16)
  4453. *size = 3;
  4454. else
  4455. #endif
  4456. *size = 2;
  4457. return MP_OKAY;
  4458. }
  4459. /* digs is the digit count */
  4460. digs = 0;
  4461. /* init a copy of the input */
  4462. if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
  4463. return res;
  4464. }
  4465. /* force temp to positive */
  4466. t.sign = MP_ZPOS;
  4467. /* fetch out all of the digits */
  4468. while (mp_iszero (&t) == MP_NO) {
  4469. if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
  4470. mp_clear (&t);
  4471. return res;
  4472. }
  4473. ++digs;
  4474. }
  4475. mp_clear (&t);
  4476. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  4477. /* For hexadecimal output, add zero padding when number of digits is odd */
  4478. if ((digs & 1) && (radix == 16)) {
  4479. ++digs;
  4480. }
  4481. #endif
  4482. /* if it's negative add one for the sign */
  4483. if (a->sign == MP_NEG) {
  4484. ++digs;
  4485. }
  4486. /* return digs + 1, the 1 is for the NULL byte that would be required. */
  4487. *size = digs + 1;
  4488. return MP_OKAY;
  4489. }
  4490. /* stores a bignum as a ASCII string in a given radix (2..64) */
  4491. int mp_toradix (mp_int *a, char *str, int radix)
  4492. {
  4493. int res, digs;
  4494. mp_int t;
  4495. mp_digit d;
  4496. char *_s = str;
  4497. /* check range of the radix */
  4498. if (radix < MP_RADIX_BIN || radix > MP_RADIX_MAX) {
  4499. return MP_VAL;
  4500. }
  4501. /* quick out if its zero */
  4502. if (mp_iszero(a) == MP_YES) {
  4503. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  4504. if (radix == 16) {
  4505. *str++ = '0';
  4506. }
  4507. #endif
  4508. *str++ = '0';
  4509. *str = '\0';
  4510. return MP_OKAY;
  4511. }
  4512. if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
  4513. return res;
  4514. }
  4515. /* if it is negative output a - */
  4516. if (t.sign == MP_NEG) {
  4517. ++_s;
  4518. *str++ = '-';
  4519. t.sign = MP_ZPOS;
  4520. }
  4521. digs = 0;
  4522. while (mp_iszero (&t) == MP_NO) {
  4523. if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
  4524. mp_clear (&t);
  4525. return res;
  4526. }
  4527. *str++ = mp_s_rmap[d];
  4528. ++digs;
  4529. }
  4530. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  4531. /* For hexadecimal output, add zero padding when number of digits is odd */
  4532. if ((digs & 1) && (radix == 16)) {
  4533. *str++ = mp_s_rmap[0];
  4534. ++digs;
  4535. }
  4536. #endif
  4537. /* reverse the digits of the string. In this case _s points
  4538. * to the first digit [excluding the sign] of the number]
  4539. */
  4540. bn_reverse ((unsigned char *)_s, digs);
  4541. /* append a NULL so the string is properly terminated */
  4542. *str = '\0';
  4543. mp_clear (&t);
  4544. return MP_OKAY;
  4545. }
  4546. #ifdef WOLFSSL_DEBUG_MATH
  4547. void mp_dump(const char* desc, mp_int* a, byte verbose)
  4548. {
  4549. char *buffer;
  4550. int size = a->alloc;
  4551. buffer = (char*)XMALLOC(size * sizeof(mp_digit) * 2, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4552. if (buffer == NULL) {
  4553. return;
  4554. }
  4555. printf("%s: ptr=%p, used=%d, sign=%d, size=%d, mpd=%d\n",
  4556. desc, a, a->used, a->sign, size, (int)sizeof(mp_digit));
  4557. mp_tohex(a, buffer);
  4558. printf(" %s\n ", buffer);
  4559. if (verbose) {
  4560. int i;
  4561. for(i=0; i<a->alloc * (int)sizeof(mp_digit); i++) {
  4562. printf("%02x ", *(((byte*)a->dp) + i));
  4563. }
  4564. printf("\n");
  4565. }
  4566. XFREE(buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  4567. }
  4568. #endif /* WOLFSSL_DEBUG_MATH */
  4569. #endif /* WC_MP_TO_RADIX */
  4570. #endif /* WOLFSSL_SP_MATH */
  4571. #endif /* !USE_FAST_MATH && USE_INTEGER_HEAP_MATH */
  4572. #endif /* NO_BIG_INT */