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.
5 * This file is part of GnuPG.
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.
16 #include <linux/sched.h>
17 #include <linux/string.h>
18 #include "mpi-internal.h"
22 * RES = BASE ^ EXP mod MOD
24 int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
26 mpi_ptr_t mp_marker = NULL, bp_marker = NULL, ep_marker = NULL;
27 mpi_ptr_t xp_marker = NULL;
28 mpi_ptr_t tspace = NULL;
29 mpi_ptr_t rp, ep, mp, bp;
30 mpi_size_t esize, msize, bsize, rsize;
31 int msign, bsign, rsign;
36 mpi_size_t tsize = 0; /* to avoid compiler warning */
37 /* fixme: we should check that the warning is void */
52 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
53 * depending on if MOD equals 1. */
54 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
56 if (mpi_resize(res, 1) < 0)
65 /* Normalize MOD (i.e. make its most significant bit set) as required by
66 * mpn_divrem. This will make the intermediate values in the calculation
67 * slightly larger, but the correct result is obtained after a final
68 * reduction using the original MOD value. */
69 mp = mp_marker = mpi_alloc_limb_space(msize);
72 mod_shift_cnt = count_leading_zeros(mod->d[msize - 1]);
74 mpihelp_lshift(mp, mod->d, msize, mod_shift_cnt);
76 MPN_COPY(mp, mod->d, msize);
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);
86 MPN_COPY(bp, base->d, bsize);
87 /* We don't care about the quotient, store it above the remainder,
89 mpihelp_divrem(bp + msize, 0, bp, bsize, mp, msize);
91 /* Canonicalize the base, since we are going to multiply with it
92 * quite a few times. */
93 MPN_NORMALIZE(bp, bsize);
103 if (res->alloced < size) {
104 /* We have to allocate more space for RES. If any of the input
105 * parameters are identical to RES, defer deallocation of the old
107 if (rp == ep || rp == mp || rp == bp) {
108 rp = mpi_alloc_limb_space(size);
113 if (mpi_resize(res, size) < 0)
117 } else { /* Make BASE, EXP and MOD not overlap with RES. */
119 /* RES and BASE are identical. Allocate temp. space for BASE. */
121 bp = bp_marker = mpi_alloc_limb_space(bsize);
124 MPN_COPY(bp, rp, bsize);
127 /* RES and EXP are identical. Allocate temp. space for EXP. */
128 ep = ep_marker = mpi_alloc_limb_space(esize);
131 MPN_COPY(ep, rp, esize);
134 /* RES and MOD are identical. Allocate temporary space for MOD. */
136 mp = mp_marker = mpi_alloc_limb_space(msize);
139 MPN_COPY(mp, rp, msize);
143 MPN_COPY(rp, bp, bsize);
152 mpi_limb_t carry_limb;
153 struct karatsuba_ctx karactx;
155 xp = xp_marker = mpi_alloc_limb_space(2 * (msize + 1));
159 memset(&karactx, 0, sizeof karactx);
160 negative_result = (ep[0] & 1) && base->sign;
164 c = count_leading_zeros(e);
165 e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
166 c = BITS_PER_MPI_LIMB - 1 - c;
170 * Make the result be pointed to alternately by XP and RP. This
171 * helps us avoid block copying, which would otherwise be necessary
172 * with the overlap restrictions of mpihelp_divmod. With 50% probability
173 * the result after this loop will be in the area originally pointed
174 * by RP (==RES->d), and with 50% probability in the area originally
183 /*if (mpihelp_mul_n(xp, rp, rp, rsize) < 0) goto enomem */
184 if (rsize < KARATSUBA_THRESHOLD)
185 mpih_sqr_n_basecase(xp, rp, rsize);
190 mpi_alloc_limb_space(tsize);
193 } else if (tsize < (2 * rsize)) {
194 mpi_free_limb_space(tspace);
197 mpi_alloc_limb_space(tsize);
201 mpih_sqr_n(xp, rp, rsize, tspace);
206 mpihelp_divrem(xp + msize, 0, xp, xsize,
216 if ((mpi_limb_signed_t) e < 0) {
217 /*mpihelp_mul( xp, rp, rsize, bp, bsize ); */
218 if (bsize < KARATSUBA_THRESHOLD) {
221 (xp, rp, rsize, bp, bsize,
225 if (mpihelp_mul_karatsuba_case
226 (xp, rp, rsize, bp, bsize,
231 xsize = rsize + bsize;
233 mpihelp_divrem(xp + msize, 0,
253 c = BITS_PER_MPI_LIMB;
256 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
257 * steps. Adjust the result by reducing it with the original MOD.
259 * Also make sure the result is put in RES->d (where it already
260 * might be, see above).
264 mpihelp_lshift(res->d, rp, rsize, mod_shift_cnt);
267 rp[rsize] = carry_limb;
271 MPN_COPY(res->d, rp, rsize);
275 if (rsize >= msize) {
276 mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
280 /* Remove any leading zero words from the result. */
282 mpihelp_rshift(rp, rp, rsize, mod_shift_cnt);
283 MPN_NORMALIZE(rp, rsize);
285 mpihelp_release_karatsuba_ctx(&karactx);
288 if (negative_result && rsize) {
290 mpihelp_rshift(mp, mp, msize, mod_shift_cnt);
291 mpihelp_sub(rp, mp, msize, rp, rsize);
294 MPN_NORMALIZE(rp, rsize);
303 mpi_assign_limb_space(res, rp, size);
305 mpi_free_limb_space(mp_marker);
307 mpi_free_limb_space(bp_marker);
309 mpi_free_limb_space(ep_marker);
311 mpi_free_limb_space(xp_marker);
313 mpi_free_limb_space(tspace);
316 EXPORT_SYMBOL_GPL(mpi_powm);