1 /* crypto/bn/bn_exp.c */
2 /* Copyright (C) 1995-1997 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.]
64 int BN_mod_mul(ret
, a
, b
, m
, ctx
)
74 t
=ctx
->bn
[ctx
->tos
++];
76 { if (!BN_sqr(t
,a
,ctx
)) goto err
; }
78 { if (!BN_mul(t
,a
,b
)) goto err
; }
79 if (!BN_mod(ret
,t
,m
,ctx
)) goto err
;
87 /* this one works - simple but works */
88 int BN_mod_exp(r
,a
,p
,m
,ctx
)
95 v
=ctx
->bn
[ctx
->tos
++];
96 tmp
=ctx
->bn
[ctx
->tos
++];
98 if (BN_copy(v
,a
) == NULL
) goto err
;
102 { if (BN_copy(r
,a
) == NULL
) goto err
; }
103 else { if (BN_one(r
)) goto err
; }
105 for (i
=1; i
<bits
; i
++)
107 if (!BN_sqr(tmp
,v
,ctx
)) goto err
;
108 if (!BN_mod(v
,tmp
,m
,ctx
)) goto err
;
109 if (BN_is_bit_set(p
,i
))
111 if (!BN_mul(tmp
,r
,v
)) goto err
;
112 if (!BN_mod(r
,tmp
,m
,ctx
)) goto err
;
123 int BN_mod_exp(r
,a
,p
,m
,ctx
)
133 /* I have finally been able to take out this pre-condition of
134 * the top bit being set. It was caused by an error in BN_div
135 * with negatives. There was also another problem when for a^b%m
136 * a >= m. eay 07-May-97 */
137 /* if ((m->d[m->top-1]&BN_TBIT) && BN_is_odd(m)) */
140 { ret
=BN_mod_exp_mont(r
,a
,p
,m
,ctx
); }
144 { ret
=BN_mod_exp_recp(r
,a
,p
,m
,ctx
); }
146 { ret
=BN_mod_exp_simple(r
,a
,p
,m
,ctx
); }
152 /* #ifdef RECP_MUL_MOD */
153 int BN_mod_exp_recp(r
,a
,p
,m
,ctx
)
160 int nb
,i
,j
,bits
,ret
=0,wstart
,wend
,window
,wvalue
;
165 d
=ctx
->bn
[ctx
->tos
++];
166 aa
=ctx
->bn
[ctx
->tos
++];
174 nb
=BN_reciprocal(d
,m
,ctx
);
175 if (nb
== -1) goto err
;
178 if (!BN_mod(val
[0],a
,m
,ctx
)) goto err
; /* 1 */
179 if (!BN_mod_mul_reciprocal(aa
,val
[0],val
[0],m
,d
,nb
,ctx
))
182 if (bits
<= 17) /* This is probably 3 or 0x10001, so just do singles */
184 else if (bits
>= 256)
185 window
=5; /* max size of window */
186 else if (bits
>= 128)
195 if (!BN_mod_mul_reciprocal(val
[i
],val
[i
-1],aa
,m
,d
,nb
,ctx
))
201 start
=1; /* This is used to avoid multiplication etc
202 * when there is only the value '1' in the
204 wvalue
=0; /* The 'value' of the window */
205 wstart
=bits
-1; /* The top bit of the window */
206 wend
=0; /* The bottom bit of the window */
208 if (!BN_one(r
)) goto err
;
212 if (BN_is_bit_set(p
,wstart
) == 0)
215 if (!BN_mod_mul_reciprocal(r
,r
,r
,m
,d
,nb
,ctx
))
217 if (wstart
== 0) break;
221 /* We now have wstart on a 'set' bit, we now need to work out
222 * how bit a window to do. To do this we need to scan
223 * forward until the last set bit before the end of the
228 for (i
=1; i
<window
; i
++)
230 if (wstart
-i
< 0) break;
231 if (BN_is_bit_set(p
,wstart
-i
))
239 /* wend is the size of the current window */
241 /* add the 'bytes above' */
245 if (!BN_mod_mul_reciprocal(r
,r
,r
,m
,d
,nb
,ctx
))
249 /* wvalue will be an odd number < 2^window */
250 if (!BN_mod_mul_reciprocal(r
,r
,val
[wvalue
>>1],m
,d
,nb
,ctx
))
253 /* move the 'window' down further */
257 if (wstart
< 0) break;
263 if (val
[i
] != NULL
) BN_clear_free(val
[i
]);
268 /* #ifdef MONT_MUL_MOD */
269 int BN_mod_exp_mont(r
,a
,p
,m
,ctx
)
276 int i
,j
,bits
,ret
=0,wstart
,wend
,window
,wvalue
;
280 BN_MONT_CTX
*mont
=NULL
;
284 BNerr(BN_F_BN_MOD_EXP_MONT
,BN_R_CALLED_WITH_EVEN_MODULUS
);
287 d
=ctx
->bn
[ctx
->tos
++];
295 /* If this is not done, things will break in the montgomery
298 if ((mont
=BN_MONT_CTX_new()) == NULL
) goto err
;
299 if (!BN_MONT_CTX_set(mont
,m
,ctx
)) goto err
;
302 if (BN_ucmp(a
,m
) >= 0)
304 BN_mod(val
[0],a
,m
,ctx
);
309 if (!BN_to_montgomery(val
[0],aa
,mont
,ctx
)) goto err
; /* 1 */
310 if (!BN_mod_mul_montgomery(d
,val
[0],val
[0],mont
,ctx
)) goto err
; /* 2 */
312 if (bits
<= 17) /* This is probably 3 or 0x10001, so just do singles */
314 else if (bits
>= 256)
315 window
=5; /* max size of window */
316 else if (bits
>= 128)
325 if (!BN_mod_mul_montgomery(val
[i
],val
[i
-1],d
,mont
,ctx
))
331 start
=1; /* This is used to avoid multiplication etc
332 * when there is only the value '1' in the
334 wvalue
=0; /* The 'value' of the window */
335 wstart
=bits
-1; /* The top bit of the window */
336 wend
=0; /* The bottom bit of the window */
338 if (!BN_to_montgomery(r
,BN_value_one(),mont
,ctx
)) goto err
;
341 if (BN_is_bit_set(p
,wstart
) == 0)
344 if (!BN_mod_mul_montgomery(r
,r
,r
,mont
,ctx
))
346 if (wstart
== 0) break;
350 /* We now have wstart on a 'set' bit, we now need to work out
351 * how bit a window to do. To do this we need to scan
352 * forward until the last set bit before the end of the
357 for (i
=1; i
<window
; i
++)
359 if (wstart
-i
< 0) break;
360 if (BN_is_bit_set(p
,wstart
-i
))
368 /* wend is the size of the current window */
370 /* add the 'bytes above' */
374 if (!BN_mod_mul_montgomery(r
,r
,r
,mont
,ctx
))
378 /* wvalue will be an odd number < 2^window */
379 if (!BN_mod_mul_montgomery(r
,r
,val
[wvalue
>>1],mont
,ctx
))
382 /* move the 'window' down further */
386 if (wstart
< 0) break;
388 BN_from_montgomery(r
,r
,mont
,ctx
);
391 if (mont
!= NULL
) BN_MONT_CTX_free(mont
);
394 if (val
[i
] != NULL
) BN_clear_free(val
[i
]);
399 /* The old fallback, simple version :-) */
400 int BN_mod_exp_simple(r
,a
,p
,m
,ctx
)
407 int i
,j
,bits
,ret
=0,wstart
,wend
,window
,wvalue
;
412 d
=ctx
->bn
[ctx
->tos
++];
422 if (!BN_mod(val
[0],a
,m
,ctx
)) goto err
; /* 1 */
423 if (!BN_mod_mul(d
,val
[0],val
[0],m
,ctx
))
426 if (bits
<= 17) /* This is probably 3 or 0x10001, so just do singles */
428 else if (bits
>= 256)
429 window
=5; /* max size of window */
430 else if (bits
>= 128)
439 if (!BN_mod_mul(val
[i
],val
[i
-1],d
,m
,ctx
))
445 start
=1; /* This is used to avoid multiplication etc
446 * when there is only the value '1' in the
448 wvalue
=0; /* The 'value' of the window */
449 wstart
=bits
-1; /* The top bit of the window */
450 wend
=0; /* The bottom bit of the window */
452 if (!BN_one(r
)) goto err
;
456 if (BN_is_bit_set(p
,wstart
) == 0)
459 if (!BN_mod_mul(r
,r
,r
,m
,ctx
))
461 if (wstart
== 0) break;
465 /* We now have wstart on a 'set' bit, we now need to work out
466 * how bit a window to do. To do this we need to scan
467 * forward until the last set bit before the end of the
472 for (i
=1; i
<window
; i
++)
474 if (wstart
-i
< 0) break;
475 if (BN_is_bit_set(p
,wstart
-i
))
483 /* wend is the size of the current window */
485 /* add the 'bytes above' */
489 if (!BN_mod_mul(r
,r
,r
,m
,ctx
))
493 /* wvalue will be an odd number < 2^window */
494 if (!BN_mod_mul(r
,r
,val
[wvalue
>>1],m
,ctx
))
497 /* move the 'window' down further */
501 if (wstart
< 0) break;
507 if (val
[i
] != NULL
) BN_clear_free(val
[i
]);