| 1 |
/* mpi-pow.c - MPI functions for exponentiation |
| 2 |
* Copyright (C) 1994, 1996, 1998, 2000, 2002 |
| 3 |
* 2003 Free Software Foundation, Inc. |
| 4 |
* 2013 g10 Code GmbH |
| 5 |
* |
| 6 |
* This file is part of Libgcrypt. |
| 7 |
* |
| 8 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as |
| 10 |
* published by the Free Software Foundation; either version 2.1 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
* Note: This code is heavily based on the GNU MP Library. |
| 22 |
* Actually it's the same code with only minor changes in the |
| 23 |
* way the data is stored; this is to support the abstraction |
| 24 |
* of an optional secure memory allocation which may be used |
| 25 |
* to avoid revealing of sensitive data due to paging etc. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <config.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "mpi-internal.h" |
| 34 |
#include "longlong.h" |
| 35 |
|
| 36 |
|
| 37 |
#ifndef USE_ALGORITHM_LLI_EXPONENTIATION |
| 38 |
/* |
| 39 |
* When you don't need least-leak implementation, please add compilation option |
| 40 |
* -DUSE_ALGORITHM_LLI_EXPONENTIATION=0 |
| 41 |
* |
| 42 |
* For performance (by tests/benchmark rsa), it's comparable to leaky |
| 43 |
* sliding window implementation on 64-bit architecture. On 32-bit |
| 44 |
* architecture, it's slower with 3072-bit and 4096-bit. In future, |
| 45 |
* it's good to have non-leaky Karatsuba multiplication, then, it's |
| 46 |
* possible to use this for all architectures. |
| 47 |
* |
| 48 |
*/ |
| 49 |
#define USE_ALGORITHM_LLI_EXPONENTIATION (BITS_PER_MPI_LIMB >= 64) |
| 50 |
#endif |
| 51 |
|
| 52 |
/* |
| 53 |
* When you need old implementation, please add compilation option |
| 54 |
* -DUSE_ALGORITHM_SIMPLE_EXPONENTIATION |
| 55 |
* or expose this line: |
| 56 |
#define USE_ALGORITHM_SIMPLE_EXPONENTIATION 1 |
| 57 |
*/ |
| 58 |
|
| 59 |
const char * |
| 60 |
_gcry_mpi_get_powm_config (void) |
| 61 |
{ |
| 62 |
#if USE_ALGORITHM_LLI_EXPONENTIATION |
| 63 |
return "fixed-window"; |
| 64 |
#else |
| 65 |
return "sliding-window"; |
| 66 |
#endif |
| 67 |
} |
| 68 |
|
| 69 |
#if defined(USE_ALGORITHM_SIMPLE_EXPONENTIATION) |
| 70 |
/**************** |
| 71 |
* RES = BASE ^ EXPO mod MOD |
| 72 |
*/ |
| 73 |
void |
| 74 |
_gcry_mpi_powm (gcry_mpi_t res, |
| 75 |
gcry_mpi_t base, gcry_mpi_t expo, gcry_mpi_t mod) |
| 76 |
{ |
| 77 |
/* Pointer to the limbs of the arguments, their size and signs. */ |
| 78 |
mpi_ptr_t rp, ep, mp, bp; |
| 79 |
mpi_size_t esize, msize, bsize, rsize; |
| 80 |
int msign, bsign, rsign; |
| 81 |
/* Flags telling the secure allocation status of the arguments. */ |
| 82 |
int esec, msec, bsec; |
| 83 |
/* Size of the result including space for temporary values. */ |
| 84 |
mpi_size_t size; |
| 85 |
/* Helper. */ |
| 86 |
int mod_shift_cnt; |
| 87 |
int negative_result; |
| 88 |
mpi_ptr_t mp_marker = NULL; |
| 89 |
mpi_ptr_t bp_marker = NULL; |
| 90 |
mpi_ptr_t ep_marker = NULL; |
| 91 |
mpi_ptr_t xp_marker = NULL; |
| 92 |
unsigned int mp_nlimbs = 0; |
| 93 |
unsigned int bp_nlimbs = 0; |
| 94 |
unsigned int ep_nlimbs = 0; |
| 95 |
unsigned int xp_nlimbs = 0; |
| 96 |
mpi_ptr_t tspace = NULL; |
| 97 |
mpi_size_t tsize = 0; |
| 98 |
|
| 99 |
|
| 100 |
esize = expo->nlimbs; |
| 101 |
msize = mod->nlimbs; |
| 102 |
size = 2 * msize; |
| 103 |
msign = mod->sign; |
| 104 |
|
| 105 |
esec = mpi_is_secure(expo); |
| 106 |
msec = mpi_is_secure(mod); |
| 107 |
bsec = mpi_is_secure(base); |
| 108 |
|
| 109 |
rp = res->d; |
| 110 |
ep = expo->d; |
| 111 |
MPN_NORMALIZE(ep, esize); |
| 112 |
|
| 113 |
if (!msize) |
| 114 |
_gcry_divide_by_zero(); |
| 115 |
|
| 116 |
if (!esize) |
| 117 |
{ |
| 118 |
/* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0 depending |
| 119 |
on if MOD equals 1. */ |
| 120 |
res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1; |
| 121 |
if (res->nlimbs) |
| 122 |
{ |
| 123 |
RESIZE_IF_NEEDED (res, 1); |
| 124 |
rp = res->d; |
| 125 |
rp[0] = 1; |
| 126 |
} |
| 127 |
res->sign = 0; |
| 128 |
goto leave; |
| 129 |
} |
| 130 |
|
| 131 |
/* Normalize MOD (i.e. make its most significant bit set) as |
| 132 |
required by mpn_divrem. This will make the intermediate values |
| 133 |
in the calculation slightly larger, but the correct result is |
| 134 |
obtained after a final reduction using the original MOD value. */ |
| 135 |
mp_nlimbs = msec? msize:0; |
| 136 |
mp = mp_marker = mpi_alloc_limb_space(msize, msec); |
| 137 |
count_leading_zeros (mod_shift_cnt, mod->d[msize-1]); |
| 138 |
if (mod_shift_cnt) |
| 139 |
_gcry_mpih_lshift (mp, mod->d, msize, mod_shift_cnt); |
| 140 |
else |
| 141 |
MPN_COPY( mp, mod->d, msize ); |
| 142 |
|
| 143 |
bsize = base->nlimbs; |
| 144 |
bsign = base->sign; |
| 145 |
if (bsize > msize) |
| 146 |
{ |
| 147 |
/* The base is larger than the module. Reduce it. |
| 148 |
|
| 149 |
Allocate (BSIZE + 1) with space for remainder and quotient. |
| 150 |
(The quotient is (bsize - msize + 1) limbs.) */ |
| 151 |
bp_nlimbs = bsec ? (bsize + 1):0; |
| 152 |
bp = bp_marker = mpi_alloc_limb_space( bsize + 1, bsec ); |
| 153 |
MPN_COPY ( bp, base->d, bsize ); |
| 154 |
/* We don't care about the quotient, store it above the |
| 155 |
* remainder, at BP + MSIZE. */ |
| 156 |
_gcry_mpih_divrem( bp + msize, 0, bp, bsize, mp, msize ); |
| 157 |
bsize = msize; |
| 158 |
/* Canonicalize the base, since we are going to multiply with it |
| 159 |
quite a few times. */ |
| 160 |
MPN_NORMALIZE( bp, bsize ); |
| 161 |
} |
| 162 |
else |
| 163 |
bp = base->d; |
| 164 |
|
| 165 |
if (!bsize) |
| 166 |
{ |
| 167 |
res->nlimbs = 0; |
| 168 |
res->sign = 0; |
| 169 |
goto leave; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/* Make BASE, EXPO and MOD not overlap with RES. */ |
| 174 |
if ( rp == bp ) |
| 175 |
{ |
| 176 |
/* RES and BASE are identical. Allocate temp. space for BASE. */ |
| 177 |
gcry_assert (!bp_marker); |
| 178 |
bp_nlimbs = bsec? bsize:0; |
| 179 |
bp = bp_marker = mpi_alloc_limb_space( bsize, bsec ); |
| 180 |
MPN_COPY(bp, rp, bsize); |
| 181 |
} |
| 182 |
if ( rp == ep ) |
| 183 |
{ |
| 184 |
/* RES and EXPO are identical. Allocate temp. space for EXPO. */ |
| 185 |
ep_nlimbs = esec? esize:0; |
| 186 |
ep = ep_marker = mpi_alloc_limb_space( esize, esec ); |
| 187 |
MPN_COPY(ep, rp, esize); |
| 188 |
} |
| 189 |
if ( rp == mp ) |
| 190 |
{ |
| 191 |
/* RES and MOD are identical. Allocate temporary space for MOD.*/ |
| 192 |
gcry_assert (!mp_marker); |
| 193 |
mp_nlimbs = msec?msize:0; |
| 194 |
mp = mp_marker = mpi_alloc_limb_space( msize, msec ); |
| 195 |
MPN_COPY(mp, rp, msize); |
| 196 |
} |
| 197 |
|
| 198 |
/* Copy base to the result. */ |
| 199 |
if (res->alloced < size) |
| 200 |
{ |
| 201 |
mpi_resize (res, size); |
| 202 |
rp = res->d; |
| 203 |
} |
| 204 |
MPN_COPY ( rp, bp, bsize ); |
| 205 |
rsize = bsize; |
| 206 |
rsign = 0; |
| 207 |
|
| 208 |
/* Main processing. */ |
| 209 |
{ |
| 210 |
mpi_size_t i; |
| 211 |
mpi_ptr_t xp; |
| 212 |
int c; |
| 213 |
mpi_limb_t e; |
| 214 |
mpi_limb_t carry_limb; |
| 215 |
struct karatsuba_ctx karactx; |
| 216 |
struct gcry_mpi w, u; |
| 217 |
|
| 218 |
xp_nlimbs = msec? size:0; |
| 219 |
xp = xp_marker = mpi_alloc_limb_space( size, msec ); |
| 220 |
|
| 221 |
w.sign = u.sign = 0; |
| 222 |
w.flags = u.flags = 0; |
| 223 |
w.alloced = w.nlimbs = size; /* RES->alloc may be longer. */ |
| 224 |
u.alloced = u.nlimbs = size; |
| 225 |
|
| 226 |
memset( &karactx, 0, sizeof karactx ); |
| 227 |
negative_result = (ep[0] & 1) && bsign; |
| 228 |
|
| 229 |
i = esize - 1; |
| 230 |
e = ep[i]; |
| 231 |
count_leading_zeros (c, e); |
| 232 |
e = (e << c) << 1; /* Shift the expo bits to the left, lose msb. */ |
| 233 |
c = BITS_PER_MPI_LIMB - 1 - c; |
| 234 |
|
| 235 |
/* Main loop. |
| 236 |
|
| 237 |
Make the result be pointed to alternately by XP and RP. This |
| 238 |
helps us avoid block copying, which would otherwise be |
| 239 |
necessary with the overlap restrictions of |
| 240 |
_gcry_mpih_divmod. With 50% probability the result after this |
| 241 |
loop will be in the area originally pointed by RP (==RES->d), |
| 242 |
and with 50% probability in the area originally pointed to by XP. */ |
| 243 |
for (;;) |
| 244 |
{ |
| 245 |
while (c) |
| 246 |
{ |
| 247 |
mpi_ptr_t tp; |
| 248 |
mpi_size_t xsize; |
| 249 |
|
| 250 |
/*mpih_mul_n(xp, rp, rp, rsize);*/ |
| 251 |
if ( rsize < KARATSUBA_THRESHOLD ) |
| 252 |
_gcry_mpih_sqr_n_basecase( xp, rp, rsize ); |
| 253 |
else |
| 254 |
{ |
| 255 |
if ( !tspace ) |
| 256 |
{ |
| 257 |
tsize = 2 * rsize; |
| 258 |
tspace = mpi_alloc_limb_space( tsize, 0 ); |
| 259 |
} |
| 260 |
else if ( tsize < (2*rsize) ) |
| 261 |
{ |
| 262 |
_gcry_mpi_free_limb_space (tspace, 0); |
| 263 |
tsize = 2 * rsize; |
| 264 |
tspace = mpi_alloc_limb_space (tsize, 0 ); |
| 265 |
} |
| 266 |
_gcry_mpih_sqr_n (xp, rp, rsize, tspace); |
| 267 |
} |
| 268 |
|
| 269 |
xsize = 2 * rsize; |
| 270 |
if ( xsize > msize ) |
| 271 |
{ |
| 272 |
_gcry_mpih_divrem(xp + msize, 0, xp, xsize, mp, msize); |
| 273 |
xsize = msize; |
| 274 |
} |
| 275 |
|
| 276 |
tp = rp; rp = xp; xp = tp; |
| 277 |
rsize = xsize; |
| 278 |
|
| 279 |
/* To mitigate the Yarom/Falkner flush+reload cache |
| 280 |
* side-channel attack on the RSA secret exponent, we do |
| 281 |
* the multiplication regardless of the value of the |
| 282 |
* high-bit of E. But to avoid this performance penalty |
| 283 |
* we do it only if the exponent has been stored in secure |
| 284 |
* memory and we can thus assume it is a secret exponent. */ |
| 285 |
if (esec || (mpi_limb_signed_t)e < 0) |
| 286 |
{ |
| 287 |
/*mpih_mul( xp, rp, rsize, bp, bsize );*/ |
| 288 |
if( bsize < KARATSUBA_THRESHOLD ) |
| 289 |
_gcry_mpih_mul ( xp, rp, rsize, bp, bsize ); |
| 290 |
else |
| 291 |
_gcry_mpih_mul_karatsuba_case (xp, rp, rsize, bp, bsize, |
| 292 |
&karactx); |
| 293 |
|
| 294 |
xsize = rsize + bsize; |
| 295 |
if ( xsize > msize ) |
| 296 |
{ |
| 297 |
_gcry_mpih_divrem(xp + msize, 0, xp, xsize, mp, msize); |
| 298 |
xsize = msize; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
w.d = rp; |
| 303 |
u.d = xp; |
| 304 |
mpi_set_cond (&w, &u, ((mpi_limb_signed_t)e < 0)); |
| 305 |
|
| 306 |
e <<= 1; |
| 307 |
c--; |
| 308 |
} |
| 309 |
|
| 310 |
i--; |
| 311 |
if ( i < 0 ) |
| 312 |
break; |
| 313 |
e = ep[i]; |
| 314 |
c = BITS_PER_MPI_LIMB; |
| 315 |
} |
| 316 |
|
| 317 |
/* We shifted MOD, the modulo reduction argument, left |
| 318 |
MOD_SHIFT_CNT steps. Adjust the result by reducing it with the |
| 319 |
original MOD. |
| 320 |
|
| 321 |
Also make sure the result is put in RES->d (where it already |
| 322 |
might be, see above). */ |
| 323 |
if ( mod_shift_cnt ) |
| 324 |
{ |
| 325 |
carry_limb = _gcry_mpih_lshift( res->d, rp, rsize, mod_shift_cnt); |
| 326 |
rp = res->d; |
| 327 |
if ( carry_limb ) |
| 328 |
{ |
| 329 |
rp[rsize] = carry_limb; |
| 330 |
rsize++; |
| 331 |
} |
| 332 |
} |
| 333 |
else if (res->d != rp) |
| 334 |
{ |
| 335 |
MPN_COPY (res->d, rp, rsize); |
| 336 |
rp = res->d; |
| 337 |
} |
| 338 |
|
| 339 |
if ( rsize >= msize ) |
| 340 |
{ |
| 341 |
_gcry_mpih_divrem(rp + msize, 0, rp, rsize, mp, msize); |
| 342 |
rsize = msize; |
| 343 |
} |
| 344 |
|
| 345 |
/* Remove any leading zero words from the result. */ |
| 346 |
if ( mod_shift_cnt ) |
| 347 |
_gcry_mpih_rshift( rp, rp, rsize, mod_shift_cnt); |
| 348 |
MPN_NORMALIZE (rp, rsize); |
| 349 |
|
| 350 |
_gcry_mpih_release_karatsuba_ctx (&karactx ); |
| 351 |
} |
| 352 |
|
| 353 |
/* Fixup for negative results. */ |
| 354 |
if ( negative_result && rsize ) |
| 355 |
{ |
| 356 |
if ( mod_shift_cnt ) |
| 357 |
_gcry_mpih_rshift( mp, mp, msize, mod_shift_cnt); |
| 358 |
_gcry_mpih_sub( rp, mp, msize, rp, rsize); |
| 359 |
rsize = msize; |
| 360 |
rsign = msign; |
| 361 |
MPN_NORMALIZE(rp, rsize); |
| 362 |
} |
| 363 |
gcry_assert (res->d == rp); |
| 364 |
res->nlimbs = rsize; |
| 365 |
res->sign = rsign; |
| 366 |
|
| 367 |
leave: |
| 368 |
if (mp_marker) |
| 369 |
_gcry_mpi_free_limb_space( mp_marker, mp_nlimbs ); |
| 370 |
if (bp_marker) |
| 371 |
_gcry_mpi_free_limb_space( bp_marker, bp_nlimbs ); |
| 372 |
if (ep_marker) |
| 373 |
_gcry_mpi_free_limb_space( ep_marker, ep_nlimbs ); |
| 374 |
if (xp_marker) |
| 375 |
_gcry_mpi_free_limb_space( xp_marker, xp_nlimbs ); |
| 376 |
if (tspace) |
| 377 |
_gcry_mpi_free_limb_space( tspace, 0 ); |
| 378 |
} |
| 379 |
#else |
| 380 |
/** |
| 381 |
* Internal function to compute |
| 382 |
* |
| 383 |
* X = R * S mod M |
| 384 |
* |
| 385 |
* and set the size of X at the pointer XSIZE_P. |
| 386 |
* Use karatsuba structure at KARACTX_P. |
| 387 |
* |
| 388 |
* Condition: |
| 389 |
* RSIZE >= SSIZE |
| 390 |
* Enough space for X is allocated beforehand. |
| 391 |
* |
| 392 |
* For generic cases, we can/should use gcry_mpi_mulm. |
| 393 |
* This function is use for specific internal case. |
| 394 |
*/ |
| 395 |
static void |
| 396 |
mul_mod (mpi_ptr_t xp, mpi_size_t *xsize_p, |
| 397 |
mpi_ptr_t rp, mpi_size_t rsize, |
| 398 |
mpi_ptr_t sp, mpi_size_t ssize, |
| 399 |
mpi_ptr_t mp, mpi_size_t msize, |
| 400 |
struct karatsuba_ctx *karactx_p) |
| 401 |
{ |
| 402 |
if( ssize < KARATSUBA_THRESHOLD ) |
| 403 |
_gcry_mpih_mul ( xp, rp, rsize, sp, ssize ); |
| 404 |
else |
| 405 |
_gcry_mpih_mul_karatsuba_case (xp, rp, rsize, sp, ssize, karactx_p); |
| 406 |
|
| 407 |
if (rsize + ssize > msize) |
| 408 |
{ |
| 409 |
_gcry_mpih_divrem (xp + msize, 0, xp, rsize + ssize, mp, msize); |
| 410 |
*xsize_p = msize; |
| 411 |
} |
| 412 |
else |
| 413 |
*xsize_p = rsize + ssize; |
| 414 |
} |
| 415 |
|
| 416 |
#define SIZE_PRECOMP ((1 << (5 - 1))) |
| 417 |
|
| 418 |
/**************** |
| 419 |
* RES = BASE ^ EXPO mod MOD |
| 420 |
* |
| 421 |
* To mitigate the Yarom/Falkner flush+reload cache side-channel |
| 422 |
* attack on the RSA secret exponent, we don't use the square |
| 423 |
* routine but multiplication. |
| 424 |
* |
| 425 |
* Reference: |
| 426 |
* Handbook of Applied Cryptography |
| 427 |
* Algorithm 14.83: Modified left-to-right k-ary exponentiation |
| 428 |
*/ |
| 429 |
void |
| 430 |
_gcry_mpi_powm (gcry_mpi_t res, |
| 431 |
gcry_mpi_t base, gcry_mpi_t expo, gcry_mpi_t mod) |
| 432 |
{ |
| 433 |
/* Pointer to the limbs of the arguments, their size and signs. */ |
| 434 |
mpi_ptr_t rp, ep, mp, bp; |
| 435 |
mpi_size_t esize, msize, bsize, rsize; |
| 436 |
int msign, bsign, rsign; |
| 437 |
/* Flags telling the secure allocation status of the arguments. */ |
| 438 |
int esec, msec, bsec; |
| 439 |
/* Size of the result including space for temporary values. */ |
| 440 |
mpi_size_t size; |
| 441 |
/* Helper. */ |
| 442 |
int mod_shift_cnt; |
| 443 |
int negative_result; |
| 444 |
mpi_ptr_t mp_marker = NULL; |
| 445 |
mpi_ptr_t bp_marker = NULL; |
| 446 |
mpi_ptr_t ep_marker = NULL; |
| 447 |
mpi_ptr_t xp_marker = NULL; |
| 448 |
unsigned int mp_nlimbs = 0; |
| 449 |
unsigned int bp_nlimbs = 0; |
| 450 |
unsigned int ep_nlimbs = 0; |
| 451 |
unsigned int xp_nlimbs = 0; |
| 452 |
mpi_ptr_t precomp[SIZE_PRECOMP]; /* Pre-computed array: BASE^1, ^3, ^5, ... */ |
| 453 |
mpi_size_t precomp_size[SIZE_PRECOMP]; |
| 454 |
mpi_size_t W; |
| 455 |
mpi_ptr_t base_u; |
| 456 |
mpi_size_t base_u_size; |
| 457 |
mpi_size_t max_u_size; |
| 458 |
|
| 459 |
esize = expo->nlimbs; |
| 460 |
msize = mod->nlimbs; |
| 461 |
size = 2 * msize; |
| 462 |
msign = mod->sign; |
| 463 |
|
| 464 |
ep = expo->d; |
| 465 |
MPN_NORMALIZE(ep, esize); |
| 466 |
|
| 467 |
if (esize * BITS_PER_MPI_LIMB > 512) |
| 468 |
W = 5; |
| 469 |
else if (esize * BITS_PER_MPI_LIMB > 256) |
| 470 |
W = 4; |
| 471 |
else if (esize * BITS_PER_MPI_LIMB > 128) |
| 472 |
W = 3; |
| 473 |
else if (esize * BITS_PER_MPI_LIMB > 64) |
| 474 |
W = 2; |
| 475 |
else |
| 476 |
W = 1; |
| 477 |
|
| 478 |
esec = mpi_is_secure(expo); |
| 479 |
msec = mpi_is_secure(mod); |
| 480 |
bsec = mpi_is_secure(base); |
| 481 |
|
| 482 |
rp = res->d; |
| 483 |
|
| 484 |
if (!msize) |
| 485 |
_gcry_divide_by_zero(); |
| 486 |
|
| 487 |
if (!esize) |
| 488 |
{ |
| 489 |
/* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0 depending |
| 490 |
on if MOD equals 1. */ |
| 491 |
res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1; |
| 492 |
if (res->nlimbs) |
| 493 |
{ |
| 494 |
RESIZE_IF_NEEDED (res, 1); |
| 495 |
rp = res->d; |
| 496 |
rp[0] = 1; |
| 497 |
} |
| 498 |
res->sign = 0; |
| 499 |
goto leave; |
| 500 |
} |
| 501 |
|
| 502 |
/* Normalize MOD (i.e. make its most significant bit set) as |
| 503 |
required by mpn_divrem. This will make the intermediate values |
| 504 |
in the calculation slightly larger, but the correct result is |
| 505 |
obtained after a final reduction using the original MOD value. */ |
| 506 |
mp_nlimbs = msec? msize:0; |
| 507 |
mp = mp_marker = mpi_alloc_limb_space(msize, msec); |
| 508 |
count_leading_zeros (mod_shift_cnt, mod->d[msize-1]); |
| 509 |
if (mod_shift_cnt) |
| 510 |
_gcry_mpih_lshift (mp, mod->d, msize, mod_shift_cnt); |
| 511 |
else |
| 512 |
MPN_COPY( mp, mod->d, msize ); |
| 513 |
|
| 514 |
bsize = base->nlimbs; |
| 515 |
bsign = base->sign; |
| 516 |
if (bsize > msize) |
| 517 |
{ |
| 518 |
/* The base is larger than the module. Reduce it. |
| 519 |
|
| 520 |
Allocate (BSIZE + 1) with space for remainder and quotient. |
| 521 |
(The quotient is (bsize - msize + 1) limbs.) */ |
| 522 |
bp_nlimbs = bsec ? (bsize + 1):0; |
| 523 |
bp = bp_marker = mpi_alloc_limb_space( bsize + 1, bsec ); |
| 524 |
MPN_COPY ( bp, base->d, bsize ); |
| 525 |
/* We don't care about the quotient, store it above the |
| 526 |
* remainder, at BP + MSIZE. */ |
| 527 |
_gcry_mpih_divrem( bp + msize, 0, bp, bsize, mp, msize ); |
| 528 |
bsize = msize; |
| 529 |
/* Canonicalize the base, since we are going to multiply with it |
| 530 |
quite a few times. */ |
| 531 |
MPN_NORMALIZE( bp, bsize ); |
| 532 |
} |
| 533 |
else |
| 534 |
bp = base->d; |
| 535 |
|
| 536 |
if (!bsize) |
| 537 |
{ |
| 538 |
res->nlimbs = 0; |
| 539 |
res->sign = 0; |
| 540 |
goto leave; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
/* Make BASE, EXPO not overlap with RES. We don't need to check MOD |
| 545 |
because that has already been copied to the MP var. */ |
| 546 |
if ( rp == bp ) |
| 547 |
{ |
| 548 |
/* RES and BASE are identical. Allocate temp. space for BASE. */ |
| 549 |
gcry_assert (!bp_marker); |
| 550 |
bp_nlimbs = bsec? bsize:0; |
| 551 |
bp = bp_marker = mpi_alloc_limb_space( bsize, bsec ); |
| 552 |
MPN_COPY(bp, rp, bsize); |
| 553 |
} |
| 554 |
if ( rp == ep ) |
| 555 |
{ |
| 556 |
/* RES and EXPO are identical. Allocate temp. space for EXPO. */ |
| 557 |
ep_nlimbs = esec? esize:0; |
| 558 |
ep = ep_marker = mpi_alloc_limb_space( esize, esec ); |
| 559 |
MPN_COPY(ep, rp, esize); |
| 560 |
} |
| 561 |
|
| 562 |
/* Copy base to the result. */ |
| 563 |
if (res->alloced < size) |
| 564 |
{ |
| 565 |
mpi_resize (res, size); |
| 566 |
rp = res->d; |
| 567 |
} |
| 568 |
|
| 569 |
#if USE_ALGORITHM_LLI_EXPONENTIATION |
| 570 |
if ((esec || bsec || msec) && (mod->d[0] & 1)) |
| 571 |
{ |
| 572 |
mpi_ptr_t bp1 = NULL; |
| 573 |
|
| 574 |
if (bsize < msize) |
| 575 |
{ |
| 576 |
bp1 = mpi_alloc_limb_space (msize, 1); |
| 577 |
MPN_ZERO (bp1, msize); |
| 578 |
MPN_COPY (bp1, bp, bsize); |
| 579 |
} |
| 580 |
|
| 581 |
_gcry_mpih_powm_lli (rp, bp1?bp1:bp, mod->d, msize, ep, esize); |
| 582 |
if (bp1) |
| 583 |
_gcry_mpi_free_limb_space (bp1, msize); |
| 584 |
|
| 585 |
rsign = 0; |
| 586 |
negative_result = (ep[0] & 1) && bsign; |
| 587 |
if (negative_result) |
| 588 |
rsign = msign; |
| 589 |
|
| 590 |
res->nlimbs = msize; |
| 591 |
res->sign = rsign; |
| 592 |
goto leave; |
| 593 |
} |
| 594 |
#endif |
| 595 |
|
| 596 |
/* Main processing. */ |
| 597 |
{ |
| 598 |
mpi_size_t i, j, k; |
| 599 |
mpi_ptr_t xp; |
| 600 |
mpi_size_t xsize = 0; |
| 601 |
int c; |
| 602 |
mpi_limb_t e; |
| 603 |
mpi_limb_t carry_limb; |
| 604 |
struct karatsuba_ctx karactx; |
| 605 |
mpi_ptr_t tp; |
| 606 |
|
| 607 |
xp_nlimbs = msec? size:0; |
| 608 |
xp = xp_marker = mpi_alloc_limb_space( size, msec ); |
| 609 |
|
| 610 |
memset( &karactx, 0, sizeof karactx ); |
| 611 |
negative_result = (ep[0] & 1) && bsign; |
| 612 |
|
| 613 |
/* Precompute PRECOMP[], BASE^(2 * i + 1), BASE^1, ^3, ^5, ... */ |
| 614 |
if (W > 1) /* X := BASE^2 */ |
| 615 |
mul_mod (xp, &xsize, bp, bsize, bp, bsize, mp, msize, &karactx); |
| 616 |
base_u = precomp[0] = mpi_alloc_limb_space (bsize, esec); |
| 617 |
base_u_size = max_u_size = precomp_size[0] = bsize; |
| 618 |
MPN_COPY (precomp[0], bp, bsize); |
| 619 |
for (i = 1; i < (1 << (W - 1)); i++) |
| 620 |
{ /* PRECOMP[i] = BASE^(2 * i + 1) */ |
| 621 |
if (xsize >= base_u_size) |
| 622 |
mul_mod (rp, &rsize, xp, xsize, base_u, base_u_size, |
| 623 |
mp, msize, &karactx); |
| 624 |
else |
| 625 |
mul_mod (rp, &rsize, base_u, base_u_size, xp, xsize, |
| 626 |
mp, msize, &karactx); |
| 627 |
base_u = precomp[i] = mpi_alloc_limb_space (rsize, esec); |
| 628 |
base_u_size = precomp_size[i] = rsize; |
| 629 |
if (max_u_size < base_u_size) |
| 630 |
max_u_size = base_u_size; |
| 631 |
MPN_COPY (precomp[i], rp, rsize); |
| 632 |
} |
| 633 |
|
| 634 |
if (msize > max_u_size) |
| 635 |
max_u_size = msize; |
| 636 |
base_u = mpi_alloc_limb_space (max_u_size, esec); |
| 637 |
MPN_ZERO (base_u, max_u_size); |
| 638 |
|
| 639 |
i = esize - 1; |
| 640 |
|
| 641 |
/* Main loop. |
| 642 |
|
| 643 |
Make the result be pointed to alternately by XP and RP. This |
| 644 |
helps us avoid block copying, which would otherwise be |
| 645 |
necessary with the overlap restrictions of |
| 646 |
_gcry_mpih_divmod. With 50% probability the result after this |
| 647 |
loop will be in the area originally pointed by RP (==RES->d), |
| 648 |
and with 50% probability in the area originally pointed to by XP. */ |
| 649 |
rsign = 0; |
| 650 |
if (W == 1) |
| 651 |
{ |
| 652 |
rsize = bsize; |
| 653 |
} |
| 654 |
else |
| 655 |
{ |
| 656 |
rsize = msize; |
| 657 |
MPN_ZERO (rp, rsize); |
| 658 |
} |
| 659 |
MPN_COPY ( rp, bp, bsize ); |
| 660 |
|
| 661 |
e = ep[i]; |
| 662 |
count_leading_zeros (c, e); |
| 663 |
e = (e << c) << 1; |
| 664 |
c = BITS_PER_MPI_LIMB - 1 - c; |
| 665 |
|
| 666 |
j = 0; |
| 667 |
|
| 668 |
for (;;) |
| 669 |
if (e == 0) |
| 670 |
{ |
| 671 |
j += c; |
| 672 |
if ( --i < 0 ) |
| 673 |
break; |
| 674 |
|
| 675 |
e = ep[i]; |
| 676 |
c = BITS_PER_MPI_LIMB; |
| 677 |
} |
| 678 |
else |
| 679 |
{ |
| 680 |
int c0; |
| 681 |
mpi_limb_t e0; |
| 682 |
struct gcry_mpi w, u; |
| 683 |
w.sign = u.sign = 0; |
| 684 |
w.flags = u.flags = 0; |
| 685 |
w.d = base_u; |
| 686 |
|
| 687 |
count_leading_zeros (c0, e); |
| 688 |
e = (e << c0); |
| 689 |
c -= c0; |
| 690 |
j += c0; |
| 691 |
|
| 692 |
e0 = (e >> (BITS_PER_MPI_LIMB - W)); |
| 693 |
if (c >= W) |
| 694 |
c0 = 0; |
| 695 |
else |
| 696 |
{ |
| 697 |
if ( --i < 0 ) |
| 698 |
{ |
| 699 |
e0 = (e >> (BITS_PER_MPI_LIMB - c)); |
| 700 |
j += c - W; |
| 701 |
goto last_step; |
| 702 |
} |
| 703 |
else |
| 704 |
{ |
| 705 |
c0 = c; |
| 706 |
e = ep[i]; |
| 707 |
c = BITS_PER_MPI_LIMB; |
| 708 |
e0 |= (e >> (BITS_PER_MPI_LIMB - (W - c0))); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
e = e << (W - c0); |
| 713 |
c -= (W - c0); |
| 714 |
|
| 715 |
last_step: |
| 716 |
count_trailing_zeros (c0, e0); |
| 717 |
e0 = (e0 >> c0) >> 1; |
| 718 |
|
| 719 |
for (j += W - c0; j >= 0; j--) |
| 720 |
{ |
| 721 |
|
| 722 |
/* |
| 723 |
* base_u <= precomp[e0] |
| 724 |
* base_u_size <= precomp_size[e0] |
| 725 |
*/ |
| 726 |
base_u_size = 0; |
| 727 |
for (k = 0; k < (1<< (W - 1)); k++) |
| 728 |
{ |
| 729 |
w.alloced = w.nlimbs = precomp_size[k]; |
| 730 |
u.alloced = u.nlimbs = precomp_size[k]; |
| 731 |
u.d = precomp[k]; |
| 732 |
|
| 733 |
mpi_set_cond (&w, &u, k == e0); |
| 734 |
base_u_size |= ( precomp_size[k] & (0UL - (k == e0)) ); |
| 735 |
} |
| 736 |
|
| 737 |
w.alloced = w.nlimbs = rsize; |
| 738 |
u.alloced = u.nlimbs = rsize; |
| 739 |
u.d = rp; |
| 740 |
mpi_set_cond (&w, &u, j != 0); |
| 741 |
base_u_size ^= ((base_u_size ^ rsize) & (0UL - (j != 0))); |
| 742 |
|
| 743 |
mul_mod (xp, &xsize, rp, rsize, base_u, base_u_size, |
| 744 |
mp, msize, &karactx); |
| 745 |
tp = rp; rp = xp; xp = tp; |
| 746 |
rsize = xsize; |
| 747 |
} |
| 748 |
|
| 749 |
j = c0; |
| 750 |
if ( i < 0 ) |
| 751 |
break; |
| 752 |
} |
| 753 |
|
| 754 |
while (j--) |
| 755 |
{ |
| 756 |
mul_mod (xp, &xsize, rp, rsize, rp, rsize, mp, msize, &karactx); |
| 757 |
tp = rp; rp = xp; xp = tp; |
| 758 |
rsize = xsize; |
| 759 |
} |
| 760 |
|
| 761 |
/* We shifted MOD, the modulo reduction argument, left |
| 762 |
MOD_SHIFT_CNT steps. Adjust the result by reducing it with the |
| 763 |
original MOD. |
| 764 |
|
| 765 |
Also make sure the result is put in RES->d (where it already |
| 766 |
might be, see above). */ |
| 767 |
if ( mod_shift_cnt ) |
| 768 |
{ |
| 769 |
carry_limb = _gcry_mpih_lshift( res->d, rp, rsize, mod_shift_cnt); |
| 770 |
rp = res->d; |
| 771 |
if ( carry_limb ) |
| 772 |
{ |
| 773 |
rp[rsize] = carry_limb; |
| 774 |
rsize++; |
| 775 |
} |
| 776 |
} |
| 777 |
else if (res->d != rp) |
| 778 |
{ |
| 779 |
MPN_COPY (res->d, rp, rsize); |
| 780 |
rp = res->d; |
| 781 |
} |
| 782 |
|
| 783 |
if ( rsize >= msize ) |
| 784 |
{ |
| 785 |
_gcry_mpih_divrem(rp + msize, 0, rp, rsize, mp, msize); |
| 786 |
rsize = msize; |
| 787 |
} |
| 788 |
|
| 789 |
/* Remove any leading zero words from the result. */ |
| 790 |
if ( mod_shift_cnt ) |
| 791 |
_gcry_mpih_rshift( rp, rp, rsize, mod_shift_cnt); |
| 792 |
MPN_NORMALIZE (rp, rsize); |
| 793 |
|
| 794 |
_gcry_mpih_release_karatsuba_ctx (&karactx ); |
| 795 |
for (i = 0; i < (1 << (W - 1)); i++) |
| 796 |
_gcry_mpi_free_limb_space( precomp[i], esec ? precomp_size[i] : 0 ); |
| 797 |
_gcry_mpi_free_limb_space (base_u, esec ? max_u_size : 0); |
| 798 |
} |
| 799 |
|
| 800 |
/* Fixup for negative results. */ |
| 801 |
if ( negative_result && rsize ) |
| 802 |
{ |
| 803 |
if ( mod_shift_cnt ) |
| 804 |
_gcry_mpih_rshift( mp, mp, msize, mod_shift_cnt); |
| 805 |
_gcry_mpih_sub( rp, mp, msize, rp, rsize); |
| 806 |
rsize = msize; |
| 807 |
rsign = msign; |
| 808 |
MPN_NORMALIZE(rp, rsize); |
| 809 |
} |
| 810 |
gcry_assert (res->d == rp); |
| 811 |
res->nlimbs = rsize; |
| 812 |
res->sign = rsign; |
| 813 |
|
| 814 |
leave: |
| 815 |
if (mp_marker) |
| 816 |
_gcry_mpi_free_limb_space( mp_marker, mp_nlimbs ); |
| 817 |
if (bp_marker) |
| 818 |
_gcry_mpi_free_limb_space( bp_marker, bp_nlimbs ); |
| 819 |
if (ep_marker) |
| 820 |
_gcry_mpi_free_limb_space( ep_marker, ep_nlimbs ); |
| 821 |
if (xp_marker) |
| 822 |
_gcry_mpi_free_limb_space( xp_marker, xp_nlimbs ); |
| 823 |
} |
| 824 |
#endif |