tfm.c 146 KB

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