sl@0: /* crypto/bn/bn_lib.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #ifndef BN_DEBUG sl@0: # undef NDEBUG /* avoid conflicting definitions */ sl@0: # define NDEBUG sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include "bn_lcl.h" sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: const char BN_version[]="Big Number" OPENSSL_VERSION_PTEXT; sl@0: sl@0: /* This stuff appears to be completely unused, so is deprecated */ sl@0: #ifndef OPENSSL_NO_DEPRECATED sl@0: /* For a 32 bit machine sl@0: * 2 - 4 == 128 sl@0: * 3 - 8 == 256 sl@0: * 4 - 16 == 512 sl@0: * 5 - 32 == 1024 sl@0: * 6 - 64 == 2048 sl@0: * 7 - 128 == 4096 sl@0: * 8 - 256 == 8192 sl@0: */ sl@0: sl@0: static int bn_limit_bits=0; sl@0: static int bn_limit_num=8; /* (1<= 0) sl@0: { sl@0: if (mult > (int)(sizeof(int)*8)-1) sl@0: mult=sizeof(int)*8-1; sl@0: bn_limit_bits=mult; sl@0: bn_limit_num=1<= 0) sl@0: { sl@0: if (high > (int)(sizeof(int)*8)-1) sl@0: high=sizeof(int)*8-1; sl@0: bn_limit_bits_high=high; sl@0: bn_limit_num_high=1<= 0) sl@0: { sl@0: if (low > (int)(sizeof(int)*8)-1) sl@0: low=sizeof(int)*8-1; sl@0: bn_limit_bits_low=low; sl@0: bn_limit_num_low=1<= 0) sl@0: { sl@0: if (mont > (int)(sizeof(int)*8)-1) sl@0: mont=sizeof(int)*8-1; sl@0: bn_limit_bits_mont=mont; sl@0: bn_limit_num_mont=1<>56)]+56); sl@0: } sl@0: else return(bits[(int)(l>>48)]+48); sl@0: } sl@0: else sl@0: { sl@0: if (l & 0x0000ff0000000000L) sl@0: { sl@0: return(bits[(int)(l>>40)]+40); sl@0: } sl@0: else return(bits[(int)(l>>32)]+32); sl@0: } sl@0: } sl@0: else sl@0: #else sl@0: #ifdef SIXTY_FOUR_BIT sl@0: if (l & 0xffffffff00000000LL) sl@0: { sl@0: if (l & 0xffff000000000000LL) sl@0: { sl@0: if (l & 0xff00000000000000LL) sl@0: { sl@0: return(bits[(int)(l>>56)]+56); sl@0: } sl@0: else return(bits[(int)(l>>48)]+48); sl@0: } sl@0: else sl@0: { sl@0: if (l & 0x0000ff0000000000LL) sl@0: { sl@0: return(bits[(int)(l>>40)]+40); sl@0: } sl@0: else return(bits[(int)(l>>32)]+32); sl@0: } sl@0: } sl@0: else sl@0: #endif sl@0: #endif sl@0: { sl@0: #if defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) sl@0: if (l & 0xffff0000L) sl@0: { sl@0: if (l & 0xff000000L) sl@0: return(bits[(int)(l>>24L)]+24); sl@0: else return(bits[(int)(l>>16L)]+16); sl@0: } sl@0: else sl@0: #endif sl@0: { sl@0: #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) sl@0: if (l & 0xff00L) sl@0: return(bits[(int)(l>>8)]+8); sl@0: else sl@0: #endif sl@0: return(bits[(int)(l )] ); sl@0: } sl@0: } sl@0: } sl@0: sl@0: EXPORT_C int BN_num_bits(const BIGNUM *a) sl@0: { sl@0: int i = a->top - 1; sl@0: bn_check_top(a); sl@0: sl@0: if (BN_is_zero(a)) return 0; sl@0: return ((i*BN_BITS2) + BN_num_bits_word(a->d[i])); sl@0: } sl@0: sl@0: EXPORT_C void BN_clear_free(BIGNUM *a) sl@0: { sl@0: int i; sl@0: sl@0: if (a == NULL) return; sl@0: bn_check_top(a); sl@0: if (a->d != NULL) sl@0: { sl@0: OPENSSL_cleanse(a->d,a->dmax*sizeof(a->d[0])); sl@0: if (!(BN_get_flags(a,BN_FLG_STATIC_DATA))) sl@0: OPENSSL_free(a->d); sl@0: } sl@0: i=BN_get_flags(a,BN_FLG_MALLOCED); sl@0: OPENSSL_cleanse(a,sizeof(BIGNUM)); sl@0: if (i) sl@0: OPENSSL_free(a); sl@0: } sl@0: sl@0: EXPORT_C void BN_free(BIGNUM *a) sl@0: { sl@0: if (a == NULL) return; sl@0: bn_check_top(a); sl@0: if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA))) sl@0: OPENSSL_free(a->d); sl@0: if (a->flags & BN_FLG_MALLOCED) sl@0: OPENSSL_free(a); sl@0: else sl@0: { sl@0: #ifndef OPENSSL_NO_DEPRECATED sl@0: a->flags|=BN_FLG_FREE; sl@0: #endif sl@0: a->d = NULL; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void BN_init(BIGNUM *a) sl@0: { sl@0: memset(a,0,sizeof(BIGNUM)); sl@0: bn_check_top(a); sl@0: } sl@0: sl@0: EXPORT_C BIGNUM *BN_new(void) sl@0: { sl@0: BIGNUM *ret; sl@0: sl@0: if ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL) sl@0: { sl@0: BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: ret->flags=BN_FLG_MALLOCED; sl@0: ret->top=0; sl@0: ret->neg=0; sl@0: ret->dmax=0; sl@0: ret->d=NULL; sl@0: bn_check_top(ret); sl@0: return(ret); sl@0: } sl@0: sl@0: /* This is used both by bn_expand2() and bn_dup_expand() */ sl@0: /* The caller MUST check that words > b->dmax before calling this */ sl@0: static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) sl@0: { sl@0: BN_ULONG *A,*a = NULL; sl@0: const BN_ULONG *B; sl@0: int i; sl@0: sl@0: bn_check_top(b); sl@0: sl@0: if (words > (INT_MAX/(4*BN_BITS2))) sl@0: { sl@0: BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_BIGNUM_TOO_LONG); sl@0: return NULL; sl@0: } sl@0: if (BN_get_flags(b,BN_FLG_STATIC_DATA)) sl@0: { sl@0: BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); sl@0: return(NULL); sl@0: } sl@0: a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*words); sl@0: if (A == NULL) sl@0: { sl@0: BNerr(BN_F_BN_EXPAND_INTERNAL,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: #if 1 sl@0: B=b->d; sl@0: /* Check if the previous number needs to be copied */ sl@0: if (B != NULL) sl@0: { sl@0: for (i=b->top>>2; i>0; i--,A+=4,B+=4) sl@0: { sl@0: /* sl@0: * The fact that the loop is unrolled sl@0: * 4-wise is a tribute to Intel. It's sl@0: * the one that doesn't have enough sl@0: * registers to accomodate more data. sl@0: * I'd unroll it 8-wise otherwise:-) sl@0: * sl@0: * sl@0: */ sl@0: BN_ULONG a0,a1,a2,a3; sl@0: a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3]; sl@0: A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3; sl@0: } sl@0: switch (b->top&3) sl@0: { sl@0: case 3: A[2]=B[2]; sl@0: case 2: A[1]=B[1]; sl@0: case 1: A[0]=B[0]; sl@0: case 0: /* workaround for ultrix cc: without 'case 0', the optimizer does sl@0: * the switch table by doing a=top&3; a--; goto jump_table[a]; sl@0: * which fails for top== 0 */ sl@0: ; sl@0: } sl@0: } sl@0: sl@0: #else sl@0: memset(A,0,sizeof(BN_ULONG)*words); sl@0: memcpy(A,b->d,sizeof(b->d[0])*b->top); sl@0: #endif sl@0: sl@0: return(a); sl@0: } sl@0: sl@0: /* This is an internal function that can be used instead of bn_expand2() sl@0: * when there is a need to copy BIGNUMs instead of only expanding the sl@0: * data part, while still expanding them. sl@0: * Especially useful when needing to expand BIGNUMs that are declared sl@0: * 'const' and should therefore not be changed. sl@0: * The reason to use this instead of a BN_dup() followed by a bn_expand2() sl@0: * is memory allocation overhead. A BN_dup() followed by a bn_expand2() sl@0: * will allocate new memory for the BIGNUM data twice, and free it once, sl@0: * while bn_dup_expand() makes sure allocation is made only once. sl@0: */ sl@0: sl@0: #ifndef OPENSSL_NO_DEPRECATED sl@0: EXPORT_C BIGNUM *bn_dup_expand(const BIGNUM *b, int words) sl@0: { sl@0: BIGNUM *r = NULL; sl@0: sl@0: bn_check_top(b); sl@0: sl@0: /* This function does not work if sl@0: * words <= b->dmax && top < words sl@0: * because BN_dup() does not preserve 'dmax'! sl@0: * (But bn_dup_expand() is not used anywhere yet.) sl@0: */ sl@0: sl@0: if (words > b->dmax) sl@0: { sl@0: BN_ULONG *a = bn_expand_internal(b, words); sl@0: sl@0: if (a) sl@0: { sl@0: r = BN_new(); sl@0: if (r) sl@0: { sl@0: r->top = b->top; sl@0: r->dmax = words; sl@0: r->neg = b->neg; sl@0: r->d = a; sl@0: } sl@0: else sl@0: { sl@0: /* r == NULL, BN_new failure */ sl@0: OPENSSL_free(a); sl@0: } sl@0: } sl@0: /* If a == NULL, there was an error in allocation in sl@0: bn_expand_internal(), and NULL should be returned */ sl@0: } sl@0: else sl@0: { sl@0: r = BN_dup(b); sl@0: } sl@0: sl@0: bn_check_top(r); sl@0: return r; sl@0: } sl@0: #endif sl@0: sl@0: /* This is an internal function that should not be used in applications. sl@0: * It ensures that 'b' has enough room for a 'words' word number sl@0: * and initialises any unused part of b->d with leading zeros. sl@0: * It is mostly used by the various BIGNUM routines. If there is an error, sl@0: * NULL is returned. If not, 'b' is returned. */ sl@0: sl@0: EXPORT_C BIGNUM *bn_expand2(BIGNUM *b, int words) sl@0: { sl@0: bn_check_top(b); sl@0: sl@0: if (words > b->dmax) sl@0: { sl@0: BN_ULONG *a = bn_expand_internal(b, words); sl@0: if(!a) return NULL; sl@0: if(b->d) OPENSSL_free(b->d); sl@0: b->d=a; sl@0: b->dmax=words; sl@0: } sl@0: sl@0: /* None of this should be necessary because of what b->top means! */ sl@0: #if 0 sl@0: /* NB: bn_wexpand() calls this only if the BIGNUM really has to grow */ sl@0: if (b->top < b->dmax) sl@0: { sl@0: int i; sl@0: BN_ULONG *A = &(b->d[b->top]); sl@0: for (i=(b->dmax - b->top)>>3; i>0; i--,A+=8) sl@0: { sl@0: A[0]=0; A[1]=0; A[2]=0; A[3]=0; sl@0: A[4]=0; A[5]=0; A[6]=0; A[7]=0; sl@0: } sl@0: for (i=(b->dmax - b->top)&7; i>0; i--,A++) sl@0: A[0]=0; sl@0: assert(A == &(b->d[b->dmax])); sl@0: } sl@0: #endif sl@0: bn_check_top(b); sl@0: return b; sl@0: } sl@0: sl@0: EXPORT_C BIGNUM *BN_dup(const BIGNUM *a) sl@0: { sl@0: BIGNUM *t; sl@0: sl@0: if (a == NULL) return NULL; sl@0: bn_check_top(a); sl@0: sl@0: t = BN_new(); sl@0: if (t == NULL) return NULL; sl@0: if(!BN_copy(t, a)) sl@0: { sl@0: BN_free(t); sl@0: return NULL; sl@0: } sl@0: bn_check_top(t); sl@0: return t; sl@0: } sl@0: sl@0: EXPORT_C BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) sl@0: { sl@0: int i; sl@0: BN_ULONG *A; sl@0: const BN_ULONG *B; sl@0: sl@0: bn_check_top(b); sl@0: sl@0: if (a == b) return(a); sl@0: if (bn_wexpand(a,b->top) == NULL) return(NULL); sl@0: sl@0: #if 1 sl@0: A=a->d; sl@0: B=b->d; sl@0: for (i=b->top>>2; i>0; i--,A+=4,B+=4) sl@0: { sl@0: BN_ULONG a0,a1,a2,a3; sl@0: a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3]; sl@0: A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3; sl@0: } sl@0: switch (b->top&3) sl@0: { sl@0: case 3: A[2]=B[2]; sl@0: case 2: A[1]=B[1]; sl@0: case 1: A[0]=B[0]; sl@0: case 0: ; /* ultrix cc workaround, see comments in bn_expand_internal */ sl@0: } sl@0: #else sl@0: memcpy(a->d,b->d,sizeof(b->d[0])*b->top); sl@0: #endif sl@0: sl@0: a->top=b->top; sl@0: a->neg=b->neg; sl@0: bn_check_top(a); sl@0: return(a); sl@0: } sl@0: sl@0: EXPORT_C void BN_swap(BIGNUM *a, BIGNUM *b) sl@0: { sl@0: int flags_old_a, flags_old_b; sl@0: BN_ULONG *tmp_d; sl@0: int tmp_top, tmp_dmax, tmp_neg; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: flags_old_a = a->flags; sl@0: flags_old_b = b->flags; sl@0: sl@0: tmp_d = a->d; sl@0: tmp_top = a->top; sl@0: tmp_dmax = a->dmax; sl@0: tmp_neg = a->neg; sl@0: sl@0: a->d = b->d; sl@0: a->top = b->top; sl@0: a->dmax = b->dmax; sl@0: a->neg = b->neg; sl@0: sl@0: b->d = tmp_d; sl@0: b->top = tmp_top; sl@0: b->dmax = tmp_dmax; sl@0: b->neg = tmp_neg; sl@0: sl@0: a->flags = (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA); sl@0: b->flags = (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA); sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: } sl@0: sl@0: EXPORT_C void BN_clear(BIGNUM *a) sl@0: { sl@0: bn_check_top(a); sl@0: if (a->d != NULL) sl@0: memset(a->d,0,a->dmax*sizeof(a->d[0])); sl@0: a->top=0; sl@0: a->neg=0; sl@0: } sl@0: sl@0: EXPORT_C BN_ULONG BN_get_word(const BIGNUM *a) sl@0: { sl@0: if (a->top > 1) sl@0: return BN_MASK2; sl@0: else if (a->top == 1) sl@0: return a->d[0]; sl@0: /* a->top == 0 */ sl@0: return 0; sl@0: } sl@0: sl@0: EXPORT_C int BN_set_word(BIGNUM *a, BN_ULONG w) sl@0: { sl@0: bn_check_top(a); sl@0: if (bn_expand(a,(int)sizeof(BN_ULONG)*8) == NULL) return(0); sl@0: a->neg = 0; sl@0: a->d[0] = w; sl@0: a->top = (w ? 1 : 0); sl@0: bn_check_top(a); sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) sl@0: { sl@0: unsigned int i,m; sl@0: unsigned int n; sl@0: BN_ULONG l; sl@0: BIGNUM *bn = NULL; sl@0: sl@0: if (ret == NULL) sl@0: ret = bn = BN_new(); sl@0: if (ret == NULL) return(NULL); sl@0: bn_check_top(ret); sl@0: l=0; sl@0: n=len; sl@0: if (n == 0) sl@0: { sl@0: ret->top=0; sl@0: return(ret); sl@0: } sl@0: i=((n-1)/BN_BYTES)+1; sl@0: m=((n-1)%(BN_BYTES)); sl@0: if (bn_wexpand(ret, (int)i) == NULL) sl@0: { sl@0: if (bn) BN_free(bn); sl@0: return NULL; sl@0: } sl@0: ret->top=i; sl@0: ret->neg=0; sl@0: while (n--) sl@0: { sl@0: l=(l<<8L)| *(s++); sl@0: if (m-- == 0) sl@0: { sl@0: ret->d[--i]=l; sl@0: l=0; sl@0: m=BN_BYTES-1; sl@0: } sl@0: } sl@0: /* need to call this due to clear byte at top if avoiding sl@0: * having the top bit set (-ve number) */ sl@0: bn_correct_top(ret); sl@0: return(ret); sl@0: } sl@0: sl@0: /* ignore negative */ sl@0: EXPORT_C int BN_bn2bin(const BIGNUM *a, unsigned char *to) sl@0: { sl@0: int n,i; sl@0: BN_ULONG l; sl@0: sl@0: bn_check_top(a); sl@0: n=i=BN_num_bytes(a); sl@0: while (i--) sl@0: { sl@0: l=a->d[i/BN_BYTES]; sl@0: *(to++)=(unsigned char)(l>>(8*(i%BN_BYTES)))&0xff; sl@0: } sl@0: return(n); sl@0: } sl@0: sl@0: EXPORT_C int BN_ucmp(const BIGNUM *a, const BIGNUM *b) sl@0: { sl@0: int i; sl@0: BN_ULONG t1,t2,*ap,*bp; sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: i=a->top-b->top; sl@0: if (i != 0) return(i); sl@0: ap=a->d; sl@0: bp=b->d; sl@0: for (i=a->top-1; i>=0; i--) sl@0: { sl@0: t1= ap[i]; sl@0: t2= bp[i]; sl@0: if (t1 != t2) sl@0: return((t1 > t2) ? 1 : -1); sl@0: } sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int BN_cmp(const BIGNUM *a, const BIGNUM *b) sl@0: { sl@0: int i; sl@0: int gt,lt; sl@0: BN_ULONG t1,t2; sl@0: sl@0: if ((a == NULL) || (b == NULL)) sl@0: { sl@0: if (a != NULL) sl@0: return(-1); sl@0: else if (b != NULL) sl@0: return(1); sl@0: else sl@0: return(0); sl@0: } sl@0: sl@0: bn_check_top(a); sl@0: bn_check_top(b); sl@0: sl@0: if (a->neg != b->neg) sl@0: { sl@0: if (a->neg) sl@0: return(-1); sl@0: else return(1); sl@0: } sl@0: if (a->neg == 0) sl@0: { gt=1; lt= -1; } sl@0: else { gt= -1; lt=1; } sl@0: sl@0: if (a->top > b->top) return(gt); sl@0: if (a->top < b->top) return(lt); sl@0: for (i=a->top-1; i>=0; i--) sl@0: { sl@0: t1=a->d[i]; sl@0: t2=b->d[i]; sl@0: if (t1 > t2) return(gt); sl@0: if (t1 < t2) return(lt); sl@0: } sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int BN_set_bit(BIGNUM *a, int n) sl@0: { sl@0: int i,j,k; sl@0: sl@0: if (n < 0) sl@0: return 0; sl@0: sl@0: i=n/BN_BITS2; sl@0: j=n%BN_BITS2; sl@0: if (a->top <= i) sl@0: { sl@0: if (bn_wexpand(a,i+1) == NULL) return(0); sl@0: for(k=a->top; kd[k]=0; sl@0: a->top=i+1; sl@0: } sl@0: sl@0: a->d[i]|=(((BN_ULONG)1)<top <= i) return(0); sl@0: sl@0: a->d[i]&=(~(((BN_ULONG)1)<top <= i) return 0; sl@0: return(((a->d[i])>>j)&((BN_ULONG)1)); sl@0: } sl@0: sl@0: EXPORT_C int BN_mask_bits(BIGNUM *a, int n) sl@0: { sl@0: int b,w; sl@0: sl@0: bn_check_top(a); sl@0: if (n < 0) return 0; sl@0: sl@0: w=n/BN_BITS2; sl@0: b=n%BN_BITS2; sl@0: if (w >= a->top) return 0; sl@0: if (b == 0) sl@0: a->top=w; sl@0: else sl@0: { sl@0: a->top=w+1; sl@0: a->d[w]&= ~(BN_MASK2<neg = 1; sl@0: else sl@0: a->neg = 0; sl@0: } sl@0: sl@0: EXPORT_C int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) sl@0: { sl@0: int i; sl@0: BN_ULONG aa,bb; sl@0: sl@0: aa=a[n-1]; sl@0: bb=b[n-1]; sl@0: if (aa != bb) return((aa > bb)?1:-1); sl@0: for (i=n-2; i>=0; i--) sl@0: { sl@0: aa=a[i]; sl@0: bb=b[i]; sl@0: if (aa != bb) return((aa > bb)?1:-1); sl@0: } sl@0: return(0); sl@0: } sl@0: sl@0: /* Here follows a specialised variants of bn_cmp_words(). It has the sl@0: property of performing the operation on arrays of different sizes. sl@0: The sizes of those arrays is expressed through cl, which is the sl@0: common length ( basicall, min(len(a),len(b)) ), and dl, which is the sl@0: delta between the two lengths, calculated as len(a)-len(b). sl@0: All lengths are the number of BN_ULONGs... */ sl@0: sl@0: EXPORT_C int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, sl@0: int cl, int dl) sl@0: { sl@0: int n,i; sl@0: n = cl-1; sl@0: sl@0: if (dl < 0) sl@0: { sl@0: for (i=dl; i<0; i++) sl@0: { sl@0: if (b[n-i] != 0) sl@0: return -1; /* a < b */ sl@0: } sl@0: } sl@0: if (dl > 0) sl@0: { sl@0: for (i=dl; i>0; i--) sl@0: { sl@0: if (a[n+i] != 0) sl@0: return 1; /* a > b */ sl@0: } sl@0: } sl@0: return bn_cmp_words(a,b,cl); sl@0: }