mpi-pow.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/sched.h>
  29. #include <linux/string.h>
  30. #include "mpi-internal.h"
  31. #include "longlong.h"
  32. /****************
  33. * RES = BASE ^ EXP mod MOD
  34. */
  35. int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
  36. {
  37. mpi_ptr_t mp_marker = NULL, bp_marker = NULL, ep_marker = NULL;
  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. mod_shift_cnt = count_leading_zeros(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. struct karatsuba_ctx karactx;
  156. xp = xp_marker = mpi_alloc_limb_space(2 * (msize + 1));
  157. if (!xp)
  158. goto enomem;
  159. memset(&karactx, 0, sizeof karactx);
  160. negative_result = (ep[0] & 1) && base->sign;
  161. i = esize - 1;
  162. e = ep[i];
  163. c = count_leading_zeros(e);
  164. e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
  165. c = BITS_PER_MPI_LIMB - 1 - c;
  166. /* Main loop.
  167. *
  168. * Make the result be pointed to alternately by XP and RP. This
  169. * helps us avoid block copying, which would otherwise be necessary
  170. * with the overlap restrictions of mpihelp_divmod. With 50% probability
  171. * the result after this loop will be in the area originally pointed
  172. * by RP (==RES->d), and with 50% probability in the area originally
  173. * pointed to by XP.
  174. */
  175. for (;;) {
  176. while (c) {
  177. mpi_ptr_t tp;
  178. mpi_size_t xsize;
  179. /*if (mpihelp_mul_n(xp, rp, rp, rsize) < 0) goto enomem */
  180. if (rsize < KARATSUBA_THRESHOLD)
  181. mpih_sqr_n_basecase(xp, rp, rsize);
  182. else {
  183. if (!tspace) {
  184. tsize = 2 * rsize;
  185. tspace =
  186. mpi_alloc_limb_space(tsize);
  187. if (!tspace)
  188. goto enomem;
  189. } else if (tsize < (2 * rsize)) {
  190. mpi_free_limb_space(tspace);
  191. tsize = 2 * rsize;
  192. tspace =
  193. mpi_alloc_limb_space(tsize);
  194. if (!tspace)
  195. goto enomem;
  196. }
  197. mpih_sqr_n(xp, rp, rsize, tspace);
  198. }
  199. xsize = 2 * rsize;
  200. if (xsize > msize) {
  201. mpihelp_divrem(xp + msize, 0, xp, xsize,
  202. mp, msize);
  203. xsize = msize;
  204. }
  205. tp = rp;
  206. rp = xp;
  207. xp = tp;
  208. rsize = xsize;
  209. if ((mpi_limb_signed_t) e < 0) {
  210. /*mpihelp_mul( xp, rp, rsize, bp, bsize ); */
  211. if (bsize < KARATSUBA_THRESHOLD) {
  212. mpi_limb_t tmp;
  213. if (mpihelp_mul
  214. (xp, rp, rsize, bp, bsize,
  215. &tmp) < 0)
  216. goto enomem;
  217. } else {
  218. if (mpihelp_mul_karatsuba_case
  219. (xp, rp, rsize, bp, bsize,
  220. &karactx) < 0)
  221. goto enomem;
  222. }
  223. xsize = rsize + bsize;
  224. if (xsize > msize) {
  225. mpihelp_divrem(xp + msize, 0,
  226. xp, xsize, mp,
  227. msize);
  228. xsize = msize;
  229. }
  230. tp = rp;
  231. rp = xp;
  232. xp = tp;
  233. rsize = xsize;
  234. }
  235. e <<= 1;
  236. c--;
  237. cond_resched();
  238. }
  239. i--;
  240. if (i < 0)
  241. break;
  242. e = ep[i];
  243. c = BITS_PER_MPI_LIMB;
  244. }
  245. /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
  246. * steps. Adjust the result by reducing it with the original MOD.
  247. *
  248. * Also make sure the result is put in RES->d (where it already
  249. * might be, see above).
  250. */
  251. if (mod_shift_cnt) {
  252. carry_limb =
  253. mpihelp_lshift(res->d, rp, rsize, mod_shift_cnt);
  254. rp = res->d;
  255. if (carry_limb) {
  256. rp[rsize] = carry_limb;
  257. rsize++;
  258. }
  259. } else {
  260. MPN_COPY(res->d, rp, rsize);
  261. rp = res->d;
  262. }
  263. if (rsize >= msize) {
  264. mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
  265. rsize = msize;
  266. }
  267. /* Remove any leading zero words from the result. */
  268. if (mod_shift_cnt)
  269. mpihelp_rshift(rp, rp, rsize, mod_shift_cnt);
  270. MPN_NORMALIZE(rp, rsize);
  271. mpihelp_release_karatsuba_ctx(&karactx);
  272. }
  273. if (negative_result && rsize) {
  274. if (mod_shift_cnt)
  275. mpihelp_rshift(mp, mp, msize, mod_shift_cnt);
  276. mpihelp_sub(rp, mp, msize, rp, rsize);
  277. rsize = msize;
  278. rsign = msign;
  279. MPN_NORMALIZE(rp, rsize);
  280. }
  281. res->nlimbs = rsize;
  282. res->sign = rsign;
  283. leave:
  284. rc = 0;
  285. enomem:
  286. if (assign_rp)
  287. mpi_assign_limb_space(res, rp, size);
  288. if (mp_marker)
  289. mpi_free_limb_space(mp_marker);
  290. if (bp_marker)
  291. mpi_free_limb_space(bp_marker);
  292. if (ep_marker)
  293. mpi_free_limb_space(ep_marker);
  294. if (xp_marker)
  295. mpi_free_limb_space(xp_marker);
  296. if (tspace)
  297. mpi_free_limb_space(tspace);
  298. return rc;
  299. }
  300. EXPORT_SYMBOL_GPL(mpi_powm);