1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/bn.h>
58 
59 #include "internal.h"
60 
61 
BN_ucmp(const BIGNUM * a,const BIGNUM * b)62 int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
63   int i;
64   BN_ULONG t1, t2, *ap, *bp;
65 
66   i = a->top - b->top;
67   if (i != 0) {
68     return i;
69   }
70 
71   ap = a->d;
72   bp = b->d;
73   for (i = a->top - 1; i >= 0; i--) {
74     t1 = ap[i];
75     t2 = bp[i];
76     if (t1 != t2) {
77       return (t1 > t2) ? 1 : -1;
78     }
79   }
80 
81   return 0;
82 }
83 
BN_cmp(const BIGNUM * a,const BIGNUM * b)84 int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
85   int i;
86   int gt, lt;
87   BN_ULONG t1, t2;
88 
89   if ((a == NULL) || (b == NULL)) {
90     if (a != NULL) {
91       return -1;
92     } else if (b != NULL) {
93       return 1;
94     } else {
95       return 0;
96     }
97   }
98 
99   if (a->neg != b->neg) {
100     if (a->neg) {
101       return -1;
102     }
103     return 1;
104   }
105   if (a->neg == 0) {
106     gt = 1;
107     lt = -1;
108   } else {
109     gt = -1;
110     lt = 1;
111   }
112 
113   if (a->top > b->top) {
114     return gt;
115   }
116   if (a->top < b->top) {
117     return lt;
118   }
119 
120   for (i = a->top - 1; i >= 0; i--) {
121     t1 = a->d[i];
122     t2 = b->d[i];
123     if (t1 > t2) {
124       return gt;
125     } if (t1 < t2) {
126       return lt;
127     }
128   }
129 
130   return 0;
131 }
132 
bn_cmp_words(const BN_ULONG * a,const BN_ULONG * b,int n)133 int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) {
134   int i;
135   BN_ULONG aa, bb;
136 
137   aa = a[n - 1];
138   bb = b[n - 1];
139   if (aa != bb) {
140     return (aa > bb) ? 1 : -1;
141   }
142 
143   for (i = n - 2; i >= 0; i--) {
144     aa = a[i];
145     bb = b[i];
146     if (aa != bb) {
147       return (aa > bb) ? 1 : -1;
148     }
149   }
150   return 0;
151 }
152 
bn_cmp_part_words(const BN_ULONG * a,const BN_ULONG * b,int cl,int dl)153 int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) {
154   int n, i;
155   n = cl - 1;
156 
157   if (dl < 0) {
158     for (i = dl; i < 0; i++) {
159       if (b[n - i] != 0) {
160         return -1; /* a < b */
161       }
162     }
163   }
164   if (dl > 0) {
165     for (i = dl; i > 0; i--) {
166       if (a[n + i] != 0) {
167         return 1; /* a > b */
168       }
169     }
170   }
171 
172   return bn_cmp_words(a, b, cl);
173 }
174 
BN_abs_is_word(const BIGNUM * bn,BN_ULONG w)175 int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
176   switch (bn->top) {
177     case 1:
178       return bn->d[0] == w;
179     case 0:
180       return w == 0;
181     default:
182       return 0;
183   }
184 }
185 
BN_is_zero(const BIGNUM * bn)186 int BN_is_zero(const BIGNUM *bn) {
187   return bn->top == 0;
188 }
189 
BN_is_one(const BIGNUM * bn)190 int BN_is_one(const BIGNUM *bn) {
191   return bn->neg == 0 && BN_abs_is_word(bn, 1);
192 }
193 
BN_is_word(const BIGNUM * bn,BN_ULONG w)194 int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
195   return BN_abs_is_word(bn, w) && (w == 0 || bn->neg == 0);
196 }
197 
BN_is_odd(const BIGNUM * bn)198 int BN_is_odd(const BIGNUM *bn) {
199   return bn->top > 0 && (bn->d[0] & 1) == 1;
200 }
201