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 struct karatsuba_ctx karactx = {};
28 mpi_ptr_t xp_marker = NULL;
29 mpi_ptr_t tspace = NULL;
30 mpi_ptr_t rp, ep, mp, bp;
31 mpi_size_t esize, msize, bsize, rsize;
32 int msign, bsign, rsign;
37 mpi_size_t tsize = 0; /* to avoid compiler warning */
38 /* fixme: we should check that the warning is void */
53 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
54 * depending on if MOD equals 1. */
55 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
57 if (mpi_resize(res, 1) < 0)
66 /* Normalize MOD (i.e. make its most significant bit set) as required by
67 * mpn_divrem. This will make the intermediate values in the calculation
68 * slightly larger, but the correct result is obtained after a final
69 * reduction using the original MOD value. */
70 mp = mp_marker = mpi_alloc_limb_space(msize);
73 mod_shift_cnt = count_leading_zeros(mod->d[msize - 1]);
75 mpihelp_lshift(mp, mod->d, msize, mod_shift_cnt);
77 MPN_COPY(mp, mod->d, msize);
81 if (bsize > msize) { /* The base is larger than the module. Reduce it. */
82 /* Allocate (BSIZE + 1) with space for remainder and quotient.
83 * (The quotient is (bsize - msize + 1) limbs.) */
84 bp = bp_marker = mpi_alloc_limb_space(bsize + 1);
87 MPN_COPY(bp, base->d, bsize);
88 /* We don't care about the quotient, store it above the remainder,
90 mpihelp_divrem(bp + msize, 0, bp, bsize, mp, msize);
92 /* Canonicalize the base, since we are going to multiply with it
93 * quite a few times. */
94 MPN_NORMALIZE(bp, bsize);
104 if (res->alloced < size) {
105 /* We have to allocate more space for RES. If any of the input
106 * parameters are identical to RES, defer deallocation of the old
108 if (rp == ep || rp == mp || rp == bp) {
109 rp = mpi_alloc_limb_space(size);
114 if (mpi_resize(res, size) < 0)
118 } else { /* Make BASE, EXP and MOD not overlap with RES. */
120 /* RES and BASE are identical. Allocate temp. space for BASE. */
122 bp = bp_marker = mpi_alloc_limb_space(bsize);
125 MPN_COPY(bp, rp, bsize);
128 /* RES and EXP are identical. Allocate temp. space for EXP. */
129 ep = ep_marker = mpi_alloc_limb_space(esize);
132 MPN_COPY(ep, rp, esize);
135 /* RES and MOD are identical. Allocate temporary space for MOD. */
137 mp = mp_marker = mpi_alloc_limb_space(msize);
140 MPN_COPY(mp, rp, msize);
144 MPN_COPY(rp, bp, bsize);
153 mpi_limb_t carry_limb;
155 xp = xp_marker = mpi_alloc_limb_space(2 * (msize + 1));
159 negative_result = (ep[0] & 1) && base->sign;
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;
169 * Make the result be pointed to alternately by XP and RP. This
170 * helps us avoid block copying, which would otherwise be necessary
171 * with the overlap restrictions of mpihelp_divmod. With 50% probability
172 * the result after this loop will be in the area originally pointed
173 * by RP (==RES->d), and with 50% probability in the area originally
182 /*if (mpihelp_mul_n(xp, rp, rp, rsize) < 0) goto enomem */
183 if (rsize < KARATSUBA_THRESHOLD)
184 mpih_sqr_n_basecase(xp, rp, rsize);
189 mpi_alloc_limb_space(tsize);
192 } else if (tsize < (2 * rsize)) {
193 mpi_free_limb_space(tspace);
196 mpi_alloc_limb_space(tsize);
200 mpih_sqr_n(xp, rp, rsize, tspace);
205 mpihelp_divrem(xp + msize, 0, xp, xsize,
215 if ((mpi_limb_signed_t) e < 0) {
216 /*mpihelp_mul( xp, rp, rsize, bp, bsize ); */
217 if (bsize < KARATSUBA_THRESHOLD) {
220 (xp, rp, rsize, bp, bsize,
224 if (mpihelp_mul_karatsuba_case
225 (xp, rp, rsize, bp, bsize,
230 xsize = rsize + bsize;
232 mpihelp_divrem(xp + msize, 0,
252 c = BITS_PER_MPI_LIMB;
255 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
256 * steps. Adjust the result by reducing it with the original MOD.
258 * Also make sure the result is put in RES->d (where it already
259 * might be, see above).
263 mpihelp_lshift(res->d, rp, rsize, mod_shift_cnt);
266 rp[rsize] = carry_limb;
270 MPN_COPY(res->d, rp, rsize);
274 if (rsize >= msize) {
275 mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
279 /* Remove any leading zero words from the result. */
281 mpihelp_rshift(rp, rp, rsize, mod_shift_cnt);
282 MPN_NORMALIZE(rp, rsize);
285 if (negative_result && rsize) {
287 mpihelp_rshift(mp, mp, msize, mod_shift_cnt);
288 mpihelp_sub(rp, mp, msize, rp, rsize);
291 MPN_NORMALIZE(rp, rsize);
299 mpihelp_release_karatsuba_ctx(&karactx);
301 mpi_assign_limb_space(res, rp, size);
303 mpi_free_limb_space(mp_marker);
305 mpi_free_limb_space(bp_marker);
307 mpi_free_limb_space(ep_marker);
309 mpi_free_limb_space(xp_marker);
311 mpi_free_limb_space(tspace);
314 EXPORT_SYMBOL_GPL(mpi_powm);