mpi-pow.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
  62. if (res->nlimbs) {
  63. if (mpi_resize(res, 1) < 0)
  64. goto enomem;
  65. rp = res->d;
  66. rp[0] = 1;
  67. }
  68. res->sign = 0;
  69. goto leave;
  70. }
  71. /* Normalize MOD (i.e. make its most significant bit set) as required by
  72. * mpn_divrem. This will make the intermediate values in the calculation
  73. * slightly larger, but the correct result is obtained after a final
  74. * reduction using the original MOD value. */
  75. mp = mp_marker = mpi_alloc_limb_space(msize);
  76. if (!mp)
  77. goto enomem;
  78. mod_shift_cnt = count_leading_zeros(mod->d[msize - 1]);
  79. if (mod_shift_cnt)
  80. mpihelp_lshift(mp, mod->d, msize, mod_shift_cnt);
  81. else
  82. MPN_COPY(mp, mod->d, msize);
  83. bsize = base->nlimbs;
  84. bsign = base->sign;
  85. if (bsize > msize) { /* The base is larger than the module. Reduce it. */
  86. /* Allocate (BSIZE + 1) with space for remainder and quotient.
  87. * (The quotient is (bsize - msize + 1) limbs.) */
  88. bp = bp_marker = mpi_alloc_limb_space(bsize + 1);
  89. if (!bp)
  90. goto enomem;
  91. MPN_COPY(bp, base->d, bsize);
  92. /* We don't care about the quotient, store it above the remainder,
  93. * at BP + MSIZE. */
  94. mpihelp_divrem(bp + msize, 0, bp, bsize, mp, msize);
  95. bsize = msize;
  96. /* Canonicalize the base, since we are going to multiply with it
  97. * quite a few times. */
  98. MPN_NORMALIZE(bp, bsize);
  99. } else
  100. bp = base->d;
  101. if (!bsize) {
  102. res->nlimbs = 0;
  103. res->sign = 0;
  104. goto leave;
  105. }
  106. if (res->alloced < size) {
  107. /* We have to allocate more space for RES. If any of the input
  108. * parameters are identical to RES, defer deallocation of the old
  109. * space. */
  110. if (rp == ep || rp == mp || rp == bp) {
  111. rp = mpi_alloc_limb_space(size);
  112. if (!rp)
  113. goto enomem;
  114. assign_rp = 1;
  115. } else {
  116. if (mpi_resize(res, size) < 0)
  117. goto enomem;
  118. rp = res->d;
  119. }
  120. } else { /* Make BASE, EXP and MOD not overlap with RES. */
  121. if (rp == bp) {
  122. /* RES and BASE are identical. Allocate temp. space for BASE. */
  123. BUG_ON(bp_marker);
  124. bp = bp_marker = mpi_alloc_limb_space(bsize);
  125. if (!bp)
  126. goto enomem;
  127. MPN_COPY(bp, rp, bsize);
  128. }
  129. if (rp == ep) {
  130. /* RES and EXP are identical. Allocate temp. space for EXP. */
  131. ep = ep_marker = mpi_alloc_limb_space(esize);
  132. if (!ep)
  133. goto enomem;
  134. MPN_COPY(ep, rp, esize);
  135. }
  136. if (rp == mp) {
  137. /* RES and MOD are identical. Allocate temporary space for MOD. */
  138. BUG_ON(mp_marker);
  139. mp = mp_marker = mpi_alloc_limb_space(msize);
  140. if (!mp)
  141. goto enomem;
  142. MPN_COPY(mp, rp, msize);
  143. }
  144. }
  145. MPN_COPY(rp, bp, bsize);
  146. rsize = bsize;
  147. rsign = bsign;
  148. {
  149. mpi_size_t i;
  150. mpi_ptr_t xp;
  151. int c;
  152. mpi_limb_t e;
  153. mpi_limb_t carry_limb;
  154. struct karatsuba_ctx karactx;
  155. xp = xp_marker = mpi_alloc_limb_space(2 * (msize + 1));
  156. if (!xp)
  157. goto enomem;
  158. memset(&karactx, 0, sizeof karactx);
  159. negative_result = (ep[0] & 1) && base->sign;
  160. i = esize - 1;
  161. e = ep[i];
  162. c = count_leading_zeros(e);
  163. e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
  164. c = BITS_PER_MPI_LIMB - 1 - c;
  165. /* Main loop.
  166. *
  167. * Make the result be pointed to alternately by XP and RP. This
  168. * helps us avoid block copying, which would otherwise be necessary
  169. * with the overlap restrictions of mpihelp_divmod. With 50% probability
  170. * the result after this loop will be in the area originally pointed
  171. * by RP (==RES->d), and with 50% probability in the area originally
  172. * pointed to by XP.
  173. */
  174. for (;;) {
  175. while (c) {
  176. mpi_ptr_t tp;
  177. mpi_size_t xsize;
  178. /*if (mpihelp_mul_n(xp, rp, rp, rsize) < 0) goto enomem */
  179. if (rsize < KARATSUBA_THRESHOLD)
  180. mpih_sqr_n_basecase(xp, rp, rsize);
  181. else {
  182. if (!tspace) {
  183. tsize = 2 * rsize;
  184. tspace =
  185. mpi_alloc_limb_space(tsize);
  186. if (!tspace)
  187. goto enomem;
  188. } else if (tsize < (2 * rsize)) {
  189. mpi_free_limb_space(tspace);
  190. tsize = 2 * rsize;
  191. tspace =
  192. mpi_alloc_limb_space(tsize);
  193. if (!tspace)
  194. goto enomem;
  195. }
  196. mpih_sqr_n(xp, rp, rsize, tspace);
  197. }
  198. xsize = 2 * rsize;
  199. if (xsize > msize) {
  200. mpihelp_divrem(xp + msize, 0, xp, xsize,
  201. mp, msize);
  202. xsize = msize;
  203. }
  204. tp = rp;
  205. rp = xp;
  206. xp = tp;
  207. rsize = xsize;
  208. if ((mpi_limb_signed_t) e < 0) {
  209. /*mpihelp_mul( xp, rp, rsize, bp, bsize ); */
  210. if (bsize < KARATSUBA_THRESHOLD) {
  211. mpi_limb_t tmp;
  212. if (mpihelp_mul
  213. (xp, rp, rsize, bp, bsize,
  214. &tmp) < 0)
  215. goto enomem;
  216. } else {
  217. if (mpihelp_mul_karatsuba_case
  218. (xp, rp, rsize, bp, bsize,
  219. &karactx) < 0)
  220. goto enomem;
  221. }
  222. xsize = rsize + bsize;
  223. if (xsize > msize) {
  224. mpihelp_divrem(xp + msize, 0,
  225. xp, xsize, mp,
  226. msize);
  227. xsize = msize;
  228. }
  229. tp = rp;
  230. rp = xp;
  231. xp = tp;
  232. rsize = xsize;
  233. }
  234. e <<= 1;
  235. c--;
  236. }
  237. i--;
  238. if (i < 0)
  239. break;
  240. e = ep[i];
  241. c = BITS_PER_MPI_LIMB;
  242. }
  243. /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
  244. * steps. Adjust the result by reducing it with the original MOD.
  245. *
  246. * Also make sure the result is put in RES->d (where it already
  247. * might be, see above).
  248. */
  249. if (mod_shift_cnt) {
  250. carry_limb =
  251. mpihelp_lshift(res->d, rp, rsize, mod_shift_cnt);
  252. rp = res->d;
  253. if (carry_limb) {
  254. rp[rsize] = carry_limb;
  255. rsize++;
  256. }
  257. } else {
  258. MPN_COPY(res->d, rp, rsize);
  259. rp = res->d;
  260. }
  261. if (rsize >= msize) {
  262. mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
  263. rsize = msize;
  264. }
  265. /* Remove any leading zero words from the result. */
  266. if (mod_shift_cnt)
  267. mpihelp_rshift(rp, rp, rsize, mod_shift_cnt);
  268. MPN_NORMALIZE(rp, rsize);
  269. mpihelp_release_karatsuba_ctx(&karactx);
  270. }
  271. if (negative_result && rsize) {
  272. if (mod_shift_cnt)
  273. mpihelp_rshift(mp, mp, msize, mod_shift_cnt);
  274. mpihelp_sub(rp, mp, msize, rp, rsize);
  275. rsize = msize;
  276. rsign = msign;
  277. MPN_NORMALIZE(rp, rsize);
  278. }
  279. res->nlimbs = rsize;
  280. res->sign = rsign;
  281. leave:
  282. rc = 0;
  283. enomem:
  284. if (assign_rp)
  285. mpi_assign_limb_space(res, rp, size);
  286. if (mp_marker)
  287. mpi_free_limb_space(mp_marker);
  288. if (bp_marker)
  289. mpi_free_limb_space(bp_marker);
  290. if (ep_marker)
  291. mpi_free_limb_space(ep_marker);
  292. if (xp_marker)
  293. mpi_free_limb_space(xp_marker);
  294. if (tspace)
  295. mpi_free_limb_space(tspace);
  296. return rc;
  297. }
  298. EXPORT_SYMBOL_GPL(mpi_powm);