libtommath.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400
  1. /*
  2. * Minimal code for RSA support from LibTomMath 0.41
  3. * http://libtom.org/
  4. * http://libtom.org/files/ltm-0.41.tar.bz2
  5. * This library was released in public domain by Tom St Denis.
  6. *
  7. * The combination in this file may not use all of the optimized algorithms
  8. * from LibTomMath and may be considerable slower than the LibTomMath with its
  9. * default settings. The main purpose of having this version here is to make it
  10. * easier to build bignum.c wrapper without having to install and build an
  11. * external library.
  12. *
  13. * If CONFIG_INTERNAL_LIBTOMMATH is defined, bignum.c includes this
  14. * libtommath.c file instead of using the external LibTomMath library.
  15. */
  16. #ifndef CHAR_BIT
  17. #define CHAR_BIT 8
  18. #endif
  19. #define BN_MP_INVMOD_C
  20. #define BN_S_MP_EXPTMOD_C /* Note: #undef in tommath_superclass.h; this would
  21. * require BN_MP_EXPTMOD_FAST_C instead */
  22. #define BN_S_MP_MUL_DIGS_C
  23. #define BN_MP_INVMOD_SLOW_C
  24. #define BN_S_MP_SQR_C
  25. #define BN_S_MP_MUL_HIGH_DIGS_C /* Note: #undef in tommath_superclass.h; this
  26. * would require other than mp_reduce */
  27. #ifdef LTM_FAST
  28. /* Use faster div at the cost of about 1 kB */
  29. #define BN_MP_MUL_D_C
  30. /* Include faster exptmod (Montgomery) at the cost of about 2.5 kB in code */
  31. #define BN_MP_EXPTMOD_FAST_C
  32. #define BN_MP_MONTGOMERY_SETUP_C
  33. #define BN_FAST_MP_MONTGOMERY_REDUCE_C
  34. #define BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  35. #define BN_MP_MUL_2_C
  36. /* Include faster sqr at the cost of about 0.5 kB in code */
  37. #define BN_FAST_S_MP_SQR_C
  38. /* About 0.25 kB of code, but ~1.7kB of stack space! */
  39. #define BN_FAST_S_MP_MUL_DIGS_C
  40. #else /* LTM_FAST */
  41. #define BN_MP_DIV_SMALL
  42. #define BN_MP_INIT_MULTI_C
  43. #define BN_MP_CLEAR_MULTI_C
  44. #define BN_MP_ABS_C
  45. #endif /* LTM_FAST */
  46. /* Current uses do not require support for negative exponent in exptmod, so we
  47. * can save about 1.5 kB in leaving out invmod. */
  48. #define LTM_NO_NEG_EXP
  49. /* from tommath.h */
  50. #ifndef MIN
  51. #define MIN(x,y) ((x)<(y)?(x):(y))
  52. #endif
  53. #ifndef MAX
  54. #define MAX(x,y) ((x)>(y)?(x):(y))
  55. #endif
  56. #define OPT_CAST(x)
  57. #ifdef __x86_64__
  58. typedef unsigned long mp_digit;
  59. typedef unsigned long mp_word __attribute__((mode(TI)));
  60. #define DIGIT_BIT 60
  61. #define MP_64BIT
  62. #else
  63. typedef unsigned long mp_digit;
  64. typedef u64 mp_word;
  65. #define DIGIT_BIT 28
  66. #define MP_28BIT
  67. #endif
  68. #define XMALLOC os_malloc
  69. #define XFREE os_free
  70. #define XREALLOC os_realloc
  71. #define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
  72. #define MP_LT -1 /* less than */
  73. #define MP_EQ 0 /* equal to */
  74. #define MP_GT 1 /* greater than */
  75. #define MP_ZPOS 0 /* positive integer */
  76. #define MP_NEG 1 /* negative */
  77. #define MP_OKAY 0 /* ok result */
  78. #define MP_MEM -2 /* out of mem */
  79. #define MP_VAL -3 /* invalid input */
  80. #define MP_YES 1 /* yes response */
  81. #define MP_NO 0 /* no response */
  82. typedef int mp_err;
  83. /* define this to use lower memory usage routines (exptmods mostly) */
  84. #define MP_LOW_MEM
  85. /* default precision */
  86. #ifndef MP_PREC
  87. #ifndef MP_LOW_MEM
  88. #define MP_PREC 32 /* default digits of precision */
  89. #else
  90. #define MP_PREC 8 /* default digits of precision */
  91. #endif
  92. #endif
  93. /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
  94. #define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
  95. /* the infamous mp_int structure */
  96. typedef struct {
  97. int used, alloc, sign;
  98. mp_digit *dp;
  99. } mp_int;
  100. /* ---> Basic Manipulations <--- */
  101. #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
  102. #define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
  103. #define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
  104. /* prototypes for copied functions */
  105. #define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
  106. static int s_mp_exptmod(mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
  107. static int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  108. static int s_mp_sqr(mp_int * a, mp_int * b);
  109. static int s_mp_mul_high_digs(mp_int * a, mp_int * b, mp_int * c, int digs);
  110. #ifdef BN_FAST_S_MP_MUL_DIGS_C
  111. static int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
  112. #endif
  113. #ifdef BN_MP_INIT_MULTI_C
  114. static int mp_init_multi(mp_int *mp, ...);
  115. #endif
  116. #ifdef BN_MP_CLEAR_MULTI_C
  117. static void mp_clear_multi(mp_int *mp, ...);
  118. #endif
  119. static int mp_lshd(mp_int * a, int b);
  120. static void mp_set(mp_int * a, mp_digit b);
  121. static void mp_clamp(mp_int * a);
  122. static void mp_exch(mp_int * a, mp_int * b);
  123. static void mp_rshd(mp_int * a, int b);
  124. static void mp_zero(mp_int * a);
  125. static int mp_mod_2d(mp_int * a, int b, mp_int * c);
  126. static int mp_div_2d(mp_int * a, int b, mp_int * c, mp_int * d);
  127. static int mp_init_copy(mp_int * a, mp_int * b);
  128. static int mp_mul_2d(mp_int * a, int b, mp_int * c);
  129. #ifndef LTM_NO_NEG_EXP
  130. static int mp_div_2(mp_int * a, mp_int * b);
  131. static int mp_invmod(mp_int * a, mp_int * b, mp_int * c);
  132. static int mp_invmod_slow(mp_int * a, mp_int * b, mp_int * c);
  133. #endif /* LTM_NO_NEG_EXP */
  134. static int mp_copy(mp_int * a, mp_int * b);
  135. static int mp_count_bits(mp_int * a);
  136. static int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
  137. static int mp_mod(mp_int * a, mp_int * b, mp_int * c);
  138. static int mp_grow(mp_int * a, int size);
  139. static int mp_cmp_mag(mp_int * a, mp_int * b);
  140. #ifdef BN_MP_ABS_C
  141. static int mp_abs(mp_int * a, mp_int * b);
  142. #endif
  143. static int mp_sqr(mp_int * a, mp_int * b);
  144. static int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
  145. static int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
  146. static int mp_2expt(mp_int * a, int b);
  147. static int mp_reduce_setup(mp_int * a, mp_int * b);
  148. static int mp_reduce(mp_int * x, mp_int * m, mp_int * mu);
  149. static int mp_init_size(mp_int * a, int size);
  150. #ifdef BN_MP_EXPTMOD_FAST_C
  151. static int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
  152. #endif /* BN_MP_EXPTMOD_FAST_C */
  153. #ifdef BN_FAST_S_MP_SQR_C
  154. static int fast_s_mp_sqr (mp_int * a, mp_int * b);
  155. #endif /* BN_FAST_S_MP_SQR_C */
  156. #ifdef BN_MP_MUL_D_C
  157. static int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
  158. #endif /* BN_MP_MUL_D_C */
  159. /* functions from bn_<func name>.c */
  160. /* reverse an array, used for radix code */
  161. static void bn_reverse (unsigned char *s, int len)
  162. {
  163. int ix, iy;
  164. unsigned char t;
  165. ix = 0;
  166. iy = len - 1;
  167. while (ix < iy) {
  168. t = s[ix];
  169. s[ix] = s[iy];
  170. s[iy] = t;
  171. ++ix;
  172. --iy;
  173. }
  174. }
  175. /* low level addition, based on HAC pp.594, Algorithm 14.7 */
  176. static int s_mp_add (mp_int * a, mp_int * b, mp_int * c)
  177. {
  178. mp_int *x;
  179. int olduse, res, min, max;
  180. /* find sizes, we let |a| <= |b| which means we have to sort
  181. * them. "x" will point to the input with the most digits
  182. */
  183. if (a->used > b->used) {
  184. min = b->used;
  185. max = a->used;
  186. x = a;
  187. } else {
  188. min = a->used;
  189. max = b->used;
  190. x = b;
  191. }
  192. /* init result */
  193. if (c->alloc < max + 1) {
  194. if ((res = mp_grow (c, max + 1)) != MP_OKAY) {
  195. return res;
  196. }
  197. }
  198. /* get old used digit count and set new one */
  199. olduse = c->used;
  200. c->used = max + 1;
  201. {
  202. register mp_digit u, *tmpa, *tmpb, *tmpc;
  203. register int i;
  204. /* alias for digit pointers */
  205. /* first input */
  206. tmpa = a->dp;
  207. /* second input */
  208. tmpb = b->dp;
  209. /* destination */
  210. tmpc = c->dp;
  211. /* zero the carry */
  212. u = 0;
  213. for (i = 0; i < min; i++) {
  214. /* Compute the sum at one digit, T[i] = A[i] + B[i] + U */
  215. *tmpc = *tmpa++ + *tmpb++ + u;
  216. /* U = carry bit of T[i] */
  217. u = *tmpc >> ((mp_digit)DIGIT_BIT);
  218. /* take away carry bit from T[i] */
  219. *tmpc++ &= MP_MASK;
  220. }
  221. /* now copy higher words if any, that is in A+B
  222. * if A or B has more digits add those in
  223. */
  224. if (min != max) {
  225. for (; i < max; i++) {
  226. /* T[i] = X[i] + U */
  227. *tmpc = x->dp[i] + u;
  228. /* U = carry bit of T[i] */
  229. u = *tmpc >> ((mp_digit)DIGIT_BIT);
  230. /* take away carry bit from T[i] */
  231. *tmpc++ &= MP_MASK;
  232. }
  233. }
  234. /* add carry */
  235. *tmpc++ = u;
  236. /* clear digits above oldused */
  237. for (i = c->used; i < olduse; i++) {
  238. *tmpc++ = 0;
  239. }
  240. }
  241. mp_clamp (c);
  242. return MP_OKAY;
  243. }
  244. /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
  245. static int s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
  246. {
  247. int olduse, res, min, max;
  248. /* find sizes */
  249. min = b->used;
  250. max = a->used;
  251. /* init result */
  252. if (c->alloc < max) {
  253. if ((res = mp_grow (c, max)) != MP_OKAY) {
  254. return res;
  255. }
  256. }
  257. olduse = c->used;
  258. c->used = max;
  259. {
  260. register mp_digit u, *tmpa, *tmpb, *tmpc;
  261. register int i;
  262. /* alias for digit pointers */
  263. tmpa = a->dp;
  264. tmpb = b->dp;
  265. tmpc = c->dp;
  266. /* set carry to zero */
  267. u = 0;
  268. for (i = 0; i < min; i++) {
  269. /* T[i] = A[i] - B[i] - U */
  270. *tmpc = *tmpa++ - *tmpb++ - u;
  271. /* U = carry bit of T[i]
  272. * Note this saves performing an AND operation since
  273. * if a carry does occur it will propagate all the way to the
  274. * MSB. As a result a single shift is enough to get the carry
  275. */
  276. u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
  277. /* Clear carry from T[i] */
  278. *tmpc++ &= MP_MASK;
  279. }
  280. /* now copy higher words if any, e.g. if A has more digits than B */
  281. for (; i < max; i++) {
  282. /* T[i] = A[i] - U */
  283. *tmpc = *tmpa++ - u;
  284. /* U = carry bit of T[i] */
  285. u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1));
  286. /* Clear carry from T[i] */
  287. *tmpc++ &= MP_MASK;
  288. }
  289. /* clear digits above used (since we may not have grown result above) */
  290. for (i = c->used; i < olduse; i++) {
  291. *tmpc++ = 0;
  292. }
  293. }
  294. mp_clamp (c);
  295. return MP_OKAY;
  296. }
  297. /* init a new mp_int */
  298. static int mp_init (mp_int * a)
  299. {
  300. int i;
  301. /* allocate memory required and clear it */
  302. a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC);
  303. if (a->dp == NULL) {
  304. return MP_MEM;
  305. }
  306. /* set the digits to zero */
  307. for (i = 0; i < MP_PREC; i++) {
  308. a->dp[i] = 0;
  309. }
  310. /* set the used to zero, allocated digits to the default precision
  311. * and sign to positive */
  312. a->used = 0;
  313. a->alloc = MP_PREC;
  314. a->sign = MP_ZPOS;
  315. return MP_OKAY;
  316. }
  317. /* clear one (frees) */
  318. static void mp_clear (mp_int * a)
  319. {
  320. int i;
  321. /* only do anything if a hasn't been freed previously */
  322. if (a->dp != NULL) {
  323. /* first zero the digits */
  324. for (i = 0; i < a->used; i++) {
  325. a->dp[i] = 0;
  326. }
  327. /* free ram */
  328. XFREE(a->dp);
  329. /* reset members to make debugging easier */
  330. a->dp = NULL;
  331. a->alloc = a->used = 0;
  332. a->sign = MP_ZPOS;
  333. }
  334. }
  335. /* high level addition (handles signs) */
  336. static int mp_add (mp_int * a, mp_int * b, mp_int * c)
  337. {
  338. int sa, sb, res;
  339. /* get sign of both inputs */
  340. sa = a->sign;
  341. sb = b->sign;
  342. /* handle two cases, not four */
  343. if (sa == sb) {
  344. /* both positive or both negative */
  345. /* add their magnitudes, copy the sign */
  346. c->sign = sa;
  347. res = s_mp_add (a, b, c);
  348. } else {
  349. /* one positive, the other negative */
  350. /* subtract the one with the greater magnitude from */
  351. /* the one of the lesser magnitude. The result gets */
  352. /* the sign of the one with the greater magnitude. */
  353. if (mp_cmp_mag (a, b) == MP_LT) {
  354. c->sign = sb;
  355. res = s_mp_sub (b, a, c);
  356. } else {
  357. c->sign = sa;
  358. res = s_mp_sub (a, b, c);
  359. }
  360. }
  361. return res;
  362. }
  363. /* high level subtraction (handles signs) */
  364. static int mp_sub (mp_int * a, mp_int * b, mp_int * c)
  365. {
  366. int sa, sb, res;
  367. sa = a->sign;
  368. sb = b->sign;
  369. if (sa != sb) {
  370. /* subtract a negative from a positive, OR */
  371. /* subtract a positive from a negative. */
  372. /* In either case, ADD their magnitudes, */
  373. /* and use the sign of the first number. */
  374. c->sign = sa;
  375. res = s_mp_add (a, b, c);
  376. } else {
  377. /* subtract a positive from a positive, OR */
  378. /* subtract a negative from a negative. */
  379. /* First, take the difference between their */
  380. /* magnitudes, then... */
  381. if (mp_cmp_mag (a, b) != MP_LT) {
  382. /* Copy the sign from the first */
  383. c->sign = sa;
  384. /* The first has a larger or equal magnitude */
  385. res = s_mp_sub (a, b, c);
  386. } else {
  387. /* The result has the *opposite* sign from */
  388. /* the first number. */
  389. c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS;
  390. /* The second has a larger magnitude */
  391. res = s_mp_sub (b, a, c);
  392. }
  393. }
  394. return res;
  395. }
  396. /* high level multiplication (handles sign) */
  397. static int mp_mul (mp_int * a, mp_int * b, mp_int * c)
  398. {
  399. int res, neg;
  400. neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
  401. /* use Toom-Cook? */
  402. #ifdef BN_MP_TOOM_MUL_C
  403. if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
  404. res = mp_toom_mul(a, b, c);
  405. } else
  406. #endif
  407. #ifdef BN_MP_KARATSUBA_MUL_C
  408. /* use Karatsuba? */
  409. if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
  410. res = mp_karatsuba_mul (a, b, c);
  411. } else
  412. #endif
  413. {
  414. /* can we use the fast multiplier?
  415. *
  416. * The fast multiplier can be used if the output will
  417. * have less than MP_WARRAY digits and the number of
  418. * digits won't affect carry propagation
  419. */
  420. #ifdef BN_FAST_S_MP_MUL_DIGS_C
  421. int digs = a->used + b->used + 1;
  422. if ((digs < MP_WARRAY) &&
  423. MIN(a->used, b->used) <=
  424. (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  425. res = fast_s_mp_mul_digs (a, b, c, digs);
  426. } else
  427. #endif
  428. #ifdef BN_S_MP_MUL_DIGS_C
  429. res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
  430. #else
  431. #error mp_mul could fail
  432. res = MP_VAL;
  433. #endif
  434. }
  435. c->sign = (c->used > 0) ? neg : MP_ZPOS;
  436. return res;
  437. }
  438. /* d = a * b (mod c) */
  439. static int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  440. {
  441. int res;
  442. mp_int t;
  443. if ((res = mp_init (&t)) != MP_OKAY) {
  444. return res;
  445. }
  446. if ((res = mp_mul (a, b, &t)) != MP_OKAY) {
  447. mp_clear (&t);
  448. return res;
  449. }
  450. res = mp_mod (&t, c, d);
  451. mp_clear (&t);
  452. return res;
  453. }
  454. /* c = a mod b, 0 <= c < b */
  455. static int mp_mod (mp_int * a, mp_int * b, mp_int * c)
  456. {
  457. mp_int t;
  458. int res;
  459. if ((res = mp_init (&t)) != MP_OKAY) {
  460. return res;
  461. }
  462. if ((res = mp_div (a, b, NULL, &t)) != MP_OKAY) {
  463. mp_clear (&t);
  464. return res;
  465. }
  466. if (t.sign != b->sign) {
  467. res = mp_add (b, &t, c);
  468. } else {
  469. res = MP_OKAY;
  470. mp_exch (&t, c);
  471. }
  472. mp_clear (&t);
  473. return res;
  474. }
  475. /* this is a shell function that calls either the normal or Montgomery
  476. * exptmod functions. Originally the call to the montgomery code was
  477. * embedded in the normal function but that wasted a lot of stack space
  478. * for nothing (since 99% of the time the Montgomery code would be called)
  479. */
  480. static int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
  481. {
  482. int dr;
  483. /* modulus P must be positive */
  484. if (P->sign == MP_NEG) {
  485. return MP_VAL;
  486. }
  487. /* if exponent X is negative we have to recurse */
  488. if (X->sign == MP_NEG) {
  489. #ifdef LTM_NO_NEG_EXP
  490. return MP_VAL;
  491. #else /* LTM_NO_NEG_EXP */
  492. #ifdef BN_MP_INVMOD_C
  493. mp_int tmpG, tmpX;
  494. int err;
  495. /* first compute 1/G mod P */
  496. if ((err = mp_init(&tmpG)) != MP_OKAY) {
  497. return err;
  498. }
  499. if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) {
  500. mp_clear(&tmpG);
  501. return err;
  502. }
  503. /* now get |X| */
  504. if ((err = mp_init(&tmpX)) != MP_OKAY) {
  505. mp_clear(&tmpG);
  506. return err;
  507. }
  508. if ((err = mp_abs(X, &tmpX)) != MP_OKAY) {
  509. mp_clear_multi(&tmpG, &tmpX, NULL);
  510. return err;
  511. }
  512. /* and now compute (1/G)**|X| instead of G**X [X < 0] */
  513. err = mp_exptmod(&tmpG, &tmpX, P, Y);
  514. mp_clear_multi(&tmpG, &tmpX, NULL);
  515. return err;
  516. #else
  517. #error mp_exptmod would always fail
  518. /* no invmod */
  519. return MP_VAL;
  520. #endif
  521. #endif /* LTM_NO_NEG_EXP */
  522. }
  523. /* modified diminished radix reduction */
  524. #if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C)
  525. if (mp_reduce_is_2k_l(P) == MP_YES) {
  526. return s_mp_exptmod(G, X, P, Y, 1);
  527. }
  528. #endif
  529. #ifdef BN_MP_DR_IS_MODULUS_C
  530. /* is it a DR modulus? */
  531. dr = mp_dr_is_modulus(P);
  532. #else
  533. /* default to no */
  534. dr = 0;
  535. #endif
  536. #ifdef BN_MP_REDUCE_IS_2K_C
  537. /* if not, is it a unrestricted DR modulus? */
  538. if (dr == 0) {
  539. dr = mp_reduce_is_2k(P) << 1;
  540. }
  541. #endif
  542. /* if the modulus is odd or dr != 0 use the montgomery method */
  543. #ifdef BN_MP_EXPTMOD_FAST_C
  544. if (mp_isodd (P) == 1 || dr != 0) {
  545. return mp_exptmod_fast (G, X, P, Y, dr);
  546. } else {
  547. #endif
  548. #ifdef BN_S_MP_EXPTMOD_C
  549. /* otherwise use the generic Barrett reduction technique */
  550. return s_mp_exptmod (G, X, P, Y, 0);
  551. #else
  552. #error mp_exptmod could fail
  553. /* no exptmod for evens */
  554. return MP_VAL;
  555. #endif
  556. #ifdef BN_MP_EXPTMOD_FAST_C
  557. }
  558. #endif
  559. if (dr == 0) {
  560. /* avoid compiler warnings about possibly unused variable */
  561. }
  562. }
  563. /* compare two ints (signed)*/
  564. static int mp_cmp (mp_int * a, mp_int * b)
  565. {
  566. /* compare based on sign */
  567. if (a->sign != b->sign) {
  568. if (a->sign == MP_NEG) {
  569. return MP_LT;
  570. } else {
  571. return MP_GT;
  572. }
  573. }
  574. /* compare digits */
  575. if (a->sign == MP_NEG) {
  576. /* if negative compare opposite direction */
  577. return mp_cmp_mag(b, a);
  578. } else {
  579. return mp_cmp_mag(a, b);
  580. }
  581. }
  582. /* compare a digit */
  583. static int mp_cmp_d(mp_int * a, mp_digit b)
  584. {
  585. /* compare based on sign */
  586. if (a->sign == MP_NEG) {
  587. return MP_LT;
  588. }
  589. /* compare based on magnitude */
  590. if (a->used > 1) {
  591. return MP_GT;
  592. }
  593. /* compare the only digit of a to b */
  594. if (a->dp[0] > b) {
  595. return MP_GT;
  596. } else if (a->dp[0] < b) {
  597. return MP_LT;
  598. } else {
  599. return MP_EQ;
  600. }
  601. }
  602. #ifndef LTM_NO_NEG_EXP
  603. /* hac 14.61, pp608 */
  604. static int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
  605. {
  606. /* b cannot be negative */
  607. if (b->sign == MP_NEG || mp_iszero(b) == 1) {
  608. return MP_VAL;
  609. }
  610. #ifdef BN_FAST_MP_INVMOD_C
  611. /* if the modulus is odd we can use a faster routine instead */
  612. if (mp_isodd (b) == 1) {
  613. return fast_mp_invmod (a, b, c);
  614. }
  615. #endif
  616. #ifdef BN_MP_INVMOD_SLOW_C
  617. return mp_invmod_slow(a, b, c);
  618. #endif
  619. #ifndef BN_FAST_MP_INVMOD_C
  620. #ifndef BN_MP_INVMOD_SLOW_C
  621. #error mp_invmod would always fail
  622. #endif
  623. #endif
  624. return MP_VAL;
  625. }
  626. #endif /* LTM_NO_NEG_EXP */
  627. /* get the size for an unsigned equivalent */
  628. static int mp_unsigned_bin_size (mp_int * a)
  629. {
  630. int size = mp_count_bits (a);
  631. return (size / 8 + ((size & 7) != 0 ? 1 : 0));
  632. }
  633. #ifndef LTM_NO_NEG_EXP
  634. /* hac 14.61, pp608 */
  635. static int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
  636. {
  637. mp_int x, y, u, v, A, B, C, D;
  638. int res;
  639. /* b cannot be negative */
  640. if (b->sign == MP_NEG || mp_iszero(b) == 1) {
  641. return MP_VAL;
  642. }
  643. /* init temps */
  644. if ((res = mp_init_multi(&x, &y, &u, &v,
  645. &A, &B, &C, &D, NULL)) != MP_OKAY) {
  646. return res;
  647. }
  648. /* x = a, y = b */
  649. if ((res = mp_mod(a, b, &x)) != MP_OKAY) {
  650. goto LBL_ERR;
  651. }
  652. if ((res = mp_copy (b, &y)) != MP_OKAY) {
  653. goto LBL_ERR;
  654. }
  655. /* 2. [modified] if x,y are both even then return an error! */
  656. if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
  657. res = MP_VAL;
  658. goto LBL_ERR;
  659. }
  660. /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
  661. if ((res = mp_copy (&x, &u)) != MP_OKAY) {
  662. goto LBL_ERR;
  663. }
  664. if ((res = mp_copy (&y, &v)) != MP_OKAY) {
  665. goto LBL_ERR;
  666. }
  667. mp_set (&A, 1);
  668. mp_set (&D, 1);
  669. top:
  670. /* 4. while u is even do */
  671. while (mp_iseven (&u) == 1) {
  672. /* 4.1 u = u/2 */
  673. if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
  674. goto LBL_ERR;
  675. }
  676. /* 4.2 if A or B is odd then */
  677. if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
  678. /* A = (A+y)/2, B = (B-x)/2 */
  679. if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
  680. goto LBL_ERR;
  681. }
  682. if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
  683. goto LBL_ERR;
  684. }
  685. }
  686. /* A = A/2, B = B/2 */
  687. if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
  688. goto LBL_ERR;
  689. }
  690. if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
  691. goto LBL_ERR;
  692. }
  693. }
  694. /* 5. while v is even do */
  695. while (mp_iseven (&v) == 1) {
  696. /* 5.1 v = v/2 */
  697. if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
  698. goto LBL_ERR;
  699. }
  700. /* 5.2 if C or D is odd then */
  701. if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
  702. /* C = (C+y)/2, D = (D-x)/2 */
  703. if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
  704. goto LBL_ERR;
  705. }
  706. if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
  707. goto LBL_ERR;
  708. }
  709. }
  710. /* C = C/2, D = D/2 */
  711. if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
  712. goto LBL_ERR;
  713. }
  714. if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
  715. goto LBL_ERR;
  716. }
  717. }
  718. /* 6. if u >= v then */
  719. if (mp_cmp (&u, &v) != MP_LT) {
  720. /* u = u - v, A = A - C, B = B - D */
  721. if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
  722. goto LBL_ERR;
  723. }
  724. if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
  725. goto LBL_ERR;
  726. }
  727. if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
  728. goto LBL_ERR;
  729. }
  730. } else {
  731. /* v - v - u, C = C - A, D = D - B */
  732. if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
  733. goto LBL_ERR;
  734. }
  735. if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
  736. goto LBL_ERR;
  737. }
  738. if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
  739. goto LBL_ERR;
  740. }
  741. }
  742. /* if not zero goto step 4 */
  743. if (mp_iszero (&u) == 0)
  744. goto top;
  745. /* now a = C, b = D, gcd == g*v */
  746. /* if v != 1 then there is no inverse */
  747. if (mp_cmp_d (&v, 1) != MP_EQ) {
  748. res = MP_VAL;
  749. goto LBL_ERR;
  750. }
  751. /* if its too low */
  752. while (mp_cmp_d(&C, 0) == MP_LT) {
  753. if ((res = mp_add(&C, b, &C)) != MP_OKAY) {
  754. goto LBL_ERR;
  755. }
  756. }
  757. /* too big */
  758. while (mp_cmp_mag(&C, b) != MP_LT) {
  759. if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
  760. goto LBL_ERR;
  761. }
  762. }
  763. /* C is now the inverse */
  764. mp_exch (&C, c);
  765. res = MP_OKAY;
  766. LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
  767. return res;
  768. }
  769. #endif /* LTM_NO_NEG_EXP */
  770. /* compare maginitude of two ints (unsigned) */
  771. static int mp_cmp_mag (mp_int * a, mp_int * b)
  772. {
  773. int n;
  774. mp_digit *tmpa, *tmpb;
  775. /* compare based on # of non-zero digits */
  776. if (a->used > b->used) {
  777. return MP_GT;
  778. }
  779. if (a->used < b->used) {
  780. return MP_LT;
  781. }
  782. /* alias for a */
  783. tmpa = a->dp + (a->used - 1);
  784. /* alias for b */
  785. tmpb = b->dp + (a->used - 1);
  786. /* compare based on digits */
  787. for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
  788. if (*tmpa > *tmpb) {
  789. return MP_GT;
  790. }
  791. if (*tmpa < *tmpb) {
  792. return MP_LT;
  793. }
  794. }
  795. return MP_EQ;
  796. }
  797. /* reads a unsigned char array, assumes the msb is stored first [big endian] */
  798. static int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
  799. {
  800. int res;
  801. /* make sure there are at least two digits */
  802. if (a->alloc < 2) {
  803. if ((res = mp_grow(a, 2)) != MP_OKAY) {
  804. return res;
  805. }
  806. }
  807. /* zero the int */
  808. mp_zero (a);
  809. /* read the bytes in */
  810. while (c-- > 0) {
  811. if ((res = mp_mul_2d (a, 8, a)) != MP_OKAY) {
  812. return res;
  813. }
  814. #ifndef MP_8BIT
  815. a->dp[0] |= *b++;
  816. a->used += 1;
  817. #else
  818. a->dp[0] = (*b & MP_MASK);
  819. a->dp[1] |= ((*b++ >> 7U) & 1);
  820. a->used += 2;
  821. #endif
  822. }
  823. mp_clamp (a);
  824. return MP_OKAY;
  825. }
  826. /* store in unsigned [big endian] format */
  827. static int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
  828. {
  829. int x, res;
  830. mp_int t;
  831. if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
  832. return res;
  833. }
  834. x = 0;
  835. while (mp_iszero (&t) == 0) {
  836. #ifndef MP_8BIT
  837. b[x++] = (unsigned char) (t.dp[0] & 255);
  838. #else
  839. b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7));
  840. #endif
  841. if ((res = mp_div_2d (&t, 8, &t, NULL)) != MP_OKAY) {
  842. mp_clear (&t);
  843. return res;
  844. }
  845. }
  846. bn_reverse (b, x);
  847. mp_clear (&t);
  848. return MP_OKAY;
  849. }
  850. /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
  851. static int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
  852. {
  853. mp_digit D, r, rr;
  854. int x, res;
  855. mp_int t;
  856. /* if the shift count is <= 0 then we do no work */
  857. if (b <= 0) {
  858. res = mp_copy (a, c);
  859. if (d != NULL) {
  860. mp_zero (d);
  861. }
  862. return res;
  863. }
  864. if ((res = mp_init (&t)) != MP_OKAY) {
  865. return res;
  866. }
  867. /* get the remainder */
  868. if (d != NULL) {
  869. if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) {
  870. mp_clear (&t);
  871. return res;
  872. }
  873. }
  874. /* copy */
  875. if ((res = mp_copy (a, c)) != MP_OKAY) {
  876. mp_clear (&t);
  877. return res;
  878. }
  879. /* shift by as many digits in the bit count */
  880. if (b >= (int)DIGIT_BIT) {
  881. mp_rshd (c, b / DIGIT_BIT);
  882. }
  883. /* shift any bit count < DIGIT_BIT */
  884. D = (mp_digit) (b % DIGIT_BIT);
  885. if (D != 0) {
  886. register mp_digit *tmpc, mask, shift;
  887. /* mask */
  888. mask = (((mp_digit)1) << D) - 1;
  889. /* shift for lsb */
  890. shift = DIGIT_BIT - D;
  891. /* alias */
  892. tmpc = c->dp + (c->used - 1);
  893. /* carry */
  894. r = 0;
  895. for (x = c->used - 1; x >= 0; x--) {
  896. /* get the lower bits of this word in a temp */
  897. rr = *tmpc & mask;
  898. /* shift the current word and mix in the carry bits from the previous word */
  899. *tmpc = (*tmpc >> D) | (r << shift);
  900. --tmpc;
  901. /* set the carry to the carry bits of the current word found above */
  902. r = rr;
  903. }
  904. }
  905. mp_clamp (c);
  906. if (d != NULL) {
  907. mp_exch (&t, d);
  908. }
  909. mp_clear (&t);
  910. return MP_OKAY;
  911. }
  912. static int mp_init_copy (mp_int * a, mp_int * b)
  913. {
  914. int res;
  915. if ((res = mp_init (a)) != MP_OKAY) {
  916. return res;
  917. }
  918. return mp_copy (b, a);
  919. }
  920. /* set to zero */
  921. static void mp_zero (mp_int * a)
  922. {
  923. int n;
  924. mp_digit *tmp;
  925. a->sign = MP_ZPOS;
  926. a->used = 0;
  927. tmp = a->dp;
  928. for (n = 0; n < a->alloc; n++) {
  929. *tmp++ = 0;
  930. }
  931. }
  932. /* copy, b = a */
  933. static int mp_copy (mp_int * a, mp_int * b)
  934. {
  935. int res, n;
  936. /* if dst == src do nothing */
  937. if (a == b) {
  938. return MP_OKAY;
  939. }
  940. /* grow dest */
  941. if (b->alloc < a->used) {
  942. if ((res = mp_grow (b, a->used)) != MP_OKAY) {
  943. return res;
  944. }
  945. }
  946. /* zero b and copy the parameters over */
  947. {
  948. register mp_digit *tmpa, *tmpb;
  949. /* pointer aliases */
  950. /* source */
  951. tmpa = a->dp;
  952. /* destination */
  953. tmpb = b->dp;
  954. /* copy all the digits */
  955. for (n = 0; n < a->used; n++) {
  956. *tmpb++ = *tmpa++;
  957. }
  958. /* clear high digits */
  959. for (; n < b->used; n++) {
  960. *tmpb++ = 0;
  961. }
  962. }
  963. /* copy used count and sign */
  964. b->used = a->used;
  965. b->sign = a->sign;
  966. return MP_OKAY;
  967. }
  968. /* shift right a certain amount of digits */
  969. static void mp_rshd (mp_int * a, int b)
  970. {
  971. int x;
  972. /* if b <= 0 then ignore it */
  973. if (b <= 0) {
  974. return;
  975. }
  976. /* if b > used then simply zero it and return */
  977. if (a->used <= b) {
  978. mp_zero (a);
  979. return;
  980. }
  981. {
  982. register mp_digit *bottom, *top;
  983. /* shift the digits down */
  984. /* bottom */
  985. bottom = a->dp;
  986. /* top [offset into digits] */
  987. top = a->dp + b;
  988. /* this is implemented as a sliding window where
  989. * the window is b-digits long and digits from
  990. * the top of the window are copied to the bottom
  991. *
  992. * e.g.
  993. b-2 | b-1 | b0 | b1 | b2 | ... | bb | ---->
  994. /\ | ---->
  995. \-------------------/ ---->
  996. */
  997. for (x = 0; x < (a->used - b); x++) {
  998. *bottom++ = *top++;
  999. }
  1000. /* zero the top digits */
  1001. for (; x < a->used; x++) {
  1002. *bottom++ = 0;
  1003. }
  1004. }
  1005. /* remove excess digits */
  1006. a->used -= b;
  1007. }
  1008. /* swap the elements of two integers, for cases where you can't simply swap the
  1009. * mp_int pointers around
  1010. */
  1011. static void mp_exch (mp_int * a, mp_int * b)
  1012. {
  1013. mp_int t;
  1014. t = *a;
  1015. *a = *b;
  1016. *b = t;
  1017. }
  1018. /* trim unused digits
  1019. *
  1020. * This is used to ensure that leading zero digits are
  1021. * trimed and the leading "used" digit will be non-zero
  1022. * Typically very fast. Also fixes the sign if there
  1023. * are no more leading digits
  1024. */
  1025. static void mp_clamp (mp_int * a)
  1026. {
  1027. /* decrease used while the most significant digit is
  1028. * zero.
  1029. */
  1030. while (a->used > 0 && a->dp[a->used - 1] == 0) {
  1031. --(a->used);
  1032. }
  1033. /* reset the sign flag if used == 0 */
  1034. if (a->used == 0) {
  1035. a->sign = MP_ZPOS;
  1036. }
  1037. }
  1038. /* grow as required */
  1039. static int mp_grow (mp_int * a, int size)
  1040. {
  1041. int i;
  1042. mp_digit *tmp;
  1043. /* if the alloc size is smaller alloc more ram */
  1044. if (a->alloc < size) {
  1045. /* ensure there are always at least MP_PREC digits extra on top */
  1046. size += (MP_PREC * 2) - (size % MP_PREC);
  1047. /* reallocate the array a->dp
  1048. *
  1049. * We store the return in a temporary variable
  1050. * in case the operation failed we don't want
  1051. * to overwrite the dp member of a.
  1052. */
  1053. tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * size);
  1054. if (tmp == NULL) {
  1055. /* reallocation failed but "a" is still valid [can be freed] */
  1056. return MP_MEM;
  1057. }
  1058. /* reallocation succeeded so set a->dp */
  1059. a->dp = tmp;
  1060. /* zero excess digits */
  1061. i = a->alloc;
  1062. a->alloc = size;
  1063. for (; i < a->alloc; i++) {
  1064. a->dp[i] = 0;
  1065. }
  1066. }
  1067. return MP_OKAY;
  1068. }
  1069. #ifdef BN_MP_ABS_C
  1070. /* b = |a|
  1071. *
  1072. * Simple function copies the input and fixes the sign to positive
  1073. */
  1074. static int mp_abs (mp_int * a, mp_int * b)
  1075. {
  1076. int res;
  1077. /* copy a to b */
  1078. if (a != b) {
  1079. if ((res = mp_copy (a, b)) != MP_OKAY) {
  1080. return res;
  1081. }
  1082. }
  1083. /* force the sign of b to positive */
  1084. b->sign = MP_ZPOS;
  1085. return MP_OKAY;
  1086. }
  1087. #endif
  1088. /* set to a digit */
  1089. static void mp_set (mp_int * a, mp_digit b)
  1090. {
  1091. mp_zero (a);
  1092. a->dp[0] = b & MP_MASK;
  1093. a->used = (a->dp[0] != 0) ? 1 : 0;
  1094. }
  1095. #ifndef LTM_NO_NEG_EXP
  1096. /* b = a/2 */
  1097. static int mp_div_2(mp_int * a, mp_int * b)
  1098. {
  1099. int x, res, oldused;
  1100. /* copy */
  1101. if (b->alloc < a->used) {
  1102. if ((res = mp_grow (b, a->used)) != MP_OKAY) {
  1103. return res;
  1104. }
  1105. }
  1106. oldused = b->used;
  1107. b->used = a->used;
  1108. {
  1109. register mp_digit r, rr, *tmpa, *tmpb;
  1110. /* source alias */
  1111. tmpa = a->dp + b->used - 1;
  1112. /* dest alias */
  1113. tmpb = b->dp + b->used - 1;
  1114. /* carry */
  1115. r = 0;
  1116. for (x = b->used - 1; x >= 0; x--) {
  1117. /* get the carry for the next iteration */
  1118. rr = *tmpa & 1;
  1119. /* shift the current digit, add in carry and store */
  1120. *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
  1121. /* forward carry to next iteration */
  1122. r = rr;
  1123. }
  1124. /* zero excess digits */
  1125. tmpb = b->dp + b->used;
  1126. for (x = b->used; x < oldused; x++) {
  1127. *tmpb++ = 0;
  1128. }
  1129. }
  1130. b->sign = a->sign;
  1131. mp_clamp (b);
  1132. return MP_OKAY;
  1133. }
  1134. #endif /* LTM_NO_NEG_EXP */
  1135. /* shift left by a certain bit count */
  1136. static int mp_mul_2d (mp_int * a, int b, mp_int * c)
  1137. {
  1138. mp_digit d;
  1139. int res;
  1140. /* copy */
  1141. if (a != c) {
  1142. if ((res = mp_copy (a, c)) != MP_OKAY) {
  1143. return res;
  1144. }
  1145. }
  1146. if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) {
  1147. if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) {
  1148. return res;
  1149. }
  1150. }
  1151. /* shift by as many digits in the bit count */
  1152. if (b >= (int)DIGIT_BIT) {
  1153. if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
  1154. return res;
  1155. }
  1156. }
  1157. /* shift any bit count < DIGIT_BIT */
  1158. d = (mp_digit) (b % DIGIT_BIT);
  1159. if (d != 0) {
  1160. register mp_digit *tmpc, shift, mask, r, rr;
  1161. register int x;
  1162. /* bitmask for carries */
  1163. mask = (((mp_digit)1) << d) - 1;
  1164. /* shift for msbs */
  1165. shift = DIGIT_BIT - d;
  1166. /* alias */
  1167. tmpc = c->dp;
  1168. /* carry */
  1169. r = 0;
  1170. for (x = 0; x < c->used; x++) {
  1171. /* get the higher bits of the current word */
  1172. rr = (*tmpc >> shift) & mask;
  1173. /* shift the current word and OR in the carry */
  1174. *tmpc = ((*tmpc << d) | r) & MP_MASK;
  1175. ++tmpc;
  1176. /* set the carry to the carry bits of the current word */
  1177. r = rr;
  1178. }
  1179. /* set final carry */
  1180. if (r != 0) {
  1181. c->dp[(c->used)++] = r;
  1182. }
  1183. }
  1184. mp_clamp (c);
  1185. return MP_OKAY;
  1186. }
  1187. #ifdef BN_MP_INIT_MULTI_C
  1188. static int mp_init_multi(mp_int *mp, ...)
  1189. {
  1190. mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
  1191. int n = 0; /* Number of ok inits */
  1192. mp_int* cur_arg = mp;
  1193. va_list args;
  1194. va_start(args, mp); /* init args to next argument from caller */
  1195. while (cur_arg != NULL) {
  1196. if (mp_init(cur_arg) != MP_OKAY) {
  1197. /* Oops - error! Back-track and mp_clear what we already
  1198. succeeded in init-ing, then return error.
  1199. */
  1200. va_list clean_args;
  1201. /* end the current list */
  1202. va_end(args);
  1203. /* now start cleaning up */
  1204. cur_arg = mp;
  1205. va_start(clean_args, mp);
  1206. while (n--) {
  1207. mp_clear(cur_arg);
  1208. cur_arg = va_arg(clean_args, mp_int*);
  1209. }
  1210. va_end(clean_args);
  1211. return MP_MEM;
  1212. }
  1213. n++;
  1214. cur_arg = va_arg(args, mp_int*);
  1215. }
  1216. va_end(args);
  1217. return res; /* Assumed ok, if error flagged above. */
  1218. }
  1219. #endif
  1220. #ifdef BN_MP_CLEAR_MULTI_C
  1221. static void mp_clear_multi(mp_int *mp, ...)
  1222. {
  1223. mp_int* next_mp = mp;
  1224. va_list args;
  1225. va_start(args, mp);
  1226. while (next_mp != NULL) {
  1227. mp_clear(next_mp);
  1228. next_mp = va_arg(args, mp_int*);
  1229. }
  1230. va_end(args);
  1231. }
  1232. #endif
  1233. /* shift left a certain amount of digits */
  1234. static int mp_lshd (mp_int * a, int b)
  1235. {
  1236. int x, res;
  1237. /* if its less than zero return */
  1238. if (b <= 0) {
  1239. return MP_OKAY;
  1240. }
  1241. /* grow to fit the new digits */
  1242. if (a->alloc < a->used + b) {
  1243. if ((res = mp_grow (a, a->used + b)) != MP_OKAY) {
  1244. return res;
  1245. }
  1246. }
  1247. {
  1248. register mp_digit *top, *bottom;
  1249. /* increment the used by the shift amount then copy upwards */
  1250. a->used += b;
  1251. /* top */
  1252. top = a->dp + a->used - 1;
  1253. /* base */
  1254. bottom = a->dp + a->used - 1 - b;
  1255. /* much like mp_rshd this is implemented using a sliding window
  1256. * except the window goes the otherway around. Copying from
  1257. * the bottom to the top. see bn_mp_rshd.c for more info.
  1258. */
  1259. for (x = a->used - 1; x >= b; x--) {
  1260. *top-- = *bottom--;
  1261. }
  1262. /* zero the lower digits */
  1263. top = a->dp;
  1264. for (x = 0; x < b; x++) {
  1265. *top++ = 0;
  1266. }
  1267. }
  1268. return MP_OKAY;
  1269. }
  1270. /* returns the number of bits in an int */
  1271. static int mp_count_bits (mp_int * a)
  1272. {
  1273. int r;
  1274. mp_digit q;
  1275. /* shortcut */
  1276. if (a->used == 0) {
  1277. return 0;
  1278. }
  1279. /* get number of digits and add that */
  1280. r = (a->used - 1) * DIGIT_BIT;
  1281. /* take the last digit and count the bits in it */
  1282. q = a->dp[a->used - 1];
  1283. while (q > ((mp_digit) 0)) {
  1284. ++r;
  1285. q >>= ((mp_digit) 1);
  1286. }
  1287. return r;
  1288. }
  1289. /* calc a value mod 2**b */
  1290. static int mp_mod_2d (mp_int * a, int b, mp_int * c)
  1291. {
  1292. int x, res;
  1293. /* if b is <= 0 then zero the int */
  1294. if (b <= 0) {
  1295. mp_zero (c);
  1296. return MP_OKAY;
  1297. }
  1298. /* if the modulus is larger than the value than return */
  1299. if (b >= (int) (a->used * DIGIT_BIT)) {
  1300. res = mp_copy (a, c);
  1301. return res;
  1302. }
  1303. /* copy */
  1304. if ((res = mp_copy (a, c)) != MP_OKAY) {
  1305. return res;
  1306. }
  1307. /* zero digits above the last digit of the modulus */
  1308. for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) {
  1309. c->dp[x] = 0;
  1310. }
  1311. /* clear the digit that is not completely outside/inside the modulus */
  1312. c->dp[b / DIGIT_BIT] &=
  1313. (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
  1314. mp_clamp (c);
  1315. return MP_OKAY;
  1316. }
  1317. #ifdef BN_MP_DIV_SMALL
  1318. /* slower bit-bang division... also smaller */
  1319. static int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  1320. {
  1321. mp_int ta, tb, tq, q;
  1322. int res, n, n2;
  1323. /* is divisor zero ? */
  1324. if (mp_iszero (b) == 1) {
  1325. return MP_VAL;
  1326. }
  1327. /* if a < b then q=0, r = a */
  1328. if (mp_cmp_mag (a, b) == MP_LT) {
  1329. if (d != NULL) {
  1330. res = mp_copy (a, d);
  1331. } else {
  1332. res = MP_OKAY;
  1333. }
  1334. if (c != NULL) {
  1335. mp_zero (c);
  1336. }
  1337. return res;
  1338. }
  1339. /* init our temps */
  1340. if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) {
  1341. return res;
  1342. }
  1343. mp_set(&tq, 1);
  1344. n = mp_count_bits(a) - mp_count_bits(b);
  1345. if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
  1346. ((res = mp_abs(b, &tb)) != MP_OKAY) ||
  1347. ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
  1348. ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
  1349. goto LBL_ERR;
  1350. }
  1351. while (n-- >= 0) {
  1352. if (mp_cmp(&tb, &ta) != MP_GT) {
  1353. if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) ||
  1354. ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) {
  1355. goto LBL_ERR;
  1356. }
  1357. }
  1358. if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) ||
  1359. ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) {
  1360. goto LBL_ERR;
  1361. }
  1362. }
  1363. /* now q == quotient and ta == remainder */
  1364. n = a->sign;
  1365. n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG);
  1366. if (c != NULL) {
  1367. mp_exch(c, &q);
  1368. c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
  1369. }
  1370. if (d != NULL) {
  1371. mp_exch(d, &ta);
  1372. d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n;
  1373. }
  1374. LBL_ERR:
  1375. mp_clear_multi(&ta, &tb, &tq, &q, NULL);
  1376. return res;
  1377. }
  1378. #else
  1379. /* integer signed division.
  1380. * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
  1381. * HAC pp.598 Algorithm 14.20
  1382. *
  1383. * Note that the description in HAC is horribly
  1384. * incomplete. For example, it doesn't consider
  1385. * the case where digits are removed from 'x' in
  1386. * the inner loop. It also doesn't consider the
  1387. * case that y has fewer than three digits, etc..
  1388. *
  1389. * The overall algorithm is as described as
  1390. * 14.20 from HAC but fixed to treat these cases.
  1391. */
  1392. static int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
  1393. {
  1394. mp_int q, x, y, t1, t2;
  1395. int res, n, t, i, norm, neg;
  1396. /* is divisor zero ? */
  1397. if (mp_iszero (b) == 1) {
  1398. return MP_VAL;
  1399. }
  1400. /* if a < b then q=0, r = a */
  1401. if (mp_cmp_mag (a, b) == MP_LT) {
  1402. if (d != NULL) {
  1403. res = mp_copy (a, d);
  1404. } else {
  1405. res = MP_OKAY;
  1406. }
  1407. if (c != NULL) {
  1408. mp_zero (c);
  1409. }
  1410. return res;
  1411. }
  1412. if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) {
  1413. return res;
  1414. }
  1415. q.used = a->used + 2;
  1416. if ((res = mp_init (&t1)) != MP_OKAY) {
  1417. goto LBL_Q;
  1418. }
  1419. if ((res = mp_init (&t2)) != MP_OKAY) {
  1420. goto LBL_T1;
  1421. }
  1422. if ((res = mp_init_copy (&x, a)) != MP_OKAY) {
  1423. goto LBL_T2;
  1424. }
  1425. if ((res = mp_init_copy (&y, b)) != MP_OKAY) {
  1426. goto LBL_X;
  1427. }
  1428. /* fix the sign */
  1429. neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
  1430. x.sign = y.sign = MP_ZPOS;
  1431. /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
  1432. norm = mp_count_bits(&y) % DIGIT_BIT;
  1433. if (norm < (int)(DIGIT_BIT-1)) {
  1434. norm = (DIGIT_BIT-1) - norm;
  1435. if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
  1436. goto LBL_Y;
  1437. }
  1438. if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
  1439. goto LBL_Y;
  1440. }
  1441. } else {
  1442. norm = 0;
  1443. }
  1444. /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
  1445. n = x.used - 1;
  1446. t = y.used - 1;
  1447. /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
  1448. if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */
  1449. goto LBL_Y;
  1450. }
  1451. while (mp_cmp (&x, &y) != MP_LT) {
  1452. ++(q.dp[n - t]);
  1453. if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) {
  1454. goto LBL_Y;
  1455. }
  1456. }
  1457. /* reset y by shifting it back down */
  1458. mp_rshd (&y, n - t);
  1459. /* step 3. for i from n down to (t + 1) */
  1460. for (i = n; i >= (t + 1); i--) {
  1461. if (i > x.used) {
  1462. continue;
  1463. }
  1464. /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
  1465. * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
  1466. if (x.dp[i] == y.dp[t]) {
  1467. q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
  1468. } else {
  1469. mp_word tmp;
  1470. tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
  1471. tmp |= ((mp_word) x.dp[i - 1]);
  1472. tmp /= ((mp_word) y.dp[t]);
  1473. if (tmp > (mp_word) MP_MASK)
  1474. tmp = MP_MASK;
  1475. q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
  1476. }
  1477. /* while (q{i-t-1} * (yt * b + y{t-1})) >
  1478. xi * b**2 + xi-1 * b + xi-2
  1479. do q{i-t-1} -= 1;
  1480. */
  1481. q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
  1482. do {
  1483. q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK;
  1484. /* find left hand */
  1485. mp_zero (&t1);
  1486. t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1];
  1487. t1.dp[1] = y.dp[t];
  1488. t1.used = 2;
  1489. if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) {
  1490. goto LBL_Y;
  1491. }
  1492. /* find right hand */
  1493. t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2];
  1494. t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1];
  1495. t2.dp[2] = x.dp[i];
  1496. t2.used = 3;
  1497. } while (mp_cmp_mag(&t1, &t2) == MP_GT);
  1498. /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
  1499. if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
  1500. goto LBL_Y;
  1501. }
  1502. if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
  1503. goto LBL_Y;
  1504. }
  1505. if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) {
  1506. goto LBL_Y;
  1507. }
  1508. /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
  1509. if (x.sign == MP_NEG) {
  1510. if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
  1511. goto LBL_Y;
  1512. }
  1513. if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) {
  1514. goto LBL_Y;
  1515. }
  1516. if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
  1517. goto LBL_Y;
  1518. }
  1519. q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK;
  1520. }
  1521. }
  1522. /* now q is the quotient and x is the remainder
  1523. * [which we have to normalize]
  1524. */
  1525. /* get sign before writing to c */
  1526. x.sign = x.used == 0 ? MP_ZPOS : a->sign;
  1527. if (c != NULL) {
  1528. mp_clamp (&q);
  1529. mp_exch (&q, c);
  1530. c->sign = neg;
  1531. }
  1532. if (d != NULL) {
  1533. mp_div_2d (&x, norm, &x, NULL);
  1534. mp_exch (&x, d);
  1535. }
  1536. res = MP_OKAY;
  1537. LBL_Y:mp_clear (&y);
  1538. LBL_X:mp_clear (&x);
  1539. LBL_T2:mp_clear (&t2);
  1540. LBL_T1:mp_clear (&t1);
  1541. LBL_Q:mp_clear (&q);
  1542. return res;
  1543. }
  1544. #endif
  1545. #ifdef MP_LOW_MEM
  1546. #define TAB_SIZE 32
  1547. #else
  1548. #define TAB_SIZE 256
  1549. #endif
  1550. static int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
  1551. {
  1552. mp_int M[TAB_SIZE], res, mu;
  1553. mp_digit buf;
  1554. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  1555. int (*redux)(mp_int*,mp_int*,mp_int*);
  1556. /* find window size */
  1557. x = mp_count_bits (X);
  1558. if (x <= 7) {
  1559. winsize = 2;
  1560. } else if (x <= 36) {
  1561. winsize = 3;
  1562. } else if (x <= 140) {
  1563. winsize = 4;
  1564. } else if (x <= 450) {
  1565. winsize = 5;
  1566. } else if (x <= 1303) {
  1567. winsize = 6;
  1568. } else if (x <= 3529) {
  1569. winsize = 7;
  1570. } else {
  1571. winsize = 8;
  1572. }
  1573. #ifdef MP_LOW_MEM
  1574. if (winsize > 5) {
  1575. winsize = 5;
  1576. }
  1577. #endif
  1578. /* init M array */
  1579. /* init first cell */
  1580. if ((err = mp_init(&M[1])) != MP_OKAY) {
  1581. return err;
  1582. }
  1583. /* now init the second half of the array */
  1584. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  1585. if ((err = mp_init(&M[x])) != MP_OKAY) {
  1586. for (y = 1<<(winsize-1); y < x; y++) {
  1587. mp_clear (&M[y]);
  1588. }
  1589. mp_clear(&M[1]);
  1590. return err;
  1591. }
  1592. }
  1593. /* create mu, used for Barrett reduction */
  1594. if ((err = mp_init (&mu)) != MP_OKAY) {
  1595. goto LBL_M;
  1596. }
  1597. if (redmode == 0) {
  1598. if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
  1599. goto LBL_MU;
  1600. }
  1601. redux = mp_reduce;
  1602. } else {
  1603. if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
  1604. goto LBL_MU;
  1605. }
  1606. redux = mp_reduce_2k_l;
  1607. }
  1608. /* create M table
  1609. *
  1610. * The M table contains powers of the base,
  1611. * e.g. M[x] = G**x mod P
  1612. *
  1613. * The first half of the table is not
  1614. * computed though accept for M[0] and M[1]
  1615. */
  1616. if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
  1617. goto LBL_MU;
  1618. }
  1619. /* compute the value at M[1<<(winsize-1)] by squaring
  1620. * M[1] (winsize-1) times
  1621. */
  1622. if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
  1623. goto LBL_MU;
  1624. }
  1625. for (x = 0; x < (winsize - 1); x++) {
  1626. /* square it */
  1627. if ((err = mp_sqr (&M[1 << (winsize - 1)],
  1628. &M[1 << (winsize - 1)])) != MP_OKAY) {
  1629. goto LBL_MU;
  1630. }
  1631. /* reduce modulo P */
  1632. if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
  1633. goto LBL_MU;
  1634. }
  1635. }
  1636. /* create upper table, that is M[x] = M[x-1] * M[1] (mod P)
  1637. * for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
  1638. */
  1639. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  1640. if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
  1641. goto LBL_MU;
  1642. }
  1643. if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
  1644. goto LBL_MU;
  1645. }
  1646. }
  1647. /* setup result */
  1648. if ((err = mp_init (&res)) != MP_OKAY) {
  1649. goto LBL_MU;
  1650. }
  1651. mp_set (&res, 1);
  1652. /* set initial mode and bit cnt */
  1653. mode = 0;
  1654. bitcnt = 1;
  1655. buf = 0;
  1656. digidx = X->used - 1;
  1657. bitcpy = 0;
  1658. bitbuf = 0;
  1659. for (;;) {
  1660. /* grab next digit as required */
  1661. if (--bitcnt == 0) {
  1662. /* if digidx == -1 we are out of digits */
  1663. if (digidx == -1) {
  1664. break;
  1665. }
  1666. /* read next digit and reset the bitcnt */
  1667. buf = X->dp[digidx--];
  1668. bitcnt = (int) DIGIT_BIT;
  1669. }
  1670. /* grab the next msb from the exponent */
  1671. y = (buf >> (mp_digit)(DIGIT_BIT - 1)) & 1;
  1672. buf <<= (mp_digit)1;
  1673. /* if the bit is zero and mode == 0 then we ignore it
  1674. * These represent the leading zero bits before the first 1 bit
  1675. * in the exponent. Technically this opt is not required but it
  1676. * does lower the # of trivial squaring/reductions used
  1677. */
  1678. if (mode == 0 && y == 0) {
  1679. continue;
  1680. }
  1681. /* if the bit is zero and mode == 1 then we square */
  1682. if (mode == 1 && y == 0) {
  1683. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  1684. goto LBL_RES;
  1685. }
  1686. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  1687. goto LBL_RES;
  1688. }
  1689. continue;
  1690. }
  1691. /* else we add it to the window */
  1692. bitbuf |= (y << (winsize - ++bitcpy));
  1693. mode = 2;
  1694. if (bitcpy == winsize) {
  1695. /* ok window is filled so square as required and multiply */
  1696. /* square first */
  1697. for (x = 0; x < winsize; x++) {
  1698. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  1699. goto LBL_RES;
  1700. }
  1701. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  1702. goto LBL_RES;
  1703. }
  1704. }
  1705. /* then multiply */
  1706. if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
  1707. goto LBL_RES;
  1708. }
  1709. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  1710. goto LBL_RES;
  1711. }
  1712. /* empty window and reset */
  1713. bitcpy = 0;
  1714. bitbuf = 0;
  1715. mode = 1;
  1716. }
  1717. }
  1718. /* if bits remain then square/multiply */
  1719. if (mode == 2 && bitcpy > 0) {
  1720. /* square then multiply if the bit is set */
  1721. for (x = 0; x < bitcpy; x++) {
  1722. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  1723. goto LBL_RES;
  1724. }
  1725. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  1726. goto LBL_RES;
  1727. }
  1728. bitbuf <<= 1;
  1729. if ((bitbuf & (1 << winsize)) != 0) {
  1730. /* then multiply */
  1731. if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
  1732. goto LBL_RES;
  1733. }
  1734. if ((err = redux (&res, P, &mu)) != MP_OKAY) {
  1735. goto LBL_RES;
  1736. }
  1737. }
  1738. }
  1739. }
  1740. mp_exch (&res, Y);
  1741. err = MP_OKAY;
  1742. LBL_RES:mp_clear (&res);
  1743. LBL_MU:mp_clear (&mu);
  1744. LBL_M:
  1745. mp_clear(&M[1]);
  1746. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  1747. mp_clear (&M[x]);
  1748. }
  1749. return err;
  1750. }
  1751. /* computes b = a*a */
  1752. static int mp_sqr (mp_int * a, mp_int * b)
  1753. {
  1754. int res;
  1755. #ifdef BN_MP_TOOM_SQR_C
  1756. /* use Toom-Cook? */
  1757. if (a->used >= TOOM_SQR_CUTOFF) {
  1758. res = mp_toom_sqr(a, b);
  1759. /* Karatsuba? */
  1760. } else
  1761. #endif
  1762. #ifdef BN_MP_KARATSUBA_SQR_C
  1763. if (a->used >= KARATSUBA_SQR_CUTOFF) {
  1764. res = mp_karatsuba_sqr (a, b);
  1765. } else
  1766. #endif
  1767. {
  1768. #ifdef BN_FAST_S_MP_SQR_C
  1769. /* can we use the fast comba multiplier? */
  1770. if ((a->used * 2 + 1) < MP_WARRAY &&
  1771. a->used <
  1772. (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
  1773. res = fast_s_mp_sqr (a, b);
  1774. } else
  1775. #endif
  1776. #ifdef BN_S_MP_SQR_C
  1777. res = s_mp_sqr (a, b);
  1778. #else
  1779. #error mp_sqr could fail
  1780. res = MP_VAL;
  1781. #endif
  1782. }
  1783. b->sign = MP_ZPOS;
  1784. return res;
  1785. }
  1786. /* reduces a modulo n where n is of the form 2**p - d
  1787. This differs from reduce_2k since "d" can be larger
  1788. than a single digit.
  1789. */
  1790. static int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
  1791. {
  1792. mp_int q;
  1793. int p, res;
  1794. if ((res = mp_init(&q)) != MP_OKAY) {
  1795. return res;
  1796. }
  1797. p = mp_count_bits(n);
  1798. top:
  1799. /* q = a/2**p, a = a mod 2**p */
  1800. if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
  1801. goto ERR;
  1802. }
  1803. /* q = q * d */
  1804. if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
  1805. goto ERR;
  1806. }
  1807. /* a = a + q */
  1808. if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
  1809. goto ERR;
  1810. }
  1811. if (mp_cmp_mag(a, n) != MP_LT) {
  1812. s_mp_sub(a, n, a);
  1813. goto top;
  1814. }
  1815. ERR:
  1816. mp_clear(&q);
  1817. return res;
  1818. }
  1819. /* determines the setup value */
  1820. static int mp_reduce_2k_setup_l(mp_int *a, mp_int *d)
  1821. {
  1822. int res;
  1823. mp_int tmp;
  1824. if ((res = mp_init(&tmp)) != MP_OKAY) {
  1825. return res;
  1826. }
  1827. if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
  1828. goto ERR;
  1829. }
  1830. if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
  1831. goto ERR;
  1832. }
  1833. ERR:
  1834. mp_clear(&tmp);
  1835. return res;
  1836. }
  1837. /* computes a = 2**b
  1838. *
  1839. * Simple algorithm which zeroes the int, grows it then just sets one bit
  1840. * as required.
  1841. */
  1842. static int mp_2expt (mp_int * a, int b)
  1843. {
  1844. int res;
  1845. /* zero a as per default */
  1846. mp_zero (a);
  1847. /* grow a to accommodate the single bit */
  1848. if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
  1849. return res;
  1850. }
  1851. /* set the used count of where the bit will go */
  1852. a->used = b / DIGIT_BIT + 1;
  1853. /* put the single bit in its place */
  1854. a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
  1855. return MP_OKAY;
  1856. }
  1857. /* pre-calculate the value required for Barrett reduction
  1858. * For a given modulus "b" it calulates the value required in "a"
  1859. */
  1860. static int mp_reduce_setup (mp_int * a, mp_int * b)
  1861. {
  1862. int res;
  1863. if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
  1864. return res;
  1865. }
  1866. return mp_div (a, b, a, NULL);
  1867. }
  1868. /* reduces x mod m, assumes 0 < x < m**2, mu is
  1869. * precomputed via mp_reduce_setup.
  1870. * From HAC pp.604 Algorithm 14.42
  1871. */
  1872. static int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
  1873. {
  1874. mp_int q;
  1875. int res, um = m->used;
  1876. /* q = x */
  1877. if ((res = mp_init_copy (&q, x)) != MP_OKAY) {
  1878. return res;
  1879. }
  1880. /* q1 = x / b**(k-1) */
  1881. mp_rshd (&q, um - 1);
  1882. /* according to HAC this optimization is ok */
  1883. if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
  1884. if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) {
  1885. goto CLEANUP;
  1886. }
  1887. } else {
  1888. #ifdef BN_S_MP_MUL_HIGH_DIGS_C
  1889. if ((res = s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
  1890. goto CLEANUP;
  1891. }
  1892. #elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
  1893. if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
  1894. goto CLEANUP;
  1895. }
  1896. #else
  1897. {
  1898. #error mp_reduce would always fail
  1899. res = MP_VAL;
  1900. goto CLEANUP;
  1901. }
  1902. #endif
  1903. }
  1904. /* q3 = q2 / b**(k+1) */
  1905. mp_rshd (&q, um + 1);
  1906. /* x = x mod b**(k+1), quick (no division) */
  1907. if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
  1908. goto CLEANUP;
  1909. }
  1910. /* q = q * m mod b**(k+1), quick (no division) */
  1911. if ((res = s_mp_mul_digs (&q, m, &q, um + 1)) != MP_OKAY) {
  1912. goto CLEANUP;
  1913. }
  1914. /* x = x - q */
  1915. if ((res = mp_sub (x, &q, x)) != MP_OKAY) {
  1916. goto CLEANUP;
  1917. }
  1918. /* If x < 0, add b**(k+1) to it */
  1919. if (mp_cmp_d (x, 0) == MP_LT) {
  1920. mp_set (&q, 1);
  1921. if ((res = mp_lshd (&q, um + 1)) != MP_OKAY) {
  1922. goto CLEANUP;
  1923. }
  1924. if ((res = mp_add (x, &q, x)) != MP_OKAY) {
  1925. goto CLEANUP;
  1926. }
  1927. }
  1928. /* Back off if it's too big */
  1929. while (mp_cmp (x, m) != MP_LT) {
  1930. if ((res = s_mp_sub (x, m, x)) != MP_OKAY) {
  1931. goto CLEANUP;
  1932. }
  1933. }
  1934. CLEANUP:
  1935. mp_clear (&q);
  1936. return res;
  1937. }
  1938. /* multiplies |a| * |b| and only computes up to digs digits of result
  1939. * HAC pp. 595, Algorithm 14.12 Modified so you can control how
  1940. * many digits of output are created.
  1941. */
  1942. static int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
  1943. {
  1944. mp_int t;
  1945. int res, pa, pb, ix, iy;
  1946. mp_digit u;
  1947. mp_word r;
  1948. mp_digit tmpx, *tmpt, *tmpy;
  1949. #ifdef BN_FAST_S_MP_MUL_DIGS_C
  1950. /* can we use the fast multiplier? */
  1951. if (((digs) < MP_WARRAY) &&
  1952. MIN (a->used, b->used) <
  1953. (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  1954. return fast_s_mp_mul_digs (a, b, c, digs);
  1955. }
  1956. #endif
  1957. if ((res = mp_init_size (&t, digs)) != MP_OKAY) {
  1958. return res;
  1959. }
  1960. t.used = digs;
  1961. /* compute the digits of the product directly */
  1962. pa = a->used;
  1963. for (ix = 0; ix < pa; ix++) {
  1964. /* set the carry to zero */
  1965. u = 0;
  1966. /* limit ourselves to making digs digits of output */
  1967. pb = MIN (b->used, digs - ix);
  1968. /* setup some aliases */
  1969. /* copy of the digit from a used within the nested loop */
  1970. tmpx = a->dp[ix];
  1971. /* an alias for the destination shifted ix places */
  1972. tmpt = t.dp + ix;
  1973. /* an alias for the digits of b */
  1974. tmpy = b->dp;
  1975. /* compute the columns of the output and propagate the carry */
  1976. for (iy = 0; iy < pb; iy++) {
  1977. /* compute the column as a mp_word */
  1978. r = ((mp_word)*tmpt) +
  1979. ((mp_word)tmpx) * ((mp_word)*tmpy++) +
  1980. ((mp_word) u);
  1981. /* the new column is the lower part of the result */
  1982. *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
  1983. /* get the carry word from the result */
  1984. u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
  1985. }
  1986. /* set carry if it is placed below digs */
  1987. if (ix + iy < digs) {
  1988. *tmpt = u;
  1989. }
  1990. }
  1991. mp_clamp (&t);
  1992. mp_exch (&t, c);
  1993. mp_clear (&t);
  1994. return MP_OKAY;
  1995. }
  1996. #ifdef BN_FAST_S_MP_MUL_DIGS_C
  1997. /* Fast (comba) multiplier
  1998. *
  1999. * This is the fast column-array [comba] multiplier. It is
  2000. * designed to compute the columns of the product first
  2001. * then handle the carries afterwards. This has the effect
  2002. * of making the nested loops that compute the columns very
  2003. * simple and schedulable on super-scalar processors.
  2004. *
  2005. * This has been modified to produce a variable number of
  2006. * digits of output so if say only a half-product is required
  2007. * you don't have to compute the upper half (a feature
  2008. * required for fast Barrett reduction).
  2009. *
  2010. * Based on Algorithm 14.12 on pp.595 of HAC.
  2011. *
  2012. */
  2013. static int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
  2014. {
  2015. int olduse, res, pa, ix, iz;
  2016. mp_digit W[MP_WARRAY];
  2017. register mp_word _W;
  2018. /* grow the destination as required */
  2019. if (c->alloc < digs) {
  2020. if ((res = mp_grow (c, digs)) != MP_OKAY) {
  2021. return res;
  2022. }
  2023. }
  2024. /* number of output digits to produce */
  2025. pa = MIN(digs, a->used + b->used);
  2026. /* clear the carry */
  2027. _W = 0;
  2028. for (ix = 0; ix < pa; ix++) {
  2029. int tx, ty;
  2030. int iy;
  2031. mp_digit *tmpx, *tmpy;
  2032. /* get offsets into the two bignums */
  2033. ty = MIN(b->used-1, ix);
  2034. tx = ix - ty;
  2035. /* setup temp aliases */
  2036. tmpx = a->dp + tx;
  2037. tmpy = b->dp + ty;
  2038. /* this is the number of times the loop will iterrate, essentially
  2039. while (tx++ < a->used && ty-- >= 0) { ... }
  2040. */
  2041. iy = MIN(a->used-tx, ty+1);
  2042. /* execute loop */
  2043. for (iz = 0; iz < iy; ++iz) {
  2044. _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
  2045. }
  2046. /* store term */
  2047. W[ix] = ((mp_digit)_W) & MP_MASK;
  2048. /* make next carry */
  2049. _W = _W >> ((mp_word)DIGIT_BIT);
  2050. }
  2051. /* setup dest */
  2052. olduse = c->used;
  2053. c->used = pa;
  2054. {
  2055. register mp_digit *tmpc;
  2056. tmpc = c->dp;
  2057. for (ix = 0; ix < pa+1; ix++) {
  2058. /* now extract the previous digit [below the carry] */
  2059. *tmpc++ = W[ix];
  2060. }
  2061. /* clear unused digits [that existed in the old copy of c] */
  2062. for (; ix < olduse; ix++) {
  2063. *tmpc++ = 0;
  2064. }
  2065. }
  2066. mp_clamp (c);
  2067. return MP_OKAY;
  2068. }
  2069. #endif /* BN_FAST_S_MP_MUL_DIGS_C */
  2070. /* init an mp_init for a given size */
  2071. static int mp_init_size (mp_int * a, int size)
  2072. {
  2073. int x;
  2074. /* pad size so there are always extra digits */
  2075. size += (MP_PREC * 2) - (size % MP_PREC);
  2076. /* alloc mem */
  2077. a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size);
  2078. if (a->dp == NULL) {
  2079. return MP_MEM;
  2080. }
  2081. /* set the members */
  2082. a->used = 0;
  2083. a->alloc = size;
  2084. a->sign = MP_ZPOS;
  2085. /* zero the digits */
  2086. for (x = 0; x < size; x++) {
  2087. a->dp[x] = 0;
  2088. }
  2089. return MP_OKAY;
  2090. }
  2091. /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
  2092. static int s_mp_sqr (mp_int * a, mp_int * b)
  2093. {
  2094. mp_int t;
  2095. int res, ix, iy, pa;
  2096. mp_word r;
  2097. mp_digit u, tmpx, *tmpt;
  2098. pa = a->used;
  2099. if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) {
  2100. return res;
  2101. }
  2102. /* default used is maximum possible size */
  2103. t.used = 2*pa + 1;
  2104. for (ix = 0; ix < pa; ix++) {
  2105. /* first calculate the digit at 2*ix */
  2106. /* calculate double precision result */
  2107. r = ((mp_word) t.dp[2*ix]) +
  2108. ((mp_word)a->dp[ix])*((mp_word)a->dp[ix]);
  2109. /* store lower part in result */
  2110. t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
  2111. /* get the carry */
  2112. u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
  2113. /* left hand side of A[ix] * A[iy] */
  2114. tmpx = a->dp[ix];
  2115. /* alias for where to store the results */
  2116. tmpt = t.dp + (2*ix + 1);
  2117. for (iy = ix + 1; iy < pa; iy++) {
  2118. /* first calculate the product */
  2119. r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
  2120. /* now calculate the double precision result, note we use
  2121. * addition instead of *2 since it's easier to optimize
  2122. */
  2123. r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
  2124. /* store lower part */
  2125. *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
  2126. /* get carry */
  2127. u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
  2128. }
  2129. /* propagate upwards */
  2130. while (u != ((mp_digit) 0)) {
  2131. r = ((mp_word) *tmpt) + ((mp_word) u);
  2132. *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
  2133. u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
  2134. }
  2135. }
  2136. mp_clamp (&t);
  2137. mp_exch (&t, b);
  2138. mp_clear (&t);
  2139. return MP_OKAY;
  2140. }
  2141. /* multiplies |a| * |b| and does not compute the lower digs digits
  2142. * [meant to get the higher part of the product]
  2143. */
  2144. static int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
  2145. {
  2146. mp_int t;
  2147. int res, pa, pb, ix, iy;
  2148. mp_digit u;
  2149. mp_word r;
  2150. mp_digit tmpx, *tmpt, *tmpy;
  2151. /* can we use the fast multiplier? */
  2152. #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
  2153. if (((a->used + b->used + 1) < MP_WARRAY)
  2154. && MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  2155. return fast_s_mp_mul_high_digs (a, b, c, digs);
  2156. }
  2157. #endif
  2158. if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) {
  2159. return res;
  2160. }
  2161. t.used = a->used + b->used + 1;
  2162. pa = a->used;
  2163. pb = b->used;
  2164. for (ix = 0; ix < pa; ix++) {
  2165. /* clear the carry */
  2166. u = 0;
  2167. /* left hand side of A[ix] * B[iy] */
  2168. tmpx = a->dp[ix];
  2169. /* alias to the address of where the digits will be stored */
  2170. tmpt = &(t.dp[digs]);
  2171. /* alias for where to read the right hand side from */
  2172. tmpy = b->dp + (digs - ix);
  2173. for (iy = digs - ix; iy < pb; iy++) {
  2174. /* calculate the double precision result */
  2175. r = ((mp_word)*tmpt) +
  2176. ((mp_word)tmpx) * ((mp_word)*tmpy++) +
  2177. ((mp_word) u);
  2178. /* get the lower part */
  2179. *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
  2180. /* carry the carry */
  2181. u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
  2182. }
  2183. *tmpt = u;
  2184. }
  2185. mp_clamp (&t);
  2186. mp_exch (&t, c);
  2187. mp_clear (&t);
  2188. return MP_OKAY;
  2189. }
  2190. #ifdef BN_MP_MONTGOMERY_SETUP_C
  2191. /* setups the montgomery reduction stuff */
  2192. static int
  2193. mp_montgomery_setup (mp_int * n, mp_digit * rho)
  2194. {
  2195. mp_digit x, b;
  2196. /* fast inversion mod 2**k
  2197. *
  2198. * Based on the fact that
  2199. *
  2200. * XA = 1 (mod 2**n) => (X(2-XA)) A = 1 (mod 2**2n)
  2201. * => 2*X*A - X*X*A*A = 1
  2202. * => 2*(1) - (1) = 1
  2203. */
  2204. b = n->dp[0];
  2205. if ((b & 1) == 0) {
  2206. return MP_VAL;
  2207. }
  2208. x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
  2209. x *= 2 - b * x; /* here x*a==1 mod 2**8 */
  2210. #if !defined(MP_8BIT)
  2211. x *= 2 - b * x; /* here x*a==1 mod 2**16 */
  2212. #endif
  2213. #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
  2214. x *= 2 - b * x; /* here x*a==1 mod 2**32 */
  2215. #endif
  2216. #ifdef MP_64BIT
  2217. x *= 2 - b * x; /* here x*a==1 mod 2**64 */
  2218. #endif
  2219. /* rho = -1/m mod b */
  2220. *rho = (unsigned long)(((mp_word)1 << ((mp_word) DIGIT_BIT)) - x) & MP_MASK;
  2221. return MP_OKAY;
  2222. }
  2223. #endif
  2224. #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
  2225. /* computes xR**-1 == x (mod N) via Montgomery Reduction
  2226. *
  2227. * This is an optimized implementation of montgomery_reduce
  2228. * which uses the comba method to quickly calculate the columns of the
  2229. * reduction.
  2230. *
  2231. * Based on Algorithm 14.32 on pp.601 of HAC.
  2232. */
  2233. static int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
  2234. {
  2235. int ix, res, olduse;
  2236. mp_word W[MP_WARRAY];
  2237. /* get old used count */
  2238. olduse = x->used;
  2239. /* grow a as required */
  2240. if (x->alloc < n->used + 1) {
  2241. if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) {
  2242. return res;
  2243. }
  2244. }
  2245. /* first we have to get the digits of the input into
  2246. * an array of double precision words W[...]
  2247. */
  2248. {
  2249. register mp_word *_W;
  2250. register mp_digit *tmpx;
  2251. /* alias for the W[] array */
  2252. _W = W;
  2253. /* alias for the digits of x*/
  2254. tmpx = x->dp;
  2255. /* copy the digits of a into W[0..a->used-1] */
  2256. for (ix = 0; ix < x->used; ix++) {
  2257. *_W++ = *tmpx++;
  2258. }
  2259. /* zero the high words of W[a->used..m->used*2] */
  2260. for (; ix < n->used * 2 + 1; ix++) {
  2261. *_W++ = 0;
  2262. }
  2263. }
  2264. /* now we proceed to zero successive digits
  2265. * from the least significant upwards
  2266. */
  2267. for (ix = 0; ix < n->used; ix++) {
  2268. /* mu = ai * m' mod b
  2269. *
  2270. * We avoid a double precision multiplication (which isn't required)
  2271. * by casting the value down to a mp_digit. Note this requires
  2272. * that W[ix-1] have the carry cleared (see after the inner loop)
  2273. */
  2274. register mp_digit mu;
  2275. mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK);
  2276. /* a = a + mu * m * b**i
  2277. *
  2278. * This is computed in place and on the fly. The multiplication
  2279. * by b**i is handled by offseting which columns the results
  2280. * are added to.
  2281. *
  2282. * Note the comba method normally doesn't handle carries in the
  2283. * inner loop In this case we fix the carry from the previous
  2284. * column since the Montgomery reduction requires digits of the
  2285. * result (so far) [see above] to work. This is
  2286. * handled by fixing up one carry after the inner loop. The
  2287. * carry fixups are done in order so after these loops the
  2288. * first m->used words of W[] have the carries fixed
  2289. */
  2290. {
  2291. register int iy;
  2292. register mp_digit *tmpn;
  2293. register mp_word *_W;
  2294. /* alias for the digits of the modulus */
  2295. tmpn = n->dp;
  2296. /* Alias for the columns set by an offset of ix */
  2297. _W = W + ix;
  2298. /* inner loop */
  2299. for (iy = 0; iy < n->used; iy++) {
  2300. *_W++ += ((mp_word)mu) * ((mp_word)*tmpn++);
  2301. }
  2302. }
  2303. /* now fix carry for next digit, W[ix+1] */
  2304. W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
  2305. }
  2306. /* now we have to propagate the carries and
  2307. * shift the words downward [all those least
  2308. * significant digits we zeroed].
  2309. */
  2310. {
  2311. register mp_digit *tmpx;
  2312. register mp_word *_W, *_W1;
  2313. /* nox fix rest of carries */
  2314. /* alias for current word */
  2315. _W1 = W + ix;
  2316. /* alias for next word, where the carry goes */
  2317. _W = W + ++ix;
  2318. for (; ix <= n->used * 2 + 1; ix++) {
  2319. *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
  2320. }
  2321. /* copy out, A = A/b**n
  2322. *
  2323. * The result is A/b**n but instead of converting from an
  2324. * array of mp_word to mp_digit than calling mp_rshd
  2325. * we just copy them in the right order
  2326. */
  2327. /* alias for destination word */
  2328. tmpx = x->dp;
  2329. /* alias for shifted double precision result */
  2330. _W = W + n->used;
  2331. for (ix = 0; ix < n->used + 1; ix++) {
  2332. *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
  2333. }
  2334. /* zero oldused digits, if the input a was larger than
  2335. * m->used+1 we'll have to clear the digits
  2336. */
  2337. for (; ix < olduse; ix++) {
  2338. *tmpx++ = 0;
  2339. }
  2340. }
  2341. /* set the max used and clamp */
  2342. x->used = n->used + 1;
  2343. mp_clamp (x);
  2344. /* if A >= m then A = A - m */
  2345. if (mp_cmp_mag (x, n) != MP_LT) {
  2346. return s_mp_sub (x, n, x);
  2347. }
  2348. return MP_OKAY;
  2349. }
  2350. #endif
  2351. #ifdef BN_MP_MUL_2_C
  2352. /* b = a*2 */
  2353. static int mp_mul_2(mp_int * a, mp_int * b)
  2354. {
  2355. int x, res, oldused;
  2356. /* grow to accommodate result */
  2357. if (b->alloc < a->used + 1) {
  2358. if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
  2359. return res;
  2360. }
  2361. }
  2362. oldused = b->used;
  2363. b->used = a->used;
  2364. {
  2365. register mp_digit r, rr, *tmpa, *tmpb;
  2366. /* alias for source */
  2367. tmpa = a->dp;
  2368. /* alias for dest */
  2369. tmpb = b->dp;
  2370. /* carry */
  2371. r = 0;
  2372. for (x = 0; x < a->used; x++) {
  2373. /* get what will be the *next* carry bit from the
  2374. * MSB of the current digit
  2375. */
  2376. rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
  2377. /* now shift up this digit, add in the carry [from the previous] */
  2378. *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
  2379. /* copy the carry that would be from the source
  2380. * digit into the next iteration
  2381. */
  2382. r = rr;
  2383. }
  2384. /* new leading digit? */
  2385. if (r != 0) {
  2386. /* add a MSB which is always 1 at this point */
  2387. *tmpb = 1;
  2388. ++(b->used);
  2389. }
  2390. /* now zero any excess digits on the destination
  2391. * that we didn't write to
  2392. */
  2393. tmpb = b->dp + b->used;
  2394. for (x = b->used; x < oldused; x++) {
  2395. *tmpb++ = 0;
  2396. }
  2397. }
  2398. b->sign = a->sign;
  2399. return MP_OKAY;
  2400. }
  2401. #endif
  2402. #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  2403. /*
  2404. * shifts with subtractions when the result is greater than b.
  2405. *
  2406. * The method is slightly modified to shift B unconditionally up to just under
  2407. * the leading bit of b. This saves a lot of multiple precision shifting.
  2408. */
  2409. static int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
  2410. {
  2411. int x, bits, res;
  2412. /* how many bits of last digit does b use */
  2413. bits = mp_count_bits (b) % DIGIT_BIT;
  2414. if (b->used > 1) {
  2415. if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) {
  2416. return res;
  2417. }
  2418. } else {
  2419. mp_set(a, 1);
  2420. bits = 1;
  2421. }
  2422. /* now compute C = A * B mod b */
  2423. for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
  2424. if ((res = mp_mul_2 (a, a)) != MP_OKAY) {
  2425. return res;
  2426. }
  2427. if (mp_cmp_mag (a, b) != MP_LT) {
  2428. if ((res = s_mp_sub (a, b, a)) != MP_OKAY) {
  2429. return res;
  2430. }
  2431. }
  2432. }
  2433. return MP_OKAY;
  2434. }
  2435. #endif
  2436. #ifdef BN_MP_EXPTMOD_FAST_C
  2437. /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
  2438. *
  2439. * Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
  2440. * The value of k changes based on the size of the exponent.
  2441. *
  2442. * Uses Montgomery or Diminished Radix reduction [whichever appropriate]
  2443. */
  2444. static int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
  2445. {
  2446. mp_int M[TAB_SIZE], res;
  2447. mp_digit buf, mp;
  2448. int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
  2449. /* use a pointer to the reduction algorithm. This allows us to use
  2450. * one of many reduction algorithms without modding the guts of
  2451. * the code with if statements everywhere.
  2452. */
  2453. int (*redux)(mp_int*,mp_int*,mp_digit);
  2454. /* find window size */
  2455. x = mp_count_bits (X);
  2456. if (x <= 7) {
  2457. winsize = 2;
  2458. } else if (x <= 36) {
  2459. winsize = 3;
  2460. } else if (x <= 140) {
  2461. winsize = 4;
  2462. } else if (x <= 450) {
  2463. winsize = 5;
  2464. } else if (x <= 1303) {
  2465. winsize = 6;
  2466. } else if (x <= 3529) {
  2467. winsize = 7;
  2468. } else {
  2469. winsize = 8;
  2470. }
  2471. #ifdef MP_LOW_MEM
  2472. if (winsize > 5) {
  2473. winsize = 5;
  2474. }
  2475. #endif
  2476. /* init M array */
  2477. /* init first cell */
  2478. if ((err = mp_init(&M[1])) != MP_OKAY) {
  2479. return err;
  2480. }
  2481. /* now init the second half of the array */
  2482. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  2483. if ((err = mp_init(&M[x])) != MP_OKAY) {
  2484. for (y = 1<<(winsize-1); y < x; y++) {
  2485. mp_clear (&M[y]);
  2486. }
  2487. mp_clear(&M[1]);
  2488. return err;
  2489. }
  2490. }
  2491. /* determine and setup reduction code */
  2492. if (redmode == 0) {
  2493. #ifdef BN_MP_MONTGOMERY_SETUP_C
  2494. /* now setup montgomery */
  2495. if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
  2496. goto LBL_M;
  2497. }
  2498. #else
  2499. err = MP_VAL;
  2500. goto LBL_M;
  2501. #endif
  2502. /* automatically pick the comba one if available (saves quite a few calls/ifs) */
  2503. #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
  2504. if (((P->used * 2 + 1) < MP_WARRAY) &&
  2505. P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
  2506. redux = fast_mp_montgomery_reduce;
  2507. } else
  2508. #endif
  2509. {
  2510. #ifdef BN_MP_MONTGOMERY_REDUCE_C
  2511. /* use slower baseline Montgomery method */
  2512. redux = mp_montgomery_reduce;
  2513. #else
  2514. err = MP_VAL;
  2515. goto LBL_M;
  2516. #endif
  2517. }
  2518. } else if (redmode == 1) {
  2519. #if defined(BN_MP_DR_SETUP_C) && defined(BN_MP_DR_REDUCE_C)
  2520. /* setup DR reduction for moduli of the form B**k - b */
  2521. mp_dr_setup(P, &mp);
  2522. redux = mp_dr_reduce;
  2523. #else
  2524. err = MP_VAL;
  2525. goto LBL_M;
  2526. #endif
  2527. } else {
  2528. #if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C)
  2529. /* setup DR reduction for moduli of the form 2**k - b */
  2530. if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
  2531. goto LBL_M;
  2532. }
  2533. redux = mp_reduce_2k;
  2534. #else
  2535. err = MP_VAL;
  2536. goto LBL_M;
  2537. #endif
  2538. }
  2539. /* setup result */
  2540. if ((err = mp_init (&res)) != MP_OKAY) {
  2541. goto LBL_M;
  2542. }
  2543. /* create M table
  2544. *
  2545. *
  2546. * The first half of the table is not computed though accept for M[0] and M[1]
  2547. */
  2548. if (redmode == 0) {
  2549. #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
  2550. /* now we need R mod m */
  2551. if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
  2552. goto LBL_RES;
  2553. }
  2554. #else
  2555. err = MP_VAL;
  2556. goto LBL_RES;
  2557. #endif
  2558. /* now set M[1] to G * R mod m */
  2559. if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
  2560. goto LBL_RES;
  2561. }
  2562. } else {
  2563. mp_set(&res, 1);
  2564. if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
  2565. goto LBL_RES;
  2566. }
  2567. }
  2568. /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
  2569. if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
  2570. goto LBL_RES;
  2571. }
  2572. for (x = 0; x < (winsize - 1); x++) {
  2573. if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
  2574. goto LBL_RES;
  2575. }
  2576. if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
  2577. goto LBL_RES;
  2578. }
  2579. }
  2580. /* create upper table */
  2581. for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
  2582. if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
  2583. goto LBL_RES;
  2584. }
  2585. if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
  2586. goto LBL_RES;
  2587. }
  2588. }
  2589. /* set initial mode and bit cnt */
  2590. mode = 0;
  2591. bitcnt = 1;
  2592. buf = 0;
  2593. digidx = X->used - 1;
  2594. bitcpy = 0;
  2595. bitbuf = 0;
  2596. for (;;) {
  2597. /* grab next digit as required */
  2598. if (--bitcnt == 0) {
  2599. /* if digidx == -1 we are out of digits so break */
  2600. if (digidx == -1) {
  2601. break;
  2602. }
  2603. /* read next digit and reset bitcnt */
  2604. buf = X->dp[digidx--];
  2605. bitcnt = (int)DIGIT_BIT;
  2606. }
  2607. /* grab the next msb from the exponent */
  2608. y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
  2609. buf <<= (mp_digit)1;
  2610. /* if the bit is zero and mode == 0 then we ignore it
  2611. * These represent the leading zero bits before the first 1 bit
  2612. * in the exponent. Technically this opt is not required but it
  2613. * does lower the # of trivial squaring/reductions used
  2614. */
  2615. if (mode == 0 && y == 0) {
  2616. continue;
  2617. }
  2618. /* if the bit is zero and mode == 1 then we square */
  2619. if (mode == 1 && y == 0) {
  2620. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  2621. goto LBL_RES;
  2622. }
  2623. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  2624. goto LBL_RES;
  2625. }
  2626. continue;
  2627. }
  2628. /* else we add it to the window */
  2629. bitbuf |= (y << (winsize - ++bitcpy));
  2630. mode = 2;
  2631. if (bitcpy == winsize) {
  2632. /* ok window is filled so square as required and multiply */
  2633. /* square first */
  2634. for (x = 0; x < winsize; x++) {
  2635. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  2636. goto LBL_RES;
  2637. }
  2638. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  2639. goto LBL_RES;
  2640. }
  2641. }
  2642. /* then multiply */
  2643. if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
  2644. goto LBL_RES;
  2645. }
  2646. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  2647. goto LBL_RES;
  2648. }
  2649. /* empty window and reset */
  2650. bitcpy = 0;
  2651. bitbuf = 0;
  2652. mode = 1;
  2653. }
  2654. }
  2655. /* if bits remain then square/multiply */
  2656. if (mode == 2 && bitcpy > 0) {
  2657. /* square then multiply if the bit is set */
  2658. for (x = 0; x < bitcpy; x++) {
  2659. if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
  2660. goto LBL_RES;
  2661. }
  2662. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  2663. goto LBL_RES;
  2664. }
  2665. /* get next bit of the window */
  2666. bitbuf <<= 1;
  2667. if ((bitbuf & (1 << winsize)) != 0) {
  2668. /* then multiply */
  2669. if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
  2670. goto LBL_RES;
  2671. }
  2672. if ((err = redux (&res, P, mp)) != MP_OKAY) {
  2673. goto LBL_RES;
  2674. }
  2675. }
  2676. }
  2677. }
  2678. if (redmode == 0) {
  2679. /* fixup result if Montgomery reduction is used
  2680. * recall that any value in a Montgomery system is
  2681. * actually multiplied by R mod n. So we have
  2682. * to reduce one more time to cancel out the factor
  2683. * of R.
  2684. */
  2685. if ((err = redux(&res, P, mp)) != MP_OKAY) {
  2686. goto LBL_RES;
  2687. }
  2688. }
  2689. /* swap res with Y */
  2690. mp_exch (&res, Y);
  2691. err = MP_OKAY;
  2692. LBL_RES:mp_clear (&res);
  2693. LBL_M:
  2694. mp_clear(&M[1]);
  2695. for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
  2696. mp_clear (&M[x]);
  2697. }
  2698. return err;
  2699. }
  2700. #endif
  2701. #ifdef BN_FAST_S_MP_SQR_C
  2702. /* the jist of squaring...
  2703. * you do like mult except the offset of the tmpx [one that
  2704. * starts closer to zero] can't equal the offset of tmpy.
  2705. * So basically you set up iy like before then you min it with
  2706. * (ty-tx) so that it never happens. You double all those
  2707. * you add in the inner loop
  2708. After that loop you do the squares and add them in.
  2709. */
  2710. static int fast_s_mp_sqr (mp_int * a, mp_int * b)
  2711. {
  2712. int olduse, res, pa, ix, iz;
  2713. mp_digit W[MP_WARRAY], *tmpx;
  2714. mp_word W1;
  2715. /* grow the destination as required */
  2716. pa = a->used + a->used;
  2717. if (b->alloc < pa) {
  2718. if ((res = mp_grow (b, pa)) != MP_OKAY) {
  2719. return res;
  2720. }
  2721. }
  2722. /* number of output digits to produce */
  2723. W1 = 0;
  2724. for (ix = 0; ix < pa; ix++) {
  2725. int tx, ty, iy;
  2726. mp_word _W;
  2727. mp_digit *tmpy;
  2728. /* clear counter */
  2729. _W = 0;
  2730. /* get offsets into the two bignums */
  2731. ty = MIN(a->used-1, ix);
  2732. tx = ix - ty;
  2733. /* setup temp aliases */
  2734. tmpx = a->dp + tx;
  2735. tmpy = a->dp + ty;
  2736. /* this is the number of times the loop will iterrate, essentially
  2737. while (tx++ < a->used && ty-- >= 0) { ... }
  2738. */
  2739. iy = MIN(a->used-tx, ty+1);
  2740. /* now for squaring tx can never equal ty
  2741. * we halve the distance since they approach at a rate of 2x
  2742. * and we have to round because odd cases need to be executed
  2743. */
  2744. iy = MIN(iy, (ty-tx+1)>>1);
  2745. /* execute loop */
  2746. for (iz = 0; iz < iy; iz++) {
  2747. _W += ((mp_word)*tmpx++)*((mp_word)*tmpy--);
  2748. }
  2749. /* double the inner product and add carry */
  2750. _W = _W + _W + W1;
  2751. /* even columns have the square term in them */
  2752. if ((ix&1) == 0) {
  2753. _W += ((mp_word)a->dp[ix>>1])*((mp_word)a->dp[ix>>1]);
  2754. }
  2755. /* store it */
  2756. W[ix] = (mp_digit)(_W & MP_MASK);
  2757. /* make next carry */
  2758. W1 = _W >> ((mp_word)DIGIT_BIT);
  2759. }
  2760. /* setup dest */
  2761. olduse = b->used;
  2762. b->used = a->used+a->used;
  2763. {
  2764. mp_digit *tmpb;
  2765. tmpb = b->dp;
  2766. for (ix = 0; ix < pa; ix++) {
  2767. *tmpb++ = W[ix] & MP_MASK;
  2768. }
  2769. /* clear unused digits [that existed in the old copy of c] */
  2770. for (; ix < olduse; ix++) {
  2771. *tmpb++ = 0;
  2772. }
  2773. }
  2774. mp_clamp (b);
  2775. return MP_OKAY;
  2776. }
  2777. #endif
  2778. #ifdef BN_MP_MUL_D_C
  2779. /* multiply by a digit */
  2780. static int
  2781. mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
  2782. {
  2783. mp_digit u, *tmpa, *tmpc;
  2784. mp_word r;
  2785. int ix, res, olduse;
  2786. /* make sure c is big enough to hold a*b */
  2787. if (c->alloc < a->used + 1) {
  2788. if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
  2789. return res;
  2790. }
  2791. }
  2792. /* get the original destinations used count */
  2793. olduse = c->used;
  2794. /* set the sign */
  2795. c->sign = a->sign;
  2796. /* alias for a->dp [source] */
  2797. tmpa = a->dp;
  2798. /* alias for c->dp [dest] */
  2799. tmpc = c->dp;
  2800. /* zero carry */
  2801. u = 0;
  2802. /* compute columns */
  2803. for (ix = 0; ix < a->used; ix++) {
  2804. /* compute product and carry sum for this term */
  2805. r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
  2806. /* mask off higher bits to get a single digit */
  2807. *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
  2808. /* send carry into next iteration */
  2809. u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
  2810. }
  2811. /* store final carry [if any] and increment ix offset */
  2812. *tmpc++ = u;
  2813. ++ix;
  2814. /* now zero digits above the top */
  2815. while (ix++ < olduse) {
  2816. *tmpc++ = 0;
  2817. }
  2818. /* set used count */
  2819. c->used = a->used + 1;
  2820. mp_clamp(c);
  2821. return MP_OKAY;
  2822. }
  2823. #endif