1 /* mpi-pow.c - MPI functions
2 * Copyright (C) 1994, 1996, 1998, 2000 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 * Note: This code is heavily based on the GNU MP Library.
21 * Actually it's the same code with only minor changes in the
22 * way the data is stored; this is to support the abstraction
23 * of an optional secure memory allocation which may be used
24 * to avoid revealing of sensitive data due to paging etc.
25 * The GNU MP Library itself is published under the LGPL;
26 * however I decided to publish this code under the plain GPL.
29 #include <linux/string.h>
30 #include "mpi-internal.h"
34 * RES = BASE ^ EXP mod MOD
36 int mpi_powm(MPI res
, MPI base
, MPI exp
, MPI mod
)
38 mpi_ptr_t mp_marker
= NULL
, bp_marker
= NULL
, ep_marker
= NULL
;
39 mpi_ptr_t xp_marker
= NULL
;
40 mpi_ptr_t tspace
= NULL
;
41 mpi_ptr_t rp
, ep
, mp
, bp
;
42 mpi_size_t esize
, msize
, bsize
, rsize
;
43 int esign
, msign
, bsign
, rsign
;
48 mpi_size_t tsize
= 0; /* to avoid compiler warning */
49 /* fixme: we should check that the warning is void */
65 /* Exponent is zero, result is 1 mod MOD, i.e., 1 or 0
66 * depending on if MOD equals 1. */
68 res
->nlimbs
= (msize
== 1 && mod
->d
[0] == 1) ? 0 : 1;
73 /* Normalize MOD (i.e. make its most significant bit set) as required by
74 * mpn_divrem. This will make the intermediate values in the calculation
75 * slightly larger, but the correct result is obtained after a final
76 * reduction using the original MOD value. */
77 mp
= mp_marker
= mpi_alloc_limb_space(msize
);
80 count_leading_zeros(mod_shift_cnt
, mod
->d
[msize
- 1]);
82 mpihelp_lshift(mp
, mod
->d
, msize
, mod_shift_cnt
);
84 MPN_COPY(mp
, mod
->d
, msize
);
88 if (bsize
> msize
) { /* The base is larger than the module. Reduce it. */
89 /* Allocate (BSIZE + 1) with space for remainder and quotient.
90 * (The quotient is (bsize - msize + 1) limbs.) */
91 bp
= bp_marker
= mpi_alloc_limb_space(bsize
+ 1);
94 MPN_COPY(bp
, base
->d
, bsize
);
95 /* We don't care about the quotient, store it above the remainder,
97 mpihelp_divrem(bp
+ msize
, 0, bp
, bsize
, mp
, msize
);
99 /* Canonicalize the base, since we are going to multiply with it
100 * quite a few times. */
101 MPN_NORMALIZE(bp
, bsize
);
111 if (res
->alloced
< size
) {
112 /* We have to allocate more space for RES. If any of the input
113 * parameters are identical to RES, defer deallocation of the old
115 if (rp
== ep
|| rp
== mp
|| rp
== bp
) {
116 rp
= mpi_alloc_limb_space(size
);
121 if (mpi_resize(res
, size
) < 0)
125 } else { /* Make BASE, EXP and MOD not overlap with RES. */
127 /* RES and BASE are identical. Allocate temp. space for BASE. */
129 bp
= bp_marker
= mpi_alloc_limb_space(bsize
);
132 MPN_COPY(bp
, rp
, bsize
);
135 /* RES and EXP are identical. Allocate temp. space for EXP. */
136 ep
= ep_marker
= mpi_alloc_limb_space(esize
);
139 MPN_COPY(ep
, rp
, esize
);
142 /* RES and MOD are identical. Allocate temporary space for MOD. */
144 mp
= mp_marker
= mpi_alloc_limb_space(msize
);
147 MPN_COPY(mp
, rp
, msize
);
151 MPN_COPY(rp
, bp
, bsize
);
160 mpi_limb_t carry_limb
;
161 struct karatsuba_ctx karactx
;
163 xp
= xp_marker
= mpi_alloc_limb_space(2 * (msize
+ 1));
167 memset(&karactx
, 0, sizeof karactx
);
168 negative_result
= (ep
[0] & 1) && base
->sign
;
172 count_leading_zeros(c
, e
);
173 e
= (e
<< c
) << 1; /* shift the exp bits to the left, lose msb */
174 c
= BITS_PER_MPI_LIMB
- 1 - c
;
178 * Make the result be pointed to alternately by XP and RP. This
179 * helps us avoid block copying, which would otherwise be necessary
180 * with the overlap restrictions of mpihelp_divmod. With 50% probability
181 * the result after this loop will be in the area originally pointed
182 * by RP (==RES->d), and with 50% probability in the area originally
191 /*if (mpihelp_mul_n(xp, rp, rp, rsize) < 0) goto enomem */
192 if (rsize
< KARATSUBA_THRESHOLD
)
193 mpih_sqr_n_basecase(xp
, rp
, rsize
);
198 mpi_alloc_limb_space(tsize
);
201 } else if (tsize
< (2 * rsize
)) {
202 mpi_free_limb_space(tspace
);
205 mpi_alloc_limb_space(tsize
);
209 mpih_sqr_n(xp
, rp
, rsize
, tspace
);
214 mpihelp_divrem(xp
+ msize
, 0, xp
, xsize
,
224 if ((mpi_limb_signed_t
) e
< 0) {
225 /*mpihelp_mul( xp, rp, rsize, bp, bsize ); */
226 if (bsize
< KARATSUBA_THRESHOLD
) {
229 (xp
, rp
, rsize
, bp
, bsize
,
233 if (mpihelp_mul_karatsuba_case
234 (xp
, rp
, rsize
, bp
, bsize
,
239 xsize
= rsize
+ bsize
;
241 mpihelp_divrem(xp
+ msize
, 0,
260 c
= BITS_PER_MPI_LIMB
;
263 /* We shifted MOD, the modulo reduction argument, left MOD_SHIFT_CNT
264 * steps. Adjust the result by reducing it with the original MOD.
266 * Also make sure the result is put in RES->d (where it already
267 * might be, see above).
271 mpihelp_lshift(res
->d
, rp
, rsize
, mod_shift_cnt
);
274 rp
[rsize
] = carry_limb
;
278 MPN_COPY(res
->d
, rp
, rsize
);
282 if (rsize
>= msize
) {
283 mpihelp_divrem(rp
+ msize
, 0, rp
, rsize
, mp
, msize
);
287 /* Remove any leading zero words from the result. */
289 mpihelp_rshift(rp
, rp
, rsize
, mod_shift_cnt
);
290 MPN_NORMALIZE(rp
, rsize
);
292 mpihelp_release_karatsuba_ctx(&karactx
);
295 if (negative_result
&& rsize
) {
297 mpihelp_rshift(mp
, mp
, msize
, mod_shift_cnt
);
298 mpihelp_sub(rp
, mp
, msize
, rp
, rsize
);
301 MPN_NORMALIZE(rp
, rsize
);
310 mpi_assign_limb_space(res
, rp
, size
);
312 mpi_free_limb_space(mp_marker
);
314 mpi_free_limb_space(bp_marker
);
316 mpi_free_limb_space(ep_marker
);
318 mpi_free_limb_space(xp_marker
);
320 mpi_free_limb_space(tspace
);
323 EXPORT_SYMBOL_GPL(mpi_powm
);