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