tfm.c 123 KB

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