1 /* crypto/bn/bn_exp.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
66 int BN_mod_mul(ret, a, b, m, ctx)
80 t= &(ctx->bn[ctx->tos++]);
82 { if (!BN_sqr(t,a,ctx)) goto err; }
84 { if (!BN_mul(t,a,b,ctx)) goto err; }
85 if (!BN_mod(ret,t,m,ctx)) goto err;
93 /* this one works - simple but works */
94 int BN_mod_exp(r,a,p,m,ctx)
101 v= &(ctx->bn[ctx->tos++]);
102 tmp= &(ctx->bn[ctx->tos++]);
104 if (BN_copy(v,a) == NULL) goto err;
108 { if (BN_copy(r,a) == NULL) goto err; }
109 else { if (BN_one(r)) goto err; }
111 for (i=1; i<bits; i++)
113 if (!BN_sqr(tmp,v,ctx)) goto err;
114 if (!BN_mod(v,tmp,m,ctx)) goto err;
115 if (BN_is_bit_set(p,i))
117 if (!BN_mul(tmp,r,v,ctx)) goto err;
118 if (!BN_mod(r,tmp,m,ctx)) goto err;
129 /* this one works - simple but works */
130 int BN_exp(r,a,p,ctx)
137 v= &(ctx->bn[ctx->tos++]);
138 tmp= &(ctx->bn[ctx->tos++]);
140 if (BN_copy(v,a) == NULL) goto err;
144 { if (BN_copy(r,a) == NULL) goto err; }
145 else { if (BN_one(r)) goto err; }
147 for (i=1; i<bits; i++)
149 if (!BN_sqr(tmp,v,ctx)) goto err;
150 if (BN_is_bit_set(p,i))
152 if (!BN_mul(tmp,r,v,ctx)) goto err;
161 int BN_mod_exp(r,a,p,m,ctx)
175 /* I have finally been able to take out this pre-condition of
176 * the top bit being set. It was caused by an error in BN_div
177 * with negatives. There was also another problem when for a^b%m
178 * a >= m. eay 07-May-97 */
179 /* if ((m->d[m->top-1]&BN_TBIT) && BN_is_odd(m)) */
182 { ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL); }
186 { ret=BN_mod_exp_recp(r,a,p,m,ctx); }
188 { ret=BN_mod_exp_simple(r,a,p,m,ctx); }
194 /* #ifdef RECP_MUL_MOD */
195 int BN_mod_exp_recp(r,a,p,m,ctx)
202 int i,j,bits,ret=0,wstart,wend,window,wvalue;
205 BIGNUM val[TABLE_SIZE];
208 aa= &(ctx->bn[ctx->tos++]);
216 BN_RECP_CTX_init(&recp);
217 if (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;
222 if (!BN_mod(&(val[0]),a,m,ctx)) goto err; /* 1 */
223 if (!BN_mod_mul_reciprocal(aa,&(val[0]),&(val[0]),&recp,ctx))
226 if (bits <= 17) /* This is probably 3 or 0x10001, so just do singles */
228 else if (bits >= 256)
229 window=5; /* max size of window */
230 else if (bits >= 128)
239 if (!BN_mod_mul_reciprocal(&(val[i]),&(val[i-1]),aa,&recp,ctx))
244 start=1; /* This is used to avoid multiplication etc
245 * when there is only the value '1' in the
247 wvalue=0; /* The 'value' of the window */
248 wstart=bits-1; /* The top bit of the window */
249 wend=0; /* The bottom bit of the window */
251 if (!BN_one(r)) goto err;
255 if (BN_is_bit_set(p,wstart) == 0)
258 if (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))
260 if (wstart == 0) break;
264 /* We now have wstart on a 'set' bit, we now need to work out
265 * how bit a window to do. To do this we need to scan
266 * forward until the last set bit before the end of the
271 for (i=1; i<window; i++)
273 if (wstart-i < 0) break;
274 if (BN_is_bit_set(p,wstart-i))
282 /* wend is the size of the current window */
284 /* add the 'bytes above' */
288 if (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))
292 /* wvalue will be an odd number < 2^window */
293 if (!BN_mod_mul_reciprocal(r,r,&(val[wvalue>>1]),&recp,ctx))
296 /* move the 'window' down further */
300 if (wstart < 0) break;
306 BN_clear_free(&(val[i]));
307 BN_RECP_CTX_free(&recp);
312 /* #ifdef MONT_MUL_MOD */
313 int BN_mod_exp_mont(rr,a,p,m,ctx,in_mont)
319 BN_MONT_CTX *in_mont;
321 int i,j,bits,ret=0,wstart,wend,window,wvalue;
324 BIGNUM val[TABLE_SIZE];
325 BN_MONT_CTX *mont=NULL;
333 BNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);
336 d= &(ctx->bn[ctx->tos++]);
337 r= &(ctx->bn[ctx->tos++]);
345 /* If this is not done, things will break in the montgomery
354 if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
355 if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
360 if (BN_ucmp(a,m) >= 0)
362 BN_mod(&(val[0]),a,m,ctx);
367 if (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err; /* 1 */
368 if (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err; /* 2 */
370 if (bits <= 20) /* This is probably 3 or 0x10001, so just do singles */
372 else if (bits >= 256)
373 window=5; /* max size of window */
374 else if (bits >= 128)
383 if (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))
388 start=1; /* This is used to avoid multiplication etc
389 * when there is only the value '1' in the
391 wvalue=0; /* The 'value' of the window */
392 wstart=bits-1; /* The top bit of the window */
393 wend=0; /* The bottom bit of the window */
395 if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;
398 if (BN_is_bit_set(p,wstart) == 0)
402 if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
405 if (wstart == 0) break;
409 /* We now have wstart on a 'set' bit, we now need to work out
410 * how bit a window to do. To do this we need to scan
411 * forward until the last set bit before the end of the
416 for (i=1; i<window; i++)
418 if (wstart-i < 0) break;
419 if (BN_is_bit_set(p,wstart-i))
427 /* wend is the size of the current window */
429 /* add the 'bytes above' */
433 if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
437 /* wvalue will be an odd number < 2^window */
438 if (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))
441 /* move the 'window' down further */
445 if (wstart < 0) break;
447 BN_from_montgomery(rr,r,mont,ctx);
450 if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
453 BN_clear_free(&(val[i]));
458 /* The old fallback, simple version :-) */
459 int BN_mod_exp_simple(r,a,p,m,ctx)
466 int i,j,bits,ret=0,wstart,wend,window,wvalue,ts=0;
469 BIGNUM val[TABLE_SIZE];
471 d= &(ctx->bn[ctx->tos++]);
482 if (!BN_mod(&(val[0]),a,m,ctx)) goto err; /* 1 */
483 if (!BN_mod_mul(d,&(val[0]),&(val[0]),m,ctx))
486 if (bits <= 17) /* This is probably 3 or 0x10001, so just do singles */
488 else if (bits >= 256)
489 window=5; /* max size of window */
490 else if (bits >= 128)
499 if (!BN_mod_mul(&(val[i]),&(val[i-1]),d,m,ctx))
504 start=1; /* This is used to avoid multiplication etc
505 * when there is only the value '1' in the
507 wvalue=0; /* The 'value' of the window */
508 wstart=bits-1; /* The top bit of the window */
509 wend=0; /* The bottom bit of the window */
511 if (!BN_one(r)) goto err;
515 if (BN_is_bit_set(p,wstart) == 0)
518 if (!BN_mod_mul(r,r,r,m,ctx))
520 if (wstart == 0) break;
524 /* We now have wstart on a 'set' bit, we now need to work out
525 * how bit a window to do. To do this we need to scan
526 * forward until the last set bit before the end of the
531 for (i=1; i<window; i++)
533 if (wstart-i < 0) break;
534 if (BN_is_bit_set(p,wstart-i))
542 /* wend is the size of the current window */
544 /* add the 'bytes above' */
548 if (!BN_mod_mul(r,r,r,m,ctx))
552 /* wvalue will be an odd number < 2^window */
553 if (!BN_mod_mul(r,r,&(val[wvalue>>1]),m,ctx))
556 /* move the 'window' down further */
560 if (wstart < 0) break;
566 BN_clear_free(&(val[i]));