10Sstevel@tonic-gate /* crypto/bn/bn_recp.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 #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include "bn_lcl.h"
620Sstevel@tonic-gate
BN_RECP_CTX_init(BN_RECP_CTX * recp)630Sstevel@tonic-gate void BN_RECP_CTX_init(BN_RECP_CTX *recp)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate BN_init(&(recp->N));
660Sstevel@tonic-gate BN_init(&(recp->Nr));
670Sstevel@tonic-gate recp->num_bits=0;
680Sstevel@tonic-gate recp->flags=0;
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
BN_RECP_CTX_new(void)710Sstevel@tonic-gate BN_RECP_CTX *BN_RECP_CTX_new(void)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate BN_RECP_CTX *ret;
740Sstevel@tonic-gate
750Sstevel@tonic-gate if ((ret=(BN_RECP_CTX *)OPENSSL_malloc(sizeof(BN_RECP_CTX))) == NULL)
760Sstevel@tonic-gate return(NULL);
770Sstevel@tonic-gate
780Sstevel@tonic-gate BN_RECP_CTX_init(ret);
790Sstevel@tonic-gate ret->flags=BN_FLG_MALLOCED;
800Sstevel@tonic-gate return(ret);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
BN_RECP_CTX_free(BN_RECP_CTX * recp)830Sstevel@tonic-gate void BN_RECP_CTX_free(BN_RECP_CTX *recp)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate if(recp == NULL)
860Sstevel@tonic-gate return;
870Sstevel@tonic-gate
880Sstevel@tonic-gate BN_free(&(recp->N));
890Sstevel@tonic-gate BN_free(&(recp->Nr));
900Sstevel@tonic-gate if (recp->flags & BN_FLG_MALLOCED)
910Sstevel@tonic-gate OPENSSL_free(recp);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
BN_RECP_CTX_set(BN_RECP_CTX * recp,const BIGNUM * d,BN_CTX * ctx)940Sstevel@tonic-gate int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate if (!BN_copy(&(recp->N),d)) return 0;
97*2139Sjp161948 BN_zero(&(recp->Nr));
980Sstevel@tonic-gate recp->num_bits=BN_num_bits(d);
990Sstevel@tonic-gate recp->shift=0;
1000Sstevel@tonic-gate return(1);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
BN_mod_mul_reciprocal(BIGNUM * r,const BIGNUM * x,const BIGNUM * y,BN_RECP_CTX * recp,BN_CTX * ctx)1030Sstevel@tonic-gate int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
1040Sstevel@tonic-gate BN_RECP_CTX *recp, BN_CTX *ctx)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate int ret=0;
1070Sstevel@tonic-gate BIGNUM *a;
1080Sstevel@tonic-gate const BIGNUM *ca;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate BN_CTX_start(ctx);
1110Sstevel@tonic-gate if ((a = BN_CTX_get(ctx)) == NULL) goto err;
1120Sstevel@tonic-gate if (y != NULL)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate if (x == y)
1150Sstevel@tonic-gate { if (!BN_sqr(a,x,ctx)) goto err; }
1160Sstevel@tonic-gate else
1170Sstevel@tonic-gate { if (!BN_mul(a,x,y,ctx)) goto err; }
1180Sstevel@tonic-gate ca = a;
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate else
1210Sstevel@tonic-gate ca=x; /* Just do the mod */
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate ret = BN_div_recp(NULL,r,ca,recp,ctx);
1240Sstevel@tonic-gate err:
1250Sstevel@tonic-gate BN_CTX_end(ctx);
126*2139Sjp161948 bn_check_top(r);
1270Sstevel@tonic-gate return(ret);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
BN_div_recp(BIGNUM * dv,BIGNUM * rem,const BIGNUM * m,BN_RECP_CTX * recp,BN_CTX * ctx)1300Sstevel@tonic-gate int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
1310Sstevel@tonic-gate BN_RECP_CTX *recp, BN_CTX *ctx)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate int i,j,ret=0;
1340Sstevel@tonic-gate BIGNUM *a,*b,*d,*r;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate BN_CTX_start(ctx);
1370Sstevel@tonic-gate a=BN_CTX_get(ctx);
1380Sstevel@tonic-gate b=BN_CTX_get(ctx);
1390Sstevel@tonic-gate if (dv != NULL)
1400Sstevel@tonic-gate d=dv;
1410Sstevel@tonic-gate else
1420Sstevel@tonic-gate d=BN_CTX_get(ctx);
1430Sstevel@tonic-gate if (rem != NULL)
1440Sstevel@tonic-gate r=rem;
1450Sstevel@tonic-gate else
1460Sstevel@tonic-gate r=BN_CTX_get(ctx);
1470Sstevel@tonic-gate if (a == NULL || b == NULL || d == NULL || r == NULL) goto err;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate if (BN_ucmp(m,&(recp->N)) < 0)
1500Sstevel@tonic-gate {
151*2139Sjp161948 BN_zero(d);
1520Sstevel@tonic-gate if (!BN_copy(r,m)) return 0;
1530Sstevel@tonic-gate BN_CTX_end(ctx);
1540Sstevel@tonic-gate return(1);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /* We want the remainder
1580Sstevel@tonic-gate * Given input of ABCDEF / ab
1590Sstevel@tonic-gate * we need multiply ABCDEF by 3 digests of the reciprocal of ab
1600Sstevel@tonic-gate *
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */
1640Sstevel@tonic-gate i=BN_num_bits(m);
1650Sstevel@tonic-gate j=recp->num_bits<<1;
1660Sstevel@tonic-gate if (j>i) i=j;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /* Nr := round(2^i / N) */
1690Sstevel@tonic-gate if (i != recp->shift)
1700Sstevel@tonic-gate recp->shift=BN_reciprocal(&(recp->Nr),&(recp->N),
1710Sstevel@tonic-gate i,ctx); /* BN_reciprocal returns i, or -1 for an error */
1720Sstevel@tonic-gate if (recp->shift == -1) goto err;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /* d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))|
1750Sstevel@tonic-gate * = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))|
1760Sstevel@tonic-gate * <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)|
1770Sstevel@tonic-gate * = |m/N|
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate if (!BN_rshift(a,m,recp->num_bits)) goto err;
1800Sstevel@tonic-gate if (!BN_mul(b,a,&(recp->Nr),ctx)) goto err;
1810Sstevel@tonic-gate if (!BN_rshift(d,b,i-recp->num_bits)) goto err;
1820Sstevel@tonic-gate d->neg=0;
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate if (!BN_mul(b,&(recp->N),d,ctx)) goto err;
1850Sstevel@tonic-gate if (!BN_usub(r,m,b)) goto err;
1860Sstevel@tonic-gate r->neg=0;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate #if 1
1890Sstevel@tonic-gate j=0;
1900Sstevel@tonic-gate while (BN_ucmp(r,&(recp->N)) >= 0)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate if (j++ > 2)
1930Sstevel@tonic-gate {
194*2139Sjp161948 BNerr(BN_F_BN_DIV_RECP,BN_R_BAD_RECIPROCAL);
1950Sstevel@tonic-gate goto err;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate if (!BN_usub(r,r,&(recp->N))) goto err;
1980Sstevel@tonic-gate if (!BN_add_word(d,1)) goto err;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate #endif
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate r->neg=BN_is_zero(r)?0:m->neg;
2030Sstevel@tonic-gate d->neg=m->neg^recp->N.neg;
2040Sstevel@tonic-gate ret=1;
2050Sstevel@tonic-gate err:
2060Sstevel@tonic-gate BN_CTX_end(ctx);
207*2139Sjp161948 bn_check_top(dv);
208*2139Sjp161948 bn_check_top(rem);
2090Sstevel@tonic-gate return(ret);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /* len is the expected size of the result
2130Sstevel@tonic-gate * We actually calculate with an extra word of precision, so
2140Sstevel@tonic-gate * we can do faster division if the remainder is not required.
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate /* r := 2^len / m */
BN_reciprocal(BIGNUM * r,const BIGNUM * m,int len,BN_CTX * ctx)2170Sstevel@tonic-gate int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate int ret= -1;
220*2139Sjp161948 BIGNUM *t;
2210Sstevel@tonic-gate
222*2139Sjp161948 BN_CTX_start(ctx);
223*2139Sjp161948 if((t = BN_CTX_get(ctx)) == NULL) goto err;
2240Sstevel@tonic-gate
225*2139Sjp161948 if (!BN_set_bit(t,len)) goto err;
226*2139Sjp161948
227*2139Sjp161948 if (!BN_div(r,NULL,t,m,ctx)) goto err;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate ret=len;
2300Sstevel@tonic-gate err:
231*2139Sjp161948 bn_check_top(r);
232*2139Sjp161948 BN_CTX_end(ctx);
2330Sstevel@tonic-gate return(ret);
2340Sstevel@tonic-gate }
235