-
+ BBB0C95D94298E81A2357FDA16069A02AB02DC1B36A48E6585200DF41E235C56EA25317C5A851AA7E9F5C5C5FBEA684D039377205030715C3FD8E06F31D3BA6D
eucrypt/mpi/mpi-pow.c
(0 . 0)(1 . 291)
4308 /* mpi-pow.c - MPI functions
4309 * Modified by No Such Labs. (C) 2015. See README.
4310 *
4311 * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10,
4312 * SHA256(gnupg-1.4.10.tar.gz):
4313 * 0bfd74660a2f6cedcf7d8256db4a63c996ffebbcdc2cf54397bfb72878c5a85a
4314 * (C) 1994-2005 Free Software Foundation, Inc.
4315 *
4316 * This program is free software: you can redistribute it and/or modify
4317 * it under the terms of the GNU General Public License as published by
4318 * the Free Software Foundation, either version 3 of the License, or
4319 * (at your option) any later version.
4320 *
4321 * This program is distributed in the hope that it will be useful,
4322 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4323 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4324 * GNU General Public License for more details.
4325 *
4326 * You should have received a copy of the GNU General Public License
4327 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4328 */
4329
4330 #include <stdio.h>
4331 #include <stdlib.h>
4332 #include <string.h>
4333 #include <assert.h>
4334
4335 #include "knobs.h"
4336 #include "mpi-internal.h"
4337 #include "longlong.h"
4338
4339
4340
4341 /****************
4342 * RES = BASE ^ EXP mod MOD
4343 */
4344 void
4345 mpi_powm( MPI res, MPI base, MPI exponent, MPI mod)
4346 {
4347 mpi_ptr_t rp, ep, mp, bp;
4348 mpi_size_t esize, msize, bsize, rsize;
4349 int esign, msign, bsign, rsign;
4350 int esec, msec, bsec, rsec;
4351 mpi_size_t size;
4352 int mod_shift_cnt;
4353 int negative_result;
4354 mpi_ptr_t mp_marker=NULL, bp_marker=NULL, ep_marker=NULL;
4355 mpi_ptr_t xp_marker=NULL;
4356 int assign_rp=0;
4357 mpi_ptr_t tspace = NULL;
4358 mpi_size_t tsize=0; /* to avoid compiler warning */
4359 /* fixme: we should check that the warning is void*/
4360
4361 esize = exponent->nlimbs;
4362 msize = mod->nlimbs;
4363 size = 2 * msize;
4364 esign = exponent->sign;
4365 msign = mod->sign;
4366
4367 esec = mpi_is_secure(exponent);
4368 msec = mpi_is_secure(mod);
4369 bsec = mpi_is_secure(base);
4370 rsec = mpi_is_secure(res);
4371
4372 rp = res->d;
4373 ep = exponent->d;
4374
4375 if( !msize )
4376 msize = 1 / msize; /* provoke a signal */
4377
4378 if( !esize ) {
4379 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
4380 * depending on if MOD equals 1. */
4381 rp[0] = 1;
4382 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
4383 res->sign = 0;
4384 goto leave;
4385 }
4386
4387 /* Normalize MOD (i.e. make its most significant bit set) as required by
4388 * mpn_divrem. This will make the intermediate values in the calculation
4389 * slightly larger, but the correct result is obtained after a final
4390 * reduction using the original MOD value. */
4391 mp = mp_marker = mpi_alloc_limb_space(msize, msec);
4392 count_leading_zeros( mod_shift_cnt, mod->d[msize-1] );
4393 if( mod_shift_cnt )
4394 mpihelp_lshift( mp, mod->d, msize, mod_shift_cnt );
4395 else
4396 MPN_COPY( mp, mod->d, msize );
4397
4398 bsize = base->nlimbs;
4399 bsign = base->sign;
4400 if( bsize > msize ) { /* The base is larger than the module. Reduce it. */
4401 /* Allocate (BSIZE + 1) with space for remainder and quotient.
4402 * (The quotient is (bsize - msize + 1) limbs.) */
4403 bp = bp_marker = mpi_alloc_limb_space( bsize + 1, bsec );
4404 MPN_COPY( bp, base->d, bsize );
4405 /* We don't care about the quotient, store it above the remainder,
4406 * at BP + MSIZE. */
4407 mpihelp_divrem( bp + msize, 0, bp, bsize, mp, msize );
4408 bsize = msize;
4409 /* Canonicalize the base, since we are going to multiply with it
4410 * quite a few times. */
4411 MPN_NORMALIZE( bp, bsize );
4412 }
4413 else
4414 bp = base->d;
4415
4416 if( !bsize ) {
4417 res->nlimbs = 0;
4418 res->sign = 0;
4419 goto leave;
4420 }
4421
4422 if( res->alloced < size ) {
4423 /* We have to allocate more space for RES. If any of the input
4424 * parameters are identical to RES, defer deallocation of the old
4425 * space. */
4426 if( rp == ep || rp == mp || rp == bp ) {
4427 rp = mpi_alloc_limb_space( size, rsec );
4428 assign_rp = 1;
4429 }
4430 else {
4431 mpi_resize( res, size );
4432 rp = res->d;
4433 }
4434 }
4435 else { /* Make BASE, EXPONENT and MOD not overlap with RES. */
4436 if( rp == bp ) {
4437 /* RES and BASE are identical. Allocate temp. space for BASE. */
4438 assert( !bp_marker );
4439 bp = bp_marker = mpi_alloc_limb_space( bsize, bsec );
4440 MPN_COPY(bp, rp, bsize);
4441 }
4442 if( rp == ep ) {
4443 /* RES and EXPONENT are identical.
4444 Allocate temp. space for EXPONENT. */
4445 ep = ep_marker = mpi_alloc_limb_space( esize, esec );
4446 MPN_COPY(ep, rp, esize);
4447 }
4448 if( rp == mp ) {
4449 /* RES and MOD are identical. Allocate temporary space for MOD.*/
4450 assert( !mp_marker );
4451 mp = mp_marker = mpi_alloc_limb_space( msize, msec );
4452 MPN_COPY(mp, rp, msize);
4453 }
4454 }
4455
4456 MPN_COPY( rp, bp, bsize );
4457 rsize = bsize;
4458 rsign = bsign;
4459
4460 {
4461 mpi_size_t i;
4462 mpi_ptr_t xp = xp_marker = mpi_alloc_limb_space( 2 * (msize + 1), msec );
4463 int c;
4464 mpi_limb_t e;
4465 mpi_limb_t carry_limb;
4466 struct karatsuba_ctx karactx;
4467
4468 memset( &karactx, 0, sizeof karactx );
4469 negative_result = (ep[0] & 1) && base->sign;
4470
4471 i = esize - 1;
4472 e = ep[i];
4473 count_leading_zeros (c, e);
4474 e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
4475 c = BITS_PER_MPI_LIMB - 1 - c;
4476
4477 /* Main loop.
4478 *
4479 * Make the result be pointed to alternately by XP and RP. This
4480 * helps us avoid block copying, which would otherwise be necessary
4481 * with the overlap restrictions of mpihelp_divmod. With 50% probability
4482 * the result after this loop will be in the area originally pointed
4483 * by RP (==RES->d), and with 50% probability in the area originally
4484 * pointed to by XP.
4485 */
4486
4487 for(;;) {
4488 while( c ) {
4489 mpi_ptr_t tp;
4490 mpi_size_t xsize;
4491
4492 /*mpihelp_mul_n(xp, rp, rp, rsize);*/
4493 if( rsize < KARATSUBA_THRESHOLD )
4494 mpih_sqr_n_basecase( xp, rp, rsize );
4495 else {
4496 if( !tspace ) {
4497 tsize = 2 * rsize;
4498 tspace = mpi_alloc_limb_space( tsize, 0 );
4499 }
4500 else if( tsize < (2*rsize) ) {
4501 mpi_free_limb_space( tspace );
4502 tsize = 2 * rsize;
4503 tspace = mpi_alloc_limb_space( tsize, 0 );
4504 }
4505 mpih_sqr_n( xp, rp, rsize, tspace );
4506 }
4507
4508 xsize = 2 * rsize;
4509 if( xsize > msize ) {
4510 mpihelp_divrem(xp + msize, 0, xp, xsize, mp, msize);
4511 xsize = msize;
4512 }
4513
4514 tp = rp; rp = xp; xp = tp;
4515 rsize = xsize;
4516
4517 if( (mpi_limb_signed_t)e < 0 ) {
4518 /*mpihelp_mul( xp, rp, rsize, bp, bsize );*/
4519 if( bsize < KARATSUBA_THRESHOLD ) {
4520 mpihelp_mul( xp, rp, rsize, bp, bsize );
4521 }
4522 else {
4523 mpihelp_mul_karatsuba_case(
4524 xp, rp, rsize, bp, bsize, &karactx );
4525 }
4526
4527 xsize = rsize + bsize;
4528 if( xsize > msize ) {
4529 mpihelp_divrem(xp + msize, 0, xp, xsize, mp, msize);
4530 xsize = msize;
4531 }
4532
4533 tp = rp; rp = xp; xp = tp;
4534 rsize = xsize;
4535 }
4536 e <<= 1;
4537 c--;
4538 }
4539
4540 i--;
4541 if( i < 0 )
4542 break;
4543 e = ep[i];
4544 c = BITS_PER_MPI_LIMB;
4545 }
4546
4547 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
4548 * steps. Adjust the result by reducing it with the original MOD.
4549 *
4550 * Also make sure the result is put in RES->d (where it already
4551 * might be, see above).
4552 */
4553 if( mod_shift_cnt ) {
4554 carry_limb = mpihelp_lshift( res->d, rp, rsize, mod_shift_cnt);
4555 rp = res->d;
4556 if( carry_limb ) {
4557 rp[rsize] = carry_limb;
4558 rsize++;
4559 }
4560 }
4561 else {
4562 MPN_COPY( res->d, rp, rsize);
4563 rp = res->d;
4564 }
4565
4566 if( rsize >= msize ) {
4567 mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
4568 rsize = msize;
4569 }
4570
4571 /* Remove any leading zero words from the result. */
4572 if( mod_shift_cnt )
4573 mpihelp_rshift( rp, rp, rsize, mod_shift_cnt);
4574 MPN_NORMALIZE (rp, rsize);
4575
4576 mpihelp_release_karatsuba_ctx( &karactx );
4577 }
4578
4579 if( negative_result && rsize ) {
4580 if( mod_shift_cnt )
4581 mpihelp_rshift( mp, mp, msize, mod_shift_cnt);
4582 mpihelp_sub( rp, mp, msize, rp, rsize);
4583 rsize = msize;
4584 rsign = msign;
4585 MPN_NORMALIZE(rp, rsize);
4586 }
4587 res->nlimbs = rsize;
4588 res->sign = rsign;
4589
4590 leave:
4591 if( assign_rp ) mpi_assign_limb_space( res, rp, size );
4592 if( mp_marker ) mpi_free_limb_space( mp_marker );
4593 if( bp_marker ) mpi_free_limb_space( bp_marker );
4594 if( ep_marker ) mpi_free_limb_space( ep_marker );
4595 if( xp_marker ) mpi_free_limb_space( xp_marker );
4596 if( tspace ) mpi_free_limb_space( tspace );
4597 }
4598