tfm.c 131 KB

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