blob: 82b19e4f118999f070f40dba0a356ed0c8070cdd [file] [log] [blame]
Thomas Gleixner465ae832019-05-20 19:07:51 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +03002/* 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 *
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +03007 * 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
Eric Biggers1d9ddde12017-11-07 14:15:27 -080016#include <linux/sched.h>
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030017#include <linux/string.h>
18#include "mpi-internal.h"
19#include "longlong.h"
20
21/****************
22 * RES = BASE ^ EXP mod MOD
23 */
24int mpi_powm(MPI res, MPI base, MPI exp, MPI mod)
25{
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;
Colin Ian King6122bbb2018-07-30 09:59:36 +010031 int msign, bsign, rsign;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030032 mpi_size_t size;
33 int mod_shift_cnt;
34 int negative_result;
35 int assign_rp = 0;
36 mpi_size_t tsize = 0; /* to avoid compiler warning */
37 /* fixme: we should check that the warning is void */
38 int rc = -ENOMEM;
39
40 esize = exp->nlimbs;
41 msize = mod->nlimbs;
42 size = 2 * msize;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030043 msign = mod->sign;
44
45 rp = res->d;
46 ep = exp->d;
47
48 if (!msize)
Dmitry Kasatkine87c5e32012-01-26 19:13:21 +020049 return -EINVAL;
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030050
51 if (!esize) {
52 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
53 * depending on if MOD equals 1. */
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030054 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
Andrey Ryabininf5527ff2016-11-24 13:23:10 +000055 if (res->nlimbs) {
56 if (mpi_resize(res, 1) < 0)
57 goto enomem;
58 rp = res->d;
59 rp[0] = 1;
60 }
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030061 res->sign = 0;
62 goto leave;
63 }
64
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);
70 if (!mp)
71 goto enomem;
David Howellsaacf29b2012-09-13 13:09:33 +010072 mod_shift_cnt = count_leading_zeros(mod->d[msize - 1]);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +030073 if (mod_shift_cnt)
74 mpihelp_lshift(mp, mod->d, msize, mod_shift_cnt);
75 else
76 MPN_COPY(mp, mod->d, msize);
77
78 bsize = base->nlimbs;
79 bsign = base->sign;
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);
84 if (!bp)
85 goto enomem;
86 MPN_COPY(bp, base->d, bsize);
87 /* We don't care about the quotient, store it above the remainder,
88 * at BP + MSIZE. */
89 mpihelp_divrem(bp + msize, 0, bp, bsize, mp, msize);
90 bsize = msize;
91 /* Canonicalize the base, since we are going to multiply with it
92 * quite a few times. */
93 MPN_NORMALIZE(bp, bsize);
94 } else
95 bp = base->d;
96
97 if (!bsize) {
98 res->nlimbs = 0;
99 res->sign = 0;
100 goto leave;
101 }
102
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
106 * space. */
107 if (rp == ep || rp == mp || rp == bp) {
108 rp = mpi_alloc_limb_space(size);
109 if (!rp)
110 goto enomem;
111 assign_rp = 1;
112 } else {
113 if (mpi_resize(res, size) < 0)
114 goto enomem;
115 rp = res->d;
116 }
117 } else { /* Make BASE, EXP and MOD not overlap with RES. */
118 if (rp == bp) {
119 /* RES and BASE are identical. Allocate temp. space for BASE. */
120 BUG_ON(bp_marker);
121 bp = bp_marker = mpi_alloc_limb_space(bsize);
122 if (!bp)
123 goto enomem;
124 MPN_COPY(bp, rp, bsize);
125 }
126 if (rp == ep) {
127 /* RES and EXP are identical. Allocate temp. space for EXP. */
128 ep = ep_marker = mpi_alloc_limb_space(esize);
129 if (!ep)
130 goto enomem;
131 MPN_COPY(ep, rp, esize);
132 }
133 if (rp == mp) {
134 /* RES and MOD are identical. Allocate temporary space for MOD. */
135 BUG_ON(mp_marker);
136 mp = mp_marker = mpi_alloc_limb_space(msize);
137 if (!mp)
138 goto enomem;
139 MPN_COPY(mp, rp, msize);
140 }
141 }
142
143 MPN_COPY(rp, bp, bsize);
144 rsize = bsize;
145 rsign = bsign;
146
147 {
148 mpi_size_t i;
149 mpi_ptr_t xp;
150 int c;
151 mpi_limb_t e;
152 mpi_limb_t carry_limb;
153 struct karatsuba_ctx karactx;
154
155 xp = xp_marker = mpi_alloc_limb_space(2 * (msize + 1));
156 if (!xp)
157 goto enomem;
158
159 memset(&karactx, 0, sizeof karactx);
160 negative_result = (ep[0] & 1) && base->sign;
161
162 i = esize - 1;
163 e = ep[i];
David Howellsaacf29b2012-09-13 13:09:33 +0100164 c = count_leading_zeros(e);
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300165 e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
166 c = BITS_PER_MPI_LIMB - 1 - c;
167
168 /* Main loop.
169 *
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
175 * pointed to by XP.
176 */
177
178 for (;;) {
179 while (c) {
180 mpi_ptr_t tp;
181 mpi_size_t xsize;
182
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);
186 else {
187 if (!tspace) {
188 tsize = 2 * rsize;
189 tspace =
190 mpi_alloc_limb_space(tsize);
191 if (!tspace)
192 goto enomem;
193 } else if (tsize < (2 * rsize)) {
194 mpi_free_limb_space(tspace);
195 tsize = 2 * rsize;
196 tspace =
197 mpi_alloc_limb_space(tsize);
198 if (!tspace)
199 goto enomem;
200 }
201 mpih_sqr_n(xp, rp, rsize, tspace);
202 }
203
204 xsize = 2 * rsize;
205 if (xsize > msize) {
206 mpihelp_divrem(xp + msize, 0, xp, xsize,
207 mp, msize);
208 xsize = msize;
209 }
210
211 tp = rp;
212 rp = xp;
213 xp = tp;
214 rsize = xsize;
215
216 if ((mpi_limb_signed_t) e < 0) {
217 /*mpihelp_mul( xp, rp, rsize, bp, bsize ); */
218 if (bsize < KARATSUBA_THRESHOLD) {
219 mpi_limb_t tmp;
220 if (mpihelp_mul
221 (xp, rp, rsize, bp, bsize,
222 &tmp) < 0)
223 goto enomem;
224 } else {
225 if (mpihelp_mul_karatsuba_case
226 (xp, rp, rsize, bp, bsize,
227 &karactx) < 0)
228 goto enomem;
229 }
230
231 xsize = rsize + bsize;
232 if (xsize > msize) {
233 mpihelp_divrem(xp + msize, 0,
234 xp, xsize, mp,
235 msize);
236 xsize = msize;
237 }
238
239 tp = rp;
240 rp = xp;
241 xp = tp;
242 rsize = xsize;
243 }
244 e <<= 1;
245 c--;
Eric Biggers1d9ddde12017-11-07 14:15:27 -0800246 cond_resched();
Dmitry Kasatkincdec9cb2011-08-31 14:05:16 +0300247 }
248
249 i--;
250 if (i < 0)
251 break;
252 e = ep[i];
253 c = BITS_PER_MPI_LIMB;
254 }
255
256 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
257 * steps. Adjust the result by reducing it with the original MOD.
258 *
259 * Also make sure the result is put in RES->d (where it already
260 * might be, see above).
261 */
262 if (mod_shift_cnt) {
263 carry_limb =
264 mpihelp_lshift(res->d, rp, rsize, mod_shift_cnt);
265 rp = res->d;
266 if (carry_limb) {
267 rp[rsize] = carry_limb;
268 rsize++;
269 }
270 } else {
271 MPN_COPY(res->d, rp, rsize);
272 rp = res->d;
273 }
274
275 if (rsize >= msize) {
276 mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
277 rsize = msize;
278 }
279
280 /* Remove any leading zero words from the result. */
281 if (mod_shift_cnt)
282 mpihelp_rshift(rp, rp, rsize, mod_shift_cnt);
283 MPN_NORMALIZE(rp, rsize);
284
285 mpihelp_release_karatsuba_ctx(&karactx);
286 }
287
288 if (negative_result && rsize) {
289 if (mod_shift_cnt)
290 mpihelp_rshift(mp, mp, msize, mod_shift_cnt);
291 mpihelp_sub(rp, mp, msize, rp, rsize);
292 rsize = msize;
293 rsign = msign;
294 MPN_NORMALIZE(rp, rsize);
295 }
296 res->nlimbs = rsize;
297 res->sign = rsign;
298
299leave:
300 rc = 0;
301enomem:
302 if (assign_rp)
303 mpi_assign_limb_space(res, rp, size);
304 if (mp_marker)
305 mpi_free_limb_space(mp_marker);
306 if (bp_marker)
307 mpi_free_limb_space(bp_marker);
308 if (ep_marker)
309 mpi_free_limb_space(ep_marker);
310 if (xp_marker)
311 mpi_free_limb_space(xp_marker);
312 if (tspace)
313 mpi_free_limb_space(tspace);
314 return rc;
315}
316EXPORT_SYMBOL_GPL(mpi_powm);