mpi-pow.c 7.7 KB

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