10Sstevel@tonic-gate /* crypto/bn/bn_exp2.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate * are met:
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate * the documentation and/or other materials provided with the
710Sstevel@tonic-gate * distribution.
720Sstevel@tonic-gate *
730Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate * software must display the following acknowledgment:
750Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate * endorse or promote products derived from this software without
800Sstevel@tonic-gate * prior written permission. For written permission, please contact
810Sstevel@tonic-gate * openssl-core@openssl.org.
820Sstevel@tonic-gate *
830Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate * permission of the OpenSSL Project.
860Sstevel@tonic-gate *
870Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate * acknowledgment:
890Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate * ====================================================================
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
1080Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate *
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include "cryptlib.h"
1140Sstevel@tonic-gate #include "bn_lcl.h"
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate #define TABLE_SIZE 32
1170Sstevel@tonic-gate
BN_mod_exp2_mont(BIGNUM * rr,const BIGNUM * a1,const BIGNUM * p1,const BIGNUM * a2,const BIGNUM * p2,const BIGNUM * m,BN_CTX * ctx,BN_MONT_CTX * in_mont)1180Sstevel@tonic-gate int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
1190Sstevel@tonic-gate const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
1200Sstevel@tonic-gate BN_CTX *ctx, BN_MONT_CTX *in_mont)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate int i,j,bits,b,bits1,bits2,ret=0,wpos1,wpos2,window1,window2,wvalue1,wvalue2;
123*2139Sjp161948 int r_is_one=1;
1240Sstevel@tonic-gate BIGNUM *d,*r;
1250Sstevel@tonic-gate const BIGNUM *a_mod_m;
126*2139Sjp161948 /* Tables of variables obtained from 'ctx' */
127*2139Sjp161948 BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];
1280Sstevel@tonic-gate BN_MONT_CTX *mont=NULL;
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate bn_check_top(a1);
1310Sstevel@tonic-gate bn_check_top(p1);
1320Sstevel@tonic-gate bn_check_top(a2);
1330Sstevel@tonic-gate bn_check_top(p2);
1340Sstevel@tonic-gate bn_check_top(m);
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate if (!(m->d[0] & 1))
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate BNerr(BN_F_BN_MOD_EXP2_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);
1390Sstevel@tonic-gate return(0);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate bits1=BN_num_bits(p1);
1420Sstevel@tonic-gate bits2=BN_num_bits(p2);
1430Sstevel@tonic-gate if ((bits1 == 0) && (bits2 == 0))
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate ret = BN_one(rr);
1460Sstevel@tonic-gate return ret;
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate bits=(bits1 > bits2)?bits1:bits2;
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate BN_CTX_start(ctx);
1520Sstevel@tonic-gate d = BN_CTX_get(ctx);
1530Sstevel@tonic-gate r = BN_CTX_get(ctx);
154*2139Sjp161948 val1[0] = BN_CTX_get(ctx);
155*2139Sjp161948 val2[0] = BN_CTX_get(ctx);
156*2139Sjp161948 if(!d || !r || !val1[0] || !val2[0]) goto err;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate if (in_mont != NULL)
1590Sstevel@tonic-gate mont=in_mont;
1600Sstevel@tonic-gate else
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
1630Sstevel@tonic-gate if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate window1 = BN_window_bits_for_exponent_size(bits1);
1670Sstevel@tonic-gate window2 = BN_window_bits_for_exponent_size(bits2);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Build table for a1: val1[i] := a1^(2*i + 1) mod m for i = 0 .. 2^(window1-1)
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate if (a1->neg || BN_ucmp(a1,m) >= 0)
1730Sstevel@tonic-gate {
174*2139Sjp161948 if (!BN_mod(val1[0],a1,m,ctx))
1750Sstevel@tonic-gate goto err;
176*2139Sjp161948 a_mod_m = val1[0];
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate else
1790Sstevel@tonic-gate a_mod_m = a1;
1800Sstevel@tonic-gate if (BN_is_zero(a_mod_m))
1810Sstevel@tonic-gate {
182*2139Sjp161948 BN_zero(rr);
183*2139Sjp161948 ret = 1;
1840Sstevel@tonic-gate goto err;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
187*2139Sjp161948 if (!BN_to_montgomery(val1[0],a_mod_m,mont,ctx)) goto err;
1880Sstevel@tonic-gate if (window1 > 1)
1890Sstevel@tonic-gate {
190*2139Sjp161948 if (!BN_mod_mul_montgomery(d,val1[0],val1[0],mont,ctx)) goto err;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate j=1<<(window1-1);
1930Sstevel@tonic-gate for (i=1; i<j; i++)
1940Sstevel@tonic-gate {
195*2139Sjp161948 if(((val1[i] = BN_CTX_get(ctx)) == NULL) ||
196*2139Sjp161948 !BN_mod_mul_montgomery(val1[i],val1[i-1],
197*2139Sjp161948 d,mont,ctx))
1980Sstevel@tonic-gate goto err;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * Build table for a2: val2[i] := a2^(2*i + 1) mod m for i = 0 .. 2^(window2-1)
2050Sstevel@tonic-gate */
2060Sstevel@tonic-gate if (a2->neg || BN_ucmp(a2,m) >= 0)
2070Sstevel@tonic-gate {
208*2139Sjp161948 if (!BN_mod(val2[0],a2,m,ctx))
2090Sstevel@tonic-gate goto err;
210*2139Sjp161948 a_mod_m = val2[0];
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate else
2130Sstevel@tonic-gate a_mod_m = a2;
2140Sstevel@tonic-gate if (BN_is_zero(a_mod_m))
2150Sstevel@tonic-gate {
216*2139Sjp161948 BN_zero(rr);
217*2139Sjp161948 ret = 1;
2180Sstevel@tonic-gate goto err;
2190Sstevel@tonic-gate }
220*2139Sjp161948 if (!BN_to_montgomery(val2[0],a_mod_m,mont,ctx)) goto err;
2210Sstevel@tonic-gate if (window2 > 1)
2220Sstevel@tonic-gate {
223*2139Sjp161948 if (!BN_mod_mul_montgomery(d,val2[0],val2[0],mont,ctx)) goto err;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate j=1<<(window2-1);
2260Sstevel@tonic-gate for (i=1; i<j; i++)
2270Sstevel@tonic-gate {
228*2139Sjp161948 if(((val2[i] = BN_CTX_get(ctx)) == NULL) ||
229*2139Sjp161948 !BN_mod_mul_montgomery(val2[i],val2[i-1],
230*2139Sjp161948 d,mont,ctx))
2310Sstevel@tonic-gate goto err;
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /* Now compute the power product, using independent windows. */
2370Sstevel@tonic-gate r_is_one=1;
2380Sstevel@tonic-gate wvalue1=0; /* The 'value' of the first window */
2390Sstevel@tonic-gate wvalue2=0; /* The 'value' of the second window */
2400Sstevel@tonic-gate wpos1=0; /* If wvalue1 > 0, the bottom bit of the first window */
2410Sstevel@tonic-gate wpos2=0; /* If wvalue2 > 0, the bottom bit of the second window */
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;
2440Sstevel@tonic-gate for (b=bits-1; b>=0; b--)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate if (!r_is_one)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
2490Sstevel@tonic-gate goto err;
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate if (!wvalue1)
2530Sstevel@tonic-gate if (BN_is_bit_set(p1, b))
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate /* consider bits b-window1+1 .. b for this window */
2560Sstevel@tonic-gate i = b-window1+1;
2570Sstevel@tonic-gate while (!BN_is_bit_set(p1, i)) /* works for i<0 */
2580Sstevel@tonic-gate i++;
2590Sstevel@tonic-gate wpos1 = i;
2600Sstevel@tonic-gate wvalue1 = 1;
2610Sstevel@tonic-gate for (i = b-1; i >= wpos1; i--)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate wvalue1 <<= 1;
2640Sstevel@tonic-gate if (BN_is_bit_set(p1, i))
2650Sstevel@tonic-gate wvalue1++;
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate if (!wvalue2)
2700Sstevel@tonic-gate if (BN_is_bit_set(p2, b))
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate /* consider bits b-window2+1 .. b for this window */
2730Sstevel@tonic-gate i = b-window2+1;
2740Sstevel@tonic-gate while (!BN_is_bit_set(p2, i))
2750Sstevel@tonic-gate i++;
2760Sstevel@tonic-gate wpos2 = i;
2770Sstevel@tonic-gate wvalue2 = 1;
2780Sstevel@tonic-gate for (i = b-1; i >= wpos2; i--)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate wvalue2 <<= 1;
2810Sstevel@tonic-gate if (BN_is_bit_set(p2, i))
2820Sstevel@tonic-gate wvalue2++;
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate if (wvalue1 && b == wpos1)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate /* wvalue1 is odd and < 2^window1 */
289*2139Sjp161948 if (!BN_mod_mul_montgomery(r,r,val1[wvalue1>>1],mont,ctx))
2900Sstevel@tonic-gate goto err;
2910Sstevel@tonic-gate wvalue1 = 0;
2920Sstevel@tonic-gate r_is_one = 0;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate if (wvalue2 && b == wpos2)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate /* wvalue2 is odd and < 2^window2 */
298*2139Sjp161948 if (!BN_mod_mul_montgomery(r,r,val2[wvalue2>>1],mont,ctx))
2990Sstevel@tonic-gate goto err;
3000Sstevel@tonic-gate wvalue2 = 0;
3010Sstevel@tonic-gate r_is_one = 0;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate BN_from_montgomery(rr,r,mont,ctx);
3050Sstevel@tonic-gate ret=1;
3060Sstevel@tonic-gate err:
3070Sstevel@tonic-gate if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
3080Sstevel@tonic-gate BN_CTX_end(ctx);
309*2139Sjp161948 bn_check_top(rr);
3100Sstevel@tonic-gate return(ret);
3110Sstevel@tonic-gate }
312