Ingen beskrivning

tommath.c 160KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964
  1. #include "aws_tommath.h"
  2. #ifdef AWS_BN_S_MP_MUL_DIGS_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis
  4. *
  5. * LibTomMath is a library that provides multiple-precision
  6. * integer arithmetic as well as number theoretic functionality.
  7. *
  8. * The library was designed directly after the MPI library by
  9. * Michael Fromberger but has been written from scratch with
  10. * additional optimizations in place.
  11. *
  12. * The library is free for all purposes without any express
  13. * guarantee it works.
  14. *
  15. * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
  16. */
  17. /* multiplies |a| * |b| and only computes upto digs digits of result
  18. * HAC pp. 595, Algorithm 14.12 Modified so you can control how
  19. * many digits of output are created.
  20. */
  21. int aws_s_mp_mul_digs(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c, int digs)
  22. {
  23. aws_mp_int t;
  24. int res, pa, pb, ix, iy;
  25. aws_mp_digit u;
  26. aws_mp_word r;
  27. aws_mp_digit tmpx, *tmpt, *tmpy;
  28. /* can we use the fast multiplier? */
  29. if (((digs) < AWS_MP_WARRAY) &&
  30. AWS_MIN (a->used, b->used) <
  31. (1 << ((CHAR_BIT * sizeof (aws_mp_word)) - (2 * AWS_DIGIT_BIT)))) {
  32. return aws_fast_s_mp_mul_digs(a, b, c, digs);
  33. }
  34. if ((res = aws_mp_init_size(&t, digs)) != AWS_MP_OKAY) {
  35. return res;
  36. }
  37. t.used = digs;
  38. /* compute the digits of the product directly */
  39. pa = a->used;
  40. for (ix = 0; ix < pa; ix++) {
  41. /* set the carry to zero */
  42. u = 0;
  43. /* limit ourselves to making digs digits of output */
  44. pb = AWS_MIN (b->used, digs - ix);
  45. /* setup some aliases */
  46. /* copy of the digit from a used within the nested loop */
  47. tmpx = a->dp[ix];
  48. /* an alias for the destination shifted ix places */
  49. tmpt = t.dp + ix;
  50. /* an alias for the digits of b */
  51. tmpy = b->dp;
  52. /* compute the columns of the output and propagate the carry */
  53. for (iy = 0; iy < pb; iy++) {
  54. /* compute the column as a aws_mp_word */
  55. r = ((aws_mp_word)*tmpt) +
  56. ((aws_mp_word)tmpx) * ((aws_mp_word)*tmpy++) +
  57. ((aws_mp_word) u);
  58. /* the new column is the lower part of the result */
  59. *tmpt++ = (aws_mp_digit) (r & ((aws_mp_word) AWS_MP_MASK));
  60. /* get the carry word from the result */
  61. u = (aws_mp_digit) (r >> ((aws_mp_word) AWS_DIGIT_BIT));
  62. }
  63. /* set carry if it is placed below digs */
  64. if (ix + iy < digs) {
  65. *tmpt = u;
  66. }
  67. }
  68. aws_mp_clamp(&t);
  69. aws_mp_exch(&t, c);
  70. aws_mp_clear(&t);
  71. return AWS_MP_OKAY;
  72. }
  73. #endif
  74. #ifdef AWS_BN_MP_TO_UNSIGNED_BIN_N_C
  75. /* store in unsigned [big endian] format */
  76. int aws_mp_to_unsigned_bin_n(aws_mp_int *a, unsigned char *b, unsigned long *outlen)
  77. {
  78. if (*outlen < (unsigned long) aws_mp_unsigned_bin_size(a)) {
  79. return AWS_MP_VAL;
  80. }
  81. *outlen = aws_mp_unsigned_bin_size(a);
  82. return aws_mp_to_unsigned_bin(a, b);
  83. }
  84. #endif
  85. #ifdef AWS_BN_MP_EXPT_D_C
  86. /* calculate c = a**b using a square-multiply algorithm */
  87. int aws_mp_expt_d(aws_mp_int *a, aws_mp_digit b, aws_mp_int *c)
  88. {
  89. int res, x;
  90. aws_mp_int g;
  91. if ((res = aws_mp_init_copy(&g, a)) != AWS_MP_OKAY) {
  92. return res;
  93. }
  94. /* set initial result */
  95. aws_mp_set(c, 1);
  96. for (x = 0; x < (int) AWS_DIGIT_BIT; x++) {
  97. /* square */
  98. if ((res = aws_mp_sqr(c, c)) != AWS_MP_OKAY) {
  99. aws_mp_clear(&g);
  100. return res;
  101. }
  102. /* if the bit is set multiply */
  103. if ((b & (aws_mp_digit) (((aws_mp_digit)1) << (AWS_DIGIT_BIT - 1))) != 0) {
  104. if ((res = aws_mp_mul(c, &g, c)) != AWS_MP_OKAY) {
  105. aws_mp_clear(&g);
  106. return res;
  107. }
  108. }
  109. /* shift to next bit */
  110. b <<= 1;
  111. }
  112. aws_mp_clear(&g);
  113. return AWS_MP_OKAY;
  114. }
  115. #endif
  116. #ifdef AWS_BN_MP_SET_INT_C
  117. /* set a 32-bit const */
  118. int aws_mp_set_int(aws_mp_int *a, unsigned long b)
  119. {
  120. int x, res;
  121. aws_mp_zero(a);
  122. /* set four bits at a time */
  123. for (x = 0; x < 8; x++) {
  124. /* shift the number up four bits */
  125. if ((res = aws_mp_mul_2d(a, 4, a)) != AWS_MP_OKAY) {
  126. return res;
  127. }
  128. /* OR in the top four bits of the source */
  129. a->dp[0] |= (b >> 28) & 15;
  130. /* shift the source up to the next four bits */
  131. b <<= 4;
  132. /* ensure that digits are not clamped off */
  133. a->used += 1;
  134. }
  135. aws_mp_clamp(a);
  136. return AWS_MP_OKAY;
  137. }
  138. #endif
  139. #ifdef AWS_BN_MP_ADD_C
  140. /* high level addition (handles signs) */
  141. int aws_mp_add(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  142. {
  143. int sa, sb, res;
  144. /* get sign of both inputs */
  145. sa = a->sign;
  146. sb = b->sign;
  147. /* handle two cases, not four */
  148. if (sa == sb) {
  149. /* both positive or both negative */
  150. /* add their magnitudes, copy the sign */
  151. c->sign = sa;
  152. res = aws_s_mp_add(a, b, c);
  153. } else {
  154. /* one positive, the other negative */
  155. /* subtract the one with the greater magnitude from */
  156. /* the one of the lesser magnitude. The result gets */
  157. /* the sign of the one with the greater magnitude. */
  158. if (aws_mp_cmp_mag(a, b) == AWS_MP_LT) {
  159. c->sign = sb;
  160. res = aws_s_mp_sub(b, a, c);
  161. } else {
  162. c->sign = sa;
  163. res = aws_s_mp_sub(a, b, c);
  164. }
  165. }
  166. return res;
  167. }
  168. #endif
  169. #ifdef AWS_BN_MP_INIT_C
  170. /* init a new mp_int */
  171. int aws_mp_init(aws_mp_int *a)
  172. {
  173. int i;
  174. /* allocate memory required and clear it */
  175. a->dp = AWS_OPT_CAST(aws_mp_digit) AWS_XMALLOC (sizeof (aws_mp_digit) * AWS_MP_PREC);
  176. if (a->dp == NULL) {
  177. return AWS_MP_MEM;
  178. }
  179. /* set the digits to zero */
  180. for (i = 0; i < AWS_MP_PREC; i++) {
  181. a->dp[i] = 0;
  182. }
  183. /* set the used to zero, allocated digits to the default precision
  184. * and sign to positive */
  185. a->used = 0;
  186. a->alloc = AWS_MP_PREC;
  187. a->sign = AWS_MP_ZPOS;
  188. return AWS_MP_OKAY;
  189. }
  190. #endif
  191. #ifdef AWS_BN_MP_CNT_LSB_C
  192. static const int lnz[16] = {
  193. 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  194. };
  195. /* Counts the number of lsbs which are zero before the first zero bit */
  196. int aws_mp_cnt_lsb(aws_mp_int *a)
  197. {
  198. int x;
  199. aws_mp_digit q, qq;
  200. /* easy out */
  201. if (aws_mp_iszero(a) == 1) {
  202. return 0;
  203. }
  204. /* scan lower digits until non-zero */
  205. for (x = 0; x < a->used && a->dp[x] == 0; x++);
  206. q = a->dp[x];
  207. x *= AWS_DIGIT_BIT;
  208. /* now scan this digit until a 1 is found */
  209. if ((q & 1) == 0) {
  210. do {
  211. qq = q & 15;
  212. x += lnz[qq];
  213. q >>= 4;
  214. } while (qq == 0);
  215. }
  216. return x;
  217. }
  218. #endif
  219. #ifdef AWS_BN_MP_TOOM_SQR_C
  220. /* squaring using Toom-Cook 3-way algorithm */
  221. int
  222. aws_mp_toom_sqr(aws_mp_int *a, aws_mp_int *b)
  223. {
  224. aws_mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2;
  225. int res, B;
  226. /* init temps */
  227. if ((res = aws_mp_init_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL)) != AWS_MP_OKAY) {
  228. return res;
  229. }
  230. /* B */
  231. B = a->used / 3;
  232. /* a = a2 * B**2 + a1 * B + a0 */
  233. if ((res = aws_mp_mod_2d(a, AWS_DIGIT_BIT * B, &a0)) != AWS_MP_OKAY) {
  234. goto ERR;
  235. }
  236. if ((res = aws_mp_copy(a, &a1)) != AWS_MP_OKAY) {
  237. goto ERR;
  238. }
  239. aws_mp_rshd(&a1, B);
  240. aws_mp_mod_2d(&a1, AWS_DIGIT_BIT * B, &a1);
  241. if ((res = aws_mp_copy(a, &a2)) != AWS_MP_OKAY) {
  242. goto ERR;
  243. }
  244. aws_mp_rshd(&a2, B * 2);
  245. /* w0 = a0*a0 */
  246. if ((res = aws_mp_sqr(&a0, &w0)) != AWS_MP_OKAY) {
  247. goto ERR;
  248. }
  249. /* w4 = a2 * a2 */
  250. if ((res = aws_mp_sqr(&a2, &w4)) != AWS_MP_OKAY) {
  251. goto ERR;
  252. }
  253. /* w1 = (a2 + 2(a1 + 2a0))**2 */
  254. if ((res = aws_mp_mul_2(&a0, &tmp1)) != AWS_MP_OKAY) {
  255. goto ERR;
  256. }
  257. if ((res = aws_mp_add(&tmp1, &a1, &tmp1)) != AWS_MP_OKAY) {
  258. goto ERR;
  259. }
  260. if ((res = aws_mp_mul_2(&tmp1, &tmp1)) != AWS_MP_OKAY) {
  261. goto ERR;
  262. }
  263. if ((res = aws_mp_add(&tmp1, &a2, &tmp1)) != AWS_MP_OKAY) {
  264. goto ERR;
  265. }
  266. if ((res = aws_mp_sqr(&tmp1, &w1)) != AWS_MP_OKAY) {
  267. goto ERR;
  268. }
  269. /* w3 = (a0 + 2(a1 + 2a2))**2 */
  270. if ((res = aws_mp_mul_2(&a2, &tmp1)) != AWS_MP_OKAY) {
  271. goto ERR;
  272. }
  273. if ((res = aws_mp_add(&tmp1, &a1, &tmp1)) != AWS_MP_OKAY) {
  274. goto ERR;
  275. }
  276. if ((res = aws_mp_mul_2(&tmp1, &tmp1)) != AWS_MP_OKAY) {
  277. goto ERR;
  278. }
  279. if ((res = aws_mp_add(&tmp1, &a0, &tmp1)) != AWS_MP_OKAY) {
  280. goto ERR;
  281. }
  282. if ((res = aws_mp_sqr(&tmp1, &w3)) != AWS_MP_OKAY) {
  283. goto ERR;
  284. }
  285. /* w2 = (a2 + a1 + a0)**2 */
  286. if ((res = aws_mp_add(&a2, &a1, &tmp1)) != AWS_MP_OKAY) {
  287. goto ERR;
  288. }
  289. if ((res = aws_mp_add(&tmp1, &a0, &tmp1)) != AWS_MP_OKAY) {
  290. goto ERR;
  291. }
  292. if ((res = aws_mp_sqr(&tmp1, &w2)) != AWS_MP_OKAY) {
  293. goto ERR;
  294. }
  295. /* now solve the matrix
  296. 0 0 0 0 1
  297. 1 2 4 8 16
  298. 1 1 1 1 1
  299. 16 8 4 2 1
  300. 1 0 0 0 0
  301. using 12 subtractions, 4 shifts, 2 small divisions and 1 small multiplication.
  302. */
  303. /* r1 - r4 */
  304. if ((res = aws_mp_sub(&w1, &w4, &w1)) != AWS_MP_OKAY) {
  305. goto ERR;
  306. }
  307. /* r3 - r0 */
  308. if ((res = aws_mp_sub(&w3, &w0, &w3)) != AWS_MP_OKAY) {
  309. goto ERR;
  310. }
  311. /* r1/2 */
  312. if ((res = aws_mp_div_2(&w1, &w1)) != AWS_MP_OKAY) {
  313. goto ERR;
  314. }
  315. /* r3/2 */
  316. if ((res = aws_mp_div_2(&w3, &w3)) != AWS_MP_OKAY) {
  317. goto ERR;
  318. }
  319. /* r2 - r0 - r4 */
  320. if ((res = aws_mp_sub(&w2, &w0, &w2)) != AWS_MP_OKAY) {
  321. goto ERR;
  322. }
  323. if ((res = aws_mp_sub(&w2, &w4, &w2)) != AWS_MP_OKAY) {
  324. goto ERR;
  325. }
  326. /* r1 - r2 */
  327. if ((res = aws_mp_sub(&w1, &w2, &w1)) != AWS_MP_OKAY) {
  328. goto ERR;
  329. }
  330. /* r3 - r2 */
  331. if ((res = aws_mp_sub(&w3, &w2, &w3)) != AWS_MP_OKAY) {
  332. goto ERR;
  333. }
  334. /* r1 - 8r0 */
  335. if ((res = aws_mp_mul_2d(&w0, 3, &tmp1)) != AWS_MP_OKAY) {
  336. goto ERR;
  337. }
  338. if ((res = aws_mp_sub(&w1, &tmp1, &w1)) != AWS_MP_OKAY) {
  339. goto ERR;
  340. }
  341. /* r3 - 8r4 */
  342. if ((res = aws_mp_mul_2d(&w4, 3, &tmp1)) != AWS_MP_OKAY) {
  343. goto ERR;
  344. }
  345. if ((res = aws_mp_sub(&w3, &tmp1, &w3)) != AWS_MP_OKAY) {
  346. goto ERR;
  347. }
  348. /* 3r2 - r1 - r3 */
  349. if ((res = aws_mp_mul_d(&w2, 3, &w2)) != AWS_MP_OKAY) {
  350. goto ERR;
  351. }
  352. if ((res = aws_mp_sub(&w2, &w1, &w2)) != AWS_MP_OKAY) {
  353. goto ERR;
  354. }
  355. if ((res = aws_mp_sub(&w2, &w3, &w2)) != AWS_MP_OKAY) {
  356. goto ERR;
  357. }
  358. /* r1 - r2 */
  359. if ((res = aws_mp_sub(&w1, &w2, &w1)) != AWS_MP_OKAY) {
  360. goto ERR;
  361. }
  362. /* r3 - r2 */
  363. if ((res = aws_mp_sub(&w3, &w2, &w3)) != AWS_MP_OKAY) {
  364. goto ERR;
  365. }
  366. /* r1/3 */
  367. if ((res = aws_mp_div_3(&w1, &w1, NULL)) != AWS_MP_OKAY) {
  368. goto ERR;
  369. }
  370. /* r3/3 */
  371. if ((res = aws_mp_div_3(&w3, &w3, NULL)) != AWS_MP_OKAY) {
  372. goto ERR;
  373. }
  374. /* at this point shift W[n] by B*n */
  375. if ((res = aws_mp_lshd(&w1, 1 * B)) != AWS_MP_OKAY) {
  376. goto ERR;
  377. }
  378. if ((res = aws_mp_lshd(&w2, 2 * B)) != AWS_MP_OKAY) {
  379. goto ERR;
  380. }
  381. if ((res = aws_mp_lshd(&w3, 3 * B)) != AWS_MP_OKAY) {
  382. goto ERR;
  383. }
  384. if ((res = aws_mp_lshd(&w4, 4 * B)) != AWS_MP_OKAY) {
  385. goto ERR;
  386. }
  387. if ((res = aws_mp_add(&w0, &w1, b)) != AWS_MP_OKAY) {
  388. goto ERR;
  389. }
  390. if ((res = aws_mp_add(&w2, &w3, &tmp1)) != AWS_MP_OKAY) {
  391. goto ERR;
  392. }
  393. if ((res = aws_mp_add(&w4, &tmp1, &tmp1)) != AWS_MP_OKAY) {
  394. goto ERR;
  395. }
  396. if ((res = aws_mp_add(&tmp1, b, b)) != AWS_MP_OKAY) {
  397. goto ERR;
  398. }
  399. ERR:
  400. aws_mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
  401. return res;
  402. }
  403. #endif
  404. #ifdef AWS_BN_MP_FREAD_C
  405. /* read a bigint from a file stream in ASCII */
  406. int aws_mp_fread(aws_mp_int *a, int radix, FILE *stream)
  407. {
  408. int err, ch, neg, y;
  409. /* clear a */
  410. aws_mp_zero(a);
  411. /* if first digit is - then set negative */
  412. ch = fgetc(stream);
  413. if (ch == '-') {
  414. neg = AWS_MP_NEG;
  415. ch = fgetc(stream);
  416. } else {
  417. neg = AWS_MP_ZPOS;
  418. }
  419. for (;;) {
  420. /* find y in the radix map */
  421. for (y = 0; y < radix; y++) {
  422. if (aws_mp_s_rmap[y] == ch) {
  423. break;
  424. }
  425. }
  426. if (y == radix) {
  427. break;
  428. }
  429. /* shift up and add */
  430. if ((err = aws_mp_mul_d(a, radix, a)) != AWS_MP_OKAY) {
  431. return err;
  432. }
  433. if ((err = aws_mp_add_d(a, y, a)) != AWS_MP_OKAY) {
  434. return err;
  435. }
  436. ch = fgetc(stream);
  437. }
  438. if (aws_mp_cmp_d(a, 0) != AWS_MP_EQ) {
  439. a->sign = neg;
  440. }
  441. return AWS_MP_OKAY;
  442. }
  443. #endif
  444. #ifdef AWS_BN_MP_DIV_3_C
  445. /* divide by three (based on routine from MPI and the GMP manual) */
  446. int
  447. aws_mp_div_3(aws_mp_int *a, aws_mp_int *c, aws_mp_digit *d)
  448. {
  449. aws_mp_int q;
  450. aws_mp_word w, t;
  451. aws_mp_digit b;
  452. int res, ix;
  453. /* b = 2**AWS_DIGIT_BIT / 3 */
  454. b = (((aws_mp_word)1) << ((aws_mp_word)AWS_DIGIT_BIT)) / ((aws_mp_word)3);
  455. if ((res = aws_mp_init_size(&q, a->used)) != AWS_MP_OKAY) {
  456. return res;
  457. }
  458. q.used = a->used;
  459. q.sign = a->sign;
  460. w = 0;
  461. for (ix = a->used - 1; ix >= 0; ix--) {
  462. w = (w << ((aws_mp_word)AWS_DIGIT_BIT)) | ((aws_mp_word)a->dp[ix]);
  463. if (w >= 3) {
  464. /* multiply w by [1/3] */
  465. t = (w * ((aws_mp_word)b)) >> ((aws_mp_word)AWS_DIGIT_BIT);
  466. /* now subtract 3 * [w/3] from w, to get the remainder */
  467. w -= t+t+t;
  468. /* fixup the remainder as required since
  469. * the optimization is not exact.
  470. */
  471. while (w >= 3) {
  472. t += 1;
  473. w -= 3;
  474. }
  475. } else {
  476. t = 0;
  477. }
  478. q.dp[ix] = (aws_mp_digit)t;
  479. }
  480. /* [optional] store the remainder */
  481. if (d != NULL) {
  482. *d = (aws_mp_digit)w;
  483. }
  484. /* [optional] store the quotient */
  485. if (c != NULL) {
  486. aws_mp_clamp(&q);
  487. aws_mp_exch(&q, c);
  488. }
  489. aws_mp_clear(&q);
  490. return res;
  491. }
  492. #endif
  493. #ifdef AWS_BN_MP_LCM_C
  494. /* computes least common multiple as |a*b|/(a, b) */
  495. int aws_aws_mp_lcm(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  496. {
  497. int res;
  498. aws_mp_int t1, t2;
  499. if ((res = aws_mp_init_multi(&t1, &t2, NULL)) != AWS_MP_OKAY) {
  500. return res;
  501. }
  502. /* t1 = get the GCD of the two inputs */
  503. if ((res = aws_mp_gcd(a, b, &t1)) != AWS_MP_OKAY) {
  504. goto LBL_T;
  505. }
  506. /* divide the smallest by the GCD */
  507. if (aws_mp_cmp_mag(a, b) == AWS_MP_LT) {
  508. /* store quotient in t2 such that t2 * b is the LCM */
  509. if ((res = aws_mp_div(a, &t1, &t2, NULL)) != AWS_MP_OKAY) {
  510. goto LBL_T;
  511. }
  512. res = aws_mp_mul(b, &t2, c);
  513. } else {
  514. /* store quotient in t2 such that t2 * a is the LCM */
  515. if ((res = aws_mp_div(b, &t1, &t2, NULL)) != AWS_MP_OKAY) {
  516. goto LBL_T;
  517. }
  518. res = aws_mp_mul(a, &t2, c);
  519. }
  520. /* fix the sign to positive */
  521. c->sign = AWS_MP_ZPOS;
  522. LBL_T:
  523. aws_mp_clear_multi(&t1, &t2, NULL);
  524. return res;
  525. }
  526. #endif
  527. #ifdef AWS_BN_MP_REDUCE_IS_2K_C
  528. /* determines if aws_mp_reduce_2k can be used */
  529. int aws_mp_reduce_is_2k(aws_mp_int *a)
  530. {
  531. int ix, iy, iw;
  532. aws_mp_digit iz;
  533. if (a->used == 0) {
  534. return AWS_MP_NO;
  535. } else if (a->used == 1) {
  536. return AWS_MP_YES;
  537. } else if (a->used > 1) {
  538. iy = aws_mp_count_bits(a);
  539. iz = 1;
  540. iw = 1;
  541. /* Test every bit from the second digit up, must be 1 */
  542. for (ix = AWS_DIGIT_BIT; ix < iy; ix++) {
  543. if ((a->dp[iw] & iz) == 0) {
  544. return AWS_MP_NO;
  545. }
  546. iz <<= 1;
  547. if (iz > (aws_mp_digit)AWS_MP_MASK) {
  548. ++iw;
  549. iz = 1;
  550. }
  551. }
  552. }
  553. return AWS_MP_YES;
  554. }
  555. #endif
  556. #ifdef AWS_BNCORE_C
  557. /* Known optimal configurations
  558. CPU /Compiler /MUL CUTOFF/SQR CUTOFF
  559. -------------------------------------------------------------
  560. Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-)
  561. AMD Athlon64 /GCC v3.4.4 / 80/ 120/LTM 0.35
  562. */
  563. int AWS_KARATSUBA_MUL_CUTOFF = 80, /* Min. number of digits before Karatsuba multiplication is used. */
  564. AWS_KARATSUBA_SQR_CUTOFF = 120, /* Min. number of digits before Karatsuba squaring is used. */
  565. AWS_TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */
  566. AWS_TOOM_SQR_CUTOFF = 400;
  567. #endif
  568. #ifdef AWS_BN_MP_MOD_2D_C
  569. /* calc a value mod 2**b */
  570. int
  571. aws_mp_mod_2d(aws_mp_int *a, int b, aws_mp_int *c)
  572. {
  573. int x, res;
  574. /* if b is <= 0 then zero the int */
  575. if (b <= 0) {
  576. aws_mp_zero(c);
  577. return AWS_MP_OKAY;
  578. }
  579. /* if the modulus is larger than the value than return */
  580. if (b >= (int) (a->used * AWS_DIGIT_BIT)) {
  581. res = aws_mp_copy(a, c);
  582. return res;
  583. }
  584. /* copy */
  585. if ((res = aws_mp_copy(a, c)) != AWS_MP_OKAY) {
  586. return res;
  587. }
  588. /* zero digits above the last digit of the modulus */
  589. for (x = (b / AWS_DIGIT_BIT) + ((b % AWS_DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) {
  590. c->dp[x] = 0;
  591. }
  592. /* clear the digit that is not completely outside/inside the modulus */
  593. c->dp[b / AWS_DIGIT_BIT] &=
  594. (aws_mp_digit) ((((aws_mp_digit) 1) << (((aws_mp_digit) b) % AWS_DIGIT_BIT)) - ((aws_mp_digit) 1));
  595. aws_mp_clamp(c);
  596. return AWS_MP_OKAY;
  597. }
  598. #endif
  599. #ifdef AWS_BN_MP_N_ROOT_C
  600. /* find the n'th root of an integer
  601. *
  602. * Result found such that (c)**b <= a and (c+1)**b > a
  603. *
  604. * This algorithm uses Newton's approximation
  605. * x[i+1] = x[i] - f(x[i])/f'(x[i])
  606. * which will find the root in log(N) time where
  607. * each step involves a fair bit. This is not meant to
  608. * find huge roots [square and cube, etc].
  609. */
  610. int aws_mp_n_root(aws_mp_int *a, aws_mp_digit b, aws_mp_int *c)
  611. {
  612. aws_mp_int t1, t2, t3;
  613. int res, neg;
  614. /* input must be positive if b is even */
  615. if ((b & 1) == 0 && a->sign == AWS_MP_NEG) {
  616. return AWS_MP_VAL;
  617. }
  618. if ((res = aws_mp_init(&t1)) != AWS_MP_OKAY) {
  619. return res;
  620. }
  621. if ((res = aws_mp_init(&t2)) != AWS_MP_OKAY) {
  622. goto LBL_T1;
  623. }
  624. if ((res = aws_mp_init(&t3)) != AWS_MP_OKAY) {
  625. goto LBL_T2;
  626. }
  627. /* if a is negative fudge the sign but keep track */
  628. neg = a->sign;
  629. a->sign = AWS_MP_ZPOS;
  630. /* t2 = 2 */
  631. aws_mp_set(&t2, 2);
  632. do {
  633. /* t1 = t2 */
  634. if ((res = aws_mp_copy(&t2, &t1)) != AWS_MP_OKAY) {
  635. goto LBL_T3;
  636. }
  637. /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
  638. /* t3 = t1**(b-1) */
  639. if ((res = aws_mp_expt_d(&t1, b - 1, &t3)) != AWS_MP_OKAY) {
  640. goto LBL_T3;
  641. }
  642. /* numerator */
  643. /* t2 = t1**b */
  644. if ((res = aws_mp_mul(&t3, &t1, &t2)) != AWS_MP_OKAY) {
  645. goto LBL_T3;
  646. }
  647. /* t2 = t1**b - a */
  648. if ((res = aws_mp_sub(&t2, a, &t2)) != AWS_MP_OKAY) {
  649. goto LBL_T3;
  650. }
  651. /* denominator */
  652. /* t3 = t1**(b-1) * b */
  653. if ((res = aws_mp_mul_d(&t3, b, &t3)) != AWS_MP_OKAY) {
  654. goto LBL_T3;
  655. }
  656. /* t3 = (t1**b - a)/(b * t1**(b-1)) */
  657. if ((res = aws_mp_div(&t2, &t3, &t3, NULL)) != AWS_MP_OKAY) {
  658. goto LBL_T3;
  659. }
  660. if ((res = aws_mp_sub(&t1, &t3, &t2)) != AWS_MP_OKAY) {
  661. goto LBL_T3;
  662. }
  663. } while (aws_mp_cmp(&t1, &t2) != AWS_MP_EQ);
  664. /* result can be off by a few so check */
  665. for (;;) {
  666. if ((res = aws_mp_expt_d(&t1, b, &t2)) != AWS_MP_OKAY) {
  667. goto LBL_T3;
  668. }
  669. if (aws_mp_cmp(&t2, a) == AWS_MP_GT) {
  670. if ((res = aws_mp_sub_d(&t1, 1, &t1)) != AWS_MP_OKAY) {
  671. goto LBL_T3;
  672. }
  673. } else {
  674. break;
  675. }
  676. }
  677. /* reset the sign of a first */
  678. a->sign = neg;
  679. /* set the result */
  680. aws_mp_exch(&t1, c);
  681. /* set the sign of the result */
  682. c->sign = neg;
  683. res = AWS_MP_OKAY;
  684. LBL_T3:
  685. aws_mp_clear(&t3);
  686. LBL_T2:
  687. aws_mp_clear(&t2);
  688. LBL_T1:
  689. aws_mp_clear(&t1);
  690. return res;
  691. }
  692. #endif
  693. #ifdef AWS_BN_MP_MULMOD_C
  694. /* d = a * b (mod c) */
  695. int aws_mp_mulmod(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c, aws_mp_int *d)
  696. {
  697. int res;
  698. aws_mp_int t;
  699. if ((res = aws_mp_init(&t)) != AWS_MP_OKAY) {
  700. return res;
  701. }
  702. if ((res = aws_mp_mul(a, b, &t)) != AWS_MP_OKAY) {
  703. aws_mp_clear(&t);
  704. return res;
  705. }
  706. res = aws_mp_mod(&t, c, d);
  707. aws_mp_clear(&t);
  708. return res;
  709. }
  710. #endif
  711. #ifdef AWS_BN_ERROR_C
  712. static const struct {
  713. int code;
  714. char *msg;
  715. } msgs[] = {
  716. { AWS_MP_OKAY, "Successful" },
  717. { AWS_MP_MEM, "Out of heap" },
  718. { AWS_MP_VAL, "Value out of range" }
  719. };
  720. /* return a char * string for a given code */
  721. char *aws_mp_error_to_string(int code)
  722. {
  723. int x;
  724. /* scan the lookup table for the given message */
  725. for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) {
  726. if (msgs[x].code == code) {
  727. return msgs[x].msg;
  728. }
  729. }
  730. /* generic reply for invalid code */
  731. return "Invalid error code";
  732. }
  733. #endif
  734. #ifdef AWS_BN_MP_REDUCE_C
  735. /* reduces x mod m, assumes 0 < x < m**2, mu is
  736. * precomputed via aws_mp_reduce_setup.
  737. * From HAC pp.604 Algorithm 14.42
  738. */
  739. int aws_mp_reduce(aws_mp_int *x, aws_mp_int *m, aws_mp_int *mu)
  740. {
  741. aws_mp_int q;
  742. int res, um = m->used;
  743. /* q = x */
  744. if ((res = aws_mp_init_copy(&q, x)) != AWS_MP_OKAY) {
  745. return res;
  746. }
  747. /* q1 = x / b**(k-1) */
  748. aws_mp_rshd(&q, um - 1);
  749. /* according to HAC this optimization is ok */
  750. if (((unsigned long) um) > (((aws_mp_digit)1) << (AWS_DIGIT_BIT - 1))) {
  751. if ((res = aws_mp_mul(&q, mu, &q)) != AWS_MP_OKAY) {
  752. goto CLEANUP;
  753. }
  754. } else {
  755. #ifdef AWS_BN_S_MP_MUL_HIGH_DIGS_C
  756. if ((res = aws_s_mp_mul_high_digs(&q, mu, &q, um)) != AWS_MP_OKAY) {
  757. goto CLEANUP;
  758. }
  759. #elif defined(AWS_BN_FAST_S_MP_MUL_HIGH_DIGS_C)
  760. if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != AWS_MP_OKAY) {
  761. goto CLEANUP;
  762. }
  763. #else
  764. {
  765. res = AWS_MP_VAL;
  766. goto CLEANUP;
  767. }
  768. #endif
  769. }
  770. /* q3 = q2 / b**(k+1) */
  771. aws_mp_rshd(&q, um + 1);
  772. /* x = x mod b**(k+1), quick (no division) */
  773. if ((res = aws_mp_mod_2d(x, AWS_DIGIT_BIT * (um + 1), x)) != AWS_MP_OKAY) {
  774. goto CLEANUP;
  775. }
  776. /* q = q * m mod b**(k+1), quick (no division) */
  777. if ((res = aws_s_mp_mul_digs(&q, m, &q, um + 1)) != AWS_MP_OKAY) {
  778. goto CLEANUP;
  779. }
  780. /* x = x - q */
  781. if ((res = aws_mp_sub(x, &q, x)) != AWS_MP_OKAY) {
  782. goto CLEANUP;
  783. }
  784. /* If x < 0, add b**(k+1) to it */
  785. if (aws_mp_cmp_d(x, 0) == AWS_MP_LT) {
  786. aws_mp_set(&q, 1);
  787. if ((res = aws_mp_lshd(&q, um + 1)) != AWS_MP_OKAY)
  788. goto CLEANUP;
  789. if ((res = aws_mp_add(x, &q, x)) != AWS_MP_OKAY)
  790. goto CLEANUP;
  791. }
  792. /* Back off if it's too big */
  793. while (aws_mp_cmp(x, m) != AWS_MP_LT) {
  794. if ((res = aws_s_mp_sub(x, m, x)) != AWS_MP_OKAY) {
  795. goto CLEANUP;
  796. }
  797. }
  798. CLEANUP:
  799. aws_mp_clear(&q);
  800. return res;
  801. }
  802. #endif
  803. #ifdef AWS_BN_MP_KARATSUBA_SQR_C
  804. /* Karatsuba squaring, computes b = a*a using three
  805. * half size squarings
  806. *
  807. * See comments of karatsuba_mul for details. It
  808. * is essentially the same algorithm but merely
  809. * tuned to perform recursive squarings.
  810. */
  811. int aws_mp_karatsuba_sqr(aws_mp_int *a, aws_mp_int *b)
  812. {
  813. aws_mp_int x0, x1, t1, t2, x0x0, x1x1;
  814. int B, err;
  815. err = AWS_MP_MEM;
  816. /* min # of digits */
  817. B = a->used;
  818. /* now divide in two */
  819. B = B >> 1;
  820. /* init copy all the temps */
  821. if (aws_mp_init_size(&x0, B) != AWS_MP_OKAY)
  822. goto ERR;
  823. if (aws_mp_init_size(&x1, a->used - B) != AWS_MP_OKAY)
  824. goto X0;
  825. /* init temps */
  826. if (aws_mp_init_size(&t1, a->used * 2) != AWS_MP_OKAY)
  827. goto X1;
  828. if (aws_mp_init_size(&t2, a->used * 2) != AWS_MP_OKAY)
  829. goto T1;
  830. if (aws_mp_init_size(&x0x0, B * 2) != AWS_MP_OKAY)
  831. goto T2;
  832. if (aws_mp_init_size(&x1x1, (a->used - B) * 2) != AWS_MP_OKAY)
  833. goto X0X0;
  834. {
  835. register int x;
  836. register aws_mp_digit *dst, *src;
  837. src = a->dp;
  838. /* now shift the digits */
  839. dst = x0.dp;
  840. for (x = 0; x < B; x++) {
  841. *dst++ = *src++;
  842. }
  843. dst = x1.dp;
  844. for (x = B; x < a->used; x++) {
  845. *dst++ = *src++;
  846. }
  847. }
  848. x0.used = B;
  849. x1.used = a->used - B;
  850. aws_mp_clamp(&x0);
  851. /* now calc the products x0*x0 and x1*x1 */
  852. if (aws_mp_sqr(&x0, &x0x0) != AWS_MP_OKAY)
  853. goto X1X1; /* x0x0 = x0*x0 */
  854. if (aws_mp_sqr(&x1, &x1x1) != AWS_MP_OKAY)
  855. goto X1X1; /* x1x1 = x1*x1 */
  856. /* now calc (x1+x0)**2 */
  857. if (aws_s_mp_add(&x1, &x0, &t1) != AWS_MP_OKAY)
  858. goto X1X1; /* t1 = x1 - x0 */
  859. if (aws_mp_sqr(&t1, &t1) != AWS_MP_OKAY)
  860. goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
  861. /* add x0y0 */
  862. if (aws_s_mp_add(&x0x0, &x1x1, &t2) != AWS_MP_OKAY)
  863. goto X1X1; /* t2 = x0x0 + x1x1 */
  864. if (aws_s_mp_sub(&t1, &t2, &t1) != AWS_MP_OKAY)
  865. goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
  866. /* shift by B */
  867. if (aws_mp_lshd(&t1, B) != AWS_MP_OKAY)
  868. goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
  869. if (aws_mp_lshd(&x1x1, B * 2) != AWS_MP_OKAY)
  870. goto X1X1; /* x1x1 = x1x1 << 2*B */
  871. if (aws_mp_add(&x0x0, &t1, &t1) != AWS_MP_OKAY)
  872. goto X1X1; /* t1 = x0x0 + t1 */
  873. if (aws_mp_add(&t1, &x1x1, b) != AWS_MP_OKAY)
  874. goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
  875. err = AWS_MP_OKAY;
  876. X1X1:
  877. aws_mp_clear(&x1x1);
  878. X0X0:
  879. aws_mp_clear(&x0x0);
  880. T2:
  881. aws_mp_clear(&t2);
  882. T1:
  883. aws_mp_clear(&t1);
  884. X1:
  885. aws_mp_clear(&x1);
  886. X0:
  887. aws_mp_clear(&x0);
  888. ERR:
  889. return err;
  890. }
  891. #endif
  892. #ifdef AWS_BN_MP_PRIME_IS_DIVISIBLE_C
  893. /* determines if an integers is divisible by one
  894. * of the first AWS_JKTM_PRIME_SIZE primes or not
  895. *
  896. * sets result to 0 if not, 1 if yes
  897. */
  898. int aws_mp_prime_is_divisible(aws_mp_int *a, int *result)
  899. {
  900. int err, ix;
  901. aws_mp_digit res;
  902. /* default to not */
  903. *result = AWS_MP_NO;
  904. for (ix = 0; ix < AWS_JKTM_PRIME_SIZE; ix++) {
  905. /* what is a mod LBL_prime_tab[ix] */
  906. if ((err = aws_mp_mod_d(a, aws_ltm_prime_tab[ix], &res)) != AWS_MP_OKAY) {
  907. return err;
  908. }
  909. /* is the residue zero? */
  910. if (res == 0) {
  911. *result = AWS_MP_YES;
  912. return AWS_MP_OKAY;
  913. }
  914. }
  915. return AWS_MP_OKAY;
  916. }
  917. #endif
  918. #ifdef AWS_BN_MP_ZERO_C
  919. /* set to zero */
  920. void aws_mp_zero(aws_mp_int *a)
  921. {
  922. int n;
  923. aws_mp_digit *tmp;
  924. a->sign = AWS_MP_ZPOS;
  925. a->used = 0;
  926. tmp = a->dp;
  927. for (n = 0; n < a->alloc; n++) {
  928. *tmp++ = 0;
  929. }
  930. }
  931. #endif
  932. #ifdef AWS_BN_MP_TORADIX_C
  933. /* stores a bignum as a ASCII string in a given radix (2..64) */
  934. int aws_mp_toradix(aws_mp_int *a, char *str, int radix)
  935. {
  936. int res, digs;
  937. aws_mp_int t;
  938. aws_mp_digit d;
  939. char *_s = str;
  940. /* check range of the radix */
  941. if (radix < 2 || radix > 64) {
  942. return AWS_MP_VAL;
  943. }
  944. /* quick out if its zero */
  945. if (aws_mp_iszero(a) == 1) {
  946. *str++ = '0';
  947. *str = '\0';
  948. return AWS_MP_OKAY;
  949. }
  950. if ((res = aws_mp_init_copy(&t, a)) != AWS_MP_OKAY) {
  951. return res;
  952. }
  953. /* if it is negative output a - */
  954. if (t.sign == AWS_MP_NEG) {
  955. ++_s;
  956. *str++ = '-';
  957. t.sign = AWS_MP_ZPOS;
  958. }
  959. digs = 0;
  960. while (aws_mp_iszero (&t) == 0) {
  961. if ((res = aws_mp_div_d(&t, (aws_mp_digit) radix, &t, &d)) != AWS_MP_OKAY) {
  962. aws_mp_clear(&t);
  963. return res;
  964. }
  965. *str++ = aws_mp_s_rmap[d];
  966. ++digs;
  967. }
  968. /* reverse the digits of the string. In this case _s points
  969. * to the first digit [exluding the sign] of the number]
  970. */
  971. aws_bn_reverse((unsigned char *) _s, digs);
  972. /* append a NULL so the string is properly terminated */
  973. *str = '\0';
  974. aws_mp_clear(&t);
  975. return AWS_MP_OKAY;
  976. }
  977. #endif
  978. #ifdef AWS_BN_MP_DIV_D_C
  979. static int s_is_power_of_two(aws_mp_digit b, int *p)
  980. {
  981. int x;
  982. /* fast return if no power of two */
  983. if ((b==0) || (b & (b-1))) {
  984. return 0;
  985. }
  986. for (x = 0; x < AWS_DIGIT_BIT; x++) {
  987. if (b == (((aws_mp_digit)1)<<x)) {
  988. *p = x;
  989. return 1;
  990. }
  991. }
  992. return 0;
  993. }
  994. /* single digit division (based on routine from MPI) */
  995. int aws_mp_div_d(aws_mp_int *a, aws_mp_digit b, aws_mp_int *c, aws_mp_digit *d)
  996. {
  997. aws_mp_int q;
  998. aws_mp_word w;
  999. aws_mp_digit t;
  1000. int res, ix;
  1001. /* cannot divide by zero */
  1002. if (b == 0) {
  1003. return AWS_MP_VAL;
  1004. }
  1005. /* quick outs */
  1006. if (b == 1 || aws_mp_iszero(a) == 1) {
  1007. if (d != NULL) {
  1008. *d = 0;
  1009. }
  1010. if (c != NULL) {
  1011. return aws_mp_copy(a, c);
  1012. }
  1013. return AWS_MP_OKAY;
  1014. }
  1015. /* power of two ? */
  1016. if (s_is_power_of_two(b, &ix) == 1) {
  1017. if (d != NULL) {
  1018. *d = a->dp[0] & ((((aws_mp_digit)1)<<ix) - 1);
  1019. }
  1020. if (c != NULL) {
  1021. return aws_mp_div_2d(a, ix, c, NULL);
  1022. }
  1023. return AWS_MP_OKAY;
  1024. }
  1025. #ifdef AWS_BN_MP_DIV_3_C
  1026. /* three? */
  1027. if (b == 3) {
  1028. return aws_mp_div_3(a, c, d);
  1029. }
  1030. #endif
  1031. /* no easy answer [c'est la vie]. Just division */
  1032. if ((res = aws_mp_init_size(&q, a->used)) != AWS_MP_OKAY) {
  1033. return res;
  1034. }
  1035. q.used = a->used;
  1036. q.sign = a->sign;
  1037. w = 0;
  1038. for (ix = a->used - 1; ix >= 0; ix--) {
  1039. w = (w << ((aws_mp_word)AWS_DIGIT_BIT)) | ((aws_mp_word)a->dp[ix]);
  1040. if (w >= b) {
  1041. t = (aws_mp_digit)(w / b);
  1042. w -= ((aws_mp_word)t) * ((aws_mp_word)b);
  1043. } else {
  1044. t = 0;
  1045. }
  1046. q.dp[ix] = (aws_mp_digit)t;
  1047. }
  1048. if (d != NULL) {
  1049. *d = (aws_mp_digit)w;
  1050. }
  1051. if (c != NULL) {
  1052. aws_mp_clamp(&q);
  1053. aws_mp_exch(&q, c);
  1054. }
  1055. aws_mp_clear(&q);
  1056. return res;
  1057. }
  1058. #endif
  1059. #ifdef AWS_BN_MP_SUB_C
  1060. /* high level subtraction (handles signs) */
  1061. int
  1062. aws_mp_sub(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  1063. {
  1064. int sa, sb, res;
  1065. sa = a->sign;
  1066. sb = b->sign;
  1067. if (sa != sb) {
  1068. /* subtract a negative from a positive, OR */
  1069. /* subtract a positive from a negative. */
  1070. /* In either case, ADD their magnitudes, */
  1071. /* and use the sign of the first number. */
  1072. c->sign = sa;
  1073. res = aws_s_mp_add(a, b, c);
  1074. } else {
  1075. /* subtract a positive from a positive, OR */
  1076. /* subtract a negative from a negative. */
  1077. /* First, take the difference between their */
  1078. /* magnitudes, then... */
  1079. if (aws_mp_cmp_mag(a, b) != AWS_MP_LT) {
  1080. /* Copy the sign from the first */
  1081. c->sign = sa;
  1082. /* The first has a larger or equal magnitude */
  1083. res = aws_s_mp_sub(a, b, c);
  1084. } else {
  1085. /* The result has the *opposite* sign from */
  1086. /* the first number. */
  1087. c->sign = (sa == AWS_MP_ZPOS) ? AWS_MP_NEG : AWS_MP_ZPOS;
  1088. /* The second has a larger magnitude */
  1089. res = aws_s_mp_sub(b, a, c);
  1090. }
  1091. }
  1092. return res;
  1093. }
  1094. #endif
  1095. #ifdef AWS_BN_MP_INIT_SIZE_C
  1096. /* init an mp_init for a given size */
  1097. int aws_mp_init_size(aws_mp_int *a, int size)
  1098. {
  1099. int x;
  1100. /* pad size so there are always extra digits */
  1101. size += (AWS_MP_PREC * 2) - (size % AWS_MP_PREC);
  1102. /* alloc mem */
  1103. a->dp = AWS_OPT_CAST(aws_mp_digit) AWS_XMALLOC (sizeof (aws_mp_digit) * size);
  1104. if (a->dp == NULL) {
  1105. return AWS_MP_MEM;
  1106. }
  1107. /* set the members */
  1108. a->used = 0;
  1109. a->alloc = size;
  1110. a->sign = AWS_MP_ZPOS;
  1111. /* zero the digits */
  1112. for (x = 0; x < size; x++) {
  1113. a->dp[x] = 0;
  1114. }
  1115. return AWS_MP_OKAY;
  1116. }
  1117. #endif
  1118. #ifdef AWS_BN_MP_PRIME_NEXT_PRIME_C
  1119. /* finds the next prime after the number "a" using "t" trials
  1120. * of Miller-Rabin.
  1121. *
  1122. * bbs_style = 1 means the prime must be congruent to 3 mod 4
  1123. */
  1124. int aws_mp_prime_next_prime(aws_mp_int *a, int t, int bbs_style)
  1125. {
  1126. int err, res = 0, x, y;
  1127. aws_mp_digit res_tab[AWS_JKTM_PRIME_SIZE], step, kstep;
  1128. aws_mp_int b;
  1129. /* ensure t is valid */
  1130. if (t <= 0 || t > AWS_JKTM_PRIME_SIZE) {
  1131. return AWS_MP_VAL;
  1132. }
  1133. /* force positive */
  1134. a->sign = AWS_MP_ZPOS;
  1135. /* simple algo if a is less than the largest prime in the table */
  1136. if (aws_mp_cmp_d(a, aws_ltm_prime_tab[AWS_JKTM_PRIME_SIZE - 1]) == AWS_MP_LT) {
  1137. /* find which prime it is bigger than */
  1138. for (x = AWS_JKTM_PRIME_SIZE - 2; x >= 0; x--) {
  1139. if (aws_mp_cmp_d(a, aws_ltm_prime_tab[x]) != AWS_MP_LT) {
  1140. if (bbs_style == 1) {
  1141. /* ok we found a prime smaller or
  1142. * equal [so the next is larger]
  1143. *
  1144. * however, the prime must be
  1145. * congruent to 3 mod 4
  1146. */
  1147. if ((aws_ltm_prime_tab[x + 1] & 3) != 3) {
  1148. /* scan upwards for a prime congruent to 3 mod 4 */
  1149. for (y = x + 1; y < AWS_JKTM_PRIME_SIZE; y++) {
  1150. if ((aws_ltm_prime_tab[y] & 3) == 3) {
  1151. aws_mp_set(a, aws_ltm_prime_tab[y]);
  1152. return AWS_MP_OKAY;
  1153. }
  1154. }
  1155. }
  1156. } else {
  1157. aws_mp_set(a, aws_ltm_prime_tab[x + 1]);
  1158. return AWS_MP_OKAY;
  1159. }
  1160. }
  1161. }
  1162. /* at this point a maybe 1 */
  1163. if (aws_mp_cmp_d(a, 1) == AWS_MP_EQ) {
  1164. aws_mp_set(a, 2);
  1165. return AWS_MP_OKAY;
  1166. }
  1167. /* fall through to the sieve */
  1168. }
  1169. /* generate a prime congruent to 3 mod 4 or 1/3 mod 4? */
  1170. if (bbs_style == 1) {
  1171. kstep = 4;
  1172. } else {
  1173. kstep = 2;
  1174. }
  1175. /* at this point we will use a combination of a sieve and Miller-Rabin */
  1176. if (bbs_style == 1) {
  1177. /* if a mod 4 != 3 subtract the correct value to make it so */
  1178. if ((a->dp[0] & 3) != 3) {
  1179. if ((err = aws_mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != AWS_MP_OKAY) { return err; };
  1180. }
  1181. } else {
  1182. if (aws_mp_iseven(a) == 1) {
  1183. /* force odd */
  1184. if ((err = aws_mp_sub_d(a, 1, a)) != AWS_MP_OKAY) {
  1185. return err;
  1186. }
  1187. }
  1188. }
  1189. /* generate the restable */
  1190. for (x = 1; x < AWS_JKTM_PRIME_SIZE; x++) {
  1191. if ((err = aws_mp_mod_d(a, aws_ltm_prime_tab[x], res_tab + x)) != AWS_MP_OKAY) {
  1192. return err;
  1193. }
  1194. }
  1195. /* init temp used for Miller-Rabin Testing */
  1196. if ((err = aws_mp_init(&b)) != AWS_MP_OKAY) {
  1197. return err;
  1198. }
  1199. for (;;) {
  1200. /* skip to the next non-trivially divisible candidate */
  1201. step = 0;
  1202. do {
  1203. /* y == 1 if any residue was zero [e.g. cannot be prime] */
  1204. y = 0;
  1205. /* increase step to next candidate */
  1206. step += kstep;
  1207. /* compute the new residue without using division */
  1208. for (x = 1; x < AWS_JKTM_PRIME_SIZE; x++) {
  1209. /* add the step to each residue */
  1210. res_tab[x] += kstep;
  1211. /* subtract the modulus [instead of using division] */
  1212. if (res_tab[x] >= aws_ltm_prime_tab[x]) {
  1213. res_tab[x] -= aws_ltm_prime_tab[x];
  1214. }
  1215. /* set flag if zero */
  1216. if (res_tab[x] == 0) {
  1217. y = 1;
  1218. }
  1219. }
  1220. } while (y == 1 && step < ((((aws_mp_digit)1)<<AWS_DIGIT_BIT) - kstep));
  1221. /* add the step */
  1222. if ((err = aws_mp_add_d(a, step, a)) != AWS_MP_OKAY) {
  1223. goto LBL_ERR;
  1224. }
  1225. /* if didn't pass sieve and step == MAX then skip test */
  1226. if (y == 1 && step >= ((((aws_mp_digit)1)<<AWS_DIGIT_BIT) - kstep)) {
  1227. continue;
  1228. }
  1229. /* is this prime? */
  1230. for (x = 0; x < t; x++) {
  1231. aws_mp_set(&b, aws_ltm_prime_tab[x]);
  1232. if ((err = aws_mp_prime_miller_rabin(a, &b, &res)) != AWS_MP_OKAY) {
  1233. goto LBL_ERR;
  1234. }
  1235. if (res == AWS_MP_NO) {
  1236. break;
  1237. }
  1238. }
  1239. if (res == AWS_MP_YES) {
  1240. break;
  1241. }
  1242. }
  1243. err = AWS_MP_OKAY;
  1244. LBL_ERR:
  1245. aws_mp_clear(&b);
  1246. return err;
  1247. }
  1248. #endif
  1249. #ifdef AWS_BN_MP_INVMOD_C
  1250. /* hac 14.61, pp608 */
  1251. int aws_mp_invmod(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  1252. {
  1253. /* b cannot be negative */
  1254. if (b->sign == AWS_MP_NEG || aws_mp_iszero(b) == 1) {
  1255. return AWS_MP_VAL;
  1256. }
  1257. #ifdef AWS_BN_FAST_MP_INVMOD_C
  1258. /* if the modulus is odd we can use a faster routine instead */
  1259. if (aws_mp_isodd (b) == 1) {
  1260. return aws_fast_mp_invmod(a, b, c);
  1261. }
  1262. #endif
  1263. #ifdef AWS_BN_MP_INVMOD_SLOW_C
  1264. return aws_mp_invmod_slow(a, b, c);
  1265. #endif
  1266. return AWS_MP_VAL;
  1267. }
  1268. #endif
  1269. #ifdef AWS_BN_MP_DIV_2_C
  1270. /* b = a/2 */
  1271. int aws_mp_div_2(aws_mp_int *a, aws_mp_int *b)
  1272. {
  1273. int x, res, oldused;
  1274. /* copy */
  1275. if (b->alloc < a->used) {
  1276. if ((res = aws_mp_grow(b, a->used)) != AWS_MP_OKAY) {
  1277. return res;
  1278. }
  1279. }
  1280. oldused = b->used;
  1281. b->used = a->used;
  1282. {
  1283. register aws_mp_digit r, rr, *tmpa, *tmpb;
  1284. /* source alias */
  1285. tmpa = a->dp + b->used - 1;
  1286. /* dest alias */
  1287. tmpb = b->dp + b->used - 1;
  1288. /* carry */
  1289. r = 0;
  1290. for (x = b->used - 1; x >= 0; x--) {
  1291. /* get the carry for the next iteration */
  1292. rr = *tmpa & 1;
  1293. /* shift the current digit, add in carry and store */
  1294. *tmpb-- = (*tmpa-- >> 1) | (r << (AWS_DIGIT_BIT - 1));
  1295. /* forward carry to next iteration */
  1296. r = rr;
  1297. }
  1298. /* zero excess digits */
  1299. tmpb = b->dp + b->used;
  1300. for (x = b->used; x < oldused; x++) {
  1301. *tmpb++ = 0;
  1302. }
  1303. }
  1304. b->sign = a->sign;
  1305. aws_mp_clamp(b);
  1306. return AWS_MP_OKAY;
  1307. }
  1308. #endif
  1309. #ifdef AWS_BN_MP_INIT_MULTI_C
  1310. #include <stdarg.h>
  1311. int aws_mp_init_multi(aws_mp_int *mp, ...)
  1312. {
  1313. aws_mp_err res = AWS_MP_OKAY; /* Assume ok until proven otherwise */
  1314. int n = 0; /* Number of ok inits */
  1315. aws_mp_int * cur_arg = mp;
  1316. va_list args;
  1317. va_start(args, mp); /* init args to next argument from caller */
  1318. while (cur_arg != NULL) {
  1319. if (aws_mp_init(cur_arg) != AWS_MP_OKAY) {
  1320. /* Oops - error! Back-track and aws_mp_clear what we already
  1321. succeeded in init-ing, then return error.
  1322. */
  1323. va_list clean_args;
  1324. /* end the current list */
  1325. va_end(args);
  1326. /* now start cleaning up */
  1327. cur_arg = mp;
  1328. va_start(clean_args, mp);
  1329. while (n--) {
  1330. aws_mp_clear(cur_arg);
  1331. cur_arg = va_arg(clean_args, aws_mp_int*);
  1332. }
  1333. va_end(clean_args);
  1334. res = AWS_MP_MEM;
  1335. break;
  1336. }
  1337. n++;
  1338. cur_arg = va_arg(args, aws_mp_int*);
  1339. }
  1340. va_end(args);
  1341. return res; /* Assumed ok, if error flagged above. */
  1342. }
  1343. #endif
  1344. #ifdef AWS_BN_MP_READ_UNSIGNED_BIN_C
  1345. /* reads a unsigned char array, assumes the msb is stored first [big endian] */
  1346. int aws_mp_read_unsigned_bin(aws_mp_int *a, const unsigned char *b, int c)
  1347. {
  1348. int res;
  1349. /* make sure there are at least two digits */
  1350. if (a->alloc < 2) {
  1351. if ((res = aws_mp_grow(a, 2)) != AWS_MP_OKAY) {
  1352. return res;
  1353. }
  1354. }
  1355. /* zero the int */
  1356. aws_mp_zero(a);
  1357. /* read the bytes in */
  1358. while (c-- > 0) {
  1359. if ((res = aws_mp_mul_2d(a, 8, a)) != AWS_MP_OKAY) {
  1360. return res;
  1361. }
  1362. #ifndef AWS_MP_8BIT
  1363. a->dp[0] |= *b++;
  1364. a->used += 1;
  1365. #else
  1366. a->dp[0] = (*b & AWS_MP_MASK);
  1367. a->dp[1] |= ((*b++ >> 7U) & 1);
  1368. a->used += 2;
  1369. #endif
  1370. }
  1371. aws_mp_clamp(a);
  1372. return AWS_MP_OKAY;
  1373. }
  1374. #endif
  1375. #ifdef AWS_BN_MP_KARATSUBA_MUL_C
  1376. /* c = |a| * |b| using Karatsuba Multiplication using
  1377. * three half size multiplications
  1378. *
  1379. * Let B represent the radix [e.g. 2**AWS_DIGIT_BIT] and
  1380. * let n represent half of the number of digits in
  1381. * the min(a,b)
  1382. *
  1383. * a = a1 * B**n + a0
  1384. * b = b1 * B**n + b0
  1385. *
  1386. * Then, a * b =>
  1387. a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
  1388. *
  1389. * Note that a1b1 and a0b0 are used twice and only need to be
  1390. * computed once. So in total three half size (half # of
  1391. * digit) multiplications are performed, a0b0, a1b1 and
  1392. * (a1+b1)(a0+b0)
  1393. *
  1394. * Note that a multiplication of half the digits requires
  1395. * 1/4th the number of single precision multiplications so in
  1396. * total after one call 25% of the single precision multiplications
  1397. * are saved. Note also that the call to aws_mp_mul can end up back
  1398. * in this function if the a0, a1, b0, or b1 are above the threshold.
  1399. * This is known as divide-and-conquer and leads to the famous
  1400. * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
  1401. * the standard O(N**2) that the baseline/comba methods use.
  1402. * Generally though the overhead of this method doesn't pay off
  1403. * until a certain size (N ~ 80) is reached.
  1404. */
  1405. int aws_mp_karatsuba_mul(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  1406. {
  1407. aws_mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
  1408. int B, err;
  1409. /* default the return code to an error */
  1410. err = AWS_MP_MEM;
  1411. /* min # of digits */
  1412. B = AWS_MIN (a->used, b->used);
  1413. /* now divide in two */
  1414. B = B >> 1;
  1415. /* init copy all the temps */
  1416. if (aws_mp_init_size(&x0, B) != AWS_MP_OKAY)
  1417. goto ERR;
  1418. if (aws_mp_init_size(&x1, a->used - B) != AWS_MP_OKAY)
  1419. goto X0;
  1420. if (aws_mp_init_size(&y0, B) != AWS_MP_OKAY)
  1421. goto X1;
  1422. if (aws_mp_init_size(&y1, b->used - B) != AWS_MP_OKAY)
  1423. goto Y0;
  1424. /* init temps */
  1425. if (aws_mp_init_size(&t1, B * 2) != AWS_MP_OKAY)
  1426. goto Y1;
  1427. if (aws_mp_init_size(&x0y0, B * 2) != AWS_MP_OKAY)
  1428. goto T1;
  1429. if (aws_mp_init_size(&x1y1, B * 2) != AWS_MP_OKAY)
  1430. goto X0Y0;
  1431. /* now shift the digits */
  1432. x0.used = y0.used = B;
  1433. x1.used = a->used - B;
  1434. y1.used = b->used - B;
  1435. {
  1436. register int x;
  1437. register aws_mp_digit *tmpa, *tmpb, *tmpx, *tmpy;
  1438. /* we copy the digits directly instead of using higher level functions
  1439. * since we also need to shift the digits
  1440. */
  1441. tmpa = a->dp;
  1442. tmpb = b->dp;
  1443. tmpx = x0.dp;
  1444. tmpy = y0.dp;
  1445. for (x = 0; x < B; x++) {
  1446. *tmpx++ = *tmpa++;
  1447. *tmpy++ = *tmpb++;
  1448. }
  1449. tmpx = x1.dp;
  1450. for (x = B; x < a->used; x++) {
  1451. *tmpx++ = *tmpa++;
  1452. }
  1453. tmpy = y1.dp;
  1454. for (x = B; x < b->used; x++) {
  1455. *tmpy++ = *tmpb++;
  1456. }
  1457. }
  1458. /* only need to clamp the lower words since by definition the
  1459. * upper words x1/y1 must have a known number of digits
  1460. */
  1461. aws_mp_clamp(&x0);
  1462. aws_mp_clamp(&y0);
  1463. /* now calc the products x0y0 and x1y1 */
  1464. /* after this x0 is no longer required, free temp [x0==t2]! */
  1465. if (aws_mp_mul(&x0, &y0, &x0y0) != AWS_MP_OKAY)
  1466. goto X1Y1; /* x0y0 = x0*y0 */
  1467. if (aws_mp_mul(&x1, &y1, &x1y1) != AWS_MP_OKAY)
  1468. goto X1Y1; /* x1y1 = x1*y1 */
  1469. /* now calc x1+x0 and y1+y0 */
  1470. if (aws_s_mp_add(&x1, &x0, &t1) != AWS_MP_OKAY)
  1471. goto X1Y1; /* t1 = x1 - x0 */
  1472. if (aws_s_mp_add(&y1, &y0, &x0) != AWS_MP_OKAY)
  1473. goto X1Y1; /* t2 = y1 - y0 */
  1474. if (aws_mp_mul(&t1, &x0, &t1) != AWS_MP_OKAY)
  1475. goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
  1476. /* add x0y0 */
  1477. if (aws_mp_add(&x0y0, &x1y1, &x0) != AWS_MP_OKAY)
  1478. goto X1Y1; /* t2 = x0y0 + x1y1 */
  1479. if (aws_s_mp_sub(&t1, &x0, &t1) != AWS_MP_OKAY)
  1480. goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
  1481. /* shift by B */
  1482. if (aws_mp_lshd(&t1, B) != AWS_MP_OKAY)
  1483. goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
  1484. if (aws_mp_lshd(&x1y1, B * 2) != AWS_MP_OKAY)
  1485. goto X1Y1; /* x1y1 = x1y1 << 2*B */
  1486. if (aws_mp_add(&x0y0, &t1, &t1) != AWS_MP_OKAY)
  1487. goto X1Y1; /* t1 = x0y0 + t1 */
  1488. if (aws_mp_add(&t1, &x1y1, c) != AWS_MP_OKAY)
  1489. goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
  1490. /* Algorithm succeeded set the return code to AWS_MP_OKAY */
  1491. err = AWS_MP_OKAY;
  1492. X1Y1:
  1493. aws_mp_clear(&x1y1);
  1494. X0Y0:
  1495. aws_mp_clear(&x0y0);
  1496. T1:
  1497. aws_mp_clear(&t1);
  1498. Y1:
  1499. aws_mp_clear(&y1);
  1500. Y0:
  1501. aws_mp_clear(&y0);
  1502. X1:
  1503. aws_mp_clear(&x1);
  1504. X0:
  1505. aws_mp_clear(&x0);
  1506. ERR:
  1507. return err;
  1508. }
  1509. #endif
  1510. #ifdef AWS_BN_MP_MUL_2_C
  1511. /* b = a*2 */
  1512. int aws_mp_mul_2(aws_mp_int *a, aws_mp_int *b)
  1513. {
  1514. int x, res, oldused;
  1515. /* grow to accomodate result */
  1516. if (b->alloc < a->used + 1) {
  1517. if ((res = aws_mp_grow(b, a->used + 1)) != AWS_MP_OKAY) {
  1518. return res;
  1519. }
  1520. }
  1521. oldused = b->used;
  1522. b->used = a->used;
  1523. {
  1524. register aws_mp_digit r, rr, *tmpa, *tmpb;
  1525. /* alias for source */
  1526. tmpa = a->dp;
  1527. /* alias for dest */
  1528. tmpb = b->dp;
  1529. /* carry */
  1530. r = 0;
  1531. for (x = 0; x < a->used; x++) {
  1532. /* get what will be the *next* carry bit from the
  1533. * MSB of the current digit
  1534. */
  1535. rr = *tmpa >> ((aws_mp_digit)(AWS_DIGIT_BIT - 1));
  1536. /* now shift up this digit, add in the carry [from the previous] */
  1537. *tmpb++ = ((*tmpa++ << ((aws_mp_digit)1)) | r) & AWS_MP_MASK;
  1538. /* copy the carry that would be from the source
  1539. * digit into the next iteration
  1540. */
  1541. r = rr;
  1542. }
  1543. /* new leading digit? */
  1544. if (r != 0) {
  1545. /* add a MSB which is always 1 at this point */
  1546. *tmpb = 1;
  1547. ++(b->used);
  1548. }
  1549. /* now zero any excess digits on the destination
  1550. * that we didn't write to
  1551. */
  1552. tmpb = b->dp + b->used;
  1553. for (x = b->used; x < oldused; x++) {
  1554. *tmpb++ = 0;
  1555. }
  1556. }
  1557. b->sign = a->sign;
  1558. return AWS_MP_OKAY;
  1559. }
  1560. #endif
  1561. #ifdef AWS_BN_MP_AND_C
  1562. /* AND two ints together */
  1563. int
  1564. aws_mp_and(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  1565. {
  1566. int res, ix, px;
  1567. aws_mp_int t, *x;
  1568. if (a->used > b->used) {
  1569. if ((res = aws_mp_init_copy(&t, a)) != AWS_MP_OKAY) {
  1570. return res;
  1571. }
  1572. px = b->used;
  1573. x = b;
  1574. } else {
  1575. if ((res = aws_mp_init_copy(&t, b)) != AWS_MP_OKAY) {
  1576. return res;
  1577. }
  1578. px = a->used;
  1579. x = a;
  1580. }
  1581. for (ix = 0; ix < px; ix++) {
  1582. t.dp[ix] &= x->dp[ix];
  1583. }
  1584. /* zero digits above the last from the smallest mp_int */
  1585. for (; ix < t.used; ix++) {
  1586. t.dp[ix] = 0;
  1587. }
  1588. aws_mp_clamp(&t);
  1589. aws_mp_exch(c, &t);
  1590. aws_mp_clear(&t);
  1591. return AWS_MP_OKAY;
  1592. }
  1593. #endif
  1594. #ifdef AWS_BN_REVERSE_C
  1595. /* reverse an array, used for radix code */
  1596. void
  1597. aws_bn_reverse(unsigned char *s, int len)
  1598. {
  1599. int ix, iy;
  1600. unsigned char t;
  1601. ix = 0;
  1602. iy = len - 1;
  1603. while (ix < iy) {
  1604. t = s[ix];
  1605. s[ix] = s[iy];
  1606. s[iy] = t;
  1607. ++ix;
  1608. --iy;
  1609. }
  1610. }
  1611. #endif
  1612. #ifdef AWS_BN_MP_PRIME_RABIN_MILLER_TRIALS_C
  1613. static const struct {
  1614. int k, t;
  1615. } sizes[] = {
  1616. { 128, 28 },
  1617. { 256, 16 },
  1618. { 384, 10 },
  1619. { 512, 7 },
  1620. { 640, 6 },
  1621. { 768, 5 },
  1622. { 896, 4 },
  1623. { 1024, 4 }
  1624. };
  1625. /* returns # of RM trials required for a given bit size */
  1626. int aws_mp_prime_rabin_miller_trials(int size)
  1627. {
  1628. int x;
  1629. for (x = 0; x < (int)(sizeof(sizes)/(sizeof(sizes[0]))); x++) {
  1630. if (sizes[x].k == size) {
  1631. return sizes[x].t;
  1632. } else if (sizes[x].k > size) {
  1633. return (x == 0) ? sizes[0].t : sizes[x - 1].t;
  1634. }
  1635. }
  1636. return sizes[x-1].t + 1;
  1637. }
  1638. #endif
  1639. #ifdef AWS_BN_MP_INIT_COPY_C
  1640. /* creates "a" then copies b into it */
  1641. int aws_mp_init_copy(aws_mp_int *a, aws_mp_int *b)
  1642. {
  1643. int res;
  1644. if ((res = aws_mp_init(a)) != AWS_MP_OKAY) {
  1645. return res;
  1646. }
  1647. return aws_mp_copy(b, a);
  1648. }
  1649. #endif
  1650. #ifdef AWS_BN_FAST_MP_MONTGOMERY_REDUCE_C
  1651. /* computes xR**-1 == x (mod N) via Montgomery Reduction
  1652. *
  1653. * This is an optimized implementation of montgomery_reduce
  1654. * which uses the comba method to quickly calculate the columns of the
  1655. * reduction.
  1656. *
  1657. * Based on Algorithm 14.32 on pp.601 of HAC.
  1658. */
  1659. int aws_fast_mp_montgomery_reduce(aws_mp_int *x, aws_mp_int *n, aws_mp_digit rho)
  1660. {
  1661. int ix, res, olduse;
  1662. aws_mp_word W[AWS_MP_WARRAY] = {};
  1663. /* get old used count */
  1664. olduse = x->used;
  1665. /* grow a as required */
  1666. if (x->alloc < n->used + 1) {
  1667. if ((res = aws_mp_grow(x, n->used + 1)) != AWS_MP_OKAY) {
  1668. return res;
  1669. }
  1670. }
  1671. /* first we have to get the digits of the input into
  1672. * an array of double precision words W[...]
  1673. */
  1674. {
  1675. register aws_mp_word *_W;
  1676. register aws_mp_digit *tmpx;
  1677. /* alias for the W[] array */
  1678. _W = W;
  1679. /* alias for the digits of x*/
  1680. tmpx = x->dp;
  1681. /* copy the digits of a into W[0..a->used-1] */
  1682. for (ix = 0; ix < x->used; ix++) {
  1683. *_W++ = *tmpx++;
  1684. }
  1685. /* zero the high words of W[a->used..m->used*2] */
  1686. for (; ix < n->used * 2 + 1; ix++) {
  1687. *_W++ = 0;
  1688. }
  1689. }
  1690. /* now we proceed to zero successive digits
  1691. * from the least significant upwards
  1692. */
  1693. for (ix = 0; ix < n->used; ix++) {
  1694. /* mu = ai * m' mod b
  1695. *
  1696. * We avoid a double precision multiplication (which isn't required)
  1697. * by casting the value down to a aws_mp_digit. Note this requires
  1698. * that W[ix-1] have the carry cleared (see after the inner loop)
  1699. */
  1700. register aws_mp_digit mu;
  1701. mu = (aws_mp_digit) (((W[ix] & AWS_MP_MASK) * rho) & AWS_MP_MASK);
  1702. /* a = a + mu * m * b**i
  1703. *
  1704. * This is computed in place and on the fly. The multiplication
  1705. * by b**i is handled by offseting which columns the results
  1706. * are added to.
  1707. *
  1708. * Note the comba method normally doesn't handle carries in the
  1709. * inner loop In this case we fix the carry from the previous
  1710. * column since the Montgomery reduction requires digits of the
  1711. * result (so far) [see above] to work. This is
  1712. * handled by fixing up one carry after the inner loop. The
  1713. * carry fixups are done in order so after these loops the
  1714. * first m->used words of W[] have the carries fixed
  1715. */
  1716. {
  1717. register int iy;
  1718. register aws_mp_digit *tmpn;
  1719. register aws_mp_word *_W;
  1720. /* alias for the digits of the modulus */
  1721. tmpn = n->dp;
  1722. /* Alias for the columns set by an offset of ix */
  1723. _W = W + ix;
  1724. /* inner loop */
  1725. for (iy = 0; iy < n->used; iy++) {
  1726. *_W++ += ((aws_mp_word)mu) * ((aws_mp_word)*tmpn++);
  1727. }
  1728. }
  1729. /* now fix carry for next digit, W[ix+1] */
  1730. W[ix + 1] += W[ix] >> ((aws_mp_word) AWS_DIGIT_BIT);
  1731. }
  1732. /* now we have to propagate the carries and
  1733. * shift the words downward [all those least
  1734. * significant digits we zeroed].
  1735. */
  1736. {
  1737. register aws_mp_digit *tmpx;
  1738. register aws_mp_word *_W, *_W1;
  1739. /* nox fix rest of carries */
  1740. /* alias for current word */
  1741. _W1 = W + ix;
  1742. /* alias for next word, where the carry goes */
  1743. _W = W + ++ix;
  1744. for (; ix <= n->used * 2 + 1; ix++) {
  1745. *_W++ += *_W1++ >> ((aws_mp_word) AWS_DIGIT_BIT);
  1746. }
  1747. /* copy out, A = A/b**n
  1748. *
  1749. * The result is A/b**n but instead of converting from an
  1750. * array of aws_mp_word to aws_mp_digit than calling aws_mp_rshd
  1751. * we just copy them in the right order
  1752. */
  1753. /* alias for destination word */
  1754. tmpx = x->dp;
  1755. /* alias for shifted double precision result */
  1756. _W = W + n->used;
  1757. for (ix = 0; ix < n->used + 1; ix++) {
  1758. *tmpx++ = (aws_mp_digit)(*_W++ & ((aws_mp_word) AWS_MP_MASK));
  1759. }
  1760. /* zero oldused digits, if the input a was larger than
  1761. * m->used+1 we'll have to clear the digits
  1762. */
  1763. for (; ix < olduse; ix++) {
  1764. *tmpx++ = 0;
  1765. }
  1766. }
  1767. /* set the max used and clamp */
  1768. x->used = n->used + 1;
  1769. aws_mp_clamp(x);
  1770. /* if A >= m then A = A - m */
  1771. if (aws_mp_cmp_mag(x, n) != AWS_MP_LT) {
  1772. return aws_s_mp_sub(x, n, x);
  1773. }
  1774. return AWS_MP_OKAY;
  1775. }
  1776. #endif
  1777. #ifdef AWS_BN_MP_LSHD_C
  1778. /* shift left a certain amount of digits */
  1779. int aws_mp_lshd(aws_mp_int *a, int b)
  1780. {
  1781. int x, res;
  1782. /* if its less than zero return */
  1783. if (b <= 0) {
  1784. return AWS_MP_OKAY;
  1785. }
  1786. /* grow to fit the new digits */
  1787. if (a->alloc < a->used + b) {
  1788. if ((res = aws_mp_grow(a, a->used + b)) != AWS_MP_OKAY) {
  1789. return res;
  1790. }
  1791. }
  1792. {
  1793. register aws_mp_digit *top, *bottom;
  1794. /* increment the used by the shift amount then copy upwards */
  1795. a->used += b;
  1796. /* top */
  1797. top = a->dp + a->used - 1;
  1798. /* base */
  1799. bottom = a->dp + a->used - 1 - b;
  1800. /* much like aws_mp_rshd this is implemented using a sliding window
  1801. * except the window goes the otherway around. Copying from
  1802. * the bottom to the top. see bn_mp_rshd.c for more info.
  1803. */
  1804. for (x = a->used - 1; x >= b; x--) {
  1805. *top-- = *bottom--;
  1806. }
  1807. /* zero the lower digits */
  1808. top = a->dp;
  1809. for (x = 0; x < b; x++) {
  1810. *top++ = 0;
  1811. }
  1812. }
  1813. return AWS_MP_OKAY;
  1814. }
  1815. #endif
  1816. #ifdef AWS_BN_MP_CMP_C
  1817. /* compare two ints (signed)*/
  1818. int
  1819. aws_mp_cmp(aws_mp_int *a, aws_mp_int *b)
  1820. {
  1821. /* compare based on sign */
  1822. if (a->sign != b->sign) {
  1823. if (a->sign == AWS_MP_NEG) {
  1824. return AWS_MP_LT;
  1825. } else {
  1826. return AWS_MP_GT;
  1827. }
  1828. }
  1829. /* compare digits */
  1830. if (a->sign == AWS_MP_NEG) {
  1831. /* if negative compare opposite direction */
  1832. return aws_mp_cmp_mag(b, a);
  1833. } else {
  1834. return aws_mp_cmp_mag(a, b);
  1835. }
  1836. }
  1837. #endif
  1838. #ifdef AWS_BN_MP_COUNT_BITS_C
  1839. /* returns the number of bits in an int */
  1840. int
  1841. aws_mp_count_bits(aws_mp_int *a)
  1842. {
  1843. int r;
  1844. aws_mp_digit q;
  1845. /* shortcut */
  1846. if (a->used == 0) {
  1847. return 0;
  1848. }
  1849. /* get number of digits and add that */
  1850. r = (a->used - 1) * AWS_DIGIT_BIT;
  1851. /* take the last digit and count the bits in it */
  1852. q = a->dp[a->used - 1];
  1853. while (q > ((aws_mp_digit) 0)) {
  1854. ++r;
  1855. q >>= ((aws_mp_digit) 1);
  1856. }
  1857. return r;
  1858. }
  1859. #endif
  1860. #ifdef AWS_BN_MP_OR_C
  1861. /* OR two ints together */
  1862. int aws_mp_or(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  1863. {
  1864. int res, ix, px;
  1865. aws_mp_int t, *x;
  1866. if (a->used > b->used) {
  1867. if ((res = aws_mp_init_copy(&t, a)) != AWS_MP_OKAY) {
  1868. return res;
  1869. }
  1870. px = b->used;
  1871. x = b;
  1872. } else {
  1873. if ((res = aws_mp_init_copy(&t, b)) != AWS_MP_OKAY) {
  1874. return res;
  1875. }
  1876. px = a->used;
  1877. x = a;
  1878. }
  1879. for (ix = 0; ix < px; ix++) {
  1880. t.dp[ix] |= x->dp[ix];
  1881. }
  1882. aws_mp_clamp(&t);
  1883. aws_mp_exch(c, &t);
  1884. aws_mp_clear(&t);
  1885. return AWS_MP_OKAY;
  1886. }
  1887. #endif
  1888. #ifdef AWS_BN_MP_EXTEUCLID_C
  1889. /* Extended euclidean algorithm of (a, b) produces
  1890. a*u1 + b*u2 = u3
  1891. */
  1892. int aws_mp_exteuclid(aws_mp_int *a, aws_mp_int *b, aws_mp_int *U1, aws_mp_int *U2, aws_mp_int *U3)
  1893. {
  1894. aws_mp_int u1,u2,u3,v1,v2,v3,t1,t2,t3,q,tmp;
  1895. int err;
  1896. if ((err = aws_mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != AWS_MP_OKAY) {
  1897. return err;
  1898. }
  1899. /* initialize, (u1,u2,u3) = (1,0,a) */
  1900. aws_mp_set(&u1, 1);
  1901. if ((err = aws_mp_copy(a, &u3)) != AWS_MP_OKAY) { goto _ERR; }
  1902. /* initialize, (v1,v2,v3) = (0,1,b) */
  1903. aws_mp_set(&v2, 1);
  1904. if ((err = aws_mp_copy(b, &v3)) != AWS_MP_OKAY) { goto _ERR; }
  1905. /* loop while v3 != 0 */
  1906. while (aws_mp_iszero(&v3) == AWS_MP_NO) {
  1907. /* q = u3/v3 */
  1908. if ((err = aws_mp_div(&u3, &v3, &q, NULL)) != AWS_MP_OKAY) { goto _ERR; }
  1909. /* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
  1910. if ((err = aws_mp_mul(&v1, &q, &tmp)) != AWS_MP_OKAY) { goto _ERR; }
  1911. if ((err = aws_mp_sub(&u1, &tmp, &t1)) != AWS_MP_OKAY) { goto _ERR; }
  1912. if ((err = aws_mp_mul(&v2, &q, &tmp)) != AWS_MP_OKAY) { goto _ERR; }
  1913. if ((err = aws_mp_sub(&u2, &tmp, &t2)) != AWS_MP_OKAY) { goto _ERR; }
  1914. if ((err = aws_mp_mul(&v3, &q, &tmp)) != AWS_MP_OKAY) { goto _ERR; }
  1915. if ((err = aws_mp_sub(&u3, &tmp, &t3)) != AWS_MP_OKAY) { goto _ERR; }
  1916. /* (u1,u2,u3) = (v1,v2,v3) */
  1917. if ((err = aws_mp_copy(&v1, &u1)) != AWS_MP_OKAY) { goto _ERR; }
  1918. if ((err = aws_mp_copy(&v2, &u2)) != AWS_MP_OKAY) { goto _ERR; }
  1919. if ((err = aws_mp_copy(&v3, &u3)) != AWS_MP_OKAY) { goto _ERR; }
  1920. /* (v1,v2,v3) = (t1,t2,t3) */
  1921. if ((err = aws_mp_copy(&t1, &v1)) != AWS_MP_OKAY) { goto _ERR; }
  1922. if ((err = aws_mp_copy(&t2, &v2)) != AWS_MP_OKAY) { goto _ERR; }
  1923. if ((err = aws_mp_copy(&t3, &v3)) != AWS_MP_OKAY) { goto _ERR; }
  1924. }
  1925. /* make sure U3 >= 0 */
  1926. if (u3.sign == AWS_MP_NEG) {
  1927. aws_mp_neg(&u1, &u1);
  1928. aws_mp_neg(&u2, &u2);
  1929. aws_mp_neg(&u3, &u3);
  1930. }
  1931. /* copy result out */
  1932. if (U1 != NULL) {aws_mp_exch(U1, &u1); }
  1933. if (U2 != NULL) {aws_mp_exch(U2, &u2); }
  1934. if (U3 != NULL) {aws_mp_exch(U3, &u3); }
  1935. err = AWS_MP_OKAY;
  1936. _ERR:
  1937. aws_mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL);
  1938. return err;
  1939. }
  1940. #endif
  1941. #ifdef AWS_BN_MP_PRIME_RANDOM_EX_C
  1942. /* makes a truly random prime of a given size (bits),
  1943. *
  1944. * Flags are as follows:
  1945. *
  1946. * LTM_PRIME_BBS - make prime congruent to 3 mod 4
  1947. * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
  1948. * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
  1949. * LTM_PRIME_2MSB_ON - make the 2nd highest bit one
  1950. *
  1951. * You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
  1952. * have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
  1953. * so it can be NULL
  1954. *
  1955. */
  1956. /* This is possibly the mother of all prime generation functions, muahahahahaha! */
  1957. int aws_mp_prime_random_ex(aws_mp_int *a, int t, int size, int flags, aws_ltm_prime_callback cb, void *dat)
  1958. {
  1959. unsigned char *tmp, maskAND, maskOR_msb, maskOR_lsb;
  1960. int res, err, bsize, maskOR_msb_offset;
  1961. /* sanity check the input */
  1962. if (size <= 1 || t <= 0) {
  1963. return AWS_MP_VAL;
  1964. }
  1965. /* LTM_PRIME_SAFE implies LTM_PRIME_BBS */
  1966. if (flags & AWS_LTM_PRIME_SAFE) {
  1967. flags |= AWS_LTM_PRIME_BBS;
  1968. }
  1969. /* calc the byte size */
  1970. bsize = (size>>3) + ((size&7)?1:0);
  1971. /* we need a buffer of bsize bytes */
  1972. tmp = AWS_OPT_CAST(unsigned char) AWS_XMALLOC(bsize);
  1973. if (tmp == NULL) {
  1974. return AWS_MP_MEM;
  1975. }
  1976. /* calc the maskAND value for the MSbyte*/
  1977. maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7)));
  1978. /* calc the maskOR_msb */
  1979. maskOR_msb = 0;
  1980. maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
  1981. if (flags & AWS_LTM_PRIME_2MSB_ON) {
  1982. maskOR_msb |= 0x80 >> ((9 - size) & 7);
  1983. }
  1984. /* get the maskOR_lsb */
  1985. maskOR_lsb = 1;
  1986. if (flags & AWS_LTM_PRIME_BBS) {
  1987. maskOR_lsb |= 3;
  1988. }
  1989. do {
  1990. /* read the bytes */
  1991. if (cb(tmp, bsize, dat) != bsize) {
  1992. err = AWS_MP_VAL;
  1993. goto error;
  1994. }
  1995. /* work over the MSbyte */
  1996. tmp[0] &= maskAND;
  1997. tmp[0] |= 1 << ((size - 1) & 7);
  1998. /* mix in the maskORs */
  1999. tmp[maskOR_msb_offset] |= maskOR_msb;
  2000. tmp[bsize-1] |= maskOR_lsb;
  2001. /* read it in */
  2002. if ((err = aws_mp_read_unsigned_bin(a, tmp, bsize)) != AWS_MP_OKAY) { goto error; }
  2003. /* is it prime? */
  2004. if ((err = aws_mp_prime_is_prime(a, t, &res)) != AWS_MP_OKAY) { goto error; }
  2005. if (res == AWS_MP_NO) {
  2006. continue;
  2007. }
  2008. if (flags & AWS_LTM_PRIME_SAFE) {
  2009. /* see if (a-1)/2 is prime */
  2010. if ((err = aws_mp_sub_d(a, 1, a)) != AWS_MP_OKAY) { goto error; }
  2011. if ((err = aws_mp_div_2(a, a)) != AWS_MP_OKAY) { goto error; }
  2012. /* is it prime? */
  2013. if ((err = aws_mp_prime_is_prime(a, t, &res)) != AWS_MP_OKAY) { goto error; }
  2014. }
  2015. } while (res == AWS_MP_NO);
  2016. if (flags & AWS_LTM_PRIME_SAFE) {
  2017. /* restore a to the original value */
  2018. if ((err = aws_mp_mul_2(a, a)) != AWS_MP_OKAY) { goto error; }
  2019. if ((err = aws_mp_add_d(a, 1, a)) != AWS_MP_OKAY) { goto error; }
  2020. }
  2021. err = AWS_MP_OKAY;
  2022. error:
  2023. AWS_XFREE(tmp);
  2024. return err;
  2025. }
  2026. #endif
  2027. #ifdef AWS_BN_MP_JACOBI_C
  2028. /* computes the jacobi c = (a | n) (or Legendre if n is prime)
  2029. * HAC pp. 73 Algorithm 2.149
  2030. */
  2031. int aws_mp_jacobi(aws_mp_int *a, aws_mp_int *p, int *c)
  2032. {
  2033. aws_mp_int a1, p1;
  2034. int k, s, r, res;
  2035. aws_mp_digit residue;
  2036. /* if p <= 0 return AWS_MP_VAL */
  2037. if (aws_mp_cmp_d(p, 0) != AWS_MP_GT) {
  2038. return AWS_MP_VAL;
  2039. }
  2040. /* step 1. if a == 0, return 0 */
  2041. if (aws_mp_iszero (a) == 1) {
  2042. *c = 0;
  2043. return AWS_MP_OKAY;
  2044. }
  2045. /* step 2. if a == 1, return 1 */
  2046. if (aws_mp_cmp_d(a, 1) == AWS_MP_EQ) {
  2047. *c = 1;
  2048. return AWS_MP_OKAY;
  2049. }
  2050. /* default */
  2051. s = 0;
  2052. /* step 3. write a = a1 * 2**k */
  2053. if ((res = aws_mp_init_copy(&a1, a)) != AWS_MP_OKAY) {
  2054. return res;
  2055. }
  2056. if ((res = aws_mp_init(&p1)) != AWS_MP_OKAY) {
  2057. goto LBL_A1;
  2058. }
  2059. /* divide out larger power of two */
  2060. k = aws_mp_cnt_lsb(&a1);
  2061. if ((res = aws_mp_div_2d(&a1, k, &a1, NULL)) != AWS_MP_OKAY) {
  2062. goto LBL_P1;
  2063. }
  2064. /* step 4. if e is even set s=1 */
  2065. if ((k & 1) == 0) {
  2066. s = 1;
  2067. } else {
  2068. /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
  2069. residue = p->dp[0] & 7;
  2070. if (residue == 1 || residue == 7) {
  2071. s = 1;
  2072. } else if (residue == 3 || residue == 5) {
  2073. s = -1;
  2074. }
  2075. }
  2076. /* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
  2077. if (((p->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) {
  2078. s = -s;
  2079. }
  2080. /* if a1 == 1 we're done */
  2081. if (aws_mp_cmp_d(&a1, 1) == AWS_MP_EQ) {
  2082. *c = s;
  2083. } else {
  2084. /* n1 = n mod a1 */
  2085. if ((res = aws_mp_mod(p, &a1, &p1)) != AWS_MP_OKAY) {
  2086. goto LBL_P1;
  2087. }
  2088. if ((res = aws_mp_jacobi(&p1, &a1, &r)) != AWS_MP_OKAY) {
  2089. goto LBL_P1;
  2090. }
  2091. *c = s * r;
  2092. }
  2093. /* done */
  2094. res = AWS_MP_OKAY;
  2095. LBL_P1:
  2096. aws_mp_clear(&p1);
  2097. LBL_A1:
  2098. aws_mp_clear(&a1);
  2099. return res;
  2100. }
  2101. #endif
  2102. #ifdef AWS_BN_MP_RADIX_SIZE_C
  2103. /* returns size of ASCII reprensentation */
  2104. int aws_mp_radix_size(aws_mp_int *a, int radix, int *size)
  2105. {
  2106. int res, digs;
  2107. aws_mp_int t;
  2108. aws_mp_digit d;
  2109. *size = 0;
  2110. /* special case for binary */
  2111. if (radix == 2) {
  2112. *size = aws_mp_count_bits(a) + (a->sign == AWS_MP_NEG ? 1 : 0) + 1;
  2113. return AWS_MP_OKAY;
  2114. }
  2115. /* make sure the radix is in range */
  2116. if (radix < 2 || radix > 64) {
  2117. return AWS_MP_VAL;
  2118. }
  2119. if (aws_mp_iszero(a) == AWS_MP_YES) {
  2120. *size = 2;
  2121. return AWS_MP_OKAY;
  2122. }
  2123. /* digs is the digit count */
  2124. digs = 0;
  2125. /* if it's negative add one for the sign */
  2126. if (a->sign == AWS_MP_NEG) {
  2127. ++digs;
  2128. }
  2129. /* init a copy of the input */
  2130. if ((res = aws_mp_init_copy(&t, a)) != AWS_MP_OKAY) {
  2131. return res;
  2132. }
  2133. /* force temp to positive */
  2134. t.sign = AWS_MP_ZPOS;
  2135. /* fetch out all of the digits */
  2136. while (aws_mp_iszero (&t) == AWS_MP_NO) {
  2137. if ((res = aws_mp_div_d(&t, (aws_mp_digit) radix, &t, &d)) != AWS_MP_OKAY) {
  2138. aws_mp_clear(&t);
  2139. return res;
  2140. }
  2141. ++digs;
  2142. }
  2143. aws_mp_clear(&t);
  2144. /* return digs + 1, the 1 is for the NULL byte that would be required. */
  2145. *size = digs + 1;
  2146. return AWS_MP_OKAY;
  2147. }
  2148. #endif
  2149. #ifdef AWS_BN_MP_RSHD_C
  2150. /* shift right a certain amount of digits */
  2151. void aws_mp_rshd(aws_mp_int *a, int b)
  2152. {
  2153. int x;
  2154. /* if b <= 0 then ignore it */
  2155. if (b <= 0) {
  2156. return;
  2157. }
  2158. /* if b > used then simply zero it and return */
  2159. if (a->used <= b) {
  2160. aws_mp_zero(a);
  2161. return;
  2162. }
  2163. {
  2164. register aws_mp_digit *bottom, *top;
  2165. /* shift the digits down */
  2166. /* bottom */
  2167. bottom = a->dp;
  2168. /* top [offset into digits] */
  2169. top = a->dp + b;
  2170. /* this is implemented as a sliding window where
  2171. * the window is b-digits long and digits from
  2172. * the top of the window are copied to the bottom
  2173. *
  2174. * e.g.
  2175. b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
  2176. /\ | ---->
  2177. \-------------------/ ---->
  2178. */
  2179. for (x = 0; x < (a->used - b); x++) {
  2180. *bottom++ = *top++;
  2181. }
  2182. /* zero the top digits */
  2183. for (; x < a->used; x++) {
  2184. *bottom++ = 0;
  2185. }
  2186. }
  2187. /* remove excess digits */
  2188. a->used -= b;
  2189. }
  2190. #endif
  2191. #ifdef AWS_BN_MP_MUL_D_C
  2192. /* multiply by a digit */
  2193. int
  2194. aws_mp_mul_d(aws_mp_int *a, aws_mp_digit b, aws_mp_int *c)
  2195. {
  2196. aws_mp_digit u, *tmpa, *tmpc;
  2197. aws_mp_word r;
  2198. int ix, res, olduse;
  2199. /* make sure c is big enough to hold a*b */
  2200. if (c->alloc < a->used + 1) {
  2201. if ((res = aws_mp_grow(c, a->used + 1)) != AWS_MP_OKAY) {
  2202. return res;
  2203. }
  2204. }
  2205. /* get the original destinations used count */
  2206. olduse = c->used;
  2207. /* set the sign */
  2208. c->sign = a->sign;
  2209. /* alias for a->dp [source] */
  2210. tmpa = a->dp;
  2211. /* alias for c->dp [dest] */
  2212. tmpc = c->dp;
  2213. /* zero carry */
  2214. u = 0;
  2215. /* compute columns */
  2216. for (ix = 0; ix < a->used; ix++) {
  2217. /* compute product and carry sum for this term */
  2218. r = ((aws_mp_word) u) + ((aws_mp_word)*tmpa++) * ((aws_mp_word)b);
  2219. /* mask off higher bits to get a single digit */
  2220. *tmpc++ = (aws_mp_digit) (r & ((aws_mp_word) AWS_MP_MASK));
  2221. /* send carry into next iteration */
  2222. u = (aws_mp_digit) (r >> ((aws_mp_word) AWS_DIGIT_BIT));
  2223. }
  2224. /* store final carry [if any] and increment ix offset */
  2225. *tmpc++ = u;
  2226. ++ix;
  2227. /* now zero digits above the top */
  2228. while (ix++ < olduse) {
  2229. *tmpc++ = 0;
  2230. }
  2231. /* set used count */
  2232. c->used = a->used + 1;
  2233. aws_mp_clamp(c);
  2234. return AWS_MP_OKAY;
  2235. }
  2236. #endif
  2237. #ifdef AWS_BN_MP_MONTGOMERY_REDUCE_C
  2238. /* computes xR**-1 == x (mod N) via Montgomery Reduction */
  2239. int
  2240. aws_mp_montgomery_reduce(aws_mp_int *x, aws_mp_int *n, aws_mp_digit rho)
  2241. {
  2242. int ix, res, digs;
  2243. aws_mp_digit mu;
  2244. /* can the fast reduction [comba] method be used?
  2245. *
  2246. * Note that unlike in mul you're safely allowed *less*
  2247. * than the available columns [255 per default] since carries
  2248. * are fixed up in the inner loop.
  2249. */
  2250. digs = n->used * 2 + 1;
  2251. if ((digs < AWS_MP_WARRAY) &&
  2252. n->used <
  2253. (1 << ((CHAR_BIT * sizeof (aws_mp_word)) - (2 * AWS_DIGIT_BIT)))) {
  2254. return aws_fast_mp_montgomery_reduce(x, n, rho);
  2255. }
  2256. /* grow the input as required */
  2257. if (x->alloc < digs) {
  2258. if ((res = aws_mp_grow(x, digs)) != AWS_MP_OKAY) {
  2259. return res;
  2260. }
  2261. }
  2262. x->used = digs;
  2263. for (ix = 0; ix < n->used; ix++) {
  2264. /* mu = ai * rho mod b
  2265. *
  2266. * The value of rho must be precalculated via
  2267. * montgomery_setup() such that
  2268. * it equals -1/n0 mod b this allows the
  2269. * following inner loop to reduce the
  2270. * input one digit at a time
  2271. */
  2272. mu = (aws_mp_digit) (((aws_mp_word)x->dp[ix]) * ((aws_mp_word)rho) & AWS_MP_MASK);
  2273. /* a = a + mu * m * b**i */
  2274. {
  2275. register int iy;
  2276. register aws_mp_digit *tmpn, *tmpx, u;
  2277. register aws_mp_word r;
  2278. /* alias for digits of the modulus */
  2279. tmpn = n->dp;
  2280. /* alias for the digits of x [the input] */
  2281. tmpx = x->dp + ix;
  2282. /* set the carry to zero */
  2283. u = 0;
  2284. /* Multiply and add in place */
  2285. for (iy = 0; iy < n->used; iy++) {
  2286. /* compute product and sum */
  2287. r = ((aws_mp_word)mu) * ((aws_mp_word)*tmpn++) +
  2288. ((aws_mp_word) u) + ((aws_mp_word) * tmpx);
  2289. /* get carry */
  2290. u = (aws_mp_digit)(r >> ((aws_mp_word) AWS_DIGIT_BIT));
  2291. /* fix digit */
  2292. *tmpx++ = (aws_mp_digit)(r & ((aws_mp_word) AWS_MP_MASK));
  2293. }
  2294. /* At this point the ix'th digit of x should be zero */
  2295. /* propagate carries upwards as required*/
  2296. while (u) {
  2297. *tmpx += u;
  2298. u = *tmpx >> AWS_DIGIT_BIT;
  2299. *tmpx++ &= AWS_MP_MASK;
  2300. }
  2301. }
  2302. }
  2303. /* at this point the n.used'th least
  2304. * significant digits of x are all zero
  2305. * which means we can shift x to the
  2306. * right by n.used digits and the
  2307. * residue is unchanged.
  2308. */
  2309. /* x = x/b**n.used */
  2310. aws_mp_clamp(x);
  2311. aws_mp_rshd(x, n->used);
  2312. /* if x >= n then x = x - n */
  2313. if (aws_mp_cmp_mag(x, n) != AWS_MP_LT) {
  2314. return aws_s_mp_sub(x, n, x);
  2315. }
  2316. return AWS_MP_OKAY;
  2317. }
  2318. #endif
  2319. #ifdef AWS_BN_S_MP_EXPTMOD_C
  2320. #ifdef AWS_MP_LOW_MEM
  2321. #define TAB_SIZE 32
  2322. #else
  2323. #define TAB_SIZE 256
  2324. #endif
  2325. int aws_s_mp_exptmod(aws_mp_int *G, aws_mp_int *X, aws_mp_int *P, aws_mp_int *Y, int redmode)
  2326. {
  2327. aws_mp_int M[TAB_SIZE], res, mu;
  2328. aws_mp_digit buf;
  2329. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  2330. int (*redux)(aws_mp_int *, aws_mp_int *, aws_mp_int *);
  2331. /* find window size */
  2332. x = aws_mp_count_bits(X);
  2333. if (x <= 7) {
  2334. winsize = 2;
  2335. } else if (x <= 36) {
  2336. winsize = 3;
  2337. } else if (x <= 140) {
  2338. winsize = 4;
  2339. } else if (x <= 450) {
  2340. winsize = 5;
  2341. } else if (x <= 1303) {
  2342. winsize = 6;
  2343. } else if (x <= 3529) {
  2344. winsize = 7;
  2345. } else {
  2346. winsize = 8;
  2347. }
  2348. #ifdef AWS_MP_LOW_MEM
  2349. if (winsize > 5) {
  2350. winsize = 5;
  2351. }
  2352. #endif
  2353. /* init M array */
  2354. /* init first cell */
  2355. if ((err = aws_mp_init(&M[1])) != AWS_MP_OKAY) {
  2356. return err;
  2357. }
  2358. /* now init the second half of the array */
  2359. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  2360. if ((err = aws_mp_init(&M[x])) != AWS_MP_OKAY) {
  2361. for (y = 1<<(winsize-1); y < x; y++) {
  2362. aws_mp_clear(&M[y]);
  2363. }
  2364. aws_mp_clear(&M[1]);
  2365. return err;
  2366. }
  2367. }
  2368. /* create mu, used for Barrett reduction */
  2369. if ((err = aws_mp_init(&mu)) != AWS_MP_OKAY) {
  2370. goto LBL_M;
  2371. }
  2372. if (redmode == 0) {
  2373. if ((err = aws_mp_reduce_setup(&mu, P)) != AWS_MP_OKAY) {
  2374. goto LBL_MU;
  2375. }
  2376. redux = aws_mp_reduce;
  2377. } else {
  2378. if ((err = aws_mp_reduce_2k_setup_l(P, &mu)) != AWS_MP_OKAY) {
  2379. goto LBL_MU;
  2380. }
  2381. redux = aws_mp_reduce_2k_l;
  2382. }
  2383. /* create M table
  2384. *
  2385. * The M table contains powers of the base,
  2386. * e.g. M[x] = G**x mod P
  2387. *
  2388. * The first half of the table is not
  2389. * computed though accept for M[0] and M[1]
  2390. */
  2391. if ((err = aws_mp_mod(G, P, &M[1])) != AWS_MP_OKAY) {
  2392. goto LBL_MU;
  2393. }
  2394. /* compute the value at M[1<<(winsize-1)] by squaring
  2395. * M[1] (winsize-1) times
  2396. */
  2397. if ((err = aws_mp_copy(&M[1], &M[1 << (winsize - 1)])) != AWS_MP_OKAY) {
  2398. goto LBL_MU;
  2399. }
  2400. for (x = 0; x < (winsize - 1); x++) {
  2401. /* square it */
  2402. if ((err = aws_mp_sqr(&M[1 << (winsize - 1)],
  2403. &M[1 << (winsize - 1)])) != AWS_MP_OKAY) {
  2404. goto LBL_MU;
  2405. }
  2406. /* reduce modulo P */
  2407. if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != AWS_MP_OKAY) {
  2408. goto LBL_MU;
  2409. }
  2410. }
  2411. /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
  2412. * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
  2413. */
  2414. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  2415. if ((err = aws_mp_mul(&M[x - 1], &M[1], &M[x])) != AWS_MP_OKAY) {
  2416. goto LBL_MU;
  2417. }
  2418. if ((err = redux (&M[x], P, &mu)) != AWS_MP_OKAY) {
  2419. goto LBL_MU;
  2420. }
  2421. }
  2422. /* setup result */
  2423. if ((err = aws_mp_init(&res)) != AWS_MP_OKAY) {
  2424. goto LBL_MU;
  2425. }
  2426. aws_mp_set(&res, 1);
  2427. /* set initial mode and bit cnt */
  2428. mode = 0;
  2429. bitcnt = 1;
  2430. buf = 0;
  2431. digidx = X->used - 1;
  2432. bitcpy = 0;
  2433. bitbuf = 0;
  2434. for (;;) {
  2435. /* grab next digit as required */
  2436. if (--bitcnt == 0) {
  2437. /* if digidx == -1 we are out of digits */
  2438. if (digidx == -1) {
  2439. break;
  2440. }
  2441. /* read next digit and reset the bitcnt */
  2442. buf = X->dp[digidx--];
  2443. bitcnt = (int) AWS_DIGIT_BIT;
  2444. }
  2445. /* grab the next msb from the exponent */
  2446. y = (buf >> (aws_mp_digit)(AWS_DIGIT_BIT - 1)) & 1;
  2447. buf <<= (aws_mp_digit)1;
  2448. /* if the bit is zero and mode == 0 then we ignore it
  2449. * These represent the leading zero bits before the first 1 bit
  2450. * in the exponent. Technically this opt is not required but it
  2451. * does lower the # of trivial squaring/reductions used
  2452. */
  2453. if (mode == 0 && y == 0) {
  2454. continue;
  2455. }
  2456. /* if the bit is zero and mode == 1 then we square */
  2457. if (mode == 1 && y == 0) {
  2458. if ((err = aws_mp_sqr(&res, &res)) != AWS_MP_OKAY) {
  2459. goto LBL_RES;
  2460. }
  2461. if ((err = redux (&res, P, &mu)) != AWS_MP_OKAY) {
  2462. goto LBL_RES;
  2463. }
  2464. continue;
  2465. }
  2466. /* else we add it to the window */
  2467. bitbuf |= (y << (winsize - ++bitcpy));
  2468. mode = 2;
  2469. if (bitcpy == winsize) {
  2470. /* ok window is filled so square as required and multiply */
  2471. /* square first */
  2472. for (x = 0; x < winsize; x++) {
  2473. if ((err = aws_mp_sqr(&res, &res)) != AWS_MP_OKAY) {
  2474. goto LBL_RES;
  2475. }
  2476. if ((err = redux (&res, P, &mu)) != AWS_MP_OKAY) {
  2477. goto LBL_RES;
  2478. }
  2479. }
  2480. /* then multiply */
  2481. if ((err = aws_mp_mul(&res, &M[bitbuf], &res)) != AWS_MP_OKAY) {
  2482. goto LBL_RES;
  2483. }
  2484. if ((err = redux (&res, P, &mu)) != AWS_MP_OKAY) {
  2485. goto LBL_RES;
  2486. }
  2487. /* empty window and reset */
  2488. bitcpy = 0;
  2489. bitbuf = 0;
  2490. mode = 1;
  2491. }
  2492. }
  2493. /* if bits remain then square/multiply */
  2494. if (mode == 2 && bitcpy > 0) {
  2495. /* square then multiply if the bit is set */
  2496. for (x = 0; x < bitcpy; x++) {
  2497. if ((err = aws_mp_sqr(&res, &res)) != AWS_MP_OKAY) {
  2498. goto LBL_RES;
  2499. }
  2500. if ((err = redux (&res, P, &mu)) != AWS_MP_OKAY) {
  2501. goto LBL_RES;
  2502. }
  2503. bitbuf <<= 1;
  2504. if ((bitbuf & (1 << winsize)) != 0) {
  2505. /* then multiply */
  2506. if ((err = aws_mp_mul(&res, &M[1], &res)) != AWS_MP_OKAY) {
  2507. goto LBL_RES;
  2508. }
  2509. if ((err = redux (&res, P, &mu)) != AWS_MP_OKAY) {
  2510. goto LBL_RES;
  2511. }
  2512. }
  2513. }
  2514. }
  2515. aws_mp_exch(&res, Y);
  2516. err = AWS_MP_OKAY;
  2517. LBL_RES:
  2518. aws_mp_clear(&res);
  2519. LBL_MU:
  2520. aws_mp_clear(&mu);
  2521. LBL_M:
  2522. aws_mp_clear(&M[1]);
  2523. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  2524. aws_mp_clear(&M[x]);
  2525. }
  2526. return err;
  2527. }
  2528. #endif
  2529. #ifdef AWS_BN_MP_SUB_D_C
  2530. /* single digit subtraction */
  2531. int
  2532. aws_mp_sub_d(aws_mp_int *a, aws_mp_digit b, aws_mp_int *c)
  2533. {
  2534. aws_mp_digit *tmpa, *tmpc, mu;
  2535. int res, ix, oldused;
  2536. /* grow c as required */
  2537. if (c->alloc < a->used + 1) {
  2538. if ((res = aws_mp_grow(c, a->used + 1)) != AWS_MP_OKAY) {
  2539. return res;
  2540. }
  2541. }
  2542. /* if a is negative just do an unsigned
  2543. * addition [with fudged signs]
  2544. */
  2545. if (a->sign == AWS_MP_NEG) {
  2546. a->sign = AWS_MP_ZPOS;
  2547. res = aws_mp_add_d(a, b, c);
  2548. a->sign = c->sign = AWS_MP_NEG;
  2549. /* clamp */
  2550. aws_mp_clamp(c);
  2551. return res;
  2552. }
  2553. /* setup regs */
  2554. oldused = c->used;
  2555. tmpa = a->dp;
  2556. tmpc = c->dp;
  2557. /* if a <= b simply fix the single digit */
  2558. if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) {
  2559. if (a->used == 1) {
  2560. *tmpc++ = b - *tmpa;
  2561. } else {
  2562. *tmpc++ = b;
  2563. }
  2564. ix = 1;
  2565. /* negative/1digit */
  2566. c->sign = AWS_MP_NEG;
  2567. c->used = 1;
  2568. } else {
  2569. /* positive/size */
  2570. c->sign = AWS_MP_ZPOS;
  2571. c->used = a->used;
  2572. /* subtract first digit */
  2573. *tmpc = *tmpa++ - b;
  2574. mu = *tmpc >> (sizeof(aws_mp_digit) * CHAR_BIT - 1);
  2575. *tmpc++ &= AWS_MP_MASK;
  2576. /* handle rest of the digits */
  2577. for (ix = 1; ix < a->used; ix++) {
  2578. *tmpc = *tmpa++ - mu;
  2579. mu = *tmpc >> (sizeof(aws_mp_digit) * CHAR_BIT - 1);
  2580. *tmpc++ &= AWS_MP_MASK;
  2581. }
  2582. }
  2583. /* zero excess digits */
  2584. while (ix++ < oldused) {
  2585. *tmpc++ = 0;
  2586. }
  2587. aws_mp_clamp(c);
  2588. return AWS_MP_OKAY;
  2589. }
  2590. #endif
  2591. #ifdef AWS_BN_MP_DIV_2D_C
  2592. /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
  2593. int aws_mp_div_2d(aws_mp_int *a, int b, aws_mp_int *c, aws_mp_int *d)
  2594. {
  2595. aws_mp_digit D, r, rr;
  2596. int x, res;
  2597. aws_mp_int t;
  2598. /* if the shift count is <= 0 then we do no work */
  2599. if (b <= 0) {
  2600. res = aws_mp_copy(a, c);
  2601. if (d != NULL) {
  2602. aws_mp_zero(d);
  2603. }
  2604. return res;
  2605. }
  2606. if ((res = aws_mp_init(&t)) != AWS_MP_OKAY) {
  2607. return res;
  2608. }
  2609. /* get the remainder */
  2610. if (d != NULL) {
  2611. if ((res = aws_mp_mod_2d(a, b, &t)) != AWS_MP_OKAY) {
  2612. aws_mp_clear(&t);
  2613. return res;
  2614. }
  2615. }
  2616. /* copy */
  2617. if ((res = aws_mp_copy(a, c)) != AWS_MP_OKAY) {
  2618. aws_mp_clear(&t);
  2619. return res;
  2620. }
  2621. /* shift by as many digits in the bit count */
  2622. if (b >= (int)AWS_DIGIT_BIT) {
  2623. aws_mp_rshd(c, b / AWS_DIGIT_BIT);
  2624. }
  2625. /* shift any bit count < AWS_DIGIT_BIT */
  2626. D = (aws_mp_digit) (b % AWS_DIGIT_BIT);
  2627. if (D != 0) {
  2628. register aws_mp_digit *tmpc, mask, shift;
  2629. /* mask */
  2630. mask = (((aws_mp_digit)1) << D) - 1;
  2631. /* shift for lsb */
  2632. shift = AWS_DIGIT_BIT - D;
  2633. /* alias */
  2634. tmpc = c->dp + (c->used - 1);
  2635. /* carry */
  2636. r = 0;
  2637. for (x = c->used - 1; x >= 0; x--) {
  2638. /* get the lower bits of this word in a temp */
  2639. rr = *tmpc & mask;
  2640. /* shift the current word and mix in the carry bits from the previous word */
  2641. *tmpc = (*tmpc >> D) | (r << shift);
  2642. --tmpc;
  2643. /* set the carry to the carry bits of the current word found above */
  2644. r = rr;
  2645. }
  2646. }
  2647. aws_mp_clamp(c);
  2648. if (d != NULL) {
  2649. aws_mp_exch(&t, d);
  2650. }
  2651. aws_mp_clear(&t);
  2652. return AWS_MP_OKAY;
  2653. }
  2654. #endif
  2655. #ifdef AWS_BN_MP_ADD_D_C
  2656. /* single digit addition */
  2657. int
  2658. aws_mp_add_d(aws_mp_int *a, aws_mp_digit b, aws_mp_int *c)
  2659. {
  2660. int res, ix, oldused;
  2661. aws_mp_digit *tmpa, *tmpc, mu;
  2662. /* grow c as required */
  2663. if (c->alloc < a->used + 1) {
  2664. if ((res = aws_mp_grow(c, a->used + 1)) != AWS_MP_OKAY) {
  2665. return res;
  2666. }
  2667. }
  2668. /* if a is negative and |a| >= b, call c = |a| - b */
  2669. if (a->sign == AWS_MP_NEG && (a->used > 1 || a->dp[0] >= b)) {
  2670. /* temporarily fix sign of a */
  2671. a->sign = AWS_MP_ZPOS;
  2672. /* c = |a| - b */
  2673. res = aws_mp_sub_d(a, b, c);
  2674. /* fix sign */
  2675. a->sign = c->sign = AWS_MP_NEG;
  2676. /* clamp */
  2677. aws_mp_clamp(c);
  2678. return res;
  2679. }
  2680. /* old number of used digits in c */
  2681. oldused = c->used;
  2682. /* sign always positive */
  2683. c->sign = AWS_MP_ZPOS;
  2684. /* source alias */
  2685. tmpa = a->dp;
  2686. /* destination alias */
  2687. tmpc = c->dp;
  2688. /* if a is positive */
  2689. if (a->sign == AWS_MP_ZPOS) {
  2690. /* add digit, after this we're propagating
  2691. * the carry.
  2692. */
  2693. *tmpc = *tmpa++ + b;
  2694. mu = *tmpc >> AWS_DIGIT_BIT;
  2695. *tmpc++ &= AWS_MP_MASK;
  2696. /* now handle rest of the digits */
  2697. for (ix = 1; ix < a->used; ix++) {
  2698. *tmpc = *tmpa++ + mu;
  2699. mu = *tmpc >> AWS_DIGIT_BIT;
  2700. *tmpc++ &= AWS_MP_MASK;
  2701. }
  2702. /* set final carry */
  2703. ix++;
  2704. *tmpc++ = mu;
  2705. /* setup size */
  2706. c->used = a->used + 1;
  2707. } else {
  2708. /* a was negative and |a| < b */
  2709. c->used = 1;
  2710. /* the result is a single digit */
  2711. if (a->used == 1) {
  2712. *tmpc++ = b - a->dp[0];
  2713. } else {
  2714. *tmpc++ = b;
  2715. }
  2716. /* setup count so the clearing of oldused
  2717. * can fall through correctly
  2718. */
  2719. ix = 1;
  2720. }
  2721. /* now zero to oldused */
  2722. while (ix++ < oldused) {
  2723. *tmpc++ = 0;
  2724. }
  2725. aws_mp_clamp(c);
  2726. return AWS_MP_OKAY;
  2727. }
  2728. #endif
  2729. #ifdef AWS_BN_MP_SQR_C
  2730. /* computes b = a*a */
  2731. int
  2732. aws_mp_sqr(aws_mp_int *a, aws_mp_int *b)
  2733. {
  2734. int res;
  2735. #ifdef AWS_BN_MP_TOOM_SQR_C
  2736. /* use Toom-Cook? */
  2737. if (a->used >= AWS_TOOM_SQR_CUTOFF) {
  2738. res = aws_mp_toom_sqr(a, b);
  2739. /* Karatsuba? */
  2740. } else
  2741. #endif
  2742. #ifdef AWS_BN_MP_KARATSUBA_SQR_C
  2743. if (a->used >= AWS_KARATSUBA_SQR_CUTOFF) {
  2744. res = aws_mp_karatsuba_sqr(a, b);
  2745. } else
  2746. #endif
  2747. {
  2748. #ifdef AWS_BN_FAST_S_MP_SQR_C
  2749. /* can we use the fast comba multiplier? */
  2750. if ((a->used * 2 + 1) < AWS_MP_WARRAY &&
  2751. a->used <
  2752. (1 << (sizeof(aws_mp_word) * CHAR_BIT - 2*AWS_DIGIT_BIT - 1))) {
  2753. res = aws_fast_s_mp_sqr(a, b);
  2754. } else
  2755. #endif
  2756. #ifdef AWS_BN_S_MP_SQR_C
  2757. res = aws_s_mp_sqr(a, b);
  2758. #else
  2759. res = AWS_MP_VAL;
  2760. #endif
  2761. }
  2762. b->sign = AWS_MP_ZPOS;
  2763. return res;
  2764. }
  2765. #endif
  2766. #ifdef AWS_BN_MP_GROW_C
  2767. /* grow as required */
  2768. int aws_mp_grow(aws_mp_int *a, int size)
  2769. {
  2770. int i;
  2771. aws_mp_digit *tmp;
  2772. /* if the alloc size is smaller alloc more ram */
  2773. if (a->alloc < size) {
  2774. /* ensure there are always at least AWS_MP_PREC digits extra on top */
  2775. size += (AWS_MP_PREC * 2) - (size % AWS_MP_PREC);
  2776. /* reallocate the array a->dp
  2777. *
  2778. * We store the return in a temporary variable
  2779. * in case the operation failed we don't want
  2780. * to overwrite the dp member of a.
  2781. */
  2782. tmp = AWS_OPT_CAST(aws_mp_digit) AWS_XREALLOC (a->dp, sizeof (aws_mp_digit) * size);
  2783. if (tmp == NULL) {
  2784. /* reallocation failed but "a" is still valid [can be freed] */
  2785. return AWS_MP_MEM;
  2786. }
  2787. /* reallocation succeeded so set a->dp */
  2788. a->dp = tmp;
  2789. /* zero excess digits */
  2790. i = a->alloc;
  2791. a->alloc = size;
  2792. for (; i < a->alloc; i++) {
  2793. a->dp[i] = 0;
  2794. }
  2795. }
  2796. return AWS_MP_OKAY;
  2797. }
  2798. #endif
  2799. #ifdef AWS_BN_MP_MUL_2D_C
  2800. /* shift left by a certain bit count */
  2801. int aws_mp_mul_2d(aws_mp_int *a, int b, aws_mp_int *c)
  2802. {
  2803. aws_mp_digit d;
  2804. int res;
  2805. /* copy */
  2806. if (a != c) {
  2807. if ((res = aws_mp_copy(a, c)) != AWS_MP_OKAY) {
  2808. return res;
  2809. }
  2810. }
  2811. if (c->alloc < (int)(c->used + b/AWS_DIGIT_BIT + 1)) {
  2812. if ((res = aws_mp_grow(c, c->used + b / AWS_DIGIT_BIT + 1)) != AWS_MP_OKAY) {
  2813. return res;
  2814. }
  2815. }
  2816. /* shift by as many digits in the bit count */
  2817. if (b >= (int)AWS_DIGIT_BIT) {
  2818. if ((res = aws_mp_lshd(c, b / AWS_DIGIT_BIT)) != AWS_MP_OKAY) {
  2819. return res;
  2820. }
  2821. }
  2822. /* shift any bit count < AWS_DIGIT_BIT */
  2823. d = (aws_mp_digit) (b % AWS_DIGIT_BIT);
  2824. if (d != 0) {
  2825. register aws_mp_digit *tmpc, shift, mask, r, rr;
  2826. register int x;
  2827. /* bitmask for carries */
  2828. mask = (((aws_mp_digit)1) << d) - 1;
  2829. /* shift for msbs */
  2830. shift = AWS_DIGIT_BIT - d;
  2831. /* alias */
  2832. tmpc = c->dp;
  2833. /* carry */
  2834. r = 0;
  2835. for (x = 0; x < c->used; x++) {
  2836. /* get the higher bits of the current word */
  2837. rr = (*tmpc >> shift) & mask;
  2838. /* shift the current word and OR in the carry */
  2839. *tmpc = ((*tmpc << d) | r) & AWS_MP_MASK;
  2840. ++tmpc;
  2841. /* set the carry to the carry bits of the current word */
  2842. r = rr;
  2843. }
  2844. /* set final carry */
  2845. if (r != 0) {
  2846. c->dp[(c->used)++] = r;
  2847. }
  2848. }
  2849. aws_mp_clamp(c);
  2850. return AWS_MP_OKAY;
  2851. }
  2852. #endif
  2853. #ifdef AWS_BN_MP_CMP_D_C
  2854. /* compare a digit */
  2855. int aws_mp_cmp_d(aws_mp_int *a, aws_mp_digit b)
  2856. {
  2857. /* compare based on sign */
  2858. if (a->sign == AWS_MP_NEG) {
  2859. return AWS_MP_LT;
  2860. }
  2861. /* compare based on magnitude */
  2862. if (a->used > 1) {
  2863. return AWS_MP_GT;
  2864. }
  2865. /* compare the only digit of a to b */
  2866. if (a->dp[0] > b) {
  2867. return AWS_MP_GT;
  2868. } else if (a->dp[0] < b) {
  2869. return AWS_MP_LT;
  2870. } else {
  2871. return AWS_MP_EQ;
  2872. }
  2873. }
  2874. #endif
  2875. #ifdef AWS_BN_MP_CMP_MAG_C
  2876. /* compare maginitude of two ints (unsigned) */
  2877. int aws_mp_cmp_mag(aws_mp_int *a, aws_mp_int *b)
  2878. {
  2879. int n;
  2880. aws_mp_digit *tmpa, *tmpb;
  2881. /* compare based on # of non-zero digits */
  2882. if (a->used > b->used) {
  2883. return AWS_MP_GT;
  2884. }
  2885. if (a->used < b->used) {
  2886. return AWS_MP_LT;
  2887. }
  2888. /* alias for a */
  2889. tmpa = a->dp + (a->used - 1);
  2890. /* alias for b */
  2891. tmpb = b->dp + (a->used - 1);
  2892. /* compare based on digits */
  2893. for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
  2894. if (*tmpa > *tmpb) {
  2895. return AWS_MP_GT;
  2896. }
  2897. if (*tmpa < *tmpb) {
  2898. return AWS_MP_LT;
  2899. }
  2900. }
  2901. return AWS_MP_EQ;
  2902. }
  2903. #endif
  2904. #ifdef AWS_BN_MP_CLEAR_C
  2905. /* clear one (frees) */
  2906. void
  2907. aws_mp_clear(aws_mp_int *a)
  2908. {
  2909. int i;
  2910. /* only do anything if a hasn't been freed previously */
  2911. if (a->dp != NULL) {
  2912. /* first zero the digits */
  2913. for (i = 0; i < a->used; i++) {
  2914. a->dp[i] = 0;
  2915. }
  2916. /* free ram */
  2917. AWS_XFREE(a->dp);
  2918. /* reset members to make debugging easier */
  2919. a->dp = NULL;
  2920. a->alloc = a->used = 0;
  2921. a->sign = AWS_MP_ZPOS;
  2922. }
  2923. }
  2924. #endif
  2925. #ifdef AWS_BN_MP_INVMOD_SLOW_C
  2926. /* hac 14.61, pp608 */
  2927. int aws_mp_invmod_slow(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  2928. {
  2929. aws_mp_int x, y, u, v, A, B, C, D;
  2930. int res;
  2931. /* b cannot be negative */
  2932. if (b->sign == AWS_MP_NEG || aws_mp_iszero(b) == 1) {
  2933. return AWS_MP_VAL;
  2934. }
  2935. /* init temps */
  2936. if ((res = aws_mp_init_multi(&x, &y, &u, &v,
  2937. &A, &B, &C, &D, NULL)) != AWS_MP_OKAY) {
  2938. return res;
  2939. }
  2940. /* x = a, y = b */
  2941. if ((res = aws_mp_mod(a, b, &x)) != AWS_MP_OKAY) {
  2942. goto LBL_ERR;
  2943. }
  2944. if ((res = aws_mp_copy(b, &y)) != AWS_MP_OKAY) {
  2945. goto LBL_ERR;
  2946. }
  2947. /* 2. [modified] if x,y are both even then return an error! */
  2948. if (aws_mp_iseven (&x) == 1 && aws_mp_iseven (&y) == 1) {
  2949. res = AWS_MP_VAL;
  2950. goto LBL_ERR;
  2951. }
  2952. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  2953. if ((res = aws_mp_copy(&x, &u)) != AWS_MP_OKAY) {
  2954. goto LBL_ERR;
  2955. }
  2956. if ((res = aws_mp_copy(&y, &v)) != AWS_MP_OKAY) {
  2957. goto LBL_ERR;
  2958. }
  2959. aws_mp_set(&A, 1);
  2960. aws_mp_set(&D, 1);
  2961. top:
  2962. /* 4. while u is even do */
  2963. while (aws_mp_iseven (&u) == 1) {
  2964. /* 4.1 u = u/2 */
  2965. if ((res = aws_mp_div_2(&u, &u)) != AWS_MP_OKAY) {
  2966. goto LBL_ERR;
  2967. }
  2968. /* 4.2 if A or B is odd then */
  2969. if (aws_mp_isodd (&A) == 1 || aws_mp_isodd (&B) == 1) {
  2970. /* A = (A+y)/2, B = (B-x)/2 */
  2971. if ((res = aws_mp_add(&A, &y, &A)) != AWS_MP_OKAY) {
  2972. goto LBL_ERR;
  2973. }
  2974. if ((res = aws_mp_sub(&B, &x, &B)) != AWS_MP_OKAY) {
  2975. goto LBL_ERR;
  2976. }
  2977. }
  2978. /* A = A/2, B = B/2 */
  2979. if ((res = aws_mp_div_2(&A, &A)) != AWS_MP_OKAY) {
  2980. goto LBL_ERR;
  2981. }
  2982. if ((res = aws_mp_div_2(&B, &B)) != AWS_MP_OKAY) {
  2983. goto LBL_ERR;
  2984. }
  2985. }
  2986. /* 5. while v is even do */
  2987. while (aws_mp_iseven (&v) == 1) {
  2988. /* 5.1 v = v/2 */
  2989. if ((res = aws_mp_div_2(&v, &v)) != AWS_MP_OKAY) {
  2990. goto LBL_ERR;
  2991. }
  2992. /* 5.2 if C or D is odd then */
  2993. if (aws_mp_isodd (&C) == 1 || aws_mp_isodd (&D) == 1) {
  2994. /* C = (C+y)/2, D = (D-x)/2 */
  2995. if ((res = aws_mp_add(&C, &y, &C)) != AWS_MP_OKAY) {
  2996. goto LBL_ERR;
  2997. }
  2998. if ((res = aws_mp_sub(&D, &x, &D)) != AWS_MP_OKAY) {
  2999. goto LBL_ERR;
  3000. }
  3001. }
  3002. /* C = C/2, D = D/2 */
  3003. if ((res = aws_mp_div_2(&C, &C)) != AWS_MP_OKAY) {
  3004. goto LBL_ERR;
  3005. }
  3006. if ((res = aws_mp_div_2(&D, &D)) != AWS_MP_OKAY) {
  3007. goto LBL_ERR;
  3008. }
  3009. }
  3010. /* 6. if u >= v then */
  3011. if (aws_mp_cmp(&u, &v) != AWS_MP_LT) {
  3012. /* u = u - v, A = A - C, B = B - D */
  3013. if ((res = aws_mp_sub(&u, &v, &u)) != AWS_MP_OKAY) {
  3014. goto LBL_ERR;
  3015. }
  3016. if ((res = aws_mp_sub(&A, &C, &A)) != AWS_MP_OKAY) {
  3017. goto LBL_ERR;
  3018. }
  3019. if ((res = aws_mp_sub(&B, &D, &B)) != AWS_MP_OKAY) {
  3020. goto LBL_ERR;
  3021. }
  3022. } else {
  3023. /* v - v - u, C = C - A, D = D - B */
  3024. if ((res = aws_mp_sub(&v, &u, &v)) != AWS_MP_OKAY) {
  3025. goto LBL_ERR;
  3026. }
  3027. if ((res = aws_mp_sub(&C, &A, &C)) != AWS_MP_OKAY) {
  3028. goto LBL_ERR;
  3029. }
  3030. if ((res = aws_mp_sub(&D, &B, &D)) != AWS_MP_OKAY) {
  3031. goto LBL_ERR;
  3032. }
  3033. }
  3034. /* if not zero goto step 4 */
  3035. if (aws_mp_iszero (&u) == 0)
  3036. goto top;
  3037. /* now a = C, b = D, gcd == g*v */
  3038. /* if v != 1 then there is no inverse */
  3039. if (aws_mp_cmp_d(&v, 1) != AWS_MP_EQ) {
  3040. res = AWS_MP_VAL;
  3041. goto LBL_ERR;
  3042. }
  3043. /* if its too low */
  3044. while (aws_mp_cmp_d(&C, 0) == AWS_MP_LT) {
  3045. if ((res = aws_mp_add(&C, b, &C)) != AWS_MP_OKAY) {
  3046. goto LBL_ERR;
  3047. }
  3048. }
  3049. /* too big */
  3050. while (aws_mp_cmp_mag(&C, b) != AWS_MP_LT) {
  3051. if ((res = aws_mp_sub(&C, b, &C)) != AWS_MP_OKAY) {
  3052. goto LBL_ERR;
  3053. }
  3054. }
  3055. /* C is now the inverse */
  3056. aws_mp_exch(&C, c);
  3057. res = AWS_MP_OKAY;
  3058. LBL_ERR:
  3059. aws_mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
  3060. return res;
  3061. }
  3062. #endif
  3063. #ifdef AWS_BN_MP_DIV_C
  3064. #ifdef AWS_BN_MP_DIV_SMALL
  3065. /* slower bit-bang division... also smaller */
  3066. int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  3067. {
  3068. mp_int ta, tb, tq, q;
  3069. int res, n, n2;
  3070. /* is divisor zero ? */
  3071. if (aws_mp_iszero (b) == 1) {
  3072. return AWS_MP_VAL;
  3073. }
  3074. /* if a < b then q=0, r = a */
  3075. if (mp_cmp_mag (a, b) == AWS_MP_LT) {
  3076. if (d != NULL) {
  3077. res = mp_copy (a, d);
  3078. } else {
  3079. res = AWS_MP_OKAY;
  3080. }
  3081. if (c != NULL) {
  3082. mp_zero (c);
  3083. }
  3084. return res;
  3085. }
  3086. /* init our temps */
  3087. if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL) != AWS_MP_OKAY)) {
  3088. return res;
  3089. }
  3090. mp_set(&tq, 1);
  3091. n = mp_count_bits(a) - mp_count_bits(b);
  3092. if (((res = mp_abs(a, &ta)) != AWS_MP_OKAY) ||
  3093. ((res = mp_abs(b, &tb)) != AWS_MP_OKAY) ||
  3094. ((res = mp_mul_2d(&tb, n, &tb)) != AWS_MP_OKAY) ||
  3095. ((res = mp_mul_2d(&tq, n, &tq)) != AWS_MP_OKAY)) {
  3096. goto LBL_ERR;
  3097. }
  3098. while (n-- >= 0) {
  3099. if (mp_cmp(&tb, &ta) != AWS_MP_GT) {
  3100. if (((res = mp_sub(&ta, &tb, &ta)) != AWS_MP_OKAY) ||
  3101. ((res = mp_add(&q, &tq, &q)) != AWS_MP_OKAY)) {
  3102. goto LBL_ERR;
  3103. }
  3104. }
  3105. if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != AWS_MP_OKAY) ||
  3106. ((res = mp_div_2d(&tq, 1, &tq, NULL)) != AWS_MP_OKAY)) {
  3107. goto LBL_ERR;
  3108. }
  3109. }
  3110. /* now q == quotient and ta == remainder */
  3111. n = a->sign;
  3112. n2 = (a->sign == b->sign ? AWS_MP_ZPOS : AWS_MP_NEG);
  3113. if (c != NULL) {
  3114. mp_exch(c, &q);
  3115. c->sign = (aws_mp_iszero(c) == AWS_MP_YES) ? AWS_MP_ZPOS : n2;
  3116. }
  3117. if (d != NULL) {
  3118. mp_exch(d, &ta);
  3119. d->sign = (aws_mp_iszero(d) == AWS_MP_YES) ? AWS_MP_ZPOS : n;
  3120. }
  3121. LBL_ERR:
  3122. mp_clear_multi(&ta, &tb, &tq, &q, NULL);
  3123. return res;
  3124. }
  3125. #else
  3126. /* integer signed division.
  3127. * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
  3128. * HAC pp.598 Algorithm 14.20
  3129. *
  3130. * Note that the description in HAC is horribly
  3131. * incomplete. For example, it doesn't consider
  3132. * the case where digits are removed from 'x' in
  3133. * the inner loop. It also doesn't consider the
  3134. * case that y has fewer than three digits, etc..
  3135. *
  3136. * The overall algorithm is as described as
  3137. * 14.20 from HAC but fixed to treat these cases.
  3138. */
  3139. int aws_mp_div(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c, aws_mp_int *d)
  3140. {
  3141. aws_mp_int q, x, y, t1, t2;
  3142. int res, n, t, i, norm, neg;
  3143. /* is divisor zero ? */
  3144. if (aws_mp_iszero (b) == 1) {
  3145. return AWS_MP_VAL;
  3146. }
  3147. /* if a < b then q=0, r = a */
  3148. if (aws_mp_cmp_mag(a, b) == AWS_MP_LT) {
  3149. if (d != NULL) {
  3150. res = aws_mp_copy(a, d);
  3151. } else {
  3152. res = AWS_MP_OKAY;
  3153. }
  3154. if (c != NULL) {
  3155. aws_mp_zero(c);
  3156. }
  3157. return res;
  3158. }
  3159. if ((res = aws_mp_init_size(&q, a->used + 2)) != AWS_MP_OKAY) {
  3160. return res;
  3161. }
  3162. q.used = a->used + 2;
  3163. if ((res = aws_mp_init(&t1)) != AWS_MP_OKAY) {
  3164. goto LBL_Q;
  3165. }
  3166. if ((res = aws_mp_init(&t2)) != AWS_MP_OKAY) {
  3167. goto LBL_T1;
  3168. }
  3169. if ((res = aws_mp_init_copy(&x, a)) != AWS_MP_OKAY) {
  3170. goto LBL_T2;
  3171. }
  3172. if ((res = aws_mp_init_copy(&y, b)) != AWS_MP_OKAY) {
  3173. goto LBL_X;
  3174. }
  3175. /* fix the sign */
  3176. neg = (a->sign == b->sign) ? AWS_MP_ZPOS : AWS_MP_NEG;
  3177. x.sign = y.sign = AWS_MP_ZPOS;
  3178. /* normalize both x and y, ensure that y >= b/2, [b == 2**AWS_DIGIT_BIT] */
  3179. norm = aws_mp_count_bits(&y) % AWS_DIGIT_BIT;
  3180. if (norm < (int)(AWS_DIGIT_BIT-1)) {
  3181. norm = (AWS_DIGIT_BIT-1) - norm;
  3182. if ((res = aws_mp_mul_2d(&x, norm, &x)) != AWS_MP_OKAY) {
  3183. goto LBL_Y;
  3184. }
  3185. if ((res = aws_mp_mul_2d(&y, norm, &y)) != AWS_MP_OKAY) {
  3186. goto LBL_Y;
  3187. }
  3188. } else {
  3189. norm = 0;
  3190. }
  3191. /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
  3192. n = x.used - 1;
  3193. t = y.used - 1;
  3194. /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
  3195. if ((res = aws_mp_lshd(&y, n - t)) != AWS_MP_OKAY) { /* y = y*b**{n-t} */
  3196. goto LBL_Y;
  3197. }
  3198. while (aws_mp_cmp(&x, &y) != AWS_MP_LT) {
  3199. ++(q.dp[n - t]);
  3200. if ((res = aws_mp_sub(&x, &y, &x)) != AWS_MP_OKAY) {
  3201. goto LBL_Y;
  3202. }
  3203. }
  3204. /* reset y by shifting it back down */
  3205. aws_mp_rshd(&y, n - t);
  3206. /* step 3. for i from n down to (t + 1) */
  3207. for (i = n; i >= (t + 1); i--) {
  3208. if (i > x.used) {
  3209. continue;
  3210. }
  3211. /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
  3212. * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
  3213. if (x.dp[i] == y.dp[t]) {
  3214. q.dp[i - t - 1] = ((((aws_mp_digit)1) << AWS_DIGIT_BIT) - 1);
  3215. } else {
  3216. aws_mp_word tmp;
  3217. tmp = ((aws_mp_word) x.dp[i]) << ((aws_mp_word) AWS_DIGIT_BIT);
  3218. tmp |= ((aws_mp_word) x.dp[i - 1]);
  3219. tmp /= ((aws_mp_word) y.dp[t]);
  3220. if (tmp > (aws_mp_word) AWS_MP_MASK)
  3221. tmp = AWS_MP_MASK;
  3222. q.dp[i - t - 1] = (aws_mp_digit) (tmp & (aws_mp_word) (AWS_MP_MASK));
  3223. }
  3224. /* while (q{i-t-1} * (yt * b + y{t-1})) >
  3225. xi * b**2 + xi-1 * b + xi-2
  3226. do q{i-t-1} -= 1;
  3227. */
  3228. q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & AWS_MP_MASK;
  3229. do {
  3230. q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & AWS_MP_MASK;
  3231. /* find left hand */
  3232. aws_mp_zero(&t1);
  3233. t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1];
  3234. t1.dp[1] = y.dp[t];
  3235. t1.used = 2;
  3236. if ((res = aws_mp_mul_d(&t1, q.dp[i - t - 1], &t1)) != AWS_MP_OKAY) {
  3237. goto LBL_Y;
  3238. }
  3239. /* find right hand */
  3240. t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2];
  3241. t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1];
  3242. t2.dp[2] = x.dp[i];
  3243. t2.used = 3;
  3244. } while (aws_mp_cmp_mag(&t1, &t2) == AWS_MP_GT);
  3245. /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
  3246. if ((res = aws_mp_mul_d(&y, q.dp[i - t - 1], &t1)) != AWS_MP_OKAY) {
  3247. goto LBL_Y;
  3248. }
  3249. if ((res = aws_mp_lshd(&t1, i - t - 1)) != AWS_MP_OKAY) {
  3250. goto LBL_Y;
  3251. }
  3252. if ((res = aws_mp_sub(&x, &t1, &x)) != AWS_MP_OKAY) {
  3253. goto LBL_Y;
  3254. }
  3255. /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
  3256. if (x.sign == AWS_MP_NEG) {
  3257. if ((res = aws_mp_copy(&y, &t1)) != AWS_MP_OKAY) {
  3258. goto LBL_Y;
  3259. }
  3260. if ((res = aws_mp_lshd(&t1, i - t - 1)) != AWS_MP_OKAY) {
  3261. goto LBL_Y;
  3262. }
  3263. if ((res = aws_mp_add(&x, &t1, &x)) != AWS_MP_OKAY) {
  3264. goto LBL_Y;
  3265. }
  3266. q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & AWS_MP_MASK;
  3267. }
  3268. }
  3269. /* now q is the quotient and x is the remainder
  3270. * [which we have to normalize]
  3271. */
  3272. /* get sign before writing to c */
  3273. x.sign = x.used == 0 ? AWS_MP_ZPOS : a->sign;
  3274. if (c != NULL) {
  3275. aws_mp_clamp(&q);
  3276. aws_mp_exch(&q, c);
  3277. c->sign = neg;
  3278. }
  3279. if (d != NULL) {
  3280. aws_mp_div_2d(&x, norm, &x, NULL);
  3281. aws_mp_exch(&x, d);
  3282. }
  3283. res = AWS_MP_OKAY;
  3284. LBL_Y:
  3285. aws_mp_clear(&y);
  3286. LBL_X:
  3287. aws_mp_clear(&x);
  3288. LBL_T2:
  3289. aws_mp_clear(&t2);
  3290. LBL_T1:
  3291. aws_mp_clear(&t1);
  3292. LBL_Q:
  3293. aws_mp_clear(&q);
  3294. return res;
  3295. }
  3296. #endif
  3297. #endif
  3298. #ifdef AWS_BN_MP_REDUCE_2K_SETUP_L_C
  3299. /* determines the setup value */
  3300. int aws_mp_reduce_2k_setup_l(aws_mp_int *a, aws_mp_int *d)
  3301. {
  3302. int res;
  3303. aws_mp_int tmp;
  3304. if ((res = aws_mp_init(&tmp)) != AWS_MP_OKAY) {
  3305. return res;
  3306. }
  3307. if ((res = aws_mp_2expt(&tmp, aws_mp_count_bits(a))) != AWS_MP_OKAY) {
  3308. goto ERR;
  3309. }
  3310. if ((res = aws_s_mp_sub(&tmp, a, d)) != AWS_MP_OKAY) {
  3311. goto ERR;
  3312. }
  3313. ERR:
  3314. aws_mp_clear(&tmp);
  3315. return res;
  3316. }
  3317. #endif
  3318. #ifdef AWS_BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  3319. /*
  3320. * shifts with subtractions when the result is greater than b.
  3321. *
  3322. * The method is slightly modified to shift B unconditionally upto just under
  3323. * the leading bit of b. This saves alot of multiple precision shifting.
  3324. */
  3325. int aws_mp_montgomery_calc_normalization(aws_mp_int *a, aws_mp_int *b)
  3326. {
  3327. int x, bits, res;
  3328. /* how many bits of last digit does b use */
  3329. bits = aws_mp_count_bits(b) % AWS_DIGIT_BIT;
  3330. if (b->used > 1) {
  3331. if ((res = aws_mp_2expt(a, (b->used - 1) * AWS_DIGIT_BIT + bits - 1)) != AWS_MP_OKAY) {
  3332. return res;
  3333. }
  3334. } else {
  3335. aws_mp_set(a, 1);
  3336. bits = 1;
  3337. }
  3338. /* now compute C = A * B mod b */
  3339. for (x = bits - 1; x < (int)AWS_DIGIT_BIT; x++) {
  3340. if ((res = aws_mp_mul_2(a, a)) != AWS_MP_OKAY) {
  3341. return res;
  3342. }
  3343. if (aws_mp_cmp_mag(a, b) != AWS_MP_LT) {
  3344. if ((res = aws_s_mp_sub(a, b, a)) != AWS_MP_OKAY) {
  3345. return res;
  3346. }
  3347. }
  3348. }
  3349. return AWS_MP_OKAY;
  3350. }
  3351. #endif
  3352. #ifdef AWS_BN_MP_READ_SIGNED_BIN_C
  3353. /* read signed bin, big endian, first byte is 0==positive or 1==negative */
  3354. int aws_mp_read_signed_bin(aws_mp_int *a, const unsigned char *b, int c)
  3355. {
  3356. int res;
  3357. /* read magnitude */
  3358. if ((res = aws_mp_read_unsigned_bin(a, b + 1, c - 1)) != AWS_MP_OKAY) {
  3359. return res;
  3360. }
  3361. /* first byte is 0 for positive, non-zero for negative */
  3362. if (b[0] == 0) {
  3363. a->sign = AWS_MP_ZPOS;
  3364. } else {
  3365. a->sign = AWS_MP_NEG;
  3366. }
  3367. return AWS_MP_OKAY;
  3368. }
  3369. #endif
  3370. #ifdef AWS_BN_MP_XOR_C
  3371. /* XOR two ints together */
  3372. int
  3373. aws_mp_xor(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  3374. {
  3375. int res, ix, px;
  3376. aws_mp_int t, *x;
  3377. if (a->used > b->used) {
  3378. if ((res = aws_mp_init_copy(&t, a)) != AWS_MP_OKAY) {
  3379. return res;
  3380. }
  3381. px = b->used;
  3382. x = b;
  3383. } else {
  3384. if ((res = aws_mp_init_copy(&t, b)) != AWS_MP_OKAY) {
  3385. return res;
  3386. }
  3387. px = a->used;
  3388. x = a;
  3389. }
  3390. for (ix = 0; ix < px; ix++) {
  3391. t.dp[ix] ^= x->dp[ix];
  3392. }
  3393. aws_mp_clamp(&t);
  3394. aws_mp_exch(c, &t);
  3395. aws_mp_clear(&t);
  3396. return AWS_MP_OKAY;
  3397. }
  3398. #endif
  3399. #ifdef AWS_BN_MP_EXPTMOD_C
  3400. /* this is a shell function that calls either the normal or Montgomery
  3401. * exptmod functions. Originally the call to the montgomery code was
  3402. * embedded in the normal function but that wasted alot of stack space
  3403. * for nothing (since 99% of the time the Montgomery code would be called)
  3404. */
  3405. int aws_mp_exptmod(aws_mp_int *G, aws_mp_int *X, aws_mp_int *P, aws_mp_int *Y)
  3406. {
  3407. int dr;
  3408. /* modulus P must be positive */
  3409. if (P->sign == AWS_MP_NEG) {
  3410. return AWS_MP_VAL;
  3411. }
  3412. /* if exponent X is negative we have to recurse */
  3413. if (X->sign == AWS_MP_NEG) {
  3414. #ifdef AWS_BN_MP_INVMOD_C
  3415. aws_mp_int tmpG, tmpX;
  3416. int err;
  3417. /* first compute 1/G mod P */
  3418. if ((err = aws_mp_init(&tmpG)) != AWS_MP_OKAY) {
  3419. return err;
  3420. }
  3421. if ((err = aws_mp_invmod(G, P, &tmpG)) != AWS_MP_OKAY) {
  3422. aws_mp_clear(&tmpG);
  3423. return err;
  3424. }
  3425. /* now get |X| */
  3426. if ((err = aws_mp_init(&tmpX)) != AWS_MP_OKAY) {
  3427. aws_mp_clear(&tmpG);
  3428. return err;
  3429. }
  3430. if ((err = aws_mp_abs(X, &tmpX)) != AWS_MP_OKAY) {
  3431. aws_mp_clear_multi(&tmpG, &tmpX, NULL);
  3432. return err;
  3433. }
  3434. /* and now compute (1/G)**|X| instead of G**X [X < 0] */
  3435. err = aws_mp_exptmod(&tmpG, &tmpX, P, Y);
  3436. aws_mp_clear_multi(&tmpG, &tmpX, NULL);
  3437. return err;
  3438. #else
  3439. /* no invmod */
  3440. return AWS_MP_VAL;
  3441. #endif
  3442. }
  3443. /* modified diminished radix reduction */
  3444. #if defined(AWS_BN_MP_REDUCE_IS_2K_L_C) && defined(AWS_BN_MP_REDUCE_2K_L_C) && defined(AWS_BN_S_MP_EXPTMOD_C)
  3445. if (aws_mp_reduce_is_2k_l(P) == AWS_MP_YES) {
  3446. return aws_s_mp_exptmod(G, X, P, Y, 1);
  3447. }
  3448. #endif
  3449. #ifdef AWS_BN_MP_DR_IS_MODULUS_C
  3450. /* is it a DR modulus? */
  3451. dr = aws_mp_dr_is_modulus(P);
  3452. #else
  3453. /* default to no */
  3454. dr = 0;
  3455. #endif
  3456. #ifdef AWS_BN_MP_REDUCE_IS_2K_C
  3457. /* if not, is it a unrestricted DR modulus? */
  3458. if (dr == 0) {
  3459. dr = aws_mp_reduce_is_2k(P) << 1;
  3460. }
  3461. #endif
  3462. /* if the modulus is odd or dr != 0 use the montgomery method */
  3463. #ifdef AWS_BN_MP_EXPTMOD_FAST_C
  3464. if (aws_mp_isodd (P) == 1 || dr != 0) {
  3465. return aws_mp_exptmod_fast(G, X, P, Y, dr);
  3466. } else {
  3467. #endif
  3468. #ifdef AWS_BN_S_MP_EXPTMOD_C
  3469. /* otherwise use the generic Barrett reduction technique */
  3470. return aws_s_mp_exptmod(G, X, P, Y, 0);
  3471. #else
  3472. /* no exptmod for evens */
  3473. return AWS_MP_VAL;
  3474. #endif
  3475. #ifdef AWS_BN_MP_EXPTMOD_FAST_C
  3476. }
  3477. #endif
  3478. }
  3479. #endif
  3480. #ifdef AWS_BN_MP_PRIME_IS_PRIME_C
  3481. /* performs a variable number of rounds of Miller-Rabin
  3482. *
  3483. * Probability of error after t rounds is no more than
  3484. *
  3485. * Sets result to 1 if probably prime, 0 otherwise
  3486. */
  3487. int aws_mp_prime_is_prime(aws_mp_int *a, int t, int *result)
  3488. {
  3489. aws_mp_int b;
  3490. int ix, err, res;
  3491. /* default to no */
  3492. *result = AWS_MP_NO;
  3493. /* valid value of t? */
  3494. if (t <= 0 || t > AWS_JKTM_PRIME_SIZE) {
  3495. return AWS_MP_VAL;
  3496. }
  3497. /* is the input equal to one of the primes in the table? */
  3498. for (ix = 0; ix < AWS_JKTM_PRIME_SIZE; ix++) {
  3499. if (aws_mp_cmp_d(a, aws_ltm_prime_tab[ix]) == AWS_MP_EQ) {
  3500. *result = 1;
  3501. return AWS_MP_OKAY;
  3502. }
  3503. }
  3504. /* first perform trial division */
  3505. if ((err = aws_mp_prime_is_divisible(a, &res)) != AWS_MP_OKAY) {
  3506. return err;
  3507. }
  3508. /* return if it was trivially divisible */
  3509. if (res == AWS_MP_YES) {
  3510. return AWS_MP_OKAY;
  3511. }
  3512. /* now perform the miller-rabin rounds */
  3513. if ((err = aws_mp_init(&b)) != AWS_MP_OKAY) {
  3514. return err;
  3515. }
  3516. for (ix = 0; ix < t; ix++) {
  3517. /* set the prime */
  3518. aws_mp_set(&b, aws_ltm_prime_tab[ix]);
  3519. if ((err = aws_mp_prime_miller_rabin(a, &b, &res)) != AWS_MP_OKAY) {
  3520. goto LBL_B;
  3521. }
  3522. if (res == AWS_MP_NO) {
  3523. goto LBL_B;
  3524. }
  3525. }
  3526. /* passed the test */
  3527. *result = AWS_MP_YES;
  3528. LBL_B:
  3529. aws_mp_clear(&b);
  3530. return err;
  3531. }
  3532. #endif
  3533. #ifdef AWS_BN_MP_REDUCE_2K_L_C
  3534. /* reduces a modulo n where n is of the form 2**p - d
  3535. This differs from reduce_2k since "d" can be larger
  3536. than a single digit.
  3537. */
  3538. int aws_mp_reduce_2k_l(aws_mp_int *a, aws_mp_int *n, aws_mp_int *d)
  3539. {
  3540. aws_mp_int q;
  3541. int p, res;
  3542. if ((res = aws_mp_init(&q)) != AWS_MP_OKAY) {
  3543. return res;
  3544. }
  3545. p = aws_mp_count_bits(n);
  3546. top:
  3547. /* q = a/2**p, a = a mod 2**p */
  3548. if ((res = aws_mp_div_2d(a, p, &q, a)) != AWS_MP_OKAY) {
  3549. goto ERR;
  3550. }
  3551. /* q = q * d */
  3552. if ((res = aws_mp_mul(&q, d, &q)) != AWS_MP_OKAY) {
  3553. goto ERR;
  3554. }
  3555. /* a = a + q */
  3556. if ((res = aws_s_mp_add(a, &q, a)) != AWS_MP_OKAY) {
  3557. goto ERR;
  3558. }
  3559. if (aws_mp_cmp_mag(a, n) != AWS_MP_LT) {
  3560. aws_s_mp_sub(a, n, a);
  3561. goto top;
  3562. }
  3563. ERR:
  3564. aws_mp_clear(&q);
  3565. return res;
  3566. }
  3567. #endif
  3568. #ifdef AWS_BN_MP_EXPTMOD_FAST_C
  3569. /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
  3570. *
  3571. * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
  3572. * The value of k changes based on the size of the exponent.
  3573. *
  3574. * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
  3575. */
  3576. #ifdef AWS_MP_LOW_MEM
  3577. #define TAB_SIZE 32
  3578. #else
  3579. #define TAB_SIZE 256
  3580. #endif
  3581. int aws_mp_exptmod_fast(aws_mp_int *G, aws_mp_int *X, aws_mp_int *P, aws_mp_int *Y, int redmode)
  3582. {
  3583. aws_mp_int M[TAB_SIZE], res;
  3584. aws_mp_digit buf, mp;
  3585. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  3586. /* use a pointer to the reduction algorithm. This allows us to use
  3587. * one of many reduction algorithms without modding the guts of
  3588. * the code with if statements everywhere.
  3589. */
  3590. int (*redux)(aws_mp_int *, aws_mp_int *,aws_mp_digit);
  3591. /* find window size */
  3592. x = aws_mp_count_bits(X);
  3593. if (x <= 7) {
  3594. winsize = 2;
  3595. } else if (x <= 36) {
  3596. winsize = 3;
  3597. } else if (x <= 140) {
  3598. winsize = 4;
  3599. } else if (x <= 450) {
  3600. winsize = 5;
  3601. } else if (x <= 1303) {
  3602. winsize = 6;
  3603. } else if (x <= 3529) {
  3604. winsize = 7;
  3605. } else {
  3606. winsize = 8;
  3607. }
  3608. #ifdef AWS_MP_LOW_MEM
  3609. if (winsize > 5) {
  3610. winsize = 5;
  3611. }
  3612. #endif
  3613. /* init M array */
  3614. /* init first cell */
  3615. if ((err = aws_mp_init(&M[1])) != AWS_MP_OKAY) {
  3616. return err;
  3617. }
  3618. /* now init the second half of the array */
  3619. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  3620. if ((err = aws_mp_init(&M[x])) != AWS_MP_OKAY) {
  3621. for (y = 1<<(winsize-1); y < x; y++) {
  3622. aws_mp_clear(&M[y]);
  3623. }
  3624. aws_mp_clear(&M[1]);
  3625. return err;
  3626. }
  3627. }
  3628. /* determine and setup reduction code */
  3629. if (redmode == 0) {
  3630. #ifdef AWS_BN_MP_MONTGOMERY_SETUP_C
  3631. /* now setup montgomery */
  3632. if ((err = aws_mp_montgomery_setup(P, &mp)) != AWS_MP_OKAY) {
  3633. goto LBL_M;
  3634. }
  3635. #else
  3636. err = AWS_MP_VAL;
  3637. goto LBL_M;
  3638. #endif
  3639. /* automatically pick the comba one if available (saves quite a few calls/ifs) */
  3640. #ifdef AWS_BN_FAST_MP_MONTGOMERY_REDUCE_C
  3641. if (((P->used * 2 + 1) < AWS_MP_WARRAY) &&
  3642. P->used < (1 << ((CHAR_BIT * sizeof (aws_mp_word)) - (2 * AWS_DIGIT_BIT)))) {
  3643. redux = aws_fast_mp_montgomery_reduce;
  3644. } else
  3645. #endif
  3646. {
  3647. #ifdef AWS_BN_MP_MONTGOMERY_REDUCE_C
  3648. /* use slower baseline Montgomery method */
  3649. redux = aws_mp_montgomery_reduce;
  3650. #else
  3651. err = AWS_MP_VAL;
  3652. goto LBL_M;
  3653. #endif
  3654. }
  3655. } else if (redmode == 1) {
  3656. #if defined(AWS_BN_MP_DR_SETUP_C) && defined(AWS_BN_MP_DR_REDUCE_C)
  3657. /* setup DR reduction for moduli of the form B**k - b */
  3658. aws_mp_dr_setup(P, &mp);
  3659. redux = aws_mp_dr_reduce;
  3660. #else
  3661. err = AWS_MP_VAL;
  3662. goto LBL_M;
  3663. #endif
  3664. } else {
  3665. #if defined(AWS_BN_MP_REDUCE_2K_SETUP_C) && defined(AWS_BN_MP_REDUCE_2K_C)
  3666. /* setup DR reduction for moduli of the form 2**k - b */
  3667. if ((err = aws_mp_reduce_2k_setup(P, &mp)) != AWS_MP_OKAY) {
  3668. goto LBL_M;
  3669. }
  3670. redux = aws_mp_reduce_2k;
  3671. #else
  3672. err = AWS_MP_VAL;
  3673. goto LBL_M;
  3674. #endif
  3675. }
  3676. /* setup result */
  3677. if ((err = aws_mp_init(&res)) != AWS_MP_OKAY) {
  3678. goto LBL_M;
  3679. }
  3680. /* create M table
  3681. *
  3682. *
  3683. * The first half of the table is not computed though accept for M[0] and M[1]
  3684. */
  3685. if (redmode == 0) {
  3686. #ifdef AWS_BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  3687. /* now we need R mod m */
  3688. if ((err = aws_mp_montgomery_calc_normalization(&res, P)) != AWS_MP_OKAY) {
  3689. goto LBL_RES;
  3690. }
  3691. #else
  3692. err = AWS_MP_VAL;
  3693. goto LBL_RES;
  3694. #endif
  3695. /* now set M[1] to G * R mod m */
  3696. if ((err = aws_mp_mulmod(G, &res, P, &M[1])) != AWS_MP_OKAY) {
  3697. goto LBL_RES;
  3698. }
  3699. } else {
  3700. aws_mp_set(&res, 1);
  3701. if ((err = aws_mp_mod(G, P, &M[1])) != AWS_MP_OKAY) {
  3702. goto LBL_RES;
  3703. }
  3704. }
  3705. /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
  3706. if ((err = aws_mp_copy(&M[1], &M[1 << (winsize - 1)])) != AWS_MP_OKAY) {
  3707. goto LBL_RES;
  3708. }
  3709. for (x = 0; x < (winsize - 1); x++) {
  3710. if ((err = aws_mp_sqr(&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != AWS_MP_OKAY) {
  3711. goto LBL_RES;
  3712. }
  3713. if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != AWS_MP_OKAY) {
  3714. goto LBL_RES;
  3715. }
  3716. }
  3717. /* create upper table */
  3718. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  3719. if ((err = aws_mp_mul(&M[x - 1], &M[1], &M[x])) != AWS_MP_OKAY) {
  3720. goto LBL_RES;
  3721. }
  3722. if ((err = redux (&M[x], P, mp)) != AWS_MP_OKAY) {
  3723. goto LBL_RES;
  3724. }
  3725. }
  3726. /* set initial mode and bit cnt */
  3727. mode = 0;
  3728. bitcnt = 1;
  3729. buf = 0;
  3730. digidx = X->used - 1;
  3731. bitcpy = 0;
  3732. bitbuf = 0;
  3733. for (;;) {
  3734. /* grab next digit as required */
  3735. if (--bitcnt == 0) {
  3736. /* if digidx == -1 we are out of digits so break */
  3737. if (digidx == -1) {
  3738. break;
  3739. }
  3740. /* read next digit and reset bitcnt */
  3741. buf = X->dp[digidx--];
  3742. bitcnt = (int)AWS_DIGIT_BIT;
  3743. }
  3744. /* grab the next msb from the exponent */
  3745. y = (aws_mp_digit)(buf >> (AWS_DIGIT_BIT - 1)) & 1;
  3746. buf <<= (aws_mp_digit)1;
  3747. /* if the bit is zero and mode == 0 then we ignore it
  3748. * These represent the leading zero bits before the first 1 bit
  3749. * in the exponent. Technically this opt is not required but it
  3750. * does lower the # of trivial squaring/reductions used
  3751. */
  3752. if (mode == 0 && y == 0) {
  3753. continue;
  3754. }
  3755. /* if the bit is zero and mode == 1 then we square */
  3756. if (mode == 1 && y == 0) {
  3757. if ((err = aws_mp_sqr(&res, &res)) != AWS_MP_OKAY) {
  3758. goto LBL_RES;
  3759. }
  3760. if ((err = redux (&res, P, mp)) != AWS_MP_OKAY) {
  3761. goto LBL_RES;
  3762. }
  3763. continue;
  3764. }
  3765. /* else we add it to the window */
  3766. bitbuf |= (y << (winsize - ++bitcpy));
  3767. mode = 2;
  3768. if (bitcpy == winsize) {
  3769. /* ok window is filled so square as required and multiply */
  3770. /* square first */
  3771. for (x = 0; x < winsize; x++) {
  3772. if ((err = aws_mp_sqr(&res, &res)) != AWS_MP_OKAY) {
  3773. goto LBL_RES;
  3774. }
  3775. if ((err = redux (&res, P, mp)) != AWS_MP_OKAY) {
  3776. goto LBL_RES;
  3777. }
  3778. }
  3779. /* then multiply */
  3780. if ((err = aws_mp_mul(&res, &M[bitbuf], &res)) != AWS_MP_OKAY) {
  3781. goto LBL_RES;
  3782. }
  3783. if ((err = redux (&res, P, mp)) != AWS_MP_OKAY) {
  3784. goto LBL_RES;
  3785. }
  3786. /* empty window and reset */
  3787. bitcpy = 0;
  3788. bitbuf = 0;
  3789. mode = 1;
  3790. }
  3791. }
  3792. /* if bits remain then square/multiply */
  3793. if (mode == 2 && bitcpy > 0) {
  3794. /* square then multiply if the bit is set */
  3795. for (x = 0; x < bitcpy; x++) {
  3796. if ((err = aws_mp_sqr(&res, &res)) != AWS_MP_OKAY) {
  3797. goto LBL_RES;
  3798. }
  3799. if ((err = redux (&res, P, mp)) != AWS_MP_OKAY) {
  3800. goto LBL_RES;
  3801. }
  3802. /* get next bit of the window */
  3803. bitbuf <<= 1;
  3804. if ((bitbuf & (1 << winsize)) != 0) {
  3805. /* then multiply */
  3806. if ((err = aws_mp_mul(&res, &M[1], &res)) != AWS_MP_OKAY) {
  3807. goto LBL_RES;
  3808. }
  3809. if ((err = redux (&res, P, mp)) != AWS_MP_OKAY) {
  3810. goto LBL_RES;
  3811. }
  3812. }
  3813. }
  3814. }
  3815. if (redmode == 0) {
  3816. /* fixup result if Montgomery reduction is used
  3817. * recall that any value in a Montgomery system is
  3818. * actually multiplied by R mod n. So we have
  3819. * to reduce one more time to cancel out the factor
  3820. * of R.
  3821. */
  3822. if ((err = redux(&res, P, mp)) != AWS_MP_OKAY) {
  3823. goto LBL_RES;
  3824. }
  3825. }
  3826. /* swap res with Y */
  3827. aws_mp_exch(&res, Y);
  3828. err = AWS_MP_OKAY;
  3829. LBL_RES:
  3830. aws_mp_clear(&res);
  3831. LBL_M:
  3832. aws_mp_clear(&M[1]);
  3833. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  3834. aws_mp_clear(&M[x]);
  3835. }
  3836. return err;
  3837. }
  3838. #endif
  3839. #ifdef AWS_BN_S_MP_ADD_C
  3840. /* low level addition, based on HAC pp.594, Algorithm 14.7 */
  3841. int
  3842. aws_s_mp_add(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  3843. {
  3844. aws_mp_int *x;
  3845. int olduse, res, min, max;
  3846. /* find sizes, we let |a| <= |b| which means we have to sort
  3847. * them. "x" will point to the input with the most digits
  3848. */
  3849. if (a->used > b->used) {
  3850. min = b->used;
  3851. max = a->used;
  3852. x = a;
  3853. } else {
  3854. min = a->used;
  3855. max = b->used;
  3856. x = b;
  3857. }
  3858. /* init result */
  3859. if (c->alloc < max + 1) {
  3860. if ((res = aws_mp_grow(c, max + 1)) != AWS_MP_OKAY) {
  3861. return res;
  3862. }
  3863. }
  3864. /* get old used digit count and set new one */
  3865. olduse = c->used;
  3866. c->used = max + 1;
  3867. {
  3868. register aws_mp_digit u, *tmpa, *tmpb, *tmpc;
  3869. register int i;
  3870. /* alias for digit pointers */
  3871. /* first input */
  3872. tmpa = a->dp;
  3873. /* second input */
  3874. tmpb = b->dp;
  3875. /* destination */
  3876. tmpc = c->dp;
  3877. /* zero the carry */
  3878. u = 0;
  3879. for (i = 0; i < min; i++) {
  3880. /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
  3881. *tmpc = *tmpa++ + *tmpb++ + u;
  3882. /* U = carry bit of T[i] */
  3883. u = *tmpc >> ((aws_mp_digit)AWS_DIGIT_BIT);
  3884. /* take away carry bit from T[i] */
  3885. *tmpc++ &= AWS_MP_MASK;
  3886. }
  3887. /* now copy higher words if any, that is in A+B
  3888. * if A or B has more digits add those in
  3889. */
  3890. if (min != max) {
  3891. for (; i < max; i++) {
  3892. /* T[i] = X[i] + U */
  3893. *tmpc = x->dp[i] + u;
  3894. /* U = carry bit of T[i] */
  3895. u = *tmpc >> ((aws_mp_digit)AWS_DIGIT_BIT);
  3896. /* take away carry bit from T[i] */
  3897. *tmpc++ &= AWS_MP_MASK;
  3898. }
  3899. }
  3900. /* add carry */
  3901. *tmpc++ = u;
  3902. /* clear digits above oldused */
  3903. for (i = c->used; i < olduse; i++) {
  3904. *tmpc++ = 0;
  3905. }
  3906. }
  3907. aws_mp_clamp(c);
  3908. return AWS_MP_OKAY;
  3909. }
  3910. #endif
  3911. #ifdef AWS_BN_MP_PRIME_FERMAT_C
  3912. /* performs one Fermat test.
  3913. *
  3914. * If "a" were prime then b**a == b (mod a) since the order of
  3915. * the multiplicative sub-group would be phi(a) = a-1. That means
  3916. * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a).
  3917. *
  3918. * Sets result to 1 if the congruence holds, or zero otherwise.
  3919. */
  3920. int aws_mp_prime_fermat(aws_mp_int *a, aws_mp_int *b, int *result)
  3921. {
  3922. aws_mp_int t;
  3923. int err;
  3924. /* default to composite */
  3925. *result = AWS_MP_NO;
  3926. /* ensure b > 1 */
  3927. if (aws_mp_cmp_d(b, 1) != AWS_MP_GT) {
  3928. return AWS_MP_VAL;
  3929. }
  3930. /* init t */
  3931. if ((err = aws_mp_init(&t)) != AWS_MP_OKAY) {
  3932. return err;
  3933. }
  3934. /* compute t = b**a mod a */
  3935. if ((err = aws_mp_exptmod(b, a, a, &t)) != AWS_MP_OKAY) {
  3936. goto LBL_T;
  3937. }
  3938. /* is it equal to b? */
  3939. if (aws_mp_cmp(&t, b) == AWS_MP_EQ) {
  3940. *result = AWS_MP_YES;
  3941. }
  3942. err = AWS_MP_OKAY;
  3943. LBL_T:
  3944. aws_mp_clear(&t);
  3945. return err;
  3946. }
  3947. #endif
  3948. #ifdef AWS_BN_MP_ABS_C
  3949. /* b = |a|
  3950. *
  3951. * Simple function copies the input and fixes the sign to positive
  3952. */
  3953. int
  3954. aws_mp_abs(aws_mp_int *a, aws_mp_int *b)
  3955. {
  3956. int res;
  3957. /* copy a to b */
  3958. if (a != b) {
  3959. if ((res = aws_mp_copy(a, b)) != AWS_MP_OKAY) {
  3960. return res;
  3961. }
  3962. }
  3963. /* force the sign of b to positive */
  3964. b->sign = AWS_MP_ZPOS;
  3965. return AWS_MP_OKAY;
  3966. }
  3967. #endif
  3968. #ifdef AWS_BN_MP_CLAMP_C
  3969. /* trim unused digits
  3970. *
  3971. * This is used to ensure that leading zero digits are
  3972. * trimed and the leading "used" digit will be non-zero
  3973. * Typically very fast. Also fixes the sign if there
  3974. * are no more leading digits
  3975. */
  3976. void
  3977. aws_mp_clamp(aws_mp_int *a)
  3978. {
  3979. /* decrease used while the most significant digit is
  3980. * zero.
  3981. */
  3982. while (a->used > 0 && a->dp[a->used - 1] == 0) {
  3983. --(a->used);
  3984. }
  3985. /* reset the sign flag if used == 0 */
  3986. if (a->used == 0) {
  3987. a->sign = AWS_MP_ZPOS;
  3988. }
  3989. }
  3990. #endif
  3991. /* $Source$ */
  3992. /* $Revision: 0.41 $ */
  3993. /* $Date: 2007-04-18 09:58:18 +0000 $ */
  3994. #ifdef AWS_BN_MP_SIGNED_BIN_SIZE_C
  3995. /* get the size for an signed equivalent */
  3996. int aws_mp_signed_bin_size(aws_mp_int *a)
  3997. {
  3998. return 1 + aws_mp_unsigned_bin_size(a);
  3999. }
  4000. #endif
  4001. #ifdef AWS_BN_MP_SQRT_C
  4002. /* this function is less generic than mp_n_root, simpler and faster */
  4003. int aws_mp_sqrt(aws_mp_int *arg, aws_mp_int *ret)
  4004. {
  4005. int res;
  4006. aws_mp_int t1,t2;
  4007. /* must be positive */
  4008. if (arg->sign == AWS_MP_NEG) {
  4009. return AWS_MP_VAL;
  4010. }
  4011. /* easy out */
  4012. if (aws_mp_iszero(arg) == AWS_MP_YES) {
  4013. aws_mp_zero(ret);
  4014. return AWS_MP_OKAY;
  4015. }
  4016. if ((res = aws_mp_init_copy(&t1, arg)) != AWS_MP_OKAY) {
  4017. return res;
  4018. }
  4019. if ((res = aws_mp_init(&t2)) != AWS_MP_OKAY) {
  4020. goto E2;
  4021. }
  4022. /* First approx. (not very bad for large arg) */
  4023. aws_mp_rshd(&t1, t1.used / 2);
  4024. /* t1 > 0 */
  4025. if ((res = aws_mp_div(arg, &t1, &t2, NULL)) != AWS_MP_OKAY) {
  4026. goto E1;
  4027. }
  4028. if ((res = aws_mp_add(&t1, &t2, &t1)) != AWS_MP_OKAY) {
  4029. goto E1;
  4030. }
  4031. if ((res = aws_mp_div_2(&t1, &t1)) != AWS_MP_OKAY) {
  4032. goto E1;
  4033. }
  4034. /* And now t1 > sqrt(arg) */
  4035. do {
  4036. if ((res = aws_mp_div(arg, &t1, &t2, NULL)) != AWS_MP_OKAY) {
  4037. goto E1;
  4038. }
  4039. if ((res = aws_mp_add(&t1, &t2, &t1)) != AWS_MP_OKAY) {
  4040. goto E1;
  4041. }
  4042. if ((res = aws_mp_div_2(&t1, &t1)) != AWS_MP_OKAY) {
  4043. goto E1;
  4044. }
  4045. /* t1 >= sqrt(arg) >= t2 at this point */
  4046. } while (aws_mp_cmp_mag(&t1, &t2) == AWS_MP_GT);
  4047. aws_mp_exch(&t1, ret);
  4048. E1:
  4049. aws_mp_clear(&t2);
  4050. E2:
  4051. aws_mp_clear(&t1);
  4052. return res;
  4053. }
  4054. #endif
  4055. #ifdef AWS_BN_MP_MONTGOMERY_SETUP_C
  4056. /* setups the montgomery reduction stuff */
  4057. int
  4058. aws_mp_montgomery_setup(aws_mp_int *n, aws_mp_digit *rho)
  4059. {
  4060. aws_mp_digit x, b;
  4061. /* fast inversion mod 2**k
  4062. *
  4063. * Based on the fact that
  4064. *
  4065. * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
  4066. * => 2*X*A - X*X*A*A = 1
  4067. * => 2*(1) - (1) = 1
  4068. */
  4069. b = n->dp[0];
  4070. if ((b & 1) == 0) {
  4071. return AWS_MP_VAL;
  4072. }
  4073. x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
  4074. x *= 2 - b * x; /* here x*a==1 mod 2**8 */
  4075. #if !defined(AWS_MP_8BIT)
  4076. x *= 2 - b * x; /* here x*a==1 mod 2**16 */
  4077. #endif
  4078. #if defined(AWS_MP_64BIT) || !(defined(AWS_MP_8BIT) || defined(AWS_MP_16BIT))
  4079. x *= 2 - b * x; /* here x*a==1 mod 2**32 */
  4080. #endif
  4081. #ifdef AWS_MP_64BIT
  4082. x *= 2 - b * x; /* here x*a==1 mod 2**64 */
  4083. #endif
  4084. /* rho = -1/m mod b */
  4085. *rho = (unsigned long)(((aws_mp_word)1 << ((aws_mp_word) AWS_DIGIT_BIT)) - x) & AWS_MP_MASK;
  4086. return AWS_MP_OKAY;
  4087. }
  4088. #endif
  4089. #ifdef AWS_BN_MP_FWRITE_C
  4090. int aws_mp_fwrite(aws_mp_int *a, int radix, FILE *stream)
  4091. {
  4092. char *buf = NULL;
  4093. int err = 0, len = 0, x = 0;
  4094. if ((err = aws_mp_radix_size(a, radix, &len)) != AWS_MP_OKAY) {
  4095. return err;
  4096. }
  4097. buf = AWS_OPT_CAST(char) AWS_XMALLOC (len);
  4098. if (buf == NULL) {
  4099. return AWS_MP_MEM;
  4100. }
  4101. if ((err = aws_mp_toradix(a, buf, radix)) != AWS_MP_OKAY) {
  4102. AWS_XFREE (buf);
  4103. return err;
  4104. }
  4105. for (x = 0; x < len; x++) {
  4106. if (fputc(buf[x], stream) == EOF) {
  4107. AWS_XFREE (buf);
  4108. return AWS_MP_VAL;
  4109. }
  4110. }
  4111. AWS_XFREE (buf);
  4112. return AWS_MP_OKAY;
  4113. }
  4114. #endif
  4115. #ifdef AWS_BN_MP_EXCH_C
  4116. /* swap the elements of two integers, for cases where you can't simply swap the
  4117. * mp_int pointers around
  4118. */
  4119. void
  4120. aws_mp_exch(aws_mp_int *a, aws_mp_int *b)
  4121. {
  4122. aws_mp_int t;
  4123. t = *a;
  4124. *a = *b;
  4125. *b = t;
  4126. }
  4127. #endif
  4128. #ifdef AWS_BN_PRIME_TAB_C
  4129. const aws_mp_digit aws_ltm_prime_tab[] = {
  4130. 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
  4131. 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
  4132. 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
  4133. 0x0061, 0x0065, 0x0067, 0x006B, 0x006D, 0x0071, 0x007F,
  4134. #ifndef AWS_MP_8BIT
  4135. 0x0083,
  4136. 0x0089, 0x008B, 0x0095, 0x0097, 0x009D, 0x00A3, 0x00A7, 0x00AD,
  4137. 0x00B3, 0x00B5, 0x00BF, 0x00C1, 0x00C5, 0x00C7, 0x00D3, 0x00DF,
  4138. 0x00E3, 0x00E5, 0x00E9, 0x00EF, 0x00F1, 0x00FB, 0x0101, 0x0107,
  4139. 0x010D, 0x010F, 0x0115, 0x0119, 0x011B, 0x0125, 0x0133, 0x0137,
  4140. 0x0139, 0x013D, 0x014B, 0x0151, 0x015B, 0x015D, 0x0161, 0x0167,
  4141. 0x016F, 0x0175, 0x017B, 0x017F, 0x0185, 0x018D, 0x0191, 0x0199,
  4142. 0x01A3, 0x01A5, 0x01AF, 0x01B1, 0x01B7, 0x01BB, 0x01C1, 0x01C9,
  4143. 0x01CD, 0x01CF, 0x01D3, 0x01DF, 0x01E7, 0x01EB, 0x01F3, 0x01F7,
  4144. 0x01FD, 0x0209, 0x020B, 0x021D, 0x0223, 0x022D, 0x0233, 0x0239,
  4145. 0x023B, 0x0241, 0x024B, 0x0251, 0x0257, 0x0259, 0x025F, 0x0265,
  4146. 0x0269, 0x026B, 0x0277, 0x0281, 0x0283, 0x0287, 0x028D, 0x0293,
  4147. 0x0295, 0x02A1, 0x02A5, 0x02AB, 0x02B3, 0x02BD, 0x02C5, 0x02CF,
  4148. 0x02D7, 0x02DD, 0x02E3, 0x02E7, 0x02EF, 0x02F5, 0x02F9, 0x0301,
  4149. 0x0305, 0x0313, 0x031D, 0x0329, 0x032B, 0x0335, 0x0337, 0x033B,
  4150. 0x033D, 0x0347, 0x0355, 0x0359, 0x035B, 0x035F, 0x036D, 0x0371,
  4151. 0x0373, 0x0377, 0x038B, 0x038F, 0x0397, 0x03A1, 0x03A9, 0x03AD,
  4152. 0x03B3, 0x03B9, 0x03C7, 0x03CB, 0x03D1, 0x03D7, 0x03DF, 0x03E5,
  4153. 0x03F1, 0x03F5, 0x03FB, 0x03FD, 0x0407, 0x0409, 0x040F, 0x0419,
  4154. 0x041B, 0x0425, 0x0427, 0x042D, 0x043F, 0x0443, 0x0445, 0x0449,
  4155. 0x044F, 0x0455, 0x045D, 0x0463, 0x0469, 0x047F, 0x0481, 0x048B,
  4156. 0x0493, 0x049D, 0x04A3, 0x04A9, 0x04B1, 0x04BD, 0x04C1, 0x04C7,
  4157. 0x04CD, 0x04CF, 0x04D5, 0x04E1, 0x04EB, 0x04FD, 0x04FF, 0x0503,
  4158. 0x0509, 0x050B, 0x0511, 0x0515, 0x0517, 0x051B, 0x0527, 0x0529,
  4159. 0x052F, 0x0551, 0x0557, 0x055D, 0x0565, 0x0577, 0x0581, 0x058F,
  4160. 0x0593, 0x0595, 0x0599, 0x059F, 0x05A7, 0x05AB, 0x05AD, 0x05B3,
  4161. 0x05BF, 0x05C9, 0x05CB, 0x05CF, 0x05D1, 0x05D5, 0x05DB, 0x05E7,
  4162. 0x05F3, 0x05FB, 0x0607, 0x060D, 0x0611, 0x0617, 0x061F, 0x0623,
  4163. 0x062B, 0x062F, 0x063D, 0x0641, 0x0647, 0x0649, 0x064D, 0x0653
  4164. #endif
  4165. };
  4166. #endif
  4167. #ifdef AWS_BN_MP_INIT_SET_C
  4168. /* initialize and set a digit */
  4169. int aws_mp_init_set(aws_mp_int *a, aws_mp_digit b)
  4170. {
  4171. int err;
  4172. if ((err = aws_mp_init(a)) != AWS_MP_OKAY) {
  4173. return err;
  4174. }
  4175. aws_mp_set(a, b);
  4176. return err;
  4177. }
  4178. #endif
  4179. #ifdef AWS_BN_MP_GET_INT_C
  4180. /* get the lower 32-bits of an mp_int */
  4181. unsigned long aws_mp_get_int(aws_mp_int *a)
  4182. {
  4183. int i;
  4184. unsigned long res;
  4185. if (a->used == 0) {
  4186. return 0;
  4187. }
  4188. /* get number of digits of the lsb we have to read */
  4189. i = AWS_MIN(a->used,(int)((sizeof(unsigned long)*CHAR_BIT+AWS_DIGIT_BIT-1)/AWS_DIGIT_BIT))-1;
  4190. /* get most significant digit of result */
  4191. res = AWS_JKTM_DIGIT(a,i);
  4192. while (--i >= 0) {
  4193. res = (res << AWS_DIGIT_BIT) | AWS_JKTM_DIGIT(a,i);
  4194. }
  4195. /* force result to 32-bits always so it is consistent on non 32-bit platforms */
  4196. return res & 0xFFFFFFFFUL;
  4197. }
  4198. #endif
  4199. #ifdef AWS_BN_MP_MUL_C
  4200. /* high level multiplication (handles sign) */
  4201. int aws_mp_mul(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  4202. {
  4203. int res, neg;
  4204. neg = (a->sign == b->sign) ? AWS_MP_ZPOS : AWS_MP_NEG;
  4205. /* use Toom-Cook? */
  4206. #ifdef AWS_BN_MP_TOOM_MUL_C
  4207. if (AWS_MIN (a->used, b->used) >= AWS_TOOM_MUL_CUTOFF) {
  4208. res = aws_mp_toom_mul(a, b, c);
  4209. } else
  4210. #endif
  4211. #ifdef AWS_BN_MP_KARATSUBA_MUL_C
  4212. /* use Karatsuba? */
  4213. if (AWS_MIN (a->used, b->used) >= AWS_KARATSUBA_MUL_CUTOFF) {
  4214. res = aws_mp_karatsuba_mul(a, b, c);
  4215. } else
  4216. #endif
  4217. {
  4218. /* can we use the fast multiplier?
  4219. *
  4220. * The fast multiplier can be used if the output will
  4221. * have less than AWS_MP_WARRAY digits and the number of
  4222. * digits won't affect carry propagation
  4223. */
  4224. int digs = a->used + b->used + 1;
  4225. #ifdef AWS_BN_FAST_S_MP_MUL_DIGS_C
  4226. if ((digs < AWS_MP_WARRAY) &&
  4227. AWS_MIN(a->used, b->used) <=
  4228. (1 << ((CHAR_BIT * sizeof (aws_mp_word)) - (2 * AWS_DIGIT_BIT)))) {
  4229. res = aws_fast_s_mp_mul_digs(a, b, c, digs);
  4230. } else
  4231. #endif
  4232. #ifdef AWS_BN_S_MP_MUL_DIGS_C
  4233. res = aws_s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
  4234. #else
  4235. res = AWS_MP_VAL;
  4236. #endif
  4237. }
  4238. c->sign = (c->used > 0) ? neg : AWS_MP_ZPOS;
  4239. return res;
  4240. }
  4241. #endif
  4242. #ifdef AWS_BN_MP_SQRMOD_C
  4243. /* c = a * a (mod b) */
  4244. int
  4245. aws_mp_sqrmod(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  4246. {
  4247. int res;
  4248. aws_mp_int t;
  4249. if ((res = aws_mp_init(&t)) != AWS_MP_OKAY) {
  4250. return res;
  4251. }
  4252. if ((res = aws_mp_sqr(a, &t)) != AWS_MP_OKAY) {
  4253. aws_mp_clear(&t);
  4254. return res;
  4255. }
  4256. res = aws_mp_mod(&t, b, c);
  4257. aws_mp_clear(&t);
  4258. return res;
  4259. }
  4260. #endif
  4261. #ifdef AWS_BN_MP_TO_UNSIGNED_BIN_C
  4262. /* store in unsigned [big endian] format */
  4263. int aws_mp_to_unsigned_bin(aws_mp_int *a, unsigned char *b)
  4264. {
  4265. int x, res;
  4266. aws_mp_int t;
  4267. if ((res = aws_mp_init_copy(&t, a)) != AWS_MP_OKAY) {
  4268. return res;
  4269. }
  4270. x = 0;
  4271. while (aws_mp_iszero (&t) == 0) {
  4272. #ifndef AWS_MP_8BIT
  4273. b[x++] = (unsigned char) (t.dp[0] & 255);
  4274. #else
  4275. b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7));
  4276. #endif
  4277. if ((res = aws_mp_div_2d(&t, 8, &t, NULL)) != AWS_MP_OKAY) {
  4278. aws_mp_clear(&t);
  4279. return res;
  4280. }
  4281. }
  4282. aws_bn_reverse(b, x);
  4283. aws_mp_clear(&t);
  4284. return AWS_MP_OKAY;
  4285. }
  4286. #endif
  4287. #ifdef AWS_BN_S_MP_SQR_C
  4288. /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
  4289. int aws_s_mp_sqr(aws_mp_int *a, aws_mp_int *b)
  4290. {
  4291. aws_mp_int t;
  4292. int res, ix, iy, pa;
  4293. aws_mp_word r;
  4294. aws_mp_digit u, tmpx, *tmpt;
  4295. pa = a->used;
  4296. if ((res = aws_mp_init_size(&t, 2 * pa + 1)) != AWS_MP_OKAY) {
  4297. return res;
  4298. }
  4299. /* default used is maximum possible size */
  4300. t.used = 2*pa + 1;
  4301. for (ix = 0; ix < pa; ix++) {
  4302. /* first calculate the digit at 2*ix */
  4303. /* calculate double precision result */
  4304. r = ((aws_mp_word) t.dp[2*ix]) +
  4305. ((aws_mp_word)a->dp[ix])*((aws_mp_word)a->dp[ix]);
  4306. /* store lower part in result */
  4307. t.dp[ix+ix] = (aws_mp_digit) (r & ((aws_mp_word) AWS_MP_MASK));
  4308. /* get the carry */
  4309. u = (aws_mp_digit)(r >> ((aws_mp_word) AWS_DIGIT_BIT));
  4310. /* left hand side of A[ix] * A[iy] */
  4311. tmpx = a->dp[ix];
  4312. /* alias for where to store the results */
  4313. tmpt = t.dp + (2*ix + 1);
  4314. for (iy = ix + 1; iy < pa; iy++) {
  4315. /* first calculate the product */
  4316. r = ((aws_mp_word)tmpx) * ((aws_mp_word)a->dp[iy]);
  4317. /* now calculate the double precision result, note we use
  4318. * addition instead of *2 since it's easier to optimize
  4319. */
  4320. r = ((aws_mp_word) *tmpt) + r + r + ((aws_mp_word) u);
  4321. /* store lower part */
  4322. *tmpt++ = (aws_mp_digit) (r & ((aws_mp_word) AWS_MP_MASK));
  4323. /* get carry */
  4324. u = (aws_mp_digit)(r >> ((aws_mp_word) AWS_DIGIT_BIT));
  4325. }
  4326. /* propagate upwards */
  4327. while (u != ((aws_mp_digit) 0)) {
  4328. r = ((aws_mp_word) *tmpt) + ((aws_mp_word) u);
  4329. *tmpt++ = (aws_mp_digit) (r & ((aws_mp_word) AWS_MP_MASK));
  4330. u = (aws_mp_digit)(r >> ((aws_mp_word) AWS_DIGIT_BIT));
  4331. }
  4332. }
  4333. aws_mp_clamp(&t);
  4334. aws_mp_exch(&t, b);
  4335. aws_mp_clear(&t);
  4336. return AWS_MP_OKAY;
  4337. }
  4338. #endif
  4339. #ifdef AWS_BN_MP_NEG_C
  4340. /* b = -a */
  4341. int aws_mp_neg(aws_mp_int *a, aws_mp_int *b)
  4342. {
  4343. int res;
  4344. if (a != b) {
  4345. if ((res = aws_mp_copy(a, b)) != AWS_MP_OKAY) {
  4346. return res;
  4347. }
  4348. }
  4349. if (aws_mp_iszero(b) != AWS_MP_YES) {
  4350. b->sign = (a->sign == AWS_MP_ZPOS) ? AWS_MP_NEG : AWS_MP_ZPOS;
  4351. } else {
  4352. b->sign = AWS_MP_ZPOS;
  4353. }
  4354. return AWS_MP_OKAY;
  4355. }
  4356. #endif
  4357. #ifdef AWS_BN_MP_RADIX_SMAP_C
  4358. /* chars used in radix conversions */
  4359. const char *aws_mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
  4360. #endif
  4361. #ifdef AWS_BN_FAST_S_MP_MUL_DIGS_C
  4362. /* Fast (comba) multiplier
  4363. *
  4364. * This is the fast column-array [comba] multiplier. It is
  4365. * designed to compute the columns of the product first
  4366. * then handle the carries afterwards. This has the effect
  4367. * of making the nested loops that compute the columns very
  4368. * simple and schedulable on super-scalar processors.
  4369. *
  4370. * This has been modified to produce a variable number of
  4371. * digits of output so if say only a half-product is required
  4372. * you don't have to compute the upper half (a feature
  4373. * required for fast Barrett reduction).
  4374. *
  4375. * Based on Algorithm 14.12 on pp.595 of HAC.
  4376. *
  4377. */
  4378. int aws_fast_s_mp_mul_digs(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c, int digs)
  4379. {
  4380. int olduse, res, pa, ix, iz;
  4381. aws_mp_digit W[AWS_MP_WARRAY] = {};
  4382. register aws_mp_word _W;
  4383. /* grow the destination as required */
  4384. if (c->alloc < digs) {
  4385. if ((res = aws_mp_grow(c, digs)) != AWS_MP_OKAY) {
  4386. return res;
  4387. }
  4388. }
  4389. /* number of output digits to produce */
  4390. pa = AWS_MIN(digs, a->used + b->used);
  4391. /* clear the carry */
  4392. _W = 0;
  4393. for (ix = 0; ix < pa; ix++) {
  4394. int tx, ty;
  4395. int iy;
  4396. aws_mp_digit *tmpx, *tmpy;
  4397. /* get offsets into the two bignums */
  4398. ty = AWS_MIN(b->used-1, ix);
  4399. tx = ix - ty;
  4400. /* setup temp aliases */
  4401. tmpx = a->dp + tx;
  4402. tmpy = b->dp + ty;
  4403. /* this is the number of times the loop will iterrate, essentially
  4404. while (tx++ < a->used && ty-- >= 0) { ... }
  4405. */
  4406. iy = AWS_MIN(a->used-tx, ty+1);
  4407. /* execute loop */
  4408. for (iz = 0; iz < iy; ++iz) {
  4409. _W += ((aws_mp_word)*tmpx++)*((aws_mp_word)*tmpy--);
  4410. }
  4411. /* store term */
  4412. W[ix] = ((aws_mp_digit)_W) & AWS_MP_MASK;
  4413. /* make next carry */
  4414. _W = _W >> ((aws_mp_word)AWS_DIGIT_BIT);
  4415. }
  4416. /* setup dest */
  4417. olduse = c->used;
  4418. c->used = pa;
  4419. {
  4420. register aws_mp_digit *tmpc;
  4421. tmpc = c->dp;
  4422. for (ix = 0; ix < pa+1; ix++) {
  4423. /* now extract the previous digit [below the carry] */
  4424. *tmpc++ = W[ix];
  4425. }
  4426. /* clear unused digits [that existed in the old copy of c] */
  4427. for (; ix < olduse; ix++) {
  4428. *tmpc++ = 0;
  4429. }
  4430. }
  4431. aws_mp_clamp(c);
  4432. return AWS_MP_OKAY;
  4433. }
  4434. #endif
  4435. #ifdef AWS_BN_FAST_S_MP_MUL_HIGH_DIGS_C
  4436. /* this is a modified version of fast_s_mul_digs that only produces
  4437. * output digits *above* digs. See the comments for fast_s_mul_digs
  4438. * to see how it works.
  4439. *
  4440. * This is used in the Barrett reduction since for one of the multiplications
  4441. * only the higher digits were needed. This essentially halves the work.
  4442. *
  4443. * Based on Algorithm 14.12 on pp.595 of HAC.
  4444. */
  4445. int aws_fast_s_mp_mul_high_digs(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c, int digs)
  4446. {
  4447. int olduse, res, pa, ix, iz;
  4448. aws_mp_digit W[AWS_MP_WARRAY];
  4449. aws_mp_word _W;
  4450. /* grow the destination as required */
  4451. pa = a->used + b->used;
  4452. if (c->alloc < pa) {
  4453. if ((res = aws_mp_grow(c, pa)) != AWS_MP_OKAY) {
  4454. return res;
  4455. }
  4456. }
  4457. /* number of output digits to produce */
  4458. pa = a->used + b->used;
  4459. _W = 0;
  4460. for (ix = digs; ix < pa; ix++) {
  4461. int tx, ty, iy;
  4462. aws_mp_digit *tmpx, *tmpy;
  4463. /* get offsets into the two bignums */
  4464. ty = AWS_MIN(b->used-1, ix);
  4465. tx = ix - ty;
  4466. /* setup temp aliases */
  4467. tmpx = a->dp + tx;
  4468. tmpy = b->dp + ty;
  4469. /* this is the number of times the loop will iterrate, essentially its
  4470. while (tx++ < a->used && ty-- >= 0) { ... }
  4471. */
  4472. iy = AWS_MIN(a->used-tx, ty+1);
  4473. /* execute loop */
  4474. for (iz = 0; iz < iy; iz++) {
  4475. _W += ((aws_mp_word)*tmpx++)*((aws_mp_word)*tmpy--);
  4476. }
  4477. /* store term */
  4478. W[ix] = ((aws_mp_digit)_W) & AWS_MP_MASK;
  4479. /* make next carry */
  4480. _W = _W >> ((aws_mp_word)AWS_DIGIT_BIT);
  4481. }
  4482. /* setup dest */
  4483. olduse = c->used;
  4484. c->used = pa;
  4485. {
  4486. register aws_mp_digit *tmpc;
  4487. tmpc = c->dp + digs;
  4488. for (ix = digs; ix < pa; ix++) {
  4489. /* now extract the previous digit [below the carry] */
  4490. *tmpc++ = W[ix];
  4491. }
  4492. /* clear unused digits [that existed in the old copy of c] */
  4493. for (; ix < olduse; ix++) {
  4494. *tmpc++ = 0;
  4495. }
  4496. }
  4497. aws_mp_clamp(c);
  4498. return AWS_MP_OKAY;
  4499. }
  4500. #endif
  4501. #ifdef AWS_BN_MP_REDUCE_2K_C
  4502. /* reduces a modulo n where n is of the form 2**p - d */
  4503. int aws_mp_reduce_2k(aws_mp_int *a, aws_mp_int *n, aws_mp_digit d)
  4504. {
  4505. aws_mp_int q;
  4506. int p, res;
  4507. if ((res = aws_mp_init(&q)) != AWS_MP_OKAY) {
  4508. return res;
  4509. }
  4510. p = aws_mp_count_bits(n);
  4511. top:
  4512. /* q = a/2**p, a = a mod 2**p */
  4513. if ((res = aws_mp_div_2d(a, p, &q, a)) != AWS_MP_OKAY) {
  4514. goto ERR;
  4515. }
  4516. if (d != 1) {
  4517. /* q = q * d */
  4518. if ((res = aws_mp_mul_d(&q, d, &q)) != AWS_MP_OKAY) {
  4519. goto ERR;
  4520. }
  4521. }
  4522. /* a = a + q */
  4523. if ((res = aws_s_mp_add(a, &q, a)) != AWS_MP_OKAY) {
  4524. goto ERR;
  4525. }
  4526. if (aws_mp_cmp_mag(a, n) != AWS_MP_LT) {
  4527. aws_s_mp_sub(a, n, a);
  4528. goto top;
  4529. }
  4530. ERR:
  4531. aws_mp_clear(&q);
  4532. return res;
  4533. }
  4534. #endif
  4535. #ifdef AWS_BN_MP_SET_C
  4536. /* set to a digit */
  4537. void aws_mp_set(aws_mp_int *a, aws_mp_digit b)
  4538. {
  4539. aws_mp_zero(a);
  4540. a->dp[0] = b & AWS_MP_MASK;
  4541. a->used = (a->dp[0] != 0) ? 1 : 0;
  4542. }
  4543. #endif
  4544. #ifdef AWS_BN_MP_MOD_D_C
  4545. int
  4546. aws_mp_mod_d(aws_mp_int *a, aws_mp_digit b, aws_mp_digit *c)
  4547. {
  4548. return aws_mp_div_d(a, b, NULL, c);
  4549. }
  4550. #endif
  4551. #ifdef AWS_BN_MP_COPY_C
  4552. /* copy, b = a */
  4553. int
  4554. aws_mp_copy(aws_mp_int *a, aws_mp_int *b)
  4555. {
  4556. int res, n;
  4557. /* if dst == src do nothing */
  4558. if (a == b) {
  4559. return AWS_MP_OKAY;
  4560. }
  4561. /* grow dest */
  4562. if (b->alloc < a->used) {
  4563. if ((res = aws_mp_grow(b, a->used)) != AWS_MP_OKAY) {
  4564. return res;
  4565. }
  4566. }
  4567. /* zero b and copy the parameters over */
  4568. {
  4569. register aws_mp_digit *tmpa, *tmpb;
  4570. /* pointer aliases */
  4571. /* source */
  4572. tmpa = a->dp;
  4573. /* destination */
  4574. tmpb = b->dp;
  4575. /* copy all the digits */
  4576. for (n = 0; n < a->used; n++) {
  4577. *tmpb++ = *tmpa++;
  4578. }
  4579. /* clear high digits */
  4580. for (; n < b->used; n++) {
  4581. *tmpb++ = 0;
  4582. }
  4583. }
  4584. /* copy used count and sign */
  4585. b->used = a->used;
  4586. b->sign = a->sign;
  4587. return AWS_MP_OKAY;
  4588. }
  4589. #endif
  4590. #ifdef AWS_BN_MP_TO_SIGNED_BIN_N_C
  4591. /* store in signed [big endian] format */
  4592. int aws_mp_to_signed_bin_n(aws_mp_int *a, unsigned char *b, unsigned long *outlen)
  4593. {
  4594. if (*outlen < (unsigned long) aws_mp_signed_bin_size(a)) {
  4595. return AWS_MP_VAL;
  4596. }
  4597. *outlen = aws_mp_signed_bin_size(a);
  4598. return aws_mp_to_signed_bin(a, b);
  4599. }
  4600. #endif
  4601. #ifdef AWS_BN_FAST_S_MP_SQR_C
  4602. /* the jist of squaring...
  4603. * you do like mult except the offset of the tmpx [one that
  4604. * starts closer to zero] can't equal the offset of tmpy.
  4605. * So basically you set up iy like before then you min it with
  4606. * (ty-tx) so that it never happens. You double all those
  4607. * you add in the inner loop
  4608. After that loop you do the squares and add them in.
  4609. */
  4610. int aws_fast_s_mp_sqr(aws_mp_int *a, aws_mp_int *b)
  4611. {
  4612. int olduse, res, pa, ix, iz;
  4613. aws_mp_digit W[AWS_MP_WARRAY], *tmpx;
  4614. aws_mp_word W1;
  4615. /* grow the destination as required */
  4616. pa = a->used + a->used;
  4617. if (b->alloc < pa) {
  4618. if ((res = aws_mp_grow(b, pa)) != AWS_MP_OKAY) {
  4619. return res;
  4620. }
  4621. }
  4622. /* number of output digits to produce */
  4623. W1 = 0;
  4624. for (ix = 0; ix < pa; ix++) {
  4625. int tx, ty, iy;
  4626. aws_mp_word _W;
  4627. aws_mp_digit *tmpy;
  4628. /* clear counter */
  4629. _W = 0;
  4630. /* get offsets into the two bignums */
  4631. ty = AWS_MIN(a->used-1, ix);
  4632. tx = ix - ty;
  4633. /* setup temp aliases */
  4634. tmpx = a->dp + tx;
  4635. tmpy = a->dp + ty;
  4636. /* this is the number of times the loop will iterrate, essentially
  4637. while (tx++ < a->used && ty-- >= 0) { ... }
  4638. */
  4639. iy = AWS_MIN(a->used-tx, ty+1);
  4640. /* now for squaring tx can never equal ty
  4641. * we halve the distance since they approach at a rate of 2x
  4642. * and we have to round because odd cases need to be executed
  4643. */
  4644. iy = AWS_MIN(iy, (ty-tx+1)>>1);
  4645. /* execute loop */
  4646. for (iz = 0; iz < iy; iz++) {
  4647. _W += ((aws_mp_word)*tmpx++)*((aws_mp_word)*tmpy--);
  4648. }
  4649. /* double the inner product and add carry */
  4650. _W = _W + _W + W1;
  4651. /* even columns have the square term in them */
  4652. if ((ix&1) == 0) {
  4653. _W += ((aws_mp_word)a->dp[ix>>1])*((aws_mp_word)a->dp[ix>>1]);
  4654. }
  4655. /* store it */
  4656. W[ix] = (aws_mp_digit)(_W & AWS_MP_MASK);
  4657. /* make next carry */
  4658. W1 = _W >> ((aws_mp_word)AWS_DIGIT_BIT);
  4659. }
  4660. /* setup dest */
  4661. olduse = b->used;
  4662. b->used = a->used+a->used;
  4663. {
  4664. aws_mp_digit *tmpb;
  4665. tmpb = b->dp;
  4666. for (ix = 0; ix < pa; ix++) {
  4667. *tmpb++ = W[ix] & AWS_MP_MASK;
  4668. }
  4669. /* clear unused digits [that existed in the old copy of c] */
  4670. for (; ix < olduse; ix++) {
  4671. *tmpb++ = 0;
  4672. }
  4673. }
  4674. aws_mp_clamp(b);
  4675. return AWS_MP_OKAY;
  4676. }
  4677. #endif
  4678. #ifdef AWS_BN_MP_SHRINK_C
  4679. /* shrink a bignum */
  4680. int aws_mp_shrink(aws_mp_int *a)
  4681. {
  4682. aws_mp_digit *tmp;
  4683. int used = 1;
  4684. if(a->used > 0)
  4685. used = a->used;
  4686. if (a->alloc != used) {
  4687. if ((tmp = AWS_OPT_CAST(aws_mp_digit) AWS_XREALLOC (a->dp, sizeof (aws_mp_digit) * used)) == NULL) {
  4688. return AWS_MP_MEM;
  4689. }
  4690. a->dp = tmp;
  4691. a->alloc = used;
  4692. }
  4693. return AWS_MP_OKAY;
  4694. }
  4695. #endif
  4696. #ifdef AWS_BN_MP_2EXPT_C
  4697. /* computes a = 2**b
  4698. *
  4699. * Simple algorithm which zeroes the int, grows it then just sets one bit
  4700. * as required.
  4701. */
  4702. int
  4703. aws_mp_2expt(aws_mp_int *a, int b)
  4704. {
  4705. int res;
  4706. /* zero a as per default */
  4707. aws_mp_zero(a);
  4708. /* grow a to accomodate the single bit */
  4709. if ((res = aws_mp_grow(a, b / AWS_DIGIT_BIT + 1)) != AWS_MP_OKAY) {
  4710. return res;
  4711. }
  4712. /* set the used count of where the bit will go */
  4713. a->used = b / AWS_DIGIT_BIT + 1;
  4714. /* put the single bit in its place */
  4715. a->dp[b / AWS_DIGIT_BIT] = ((aws_mp_digit)1) << (b % AWS_DIGIT_BIT);
  4716. return AWS_MP_OKAY;
  4717. }
  4718. #endif
  4719. #ifdef AWS_BN_MP_DR_REDUCE_C
  4720. /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
  4721. *
  4722. * Based on algorithm from the paper
  4723. *
  4724. * "Generating Efficient Primes for Discrete Log Cryptosystems"
  4725. * Chae Hoon Lim, Pil Joong Lee,
  4726. * POSTECH Information Research Laboratories
  4727. *
  4728. * The modulus must be of a special format [see manual]
  4729. *
  4730. * Has been modified to use algorithm 7.10 from the LTM book instead
  4731. *
  4732. * Input x must be in the range 0 <= x <= (n-1)**2
  4733. */
  4734. int
  4735. aws_mp_dr_reduce(aws_mp_int *x, aws_mp_int *n, aws_mp_digit k)
  4736. {
  4737. int err, i, m;
  4738. aws_mp_word r;
  4739. aws_mp_digit mu, *tmpx1, *tmpx2;
  4740. /* m = digits in modulus */
  4741. m = n->used;
  4742. /* ensure that "x" has at least 2m digits */
  4743. if (x->alloc < m + m) {
  4744. if ((err = aws_mp_grow(x, m + m)) != AWS_MP_OKAY) {
  4745. return err;
  4746. }
  4747. }
  4748. /* top of loop, this is where the code resumes if
  4749. * another reduction pass is required.
  4750. */
  4751. top:
  4752. /* aliases for digits */
  4753. /* alias for lower half of x */
  4754. tmpx1 = x->dp;
  4755. /* alias for upper half of x, or x/B**m */
  4756. tmpx2 = x->dp + m;
  4757. /* set carry to zero */
  4758. mu = 0;
  4759. /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
  4760. for (i = 0; i < m; i++) {
  4761. r = ((aws_mp_word)*tmpx2++) * ((aws_mp_word)k) + *tmpx1 + mu;
  4762. *tmpx1++ = (aws_mp_digit)(r & AWS_MP_MASK);
  4763. mu = (aws_mp_digit)(r >> ((aws_mp_word)AWS_DIGIT_BIT));
  4764. }
  4765. /* set final carry */
  4766. *tmpx1++ = mu;
  4767. /* zero words above m */
  4768. for (i = m + 1; i < x->used; i++) {
  4769. *tmpx1++ = 0;
  4770. }
  4771. /* clamp, sub and return */
  4772. aws_mp_clamp(x);
  4773. /* if x >= n then subtract and reduce again
  4774. * Each successive "recursion" makes the input smaller and smaller.
  4775. */
  4776. if (aws_mp_cmp_mag(x, n) != AWS_MP_LT) {
  4777. aws_s_mp_sub(x, n, x);
  4778. goto top;
  4779. }
  4780. return AWS_MP_OKAY;
  4781. }
  4782. #endif
  4783. #ifdef AWS_BN_MP_TORADIX_N_C
  4784. /* stores a bignum as a ASCII string in a given radix (2..64)
  4785. *
  4786. * Stores upto maxlen-1 chars and always a NULL byte
  4787. */
  4788. int aws_mp_toradix_n(aws_mp_int *a, char *str, int radix, int maxlen)
  4789. {
  4790. int res, digs;
  4791. aws_mp_int t;
  4792. aws_mp_digit d;
  4793. char *_s = str;
  4794. /* check range of the maxlen, radix */
  4795. if (maxlen < 2 || radix < 2 || radix > 64) {
  4796. return AWS_MP_VAL;
  4797. }
  4798. /* quick out if its zero */
  4799. if (aws_mp_iszero(a) == AWS_MP_YES) {
  4800. *str++ = '0';
  4801. *str = '\0';
  4802. return AWS_MP_OKAY;
  4803. }
  4804. if ((res = aws_mp_init_copy(&t, a)) != AWS_MP_OKAY) {
  4805. return res;
  4806. }
  4807. /* if it is negative output a - */
  4808. if (t.sign == AWS_MP_NEG) {
  4809. /* we have to reverse our digits later... but not the - sign!! */
  4810. ++_s;
  4811. /* store the flag and mark the number as positive */
  4812. *str++ = '-';
  4813. t.sign = AWS_MP_ZPOS;
  4814. /* subtract a char */
  4815. --maxlen;
  4816. }
  4817. digs = 0;
  4818. while (aws_mp_iszero (&t) == 0) {
  4819. if (--maxlen < 1) {
  4820. /* no more room */
  4821. break;
  4822. }
  4823. if ((res = aws_mp_div_d(&t, (aws_mp_digit) radix, &t, &d)) != AWS_MP_OKAY) {
  4824. aws_mp_clear(&t);
  4825. return res;
  4826. }
  4827. *str++ = aws_mp_s_rmap[d];
  4828. ++digs;
  4829. }
  4830. /* reverse the digits of the string. In this case _s points
  4831. * to the first digit [exluding the sign] of the number
  4832. */
  4833. aws_bn_reverse((unsigned char *) _s, digs);
  4834. /* append a NULL so the string is properly terminated */
  4835. *str = '\0';
  4836. aws_mp_clear(&t);
  4837. return AWS_MP_OKAY;
  4838. }
  4839. #endif
  4840. #ifdef AWS_BN_MP_PRIME_MILLER_RABIN_C
  4841. /* Miller-Rabin test of "a" to the base of "b" as described in
  4842. * HAC pp. 139 Algorithm 4.24
  4843. *
  4844. * Sets result to 0 if definitely composite or 1 if probably prime.
  4845. * Randomly the chance of error is no more than 1/4 and often
  4846. * very much lower.
  4847. */
  4848. int aws_mp_prime_miller_rabin(aws_mp_int *a, aws_mp_int *b, int *result)
  4849. {
  4850. aws_mp_int n1, y, r;
  4851. int s, j, err;
  4852. /* default */
  4853. *result = AWS_MP_NO;
  4854. /* ensure b > 1 */
  4855. if (aws_mp_cmp_d(b, 1) != AWS_MP_GT) {
  4856. return AWS_MP_VAL;
  4857. }
  4858. /* get n1 = a - 1 */
  4859. if ((err = aws_mp_init_copy(&n1, a)) != AWS_MP_OKAY) {
  4860. return err;
  4861. }
  4862. if ((err = aws_mp_sub_d(&n1, 1, &n1)) != AWS_MP_OKAY) {
  4863. goto LBL_N1;
  4864. }
  4865. /* set 2**s * r = n1 */
  4866. if ((err = aws_mp_init_copy(&r, &n1)) != AWS_MP_OKAY) {
  4867. goto LBL_N1;
  4868. }
  4869. /* count the number of least significant bits
  4870. * which are zero
  4871. */
  4872. s = aws_mp_cnt_lsb(&r);
  4873. /* now divide n - 1 by 2**s */
  4874. if ((err = aws_mp_div_2d(&r, s, &r, NULL)) != AWS_MP_OKAY) {
  4875. goto LBL_R;
  4876. }
  4877. /* compute y = b**r mod a */
  4878. if ((err = aws_mp_init(&y)) != AWS_MP_OKAY) {
  4879. goto LBL_R;
  4880. }
  4881. if ((err = aws_mp_exptmod(b, &r, a, &y)) != AWS_MP_OKAY) {
  4882. goto LBL_Y;
  4883. }
  4884. /* if y != 1 and y != n1 do */
  4885. if (aws_mp_cmp_d(&y, 1) != AWS_MP_EQ && aws_mp_cmp(&y, &n1) != AWS_MP_EQ) {
  4886. j = 1;
  4887. /* while j <= s-1 and y != n1 */
  4888. while ((j <= (s - 1)) && aws_mp_cmp(&y, &n1) != AWS_MP_EQ) {
  4889. if ((err = aws_mp_sqrmod(&y, a, &y)) != AWS_MP_OKAY) {
  4890. goto LBL_Y;
  4891. }
  4892. /* if y == 1 then composite */
  4893. if (aws_mp_cmp_d(&y, 1) == AWS_MP_EQ) {
  4894. goto LBL_Y;
  4895. }
  4896. ++j;
  4897. }
  4898. /* if y != n1 then composite */
  4899. if (aws_mp_cmp(&y, &n1) != AWS_MP_EQ) {
  4900. goto LBL_Y;
  4901. }
  4902. }
  4903. /* probably prime now */
  4904. *result = AWS_MP_YES;
  4905. LBL_Y:
  4906. aws_mp_clear(&y);
  4907. LBL_R:
  4908. aws_mp_clear(&r);
  4909. LBL_N1:
  4910. aws_mp_clear(&n1);
  4911. return err;
  4912. }
  4913. #endif
  4914. #ifdef AWS_BN_MP_REDUCE_IS_2K_L_C
  4915. /* determines if reduce_2k_l can be used */
  4916. int aws_mp_reduce_is_2k_l(aws_mp_int *a)
  4917. {
  4918. int ix, iy;
  4919. if (a->used == 0) {
  4920. return AWS_MP_NO;
  4921. } else if (a->used == 1) {
  4922. return AWS_MP_YES;
  4923. } else if (a->used > 1) {
  4924. /* if more than half of the digits are -1 we're sold */
  4925. for (iy = ix = 0; ix < a->used; ix++) {
  4926. if (a->dp[ix] == AWS_MP_MASK) {
  4927. ++iy;
  4928. }
  4929. }
  4930. return (iy >= (a->used/2)) ? AWS_MP_YES : AWS_MP_NO;
  4931. }
  4932. return AWS_MP_NO;
  4933. }
  4934. #endif
  4935. #ifdef AWS_BN_MP_DR_SETUP_C
  4936. /* determines the setup value */
  4937. void aws_mp_dr_setup(aws_mp_int *a, aws_mp_digit *d)
  4938. {
  4939. /* the casts are required if AWS_DIGIT_BIT is one less than
  4940. * the number of bits in a aws_mp_digit [e.g. AWS_DIGIT_BIT==31]
  4941. */
  4942. *d = (aws_mp_digit)((((aws_mp_word)1) << ((aws_mp_word)AWS_DIGIT_BIT)) -
  4943. ((aws_mp_word)a->dp[0]));
  4944. }
  4945. #endif
  4946. #ifdef AWS_BN_MP_REDUCE_2K_SETUP_C
  4947. /* determines the setup value */
  4948. int aws_mp_reduce_2k_setup(aws_mp_int *a, aws_mp_digit *d)
  4949. {
  4950. int res, p;
  4951. aws_mp_int tmp;
  4952. if ((res = aws_mp_init(&tmp)) != AWS_MP_OKAY) {
  4953. return res;
  4954. }
  4955. p = aws_mp_count_bits(a);
  4956. if ((res = aws_mp_2expt(&tmp, p)) != AWS_MP_OKAY) {
  4957. aws_mp_clear(&tmp);
  4958. return res;
  4959. }
  4960. if ((res = aws_s_mp_sub(&tmp, a, &tmp)) != AWS_MP_OKAY) {
  4961. aws_mp_clear(&tmp);
  4962. return res;
  4963. }
  4964. *d = tmp.dp[0];
  4965. aws_mp_clear(&tmp);
  4966. return AWS_MP_OKAY;
  4967. }
  4968. #endif
  4969. #ifdef AWS_BN_MP_REDUCE_SETUP_C
  4970. /* pre-calculate the value required for Barrett reduction
  4971. * For a given modulus "b" it calulates the value required in "a"
  4972. */
  4973. int aws_mp_reduce_setup(aws_mp_int *a, aws_mp_int *b)
  4974. {
  4975. int res;
  4976. if ((res = aws_mp_2expt(a, b->used * 2 * AWS_DIGIT_BIT)) != AWS_MP_OKAY) {
  4977. return res;
  4978. }
  4979. return aws_mp_div(a, b, a, NULL);
  4980. }
  4981. #endif
  4982. #ifdef AWS_BN_MP_GCD_C
  4983. /* Greatest Common Divisor using the binary method */
  4984. int aws_mp_gcd(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  4985. {
  4986. aws_mp_int u, v;
  4987. int k, u_lsb, v_lsb, res;
  4988. /* either zero than gcd is the largest */
  4989. if (aws_mp_iszero (a) == AWS_MP_YES) {
  4990. return aws_mp_abs(b, c);
  4991. }
  4992. if (aws_mp_iszero (b) == AWS_MP_YES) {
  4993. return aws_mp_abs(a, c);
  4994. }
  4995. /* get copies of a and b we can modify */
  4996. if ((res = aws_mp_init_copy(&u, a)) != AWS_MP_OKAY) {
  4997. return res;
  4998. }
  4999. if ((res = aws_mp_init_copy(&v, b)) != AWS_MP_OKAY) {
  5000. goto LBL_U;
  5001. }
  5002. /* must be positive for the remainder of the algorithm */
  5003. u.sign = v.sign = AWS_MP_ZPOS;
  5004. /* B1. Find the common power of two for u and v */
  5005. u_lsb = aws_mp_cnt_lsb(&u);
  5006. v_lsb = aws_mp_cnt_lsb(&v);
  5007. k = AWS_MIN(u_lsb, v_lsb);
  5008. if (k > 0) {
  5009. /* divide the power of two out */
  5010. if ((res = aws_mp_div_2d(&u, k, &u, NULL)) != AWS_MP_OKAY) {
  5011. goto LBL_V;
  5012. }
  5013. if ((res = aws_mp_div_2d(&v, k, &v, NULL)) != AWS_MP_OKAY) {
  5014. goto LBL_V;
  5015. }
  5016. }
  5017. /* divide any remaining factors of two out */
  5018. if (u_lsb != k) {
  5019. if ((res = aws_mp_div_2d(&u, u_lsb - k, &u, NULL)) != AWS_MP_OKAY) {
  5020. goto LBL_V;
  5021. }
  5022. }
  5023. if (v_lsb != k) {
  5024. if ((res = aws_mp_div_2d(&v, v_lsb - k, &v, NULL)) != AWS_MP_OKAY) {
  5025. goto LBL_V;
  5026. }
  5027. }
  5028. while (aws_mp_iszero(&v) == 0) {
  5029. /* make sure v is the largest */
  5030. if (aws_mp_cmp_mag(&u, &v) == AWS_MP_GT) {
  5031. /* swap u and v to make sure v is >= u */
  5032. aws_mp_exch(&u, &v);
  5033. }
  5034. /* subtract smallest from largest */
  5035. if ((res = aws_s_mp_sub(&v, &u, &v)) != AWS_MP_OKAY) {
  5036. goto LBL_V;
  5037. }
  5038. /* Divide out all factors of two */
  5039. if ((res = aws_mp_div_2d(&v, aws_mp_cnt_lsb(&v), &v, NULL)) != AWS_MP_OKAY) {
  5040. goto LBL_V;
  5041. }
  5042. }
  5043. /* multiply by 2**k which we divided out at the beginning */
  5044. if ((res = aws_mp_mul_2d(&u, k, c)) != AWS_MP_OKAY) {
  5045. goto LBL_V;
  5046. }
  5047. c->sign = AWS_MP_ZPOS;
  5048. res = AWS_MP_OKAY;
  5049. LBL_V:
  5050. aws_mp_clear(&u);
  5051. LBL_U:
  5052. aws_mp_clear(&v);
  5053. return res;
  5054. }
  5055. #endif
  5056. #ifdef AWS_BN_S_MP_SUB_C
  5057. /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
  5058. int
  5059. aws_s_mp_sub(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  5060. {
  5061. int olduse, res, min, max;
  5062. /* find sizes */
  5063. min = b->used;
  5064. max = a->used;
  5065. /* init result */
  5066. if (c->alloc < max) {
  5067. if ((res = aws_mp_grow(c, max)) != AWS_MP_OKAY) {
  5068. return res;
  5069. }
  5070. }
  5071. olduse = c->used;
  5072. c->used = max;
  5073. {
  5074. register aws_mp_digit u, *tmpa, *tmpb, *tmpc;
  5075. register int i;
  5076. /* alias for digit pointers */
  5077. tmpa = a->dp;
  5078. tmpb = b->dp;
  5079. tmpc = c->dp;
  5080. /* set carry to zero */
  5081. u = 0;
  5082. for (i = 0; i < min; i++) {
  5083. /* T[i] = A[i] - B[i] - U */
  5084. *tmpc = *tmpa++ - *tmpb++ - u;
  5085. /* U = carry bit of T[i]
  5086. * Note this saves performing an AND operation since
  5087. * if a carry does occur it will propagate all the way to the
  5088. * MSB. As a result a single shift is enough to get the carry
  5089. */
  5090. u = *tmpc >> ((aws_mp_digit)(CHAR_BIT * sizeof (aws_mp_digit) - 1));
  5091. /* Clear carry from T[i] */
  5092. *tmpc++ &= AWS_MP_MASK;
  5093. }
  5094. /* now copy higher words if any, e.g. if A has more digits than B */
  5095. for (; i < max; i++) {
  5096. /* T[i] = A[i] - U */
  5097. *tmpc = *tmpa++ - u;
  5098. /* U = carry bit of T[i] */
  5099. u = *tmpc >> ((aws_mp_digit)(CHAR_BIT * sizeof (aws_mp_digit) - 1));
  5100. /* Clear carry from T[i] */
  5101. *tmpc++ &= AWS_MP_MASK;
  5102. }
  5103. /* clear digits above used (since we may not have grown result above) */
  5104. for (i = c->used; i < olduse; i++) {
  5105. *tmpc++ = 0;
  5106. }
  5107. }
  5108. aws_mp_clamp(c);
  5109. return AWS_MP_OKAY;
  5110. }
  5111. #endif
  5112. #ifdef AWS_BN_MP_TOOM_MUL_C
  5113. /* multiplication using the Toom-Cook 3-way algorithm
  5114. *
  5115. * Much more complicated than Karatsuba but has a lower
  5116. * asymptotic running time of O(N**1.464). This algorithm is
  5117. * only particularly useful on VERY large inputs
  5118. * (we're talking 1000s of digits here...).
  5119. */
  5120. int aws_mp_toom_mul(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  5121. {
  5122. aws_mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
  5123. int res, B;
  5124. /* init temps */
  5125. if ((res = aws_mp_init_multi(&w0, &w1, &w2, &w3, &w4,
  5126. &a0, &a1, &a2, &b0, &b1,
  5127. &b2, &tmp1, &tmp2, NULL)) != AWS_MP_OKAY) {
  5128. return res;
  5129. }
  5130. /* B */
  5131. B = AWS_MIN(a->used, b->used) / 3;
  5132. /* a = a2 * B**2 + a1 * B + a0 */
  5133. if ((res = aws_mp_mod_2d(a, AWS_DIGIT_BIT * B, &a0)) != AWS_MP_OKAY) {
  5134. goto ERR;
  5135. }
  5136. if ((res = aws_mp_copy(a, &a1)) != AWS_MP_OKAY) {
  5137. goto ERR;
  5138. }
  5139. aws_mp_rshd(&a1, B);
  5140. aws_mp_mod_2d(&a1, AWS_DIGIT_BIT * B, &a1);
  5141. if ((res = aws_mp_copy(a, &a2)) != AWS_MP_OKAY) {
  5142. goto ERR;
  5143. }
  5144. aws_mp_rshd(&a2, B * 2);
  5145. /* b = b2 * B**2 + b1 * B + b0 */
  5146. if ((res = aws_mp_mod_2d(b, AWS_DIGIT_BIT * B, &b0)) != AWS_MP_OKAY) {
  5147. goto ERR;
  5148. }
  5149. if ((res = aws_mp_copy(b, &b1)) != AWS_MP_OKAY) {
  5150. goto ERR;
  5151. }
  5152. aws_mp_rshd(&b1, B);
  5153. aws_mp_mod_2d(&b1, AWS_DIGIT_BIT * B, &b1);
  5154. if ((res = aws_mp_copy(b, &b2)) != AWS_MP_OKAY) {
  5155. goto ERR;
  5156. }
  5157. aws_mp_rshd(&b2, B * 2);
  5158. /* w0 = a0*b0 */
  5159. if ((res = aws_mp_mul(&a0, &b0, &w0)) != AWS_MP_OKAY) {
  5160. goto ERR;
  5161. }
  5162. /* w4 = a2 * b2 */
  5163. if ((res = aws_mp_mul(&a2, &b2, &w4)) != AWS_MP_OKAY) {
  5164. goto ERR;
  5165. }
  5166. /* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
  5167. if ((res = aws_mp_mul_2(&a0, &tmp1)) != AWS_MP_OKAY) {
  5168. goto ERR;
  5169. }
  5170. if ((res = aws_mp_add(&tmp1, &a1, &tmp1)) != AWS_MP_OKAY) {
  5171. goto ERR;
  5172. }
  5173. if ((res = aws_mp_mul_2(&tmp1, &tmp1)) != AWS_MP_OKAY) {
  5174. goto ERR;
  5175. }
  5176. if ((res = aws_mp_add(&tmp1, &a2, &tmp1)) != AWS_MP_OKAY) {
  5177. goto ERR;
  5178. }
  5179. if ((res = aws_mp_mul_2(&b0, &tmp2)) != AWS_MP_OKAY) {
  5180. goto ERR;
  5181. }
  5182. if ((res = aws_mp_add(&tmp2, &b1, &tmp2)) != AWS_MP_OKAY) {
  5183. goto ERR;
  5184. }
  5185. if ((res = aws_mp_mul_2(&tmp2, &tmp2)) != AWS_MP_OKAY) {
  5186. goto ERR;
  5187. }
  5188. if ((res = aws_mp_add(&tmp2, &b2, &tmp2)) != AWS_MP_OKAY) {
  5189. goto ERR;
  5190. }
  5191. if ((res = aws_mp_mul(&tmp1, &tmp2, &w1)) != AWS_MP_OKAY) {
  5192. goto ERR;
  5193. }
  5194. /* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
  5195. if ((res = aws_mp_mul_2(&a2, &tmp1)) != AWS_MP_OKAY) {
  5196. goto ERR;
  5197. }
  5198. if ((res = aws_mp_add(&tmp1, &a1, &tmp1)) != AWS_MP_OKAY) {
  5199. goto ERR;
  5200. }
  5201. if ((res = aws_mp_mul_2(&tmp1, &tmp1)) != AWS_MP_OKAY) {
  5202. goto ERR;
  5203. }
  5204. if ((res = aws_mp_add(&tmp1, &a0, &tmp1)) != AWS_MP_OKAY) {
  5205. goto ERR;
  5206. }
  5207. if ((res = aws_mp_mul_2(&b2, &tmp2)) != AWS_MP_OKAY) {
  5208. goto ERR;
  5209. }
  5210. if ((res = aws_mp_add(&tmp2, &b1, &tmp2)) != AWS_MP_OKAY) {
  5211. goto ERR;
  5212. }
  5213. if ((res = aws_mp_mul_2(&tmp2, &tmp2)) != AWS_MP_OKAY) {
  5214. goto ERR;
  5215. }
  5216. if ((res = aws_mp_add(&tmp2, &b0, &tmp2)) != AWS_MP_OKAY) {
  5217. goto ERR;
  5218. }
  5219. if ((res = aws_mp_mul(&tmp1, &tmp2, &w3)) != AWS_MP_OKAY) {
  5220. goto ERR;
  5221. }
  5222. /* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
  5223. if ((res = aws_mp_add(&a2, &a1, &tmp1)) != AWS_MP_OKAY) {
  5224. goto ERR;
  5225. }
  5226. if ((res = aws_mp_add(&tmp1, &a0, &tmp1)) != AWS_MP_OKAY) {
  5227. goto ERR;
  5228. }
  5229. if ((res = aws_mp_add(&b2, &b1, &tmp2)) != AWS_MP_OKAY) {
  5230. goto ERR;
  5231. }
  5232. if ((res = aws_mp_add(&tmp2, &b0, &tmp2)) != AWS_MP_OKAY) {
  5233. goto ERR;
  5234. }
  5235. if ((res = aws_mp_mul(&tmp1, &tmp2, &w2)) != AWS_MP_OKAY) {
  5236. goto ERR;
  5237. }
  5238. /* now solve the matrix
  5239. 0 0 0 0 1
  5240. 1 2 4 8 16
  5241. 1 1 1 1 1
  5242. 16 8 4 2 1
  5243. 1 0 0 0 0
  5244. using 12 subtractions, 4 shifts,
  5245. 2 small divisions and 1 small multiplication
  5246. */
  5247. /* r1 - r4 */
  5248. if ((res = aws_mp_sub(&w1, &w4, &w1)) != AWS_MP_OKAY) {
  5249. goto ERR;
  5250. }
  5251. /* r3 - r0 */
  5252. if ((res = aws_mp_sub(&w3, &w0, &w3)) != AWS_MP_OKAY) {
  5253. goto ERR;
  5254. }
  5255. /* r1/2 */
  5256. if ((res = aws_mp_div_2(&w1, &w1)) != AWS_MP_OKAY) {
  5257. goto ERR;
  5258. }
  5259. /* r3/2 */
  5260. if ((res = aws_mp_div_2(&w3, &w3)) != AWS_MP_OKAY) {
  5261. goto ERR;
  5262. }
  5263. /* r2 - r0 - r4 */
  5264. if ((res = aws_mp_sub(&w2, &w0, &w2)) != AWS_MP_OKAY) {
  5265. goto ERR;
  5266. }
  5267. if ((res = aws_mp_sub(&w2, &w4, &w2)) != AWS_MP_OKAY) {
  5268. goto ERR;
  5269. }
  5270. /* r1 - r2 */
  5271. if ((res = aws_mp_sub(&w1, &w2, &w1)) != AWS_MP_OKAY) {
  5272. goto ERR;
  5273. }
  5274. /* r3 - r2 */
  5275. if ((res = aws_mp_sub(&w3, &w2, &w3)) != AWS_MP_OKAY) {
  5276. goto ERR;
  5277. }
  5278. /* r1 - 8r0 */
  5279. if ((res = aws_mp_mul_2d(&w0, 3, &tmp1)) != AWS_MP_OKAY) {
  5280. goto ERR;
  5281. }
  5282. if ((res = aws_mp_sub(&w1, &tmp1, &w1)) != AWS_MP_OKAY) {
  5283. goto ERR;
  5284. }
  5285. /* r3 - 8r4 */
  5286. if ((res = aws_mp_mul_2d(&w4, 3, &tmp1)) != AWS_MP_OKAY) {
  5287. goto ERR;
  5288. }
  5289. if ((res = aws_mp_sub(&w3, &tmp1, &w3)) != AWS_MP_OKAY) {
  5290. goto ERR;
  5291. }
  5292. /* 3r2 - r1 - r3 */
  5293. if ((res = aws_mp_mul_d(&w2, 3, &w2)) != AWS_MP_OKAY) {
  5294. goto ERR;
  5295. }
  5296. if ((res = aws_mp_sub(&w2, &w1, &w2)) != AWS_MP_OKAY) {
  5297. goto ERR;
  5298. }
  5299. if ((res = aws_mp_sub(&w2, &w3, &w2)) != AWS_MP_OKAY) {
  5300. goto ERR;
  5301. }
  5302. /* r1 - r2 */
  5303. if ((res = aws_mp_sub(&w1, &w2, &w1)) != AWS_MP_OKAY) {
  5304. goto ERR;
  5305. }
  5306. /* r3 - r2 */
  5307. if ((res = aws_mp_sub(&w3, &w2, &w3)) != AWS_MP_OKAY) {
  5308. goto ERR;
  5309. }
  5310. /* r1/3 */
  5311. if ((res = aws_mp_div_3(&w1, &w1, NULL)) != AWS_MP_OKAY) {
  5312. goto ERR;
  5313. }
  5314. /* r3/3 */
  5315. if ((res = aws_mp_div_3(&w3, &w3, NULL)) != AWS_MP_OKAY) {
  5316. goto ERR;
  5317. }
  5318. /* at this point shift W[n] by B*n */
  5319. if ((res = aws_mp_lshd(&w1, 1 * B)) != AWS_MP_OKAY) {
  5320. goto ERR;
  5321. }
  5322. if ((res = aws_mp_lshd(&w2, 2 * B)) != AWS_MP_OKAY) {
  5323. goto ERR;
  5324. }
  5325. if ((res = aws_mp_lshd(&w3, 3 * B)) != AWS_MP_OKAY) {
  5326. goto ERR;
  5327. }
  5328. if ((res = aws_mp_lshd(&w4, 4 * B)) != AWS_MP_OKAY) {
  5329. goto ERR;
  5330. }
  5331. if ((res = aws_mp_add(&w0, &w1, c)) != AWS_MP_OKAY) {
  5332. goto ERR;
  5333. }
  5334. if ((res = aws_mp_add(&w2, &w3, &tmp1)) != AWS_MP_OKAY) {
  5335. goto ERR;
  5336. }
  5337. if ((res = aws_mp_add(&w4, &tmp1, &tmp1)) != AWS_MP_OKAY) {
  5338. goto ERR;
  5339. }
  5340. if ((res = aws_mp_add(&tmp1, c, c)) != AWS_MP_OKAY) {
  5341. goto ERR;
  5342. }
  5343. ERR:
  5344. aws_mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
  5345. &a0, &a1, &a2, &b0, &b1,
  5346. &b2, &tmp1, &tmp2, NULL);
  5347. return res;
  5348. }
  5349. #endif
  5350. #ifdef AWS_BN_MP_INIT_SET_INT_C
  5351. /* initialize and set a digit */
  5352. int aws_mp_init_set_int(aws_mp_int *a, unsigned long b)
  5353. {
  5354. int err;
  5355. if ((err = aws_mp_init(a)) != AWS_MP_OKAY) {
  5356. return err;
  5357. }
  5358. return aws_mp_set_int(a, b);
  5359. }
  5360. #endif
  5361. #ifdef AWS_BN_FAST_MP_INVMOD_C
  5362. /* computes the modular inverse via binary extended euclidean algorithm,
  5363. * that is c = 1/a mod b
  5364. *
  5365. * Based on slow invmod except this is optimized for the case where b is
  5366. * odd as per HAC Note 14.64 on pp. 610
  5367. */
  5368. int aws_fast_mp_invmod(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  5369. {
  5370. aws_mp_int x, y, u, v, B, D;
  5371. int res, neg;
  5372. /* 2. [modified] b must be odd */
  5373. if (aws_mp_iseven (b) == 1) {
  5374. return AWS_MP_VAL;
  5375. }
  5376. /* init all our temps */
  5377. if ((res = aws_mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != AWS_MP_OKAY) {
  5378. return res;
  5379. }
  5380. /* x == modulus, y == value to invert */
  5381. if ((res = aws_mp_copy(b, &x)) != AWS_MP_OKAY) {
  5382. goto LBL_ERR;
  5383. }
  5384. /* we need y = |a| */
  5385. if ((res = aws_mp_mod(a, b, &y)) != AWS_MP_OKAY) {
  5386. goto LBL_ERR;
  5387. }
  5388. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  5389. if ((res = aws_mp_copy(&x, &u)) != AWS_MP_OKAY) {
  5390. goto LBL_ERR;
  5391. }
  5392. if ((res = aws_mp_copy(&y, &v)) != AWS_MP_OKAY) {
  5393. goto LBL_ERR;
  5394. }
  5395. aws_mp_set(&D, 1);
  5396. top:
  5397. /* 4. while u is even do */
  5398. while (aws_mp_iseven (&u) == 1) {
  5399. /* 4.1 u = u/2 */
  5400. if ((res = aws_mp_div_2(&u, &u)) != AWS_MP_OKAY) {
  5401. goto LBL_ERR;
  5402. }
  5403. /* 4.2 if B is odd then */
  5404. if (aws_mp_isodd (&B) == 1) {
  5405. if ((res = aws_mp_sub(&B, &x, &B)) != AWS_MP_OKAY) {
  5406. goto LBL_ERR;
  5407. }
  5408. }
  5409. /* B = B/2 */
  5410. if ((res = aws_mp_div_2(&B, &B)) != AWS_MP_OKAY) {
  5411. goto LBL_ERR;
  5412. }
  5413. }
  5414. /* 5. while v is even do */
  5415. while (aws_mp_iseven (&v) == 1) {
  5416. /* 5.1 v = v/2 */
  5417. if ((res = aws_mp_div_2(&v, &v)) != AWS_MP_OKAY) {
  5418. goto LBL_ERR;
  5419. }
  5420. /* 5.2 if D is odd then */
  5421. if (aws_mp_isodd (&D) == 1) {
  5422. /* D = (D-x)/2 */
  5423. if ((res = aws_mp_sub(&D, &x, &D)) != AWS_MP_OKAY) {
  5424. goto LBL_ERR;
  5425. }
  5426. }
  5427. /* D = D/2 */
  5428. if ((res = aws_mp_div_2(&D, &D)) != AWS_MP_OKAY) {
  5429. goto LBL_ERR;
  5430. }
  5431. }
  5432. /* 6. if u >= v then */
  5433. if (aws_mp_cmp(&u, &v) != AWS_MP_LT) {
  5434. /* u = u - v, B = B - D */
  5435. if ((res = aws_mp_sub(&u, &v, &u)) != AWS_MP_OKAY) {
  5436. goto LBL_ERR;
  5437. }
  5438. if ((res = aws_mp_sub(&B, &D, &B)) != AWS_MP_OKAY) {
  5439. goto LBL_ERR;
  5440. }
  5441. } else {
  5442. /* v - v - u, D = D - B */
  5443. if ((res = aws_mp_sub(&v, &u, &v)) != AWS_MP_OKAY) {
  5444. goto LBL_ERR;
  5445. }
  5446. if ((res = aws_mp_sub(&D, &B, &D)) != AWS_MP_OKAY) {
  5447. goto LBL_ERR;
  5448. }
  5449. }
  5450. /* if not zero goto step 4 */
  5451. if (aws_mp_iszero (&u) == 0) {
  5452. goto top;
  5453. }
  5454. /* now a = C, b = D, gcd == g*v */
  5455. /* if v != 1 then there is no inverse */
  5456. if (aws_mp_cmp_d(&v, 1) != AWS_MP_EQ) {
  5457. res = AWS_MP_VAL;
  5458. goto LBL_ERR;
  5459. }
  5460. /* b is now the inverse */
  5461. neg = a->sign;
  5462. while (D.sign == AWS_MP_NEG) {
  5463. if ((res = aws_mp_add(&D, b, &D)) != AWS_MP_OKAY) {
  5464. goto LBL_ERR;
  5465. }
  5466. }
  5467. aws_mp_exch(&D, c);
  5468. c->sign = neg;
  5469. res = AWS_MP_OKAY;
  5470. LBL_ERR:
  5471. aws_mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL);
  5472. return res;
  5473. }
  5474. #endif
  5475. #ifdef AWS_BN_MP_TO_SIGNED_BIN_C
  5476. /* store in signed [big endian] format */
  5477. int aws_mp_to_signed_bin(aws_mp_int *a, unsigned char *b)
  5478. {
  5479. int res;
  5480. if ((res = aws_mp_to_unsigned_bin(a, b + 1)) != AWS_MP_OKAY) {
  5481. return res;
  5482. }
  5483. b[0] = (unsigned char) ((a->sign == AWS_MP_ZPOS) ? 0 : 1);
  5484. return AWS_MP_OKAY;
  5485. }
  5486. #endif
  5487. #ifdef AWS_BN_MP_MOD_C
  5488. /* c = a mod b, 0 <= c < b */
  5489. int
  5490. aws_mp_mod(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c)
  5491. {
  5492. aws_mp_int t;
  5493. int res;
  5494. if ((res = aws_mp_init(&t)) != AWS_MP_OKAY) {
  5495. return res;
  5496. }
  5497. if ((res = aws_mp_div(a, b, NULL, &t)) != AWS_MP_OKAY) {
  5498. aws_mp_clear(&t);
  5499. return res;
  5500. }
  5501. if (t.sign != b->sign) {
  5502. res = aws_mp_add(b, &t, c);
  5503. } else {
  5504. res = AWS_MP_OKAY;
  5505. aws_mp_exch(&t, c);
  5506. }
  5507. aws_mp_clear(&t);
  5508. return res;
  5509. }
  5510. #endif
  5511. #ifdef AWS_BN_MP_DR_IS_MODULUS_C
  5512. /* determines if a number is a valid DR modulus */
  5513. int aws_mp_dr_is_modulus(aws_mp_int *a)
  5514. {
  5515. int ix;
  5516. /* must be at least two digits */
  5517. if (a->used < 2) {
  5518. return 0;
  5519. }
  5520. /* must be of the form b**k - a [a <= b] so all
  5521. * but the first digit must be equal to -1 (mod b).
  5522. */
  5523. for (ix = 1; ix < a->used; ix++) {
  5524. if (a->dp[ix] != AWS_MP_MASK) {
  5525. return 0;
  5526. }
  5527. }
  5528. return 1;
  5529. }
  5530. #endif
  5531. #ifdef AWS_BN_MP_IS_SQUARE_C
  5532. /* Check if remainders are possible squares - fast exclude non-squares */
  5533. static const char rem_128[128] = {
  5534. 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  5535. 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  5536. 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  5537. 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  5538. 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  5539. 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  5540. 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
  5541. 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1
  5542. };
  5543. static const char rem_105[105] = {
  5544. 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,
  5545. 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
  5546. 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
  5547. 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  5548. 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1,
  5549. 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1,
  5550. 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1
  5551. };
  5552. /* Store non-zero to ret if arg is square, and zero if not */
  5553. int aws_mp_is_square(aws_mp_int *arg, int *ret)
  5554. {
  5555. int res;
  5556. aws_mp_digit c;
  5557. aws_mp_int t;
  5558. unsigned long r;
  5559. /* Default to Non-square :) */
  5560. *ret = AWS_MP_NO;
  5561. if (arg->sign == AWS_MP_NEG) {
  5562. return AWS_MP_VAL;
  5563. }
  5564. /* digits used? (TSD) */
  5565. if (arg->used == 0) {
  5566. return AWS_MP_OKAY;
  5567. }
  5568. /* First check mod 128 (suppose that AWS_DIGIT_BIT is at least 7) */
  5569. if (rem_128[127 & AWS_JKTM_DIGIT(arg,0)] == 1) {
  5570. return AWS_MP_OKAY;
  5571. }
  5572. /* Next check mod 105 (3*5*7) */
  5573. if ((res = aws_mp_mod_d(arg, 105, &c)) != AWS_MP_OKAY) {
  5574. return res;
  5575. }
  5576. if (rem_105[c] == 1) {
  5577. return AWS_MP_OKAY;
  5578. }
  5579. if ((res = aws_mp_init_set_int(&t, 11L * 13L * 17L * 19L * 23L * 29L * 31L)) != AWS_MP_OKAY) {
  5580. return res;
  5581. }
  5582. if ((res = aws_mp_mod(arg, &t, &t)) != AWS_MP_OKAY) {
  5583. goto ERR;
  5584. }
  5585. r = aws_mp_get_int(&t);
  5586. /* Check for other prime modules, note it's not an ERROR but we must
  5587. * free "t" so the easiest way is to goto ERR. We know that res
  5588. * is already equal to AWS_MP_OKAY from the aws_mp_mod call
  5589. */
  5590. if ( (1L<<(r%11)) & 0x5C4L ) goto ERR;
  5591. if ( (1L<<(r%13)) & 0x9E4L ) goto ERR;
  5592. if ( (1L<<(r%17)) & 0x5CE8L ) goto ERR;
  5593. if ( (1L<<(r%19)) & 0x4F50CL ) goto ERR;
  5594. if ( (1L<<(r%23)) & 0x7ACCA0L ) goto ERR;
  5595. if ( (1L<<(r%29)) & 0xC2EDD0CL ) goto ERR;
  5596. if ( (1L<<(r%31)) & 0x6DE2B848L ) goto ERR;
  5597. /* Final check - is sqr(sqrt(arg)) == arg ? */
  5598. if ((res = aws_mp_sqrt(arg, &t)) != AWS_MP_OKAY) {
  5599. goto ERR;
  5600. }
  5601. if ((res = aws_mp_sqr(&t, &t)) != AWS_MP_OKAY) {
  5602. goto ERR;
  5603. }
  5604. *ret = (aws_mp_cmp_mag(&t, arg) == AWS_MP_EQ) ? AWS_MP_YES : AWS_MP_NO;
  5605. ERR:
  5606. aws_mp_clear(&t);
  5607. return res;
  5608. }
  5609. #endif
  5610. #ifdef AWS_BN_MP_SUBMOD_C
  5611. /* d = a - b (mod c) */
  5612. int
  5613. aws_mp_submod(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c, aws_mp_int *d)
  5614. {
  5615. int res;
  5616. aws_mp_int t;
  5617. if ((res = aws_mp_init(&t)) != AWS_MP_OKAY) {
  5618. return res;
  5619. }
  5620. if ((res = aws_mp_sub(a, b, &t)) != AWS_MP_OKAY) {
  5621. aws_mp_clear(&t);
  5622. return res;
  5623. }
  5624. res = aws_mp_mod(&t, c, d);
  5625. aws_mp_clear(&t);
  5626. return res;
  5627. }
  5628. #endif
  5629. #ifdef AWS_BN_MP_RAND_C
  5630. /* makes a pseudo-random int of a given size */
  5631. int
  5632. aws_mp_rand(aws_mp_int *a, int digits)
  5633. {
  5634. int res;
  5635. aws_mp_digit d;
  5636. aws_mp_zero(a);
  5637. if (digits <= 0) {
  5638. return AWS_MP_OKAY;
  5639. }
  5640. /* first place a random non-zero digit */
  5641. do {
  5642. d = ((aws_mp_digit) abs (rand ())) & AWS_MP_MASK;
  5643. } while (d == 0);
  5644. if ((res = aws_mp_add_d(a, d, a)) != AWS_MP_OKAY) {
  5645. return res;
  5646. }
  5647. while (--digits > 0) {
  5648. if ((res = aws_mp_lshd(a, 1)) != AWS_MP_OKAY) {
  5649. return res;
  5650. }
  5651. if ((res = aws_mp_add_d(a, ((aws_mp_digit) abs(rand())), a)) != AWS_MP_OKAY) {
  5652. return res;
  5653. }
  5654. }
  5655. return AWS_MP_OKAY;
  5656. }
  5657. #endif
  5658. #ifdef AWS_BN_MP_READ_RADIX_C
  5659. /* read a string [ASCII] in a given radix */
  5660. int aws_mp_read_radix(aws_mp_int *a, const char *str, int radix)
  5661. {
  5662. int y, res, neg;
  5663. char ch;
  5664. /* zero the digit bignum */
  5665. aws_mp_zero(a);
  5666. /* make sure the radix is ok */
  5667. if (radix < 2 || radix > 64) {
  5668. return AWS_MP_VAL;
  5669. }
  5670. /* if the leading digit is a
  5671. * minus set the sign to negative.
  5672. */
  5673. if (*str == '-') {
  5674. ++str;
  5675. neg = AWS_MP_NEG;
  5676. } else {
  5677. neg = AWS_MP_ZPOS;
  5678. }
  5679. /* set the integer to the default of zero */
  5680. aws_mp_zero(a);
  5681. /* process each digit of the string */
  5682. while (*str) {
  5683. /* if the radix < 36 the conversion is case insensitive
  5684. * this allows numbers like 1AB and 1ab to represent the same value
  5685. * [e.g. in hex]
  5686. */
  5687. ch = (char) ((radix < 36) ? toupper (*str) : *str);
  5688. for (y = 0; y < 64; y++) {
  5689. if (ch == aws_mp_s_rmap[y]) {
  5690. break;
  5691. }
  5692. }
  5693. /* if the char was found in the map
  5694. * and is less than the given radix add it
  5695. * to the number, otherwise exit the loop.
  5696. */
  5697. if (y < radix) {
  5698. if ((res = aws_mp_mul_d(a, (aws_mp_digit) radix, a)) != AWS_MP_OKAY) {
  5699. return res;
  5700. }
  5701. if ((res = aws_mp_add_d(a, (aws_mp_digit) y, a)) != AWS_MP_OKAY) {
  5702. return res;
  5703. }
  5704. } else {
  5705. break;
  5706. }
  5707. ++str;
  5708. }
  5709. /* set the sign only if a != 0 */
  5710. if (aws_mp_iszero(a) != 1) {
  5711. a->sign = neg;
  5712. }
  5713. return AWS_MP_OKAY;
  5714. }
  5715. #endif
  5716. #ifdef AWS_BN_MP_CLEAR_MULTI_C
  5717. #include <stdarg.h>
  5718. void aws_mp_clear_multi(aws_mp_int *mp, ...)
  5719. {
  5720. aws_mp_int * next_mp = mp;
  5721. va_list args;
  5722. va_start(args, mp);
  5723. while (next_mp != NULL) {
  5724. aws_mp_clear(next_mp);
  5725. next_mp = va_arg(args, aws_mp_int*);
  5726. }
  5727. va_end(args);
  5728. }
  5729. #endif
  5730. #ifdef AWS_BN_MP_ADDMOD_C
  5731. /* d = a + b (mod c) */
  5732. int
  5733. aws_mp_addmod(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c, aws_mp_int *d)
  5734. {
  5735. int res;
  5736. aws_mp_int t;
  5737. if ((res = aws_mp_init(&t)) != AWS_MP_OKAY) {
  5738. return res;
  5739. }
  5740. if ((res = aws_mp_add(a, b, &t)) != AWS_MP_OKAY) {
  5741. aws_mp_clear(&t);
  5742. return res;
  5743. }
  5744. res = aws_mp_mod(&t, c, d);
  5745. aws_mp_clear(&t);
  5746. return res;
  5747. }
  5748. #endif
  5749. #ifdef AWS_BN_S_MP_MUL_HIGH_DIGS_C
  5750. /* multiplies |a| * |b| and does not compute the lower digs digits
  5751. * [meant to get the higher part of the product]
  5752. */
  5753. int
  5754. aws_s_mp_mul_high_digs(aws_mp_int *a, aws_mp_int *b, aws_mp_int *c, int digs)
  5755. {
  5756. aws_mp_int t;
  5757. int res, pa, pb, ix, iy;
  5758. aws_mp_digit u;
  5759. aws_mp_word r;
  5760. aws_mp_digit tmpx, *tmpt, *tmpy;
  5761. /* can we use the fast multiplier? */
  5762. #ifdef AWS_BN_FAST_S_MP_MUL_HIGH_DIGS_C
  5763. if (((a->used + b->used + 1) < AWS_MP_WARRAY)
  5764. && AWS_MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (aws_mp_word)) - (2 * AWS_DIGIT_BIT)))) {
  5765. return aws_fast_s_mp_mul_high_digs(a, b, c, digs);
  5766. }
  5767. #endif
  5768. if ((res = aws_mp_init_size(&t, a->used + b->used + 1)) != AWS_MP_OKAY) {
  5769. return res;
  5770. }
  5771. t.used = a->used + b->used + 1;
  5772. pa = a->used;
  5773. pb = b->used;
  5774. for (ix = 0; ix < pa; ix++) {
  5775. /* clear the carry */
  5776. u = 0;
  5777. /* left hand side of A[ix] * B[iy] */
  5778. tmpx = a->dp[ix];
  5779. /* alias to the address of where the digits will be stored */
  5780. tmpt = &(t.dp[digs]);
  5781. /* alias for where to read the right hand side from */
  5782. tmpy = b->dp + (digs - ix);
  5783. for (iy = digs - ix; iy < pb; iy++) {
  5784. /* calculate the double precision result */
  5785. r = ((aws_mp_word)*tmpt) +
  5786. ((aws_mp_word)tmpx) * ((aws_mp_word)*tmpy++) +
  5787. ((aws_mp_word) u);
  5788. /* get the lower part */
  5789. *tmpt++ = (aws_mp_digit) (r & ((aws_mp_word) AWS_MP_MASK));
  5790. /* carry the carry */
  5791. u = (aws_mp_digit) (r >> ((aws_mp_word) AWS_DIGIT_BIT));
  5792. }
  5793. *tmpt = u;
  5794. }
  5795. aws_mp_clamp(&t);
  5796. aws_mp_exch(&t, c);
  5797. aws_mp_clear(&t);
  5798. return AWS_MP_OKAY;
  5799. }
  5800. #endif
  5801. #ifdef AWS_BN_MP_UNSIGNED_BIN_SIZE_C
  5802. /* get the size for an unsigned equivalent */
  5803. int aws_mp_unsigned_bin_size(aws_mp_int *a)
  5804. {
  5805. int size = aws_mp_count_bits(a);
  5806. return (size / 8 + ((size & 7) != 0 ? 1 : 0));
  5807. }
  5808. #endif