-
+ 8341923461226EC55DE5B29209CC03051587DE11885CB49433FF4C0EBA2160798B7DC27573B9BC363EE0954A8FC08CBD21AF3F655A9C0BC8AA294A74F80EB2C9
mpi/mpi-pow.c
(0 . 0)(1 . 294)
7657 /* mpi-pow.c - MPI functions
7658 * Copyright (C) 1994, 1996, 1998, 2000 Free Software Foundation, Inc.
7659 *
7660 * This file is part of GnuPG.
7661 *
7662 * GnuPG is free software; you can redistribute it and/or modify
7663 * it under the terms of the GNU General Public License as published by
7664 * the Free Software Foundation; either version 3 of the License, or
7665 * (at your option) any later version.
7666 *
7667 * GnuPG is distributed in the hope that it will be useful,
7668 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7669 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7670 * GNU General Public License for more details.
7671 *
7672 * You should have received a copy of the GNU General Public License
7673 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7674 *
7675 * Note: This code is heavily based on the GNU MP Library.
7676 * Actually it's the same code with only minor changes in the
7677 * way the data is stored; this is to support the abstraction
7678 * of an optional secure memory allocation which may be used
7679 * to avoid revealing of sensitive data due to paging etc.
7680 * The GNU MP Library itself is published under the LGPL;
7681 * however I decided to publish this code under the plain GPL.
7682 */
7683
7684 #include <config.h>
7685 #include <stdio.h>
7686 #include <stdlib.h>
7687 #include <string.h>
7688 #include "mpi-internal.h"
7689 #include "longlong.h"
7690 #include <assert.h>
7691
7692
7693 /****************
7694 * RES = BASE ^ EXP mod MOD
7695 */
7696 void
7697 mpi_powm( MPI res, MPI base, MPI exponent, MPI mod)
7698 {
7699 mpi_ptr_t rp, ep, mp, bp;
7700 mpi_size_t esize, msize, bsize, rsize;
7701 int esign, msign, bsign, rsign;
7702 int esec, msec, bsec, rsec;
7703 mpi_size_t size;
7704 int mod_shift_cnt;
7705 int negative_result;
7706 mpi_ptr_t mp_marker=NULL, bp_marker=NULL, ep_marker=NULL;
7707 mpi_ptr_t xp_marker=NULL;
7708 int assign_rp=0;
7709 mpi_ptr_t tspace = NULL;
7710 mpi_size_t tsize=0; /* to avoid compiler warning */
7711 /* fixme: we should check that the warning is void*/
7712
7713 esize = exponent->nlimbs;
7714 msize = mod->nlimbs;
7715 size = 2 * msize;
7716 esign = exponent->sign;
7717 msign = mod->sign;
7718
7719 esec = mpi_is_secure(exponent);
7720 msec = mpi_is_secure(mod);
7721 bsec = mpi_is_secure(base);
7722 rsec = mpi_is_secure(res);
7723
7724 rp = res->d;
7725 ep = exponent->d;
7726
7727 if( !msize )
7728 msize = 1 / msize; /* provoke a signal */
7729
7730 if( !esize ) {
7731 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
7732 * depending on if MOD equals 1. */
7733 rp[0] = 1;
7734 res->nlimbs = (msize == 1 && mod->d[0] == 1) ? 0 : 1;
7735 res->sign = 0;
7736 goto leave;
7737 }
7738
7739 /* Normalize MOD (i.e. make its most significant bit set) as required by
7740 * mpn_divrem. This will make the intermediate values in the calculation
7741 * slightly larger, but the correct result is obtained after a final
7742 * reduction using the original MOD value. */
7743 mp = mp_marker = mpi_alloc_limb_space(msize, msec);
7744 count_leading_zeros( mod_shift_cnt, mod->d[msize-1] );
7745 if( mod_shift_cnt )
7746 mpihelp_lshift( mp, mod->d, msize, mod_shift_cnt );
7747 else
7748 MPN_COPY( mp, mod->d, msize );
7749
7750 bsize = base->nlimbs;
7751 bsign = base->sign;
7752 if( bsize > msize ) { /* The base is larger than the module. Reduce it. */
7753 /* Allocate (BSIZE + 1) with space for remainder and quotient.
7754 * (The quotient is (bsize - msize + 1) limbs.) */
7755 bp = bp_marker = mpi_alloc_limb_space( bsize + 1, bsec );
7756 MPN_COPY( bp, base->d, bsize );
7757 /* We don't care about the quotient, store it above the remainder,
7758 * at BP + MSIZE. */
7759 mpihelp_divrem( bp + msize, 0, bp, bsize, mp, msize );
7760 bsize = msize;
7761 /* Canonicalize the base, since we are going to multiply with it
7762 * quite a few times. */
7763 MPN_NORMALIZE( bp, bsize );
7764 }
7765 else
7766 bp = base->d;
7767
7768 if( !bsize ) {
7769 res->nlimbs = 0;
7770 res->sign = 0;
7771 goto leave;
7772 }
7773
7774 if( res->alloced < size ) {
7775 /* We have to allocate more space for RES. If any of the input
7776 * parameters are identical to RES, defer deallocation of the old
7777 * space. */
7778 if( rp == ep || rp == mp || rp == bp ) {
7779 rp = mpi_alloc_limb_space( size, rsec );
7780 assign_rp = 1;
7781 }
7782 else {
7783 mpi_resize( res, size );
7784 rp = res->d;
7785 }
7786 }
7787 else { /* Make BASE, EXPONENT and MOD not overlap with RES. */
7788 if( rp == bp ) {
7789 /* RES and BASE are identical. Allocate temp. space for BASE. */
7790 assert( !bp_marker );
7791 bp = bp_marker = mpi_alloc_limb_space( bsize, bsec );
7792 MPN_COPY(bp, rp, bsize);
7793 }
7794 if( rp == ep ) {
7795 /* RES and EXPONENT are identical.
7796 Allocate temp. space for EXPONENT. */
7797 ep = ep_marker = mpi_alloc_limb_space( esize, esec );
7798 MPN_COPY(ep, rp, esize);
7799 }
7800 if( rp == mp ) {
7801 /* RES and MOD are identical. Allocate temporary space for MOD.*/
7802 assert( !mp_marker );
7803 mp = mp_marker = mpi_alloc_limb_space( msize, msec );
7804 MPN_COPY(mp, rp, msize);
7805 }
7806 }
7807
7808 MPN_COPY( rp, bp, bsize );
7809 rsize = bsize;
7810 rsign = bsign;
7811
7812 {
7813 mpi_size_t i;
7814 mpi_ptr_t xp = xp_marker = mpi_alloc_limb_space( 2 * (msize + 1), msec );
7815 int c;
7816 mpi_limb_t e;
7817 mpi_limb_t carry_limb;
7818 struct karatsuba_ctx karactx;
7819
7820 memset( &karactx, 0, sizeof karactx );
7821 negative_result = (ep[0] & 1) && base->sign;
7822
7823 i = esize - 1;
7824 e = ep[i];
7825 count_leading_zeros (c, e);
7826 e = (e << c) << 1; /* shift the exp bits to the left, lose msb */
7827 c = BITS_PER_MPI_LIMB - 1 - c;
7828
7829 /* Main loop.
7830 *
7831 * Make the result be pointed to alternately by XP and RP. This
7832 * helps us avoid block copying, which would otherwise be necessary
7833 * with the overlap restrictions of mpihelp_divmod. With 50% probability
7834 * the result after this loop will be in the area originally pointed
7835 * by RP (==RES->d), and with 50% probability in the area originally
7836 * pointed to by XP.
7837 */
7838
7839 for(;;) {
7840 while( c ) {
7841 mpi_ptr_t tp;
7842 mpi_size_t xsize;
7843
7844 /*mpihelp_mul_n(xp, rp, rp, rsize);*/
7845 if( rsize < KARATSUBA_THRESHOLD )
7846 mpih_sqr_n_basecase( xp, rp, rsize );
7847 else {
7848 if( !tspace ) {
7849 tsize = 2 * rsize;
7850 tspace = mpi_alloc_limb_space( tsize, 0 );
7851 }
7852 else if( tsize < (2*rsize) ) {
7853 mpi_free_limb_space( tspace );
7854 tsize = 2 * rsize;
7855 tspace = mpi_alloc_limb_space( tsize, 0 );
7856 }
7857 mpih_sqr_n( xp, rp, rsize, tspace );
7858 }
7859
7860 xsize = 2 * rsize;
7861 if( xsize > msize ) {
7862 mpihelp_divrem(xp + msize, 0, xp, xsize, mp, msize);
7863 xsize = msize;
7864 }
7865
7866 tp = rp; rp = xp; xp = tp;
7867 rsize = xsize;
7868
7869 if( (mpi_limb_signed_t)e < 0 ) {
7870 /*mpihelp_mul( xp, rp, rsize, bp, bsize );*/
7871 if( bsize < KARATSUBA_THRESHOLD ) {
7872 mpihelp_mul( xp, rp, rsize, bp, bsize );
7873 }
7874 else {
7875 mpihelp_mul_karatsuba_case(
7876 xp, rp, rsize, bp, bsize, &karactx );
7877 }
7878
7879 xsize = rsize + bsize;
7880 if( xsize > msize ) {
7881 mpihelp_divrem(xp + msize, 0, xp, xsize, mp, msize);
7882 xsize = msize;
7883 }
7884
7885 tp = rp; rp = xp; xp = tp;
7886 rsize = xsize;
7887 }
7888 e <<= 1;
7889 c--;
7890 }
7891
7892 i--;
7893 if( i < 0 )
7894 break;
7895 e = ep[i];
7896 c = BITS_PER_MPI_LIMB;
7897 }
7898
7899 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
7900 * steps. Adjust the result by reducing it with the original MOD.
7901 *
7902 * Also make sure the result is put in RES->d (where it already
7903 * might be, see above).
7904 */
7905 if( mod_shift_cnt ) {
7906 carry_limb = mpihelp_lshift( res->d, rp, rsize, mod_shift_cnt);
7907 rp = res->d;
7908 if( carry_limb ) {
7909 rp[rsize] = carry_limb;
7910 rsize++;
7911 }
7912 }
7913 else {
7914 MPN_COPY( res->d, rp, rsize);
7915 rp = res->d;
7916 }
7917
7918 if( rsize >= msize ) {
7919 mpihelp_divrem(rp + msize, 0, rp, rsize, mp, msize);
7920 rsize = msize;
7921 }
7922
7923 /* Remove any leading zero words from the result. */
7924 if( mod_shift_cnt )
7925 mpihelp_rshift( rp, rp, rsize, mod_shift_cnt);
7926 MPN_NORMALIZE (rp, rsize);
7927
7928 mpihelp_release_karatsuba_ctx( &karactx );
7929 }
7930
7931 if( negative_result && rsize ) {
7932 if( mod_shift_cnt )
7933 mpihelp_rshift( mp, mp, msize, mod_shift_cnt);
7934 mpihelp_sub( rp, mp, msize, rp, rsize);
7935 rsize = msize;
7936 rsign = msign;
7937 MPN_NORMALIZE(rp, rsize);
7938 }
7939 res->nlimbs = rsize;
7940 res->sign = rsign;
7941
7942 leave:
7943 if( assign_rp ) mpi_assign_limb_space( res, rp, size );
7944 if( mp_marker ) mpi_free_limb_space( mp_marker );
7945 if( bp_marker ) mpi_free_limb_space( bp_marker );
7946 if( ep_marker ) mpi_free_limb_space( ep_marker );
7947 if( xp_marker ) mpi_free_limb_space( xp_marker );
7948 if( tspace ) mpi_free_limb_space( tspace );
7949 }
7950