mpi-pow.c 8.3 KB

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