| 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 |  | 
|---|
| 16 | #include <linux/export.h> | 
|---|
| 17 | #include <linux/sched.h> | 
|---|
| 18 | #include <linux/string.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include "mpi-internal.h" | 
|---|
| 21 | #include "longlong.h" | 
|---|
| 22 |  | 
|---|
| 23 | /**************** | 
|---|
| 24 | * RES = BASE ^ EXP mod MOD | 
|---|
| 25 | */ | 
|---|
| 26 | int mpi_powm(MPI res, MPI base, MPI exp, MPI mod) | 
|---|
| 27 | { | 
|---|
| 28 | mpi_ptr_t mp_marker = NULL, bp_marker = NULL, ep_marker = NULL; | 
|---|
| 29 | struct karatsuba_ctx karactx = {}; | 
|---|
| 30 | mpi_ptr_t xp_marker = NULL; | 
|---|
| 31 | mpi_ptr_t tspace = NULL; | 
|---|
| 32 | mpi_ptr_t rp, ep, mp, bp; | 
|---|
| 33 | mpi_size_t esize, msize, bsize, rsize; | 
|---|
| 34 | int msign, bsign, rsign; | 
|---|
| 35 | mpi_size_t size; | 
|---|
| 36 | int mod_shift_cnt; | 
|---|
| 37 | int negative_result; | 
|---|
| 38 | int assign_rp = 0; | 
|---|
| 39 | mpi_size_t tsize = 0;	/* to avoid compiler warning */ | 
|---|
| 40 | /* fixme: we should check that the warning is void */ | 
|---|
| 41 | int rc = -ENOMEM; | 
|---|
| 42 |  | 
|---|
| 43 | esize = exp->nlimbs; | 
|---|
| 44 | msize = mod->nlimbs; | 
|---|
| 45 | size = 2 * msize; | 
|---|
| 46 | msign = mod->sign; | 
|---|
| 47 |  | 
|---|
| 48 | rp = res->d; | 
|---|
| 49 | ep = exp->d; | 
|---|
| 50 |  | 
|---|
| 51 | if (!msize) | 
|---|
| 52 | return -EINVAL; | 
|---|
| 53 |  | 
|---|
| 54 | if (!esize) { | 
|---|
| 55 | /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0 | 
|---|
| 56 | * depending on if MOD equals 1.  */ | 
|---|
| 57 | res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1; | 
|---|
| 58 | if (res->nlimbs) { | 
|---|
| 59 | if (mpi_resize(a: res, nlimbs: 1) < 0) | 
|---|
| 60 | goto enomem; | 
|---|
| 61 | rp = res->d; | 
|---|
| 62 | rp[0] = 1; | 
|---|
| 63 | } | 
|---|
| 64 | res->sign = 0; | 
|---|
| 65 | goto leave; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | /* Normalize MOD (i.e. make its most significant bit set) as required by | 
|---|
| 69 | * mpn_divrem.  This will make the intermediate values in the calculation | 
|---|
| 70 | * slightly larger, but the correct result is obtained after a final | 
|---|
| 71 | * reduction using the original MOD value.  */ | 
|---|
| 72 | mp = mp_marker = mpi_alloc_limb_space(nlimbs: msize); | 
|---|
| 73 | if (!mp) | 
|---|
| 74 | goto enomem; | 
|---|
| 75 | mod_shift_cnt = count_leading_zeros(x: mod->d[msize - 1]); | 
|---|
| 76 | if (mod_shift_cnt) | 
|---|
| 77 | mpihelp_lshift(wp: mp, up: mod->d, usize: msize, cnt: mod_shift_cnt); | 
|---|
| 78 | else | 
|---|
| 79 | MPN_COPY(mp, mod->d, msize); | 
|---|
| 80 |  | 
|---|
| 81 | bsize = base->nlimbs; | 
|---|
| 82 | bsign = base->sign; | 
|---|
| 83 | if (bsize > msize) {	/* The base is larger than the module. Reduce it. */ | 
|---|
| 84 | /* Allocate (BSIZE + 1) with space for remainder and quotient. | 
|---|
| 85 | * (The quotient is (bsize - msize + 1) limbs.)  */ | 
|---|
| 86 | bp = bp_marker = mpi_alloc_limb_space(nlimbs: bsize + 1); | 
|---|
| 87 | if (!bp) | 
|---|
| 88 | goto enomem; | 
|---|
| 89 | MPN_COPY(bp, base->d, bsize); | 
|---|
| 90 | /* We don't care about the quotient, store it above the remainder, | 
|---|
| 91 | * at BP + MSIZE.  */ | 
|---|
| 92 | mpihelp_divrem(qp: bp + msize, qextra_limbs: 0, np: bp, nsize: bsize, dp: mp, dsize: msize); | 
|---|
| 93 | bsize = msize; | 
|---|
| 94 | /* Canonicalize the base, since we are going to multiply with it | 
|---|
| 95 | * quite a few times.  */ | 
|---|
| 96 | MPN_NORMALIZE(bp, bsize); | 
|---|
| 97 | } else | 
|---|
| 98 | bp = base->d; | 
|---|
| 99 |  | 
|---|
| 100 | if (!bsize) { | 
|---|
| 101 | res->nlimbs = 0; | 
|---|
| 102 | res->sign = 0; | 
|---|
| 103 | goto leave; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | if (res->alloced < size) { | 
|---|
| 107 | /* We have to allocate more space for RES.  If any of the input | 
|---|
| 108 | * parameters are identical to RES, defer deallocation of the old | 
|---|
| 109 | * space.  */ | 
|---|
| 110 | if (rp == ep || rp == mp || rp == bp) { | 
|---|
| 111 | rp = mpi_alloc_limb_space(nlimbs: size); | 
|---|
| 112 | if (!rp) | 
|---|
| 113 | goto enomem; | 
|---|
| 114 | assign_rp = 1; | 
|---|
| 115 | } else { | 
|---|
| 116 | if (mpi_resize(a: res, nlimbs: size) < 0) | 
|---|
| 117 | goto enomem; | 
|---|
| 118 | rp = res->d; | 
|---|
| 119 | } | 
|---|
| 120 | } else {		/* Make BASE, EXP and MOD not overlap with RES.  */ | 
|---|
| 121 | if (rp == bp) { | 
|---|
| 122 | /* RES and BASE are identical.  Allocate temp. space for BASE.  */ | 
|---|
| 123 | BUG_ON(bp_marker); | 
|---|
| 124 | bp = bp_marker = mpi_alloc_limb_space(nlimbs: bsize); | 
|---|
| 125 | if (!bp) | 
|---|
| 126 | goto enomem; | 
|---|
| 127 | MPN_COPY(bp, rp, bsize); | 
|---|
| 128 | } | 
|---|
| 129 | if (rp == ep) { | 
|---|
| 130 | /* RES and EXP are identical.  Allocate temp. space for EXP.  */ | 
|---|
| 131 | ep = ep_marker = mpi_alloc_limb_space(nlimbs: esize); | 
|---|
| 132 | if (!ep) | 
|---|
| 133 | goto enomem; | 
|---|
| 134 | MPN_COPY(ep, rp, esize); | 
|---|
| 135 | } | 
|---|
| 136 | if (rp == mp) { | 
|---|
| 137 | /* RES and MOD are identical.  Allocate temporary space for MOD. */ | 
|---|
| 138 | BUG_ON(mp_marker); | 
|---|
| 139 | mp = mp_marker = mpi_alloc_limb_space(nlimbs: msize); | 
|---|
| 140 | if (!mp) | 
|---|
| 141 | goto enomem; | 
|---|
| 142 | MPN_COPY(mp, rp, msize); | 
|---|
| 143 | } | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | MPN_COPY(rp, bp, bsize); | 
|---|
| 147 | rsize = bsize; | 
|---|
| 148 | rsign = bsign; | 
|---|
| 149 |  | 
|---|
| 150 | { | 
|---|
| 151 | mpi_size_t i; | 
|---|
| 152 | mpi_ptr_t xp; | 
|---|
| 153 | int c; | 
|---|
| 154 | mpi_limb_t e; | 
|---|
| 155 | mpi_limb_t carry_limb; | 
|---|
| 156 |  | 
|---|
| 157 | xp = xp_marker = mpi_alloc_limb_space(nlimbs: 2 * (msize + 1)); | 
|---|
| 158 | if (!xp) | 
|---|
| 159 | goto enomem; | 
|---|
| 160 |  | 
|---|
| 161 | negative_result = (ep[0] & 1) && base->sign; | 
|---|
| 162 |  | 
|---|
| 163 | i = esize - 1; | 
|---|
| 164 | e = ep[i]; | 
|---|
| 165 | c = count_leading_zeros(x: e); | 
|---|
| 166 | e = (e << c) << 1;	/* shift the exp bits to the left, lose msb */ | 
|---|
| 167 | c = BITS_PER_MPI_LIMB - 1 - c; | 
|---|
| 168 |  | 
|---|
| 169 | /* Main loop. | 
|---|
| 170 | * | 
|---|
| 171 | * Make the result be pointed to alternately by XP and RP.  This | 
|---|
| 172 | * helps us avoid block copying, which would otherwise be necessary | 
|---|
| 173 | * with the overlap restrictions of mpihelp_divmod. With 50% probability | 
|---|
| 174 | * the result after this loop will be in the area originally pointed | 
|---|
| 175 | * by RP (==RES->d), and with 50% probability in the area originally | 
|---|
| 176 | * pointed to by XP. | 
|---|
| 177 | */ | 
|---|
| 178 |  | 
|---|
| 179 | for (;;) { | 
|---|
| 180 | while (c) { | 
|---|
| 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(prodp: xp, up: rp, size: rsize); | 
|---|
| 186 | else { | 
|---|
| 187 | if (!tspace) { | 
|---|
| 188 | tsize = 2 * rsize; | 
|---|
| 189 | tspace = | 
|---|
| 190 | mpi_alloc_limb_space(nlimbs: tsize); | 
|---|
| 191 | if (!tspace) | 
|---|
| 192 | goto enomem; | 
|---|
| 193 | } else if (tsize < (2 * rsize)) { | 
|---|
| 194 | mpi_free_limb_space(a: tspace); | 
|---|
| 195 | tsize = 2 * rsize; | 
|---|
| 196 | tspace = | 
|---|
| 197 | mpi_alloc_limb_space(nlimbs: tsize); | 
|---|
| 198 | if (!tspace) | 
|---|
| 199 | goto enomem; | 
|---|
| 200 | } | 
|---|
| 201 | mpih_sqr_n(prodp: xp, up: rp, size: rsize, tspace); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | xsize = 2 * rsize; | 
|---|
| 205 | if (xsize > msize) { | 
|---|
| 206 | mpihelp_divrem(qp: xp + msize, qextra_limbs: 0, np: xp, nsize: xsize, | 
|---|
| 207 | dp: mp, dsize: msize); | 
|---|
| 208 | xsize = msize; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | swap(rp, xp); | 
|---|
| 212 | rsize = xsize; | 
|---|
| 213 |  | 
|---|
| 214 | if ((mpi_limb_signed_t) e < 0) { | 
|---|
| 215 | /*mpihelp_mul( xp, rp, rsize, bp, bsize ); */ | 
|---|
| 216 | if (bsize < KARATSUBA_THRESHOLD) { | 
|---|
| 217 | mpi_limb_t tmp; | 
|---|
| 218 | if (mpihelp_mul | 
|---|
| 219 | (prodp: xp, up: rp, usize: rsize, vp: bp, vsize: bsize, | 
|---|
| 220 | result: &tmp) < 0) | 
|---|
| 221 | goto enomem; | 
|---|
| 222 | } else { | 
|---|
| 223 | if (mpihelp_mul_karatsuba_case | 
|---|
| 224 | (prodp: xp, up: rp, usize: rsize, vp: bp, vsize: bsize, | 
|---|
| 225 | ctx: &karactx) < 0) | 
|---|
| 226 | goto enomem; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | xsize = rsize + bsize; | 
|---|
| 230 | if (xsize > msize) { | 
|---|
| 231 | mpihelp_divrem(qp: xp + msize, qextra_limbs: 0, | 
|---|
| 232 | np: xp, nsize: xsize, dp: mp, | 
|---|
| 233 | dsize: msize); | 
|---|
| 234 | xsize = msize; | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | swap(rp, xp); | 
|---|
| 238 | rsize = xsize; | 
|---|
| 239 | } | 
|---|
| 240 | e <<= 1; | 
|---|
| 241 | c--; | 
|---|
| 242 | cond_resched(); | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | i--; | 
|---|
| 246 | if (i < 0) | 
|---|
| 247 | break; | 
|---|
| 248 | e = ep[i]; | 
|---|
| 249 | c = BITS_PER_MPI_LIMB; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT | 
|---|
| 253 | * steps.  Adjust the result by reducing it with the original MOD. | 
|---|
| 254 | * | 
|---|
| 255 | * Also make sure the result is put in RES->d (where it already | 
|---|
| 256 | * might be, see above). | 
|---|
| 257 | */ | 
|---|
| 258 | if (mod_shift_cnt) { | 
|---|
| 259 | carry_limb = | 
|---|
| 260 | mpihelp_lshift(wp: res->d, up: rp, usize: rsize, cnt: mod_shift_cnt); | 
|---|
| 261 | rp = res->d; | 
|---|
| 262 | if (carry_limb) { | 
|---|
| 263 | rp[rsize] = carry_limb; | 
|---|
| 264 | rsize++; | 
|---|
| 265 | } | 
|---|
| 266 | } else { | 
|---|
| 267 | MPN_COPY(res->d, rp, rsize); | 
|---|
| 268 | rp = res->d; | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | if (rsize >= msize) { | 
|---|
| 272 | mpihelp_divrem(qp: rp + msize, qextra_limbs: 0, np: rp, nsize: rsize, dp: mp, dsize: msize); | 
|---|
| 273 | rsize = msize; | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | /* Remove any leading zero words from the result.  */ | 
|---|
| 277 | if (mod_shift_cnt) | 
|---|
| 278 | mpihelp_rshift(wp: rp, up: rp, usize: rsize, cnt: mod_shift_cnt); | 
|---|
| 279 | MPN_NORMALIZE(rp, rsize); | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | if (negative_result && rsize) { | 
|---|
| 283 | if (mod_shift_cnt) | 
|---|
| 284 | mpihelp_rshift(wp: mp, up: mp, usize: msize, cnt: mod_shift_cnt); | 
|---|
| 285 | mpihelp_sub(res_ptr: rp, s1_ptr: mp, s1_size: msize, s2_ptr: rp, s2_size: rsize); | 
|---|
| 286 | rsize = msize; | 
|---|
| 287 | rsign = msign; | 
|---|
| 288 | MPN_NORMALIZE(rp, rsize); | 
|---|
| 289 | } | 
|---|
| 290 | res->nlimbs = rsize; | 
|---|
| 291 | res->sign = rsign; | 
|---|
| 292 |  | 
|---|
| 293 | leave: | 
|---|
| 294 | rc = 0; | 
|---|
| 295 | enomem: | 
|---|
| 296 | mpihelp_release_karatsuba_ctx(ctx: &karactx); | 
|---|
| 297 | if (assign_rp) | 
|---|
| 298 | mpi_assign_limb_space(a: res, ap: rp, nlimbs: size); | 
|---|
| 299 | if (mp_marker) | 
|---|
| 300 | mpi_free_limb_space(a: mp_marker); | 
|---|
| 301 | if (bp_marker) | 
|---|
| 302 | mpi_free_limb_space(a: bp_marker); | 
|---|
| 303 | if (ep_marker) | 
|---|
| 304 | mpi_free_limb_space(a: ep_marker); | 
|---|
| 305 | if (xp_marker) | 
|---|
| 306 | mpi_free_limb_space(a: xp_marker); | 
|---|
| 307 | if (tspace) | 
|---|
| 308 | mpi_free_limb_space(a: tspace); | 
|---|
| 309 | return rc; | 
|---|
| 310 | } | 
|---|
| 311 | EXPORT_SYMBOL_GPL(mpi_powm); | 
|---|
| 312 |  | 
|---|