mpi-pow.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* mpi-pow.c - MPI functions
  2. * Copyright (C) 1994, 1996, 1998, 2000 Free Software Foundation, Inc.
  3. *
  4. * This file is part of GnuPG.
  5. *
  6. * GnuPG is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GnuPG is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  19. *
  20. * Note: This code is heavily based on the GNU MP Library.
  21. * Actually it's the same code with only minor changes in the
  22. * way the data is stored; this is to support the abstraction
  23. * of an optional secure memory allocation which may be used
  24. * to avoid revealing of sensitive data due to paging etc.
  25. * The GNU MP Library itself is published under the LGPL;
  26. * however I decided to publish this code under the plain GPL.
  27. */
  28. #include <linux/string.h>
  29. #include "mpi-internal.h"
  30. #include "longlong.h"
  31. /****************
  32. * RES = BASE ^ EXP mod MOD
  33. */
  34. int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
  35. {
  36. mpi_ptr_t mp_marker = NULL, bp_marker = NULL, ep_marker = NULL;
  37. mpi_ptr_t xp_marker = NULL;
  38. mpi_ptr_t tspace = NULL;
  39. mpi_ptr_t rp, ep, mp, bp;
  40. mpi_size_t esize, msize, bsize, rsize;
  41. int esign, msign, bsign, rsign;
  42. mpi_size_t size;
  43. int mod_shift_cnt;
  44. int negative_result;
  45. int assign_rp = 0;
  46. mpi_size_t tsize = 0; /* to avoid compiler warning */
  47. /* fixme: we should check that the warning is void */
  48. int rc = -ENOMEM;
  49. esize = exp->nlimbs;
  50. msize = mod->nlimbs;
  51. size = 2 * msize;
  52. esign = exp->sign;
  53. msign = mod->sign;
  54. rp = res->d;
  55. ep = exp->d;
  56. if (!msize)
  57. return -EINVAL;
  58. if (!esize) {
  59. /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
  60. * depending on if MOD equals 1. */
  61. rp[0] = 1;
  62. res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
  63. res->sign = 0;
  64. goto leave;
  65. }
  66. /* Normalize MOD (i.e. make its most significant bit set) as required by
  67. * mpn_divrem. This will make the intermediate values in the calculation
  68. * slightly larger, but the correct result is obtained after a final
  69. * reduction using the original MOD value. */
  70. mp = mp_marker = mpi_alloc_limb_space(msize);
  71. if (!mp)
  72. goto enomem;
  73. mod_shift_cnt = count_leading_zeros(mod->d[msize - 1]);
  74. if (mod_shift_cnt)
  75. mpihelp_lshift(mp, mod->d, msize, mod_shift_cnt);
  76. else
  77. MPN_COPY(mp, mod->d, msize);
  78. bsize = base->nlimbs;
  79. bsign = base->sign;
  80. if (bsize > msize) { /* The base is larger than the module. Reduce it. */
  81. /* Allocate (BSIZE + 1) with space for remainder and quotient.
  82. * (The quotient is (bsize - msize + 1) limbs.) */
  83. bp = bp_marker = mpi_alloc_limb_space(bsize + 1);
  84. if (!bp)
  85. goto enomem;
  86. MPN_COPY(bp, base->d, bsize);
  87. /* We don't care about the quotient, store it above the remainder,
  88. * at BP + MSIZE. */
  89. mpihelp_divrem(bp + msize, 0, bp, bsize, mp, msize);
  90. bsize = msize;
  91. /* Canonicalize the base, since we are going to multiply with it
  92. * quite a few times. */
  93. MPN_NORMALIZE(bp, bsize);
  94. } else
  95. bp = base->d;
  96. if (!bsize) {
  97. res->nlimbs = 0;
  98. res->sign = 0;
  99. goto leave;
  100. }
  101. if (res->alloced < size) {
  102. /* We have to allocate more space for RES. If any of the input
  103. * parameters are identical to RES, defer deallocation of the old
  104. * space. */
  105. if (rp == ep || rp == mp || rp == bp) {
  106. rp = mpi_alloc_limb_space(size);
  107. if (!rp)
  108. goto enomem;
  109. assign_rp = 1;
  110. } else {
  111. if (mpi_resize(res, size) < 0)
  112. goto enomem;
  113. rp = res->d;
  114. }
  115. } else { /* Make BASE, EXP and MOD not overlap with RES. */
  116. if (rp == bp) {
  117. /* RES and BASE are identical. Allocate temp. space for BASE. */
  118. BUG_ON(bp_marker);
  119. bp = bp_marker = mpi_alloc_limb_space(bsize);
  120. if (!bp)
  121. goto enomem;
  122. MPN_COPY(bp, rp, bsize);
  123. }
  124. if (rp == ep) {
  125. /* RES and EXP are identical. Allocate temp. space for EXP. */
  126. ep = ep_marker = mpi_alloc_limb_space(esize);
  127. if (!ep)
  128. goto enomem;
  129. MPN_COPY(ep, rp, esize);
  130. }
  131. if (rp == mp) {
  132. /* RES and MOD are identical. Allocate temporary space for MOD. */
  133. BUG_ON(mp_marker);
  134. mp = mp_marker = mpi_alloc_limb_space(msize);
  135. if (!mp)
  136. goto enomem;
  137. MPN_COPY(mp, rp, msize);
  138. }
  139. }
  140. MPN_COPY(rp, bp, bsize);
  141. rsize = bsize;
  142. rsign = bsign;
  143. {
  144. mpi_size_t i;
  145. mpi_ptr_t xp;
  146. int c;
  147. mpi_limb_t e;
  148. mpi_limb_t carry_limb;
  149. struct karatsuba_ctx karactx;
  150. xp = xp_marker = mpi_alloc_limb_space(2 * (msize + 1));
  151. if (!xp)
  152. goto enomem;
  153. memset(&karactx, 0, sizeof karactx);
  154. negative_result = (ep[0] & 1) && base->sign;
  155. i = esize - 1;
  156. e = ep[i];
  157. c = count_leading_zeros(e);
  158. e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
  159. c = BITS_PER_MPI_LIMB - 1 - c;
  160. /* Main loop.
  161. *
  162. * Make the result be pointed to alternately by XP and RP. This
  163. * helps us avoid block copying, which would otherwise be necessary
  164. * with the overlap restrictions of mpihelp_divmod. With 50% probability
  165. * the result after this loop will be in the area originally pointed
  166. * by RP (==RES->d), and with 50% probability in the area originally
  167. * pointed to by XP.
  168. */
  169. for (;;) {
  170. while (c) {
  171. mpi_ptr_t tp;
  172. mpi_size_t xsize;
  173. /*if (mpihelp_mul_n(xp, rp, rp, rsize) < 0) goto enomem */
  174. if (rsize < KARATSUBA_THRESHOLD)
  175. mpih_sqr_n_basecase(xp, rp, rsize);
  176. else {
  177. if (!tspace) {
  178. tsize = 2 * rsize;
  179. tspace =
  180. mpi_alloc_limb_space(tsize);
  181. if (!tspace)
  182. goto enomem;
  183. } else if (tsize < (2 * rsize)) {
  184. mpi_free_limb_space(tspace);
  185. tsize = 2 * rsize;
  186. tspace =
  187. mpi_alloc_limb_space(tsize);
  188. if (!tspace)
  189. goto enomem;
  190. }
  191. mpih_sqr_n(xp, rp, rsize, tspace);
  192. }
  193. xsize = 2 * rsize;
  194. if (xsize > msize) {
  195. mpihelp_divrem(xp + msize, 0, xp, xsize,
  196. mp, msize);
  197. xsize = msize;
  198. }
  199. tp = rp;
  200. rp = xp;
  201. xp = tp;
  202. rsize = xsize;
  203. if ((mpi_limb_signed_t) e < 0) {
  204. /*mpihelp_mul( xp, rp, rsize, bp, bsize ); */
  205. if (bsize < KARATSUBA_THRESHOLD) {
  206. mpi_limb_t tmp;
  207. if (mpihelp_mul
  208. (xp, rp, rsize, bp, bsize,
  209. &tmp) < 0)
  210. goto enomem;
  211. } else {
  212. if (mpihelp_mul_karatsuba_case
  213. (xp, rp, rsize, bp, bsize,
  214. &karactx) < 0)
  215. goto enomem;
  216. }
  217. xsize = rsize + bsize;
  218. if (xsize > msize) {
  219. mpihelp_divrem(xp + msize, 0,
  220. xp, xsize, mp,
  221. msize);
  222. xsize = msize;
  223. }
  224. tp = rp;
  225. rp = xp;
  226. xp = tp;
  227. rsize = xsize;
  228. }
  229. e <<= 1;
  230. c--;
  231. }
  232. i--;
  233. if (i < 0)
  234. break;
  235. e = ep[i];
  236. c = BITS_PER_MPI_LIMB;
  237. }
  238. /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
  239. * steps. Adjust the result by reducing it with the original MOD.
  240. *
  241. * Also make sure the result is put in RES->d (where it already
  242. * might be, see above).
  243. */
  244. if (mod_shift_cnt) {
  245. carry_limb =
  246. mpihelp_lshift(res->d, rp, rsize, mod_shift_cnt);
  247. rp = res->d;
  248. if (carry_limb) {
  249. rp[rsize] = carry_limb;
  250. rsize++;
  251. }
  252. } else {
  253. MPN_COPY(res->d, rp, rsize);
  254. rp = res->d;
  255. }
  256. if (rsize >= msize) {
  257. mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
  258. rsize = msize;
  259. }
  260. /* Remove any leading zero words from the result. */
  261. if (mod_shift_cnt)
  262. mpihelp_rshift(rp, rp, rsize, mod_shift_cnt);
  263. MPN_NORMALIZE(rp, rsize);
  264. mpihelp_release_karatsuba_ctx(&karactx);
  265. }
  266. if (negative_result && rsize) {
  267. if (mod_shift_cnt)
  268. mpihelp_rshift(mp, mp, msize, mod_shift_cnt);
  269. mpihelp_sub(rp, mp, msize, rp, rsize);
  270. rsize = msize;
  271. rsign = msign;
  272. MPN_NORMALIZE(rp, rsize);
  273. }
  274. res->nlimbs = rsize;
  275. res->sign = rsign;
  276. leave:
  277. rc = 0;
  278. enomem:
  279. if (assign_rp)
  280. mpi_assign_limb_space(res, rp, size);
  281. if (mp_marker)
  282. mpi_free_limb_space(mp_marker);
  283. if (bp_marker)
  284. mpi_free_limb_space(bp_marker);
  285. if (ep_marker)
  286. mpi_free_limb_space(ep_marker);
  287. if (xp_marker)
  288. mpi_free_limb_space(xp_marker);
  289. if (tspace)
  290. mpi_free_limb_space(tspace);
  291. return rc;
  292. }
  293. EXPORT_SYMBOL_GPL(mpi_powm);