• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <openssl/mem.h>
60 
61 #include "internal.h"
62 
63 
BN_ucmp(const BIGNUM * a,const BIGNUM * b)64 int BN_ucmp(const BIGNUM *a, const BIGNUM *b) {
65   int i;
66   BN_ULONG t1, t2, *ap, *bp;
67 
68   i = a->top - b->top;
69   if (i != 0) {
70     return i;
71   }
72 
73   ap = a->d;
74   bp = b->d;
75   for (i = a->top - 1; i >= 0; i--) {
76     t1 = ap[i];
77     t2 = bp[i];
78     if (t1 != t2) {
79       return (t1 > t2) ? 1 : -1;
80     }
81   }
82 
83   return 0;
84 }
85 
BN_cmp(const BIGNUM * a,const BIGNUM * b)86 int BN_cmp(const BIGNUM *a, const BIGNUM *b) {
87   int i;
88   int gt, lt;
89   BN_ULONG t1, t2;
90 
91   if ((a == NULL) || (b == NULL)) {
92     if (a != NULL) {
93       return -1;
94     } else if (b != NULL) {
95       return 1;
96     } else {
97       return 0;
98     }
99   }
100 
101   if (a->neg != b->neg) {
102     if (a->neg) {
103       return -1;
104     }
105     return 1;
106   }
107   if (a->neg == 0) {
108     gt = 1;
109     lt = -1;
110   } else {
111     gt = -1;
112     lt = 1;
113   }
114 
115   if (a->top > b->top) {
116     return gt;
117   }
118   if (a->top < b->top) {
119     return lt;
120   }
121 
122   for (i = a->top - 1; i >= 0; i--) {
123     t1 = a->d[i];
124     t2 = b->d[i];
125     if (t1 > t2) {
126       return gt;
127     } if (t1 < t2) {
128       return lt;
129     }
130   }
131 
132   return 0;
133 }
134 
bn_cmp_words(const BN_ULONG * a,const BN_ULONG * b,int n)135 int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) {
136   int i;
137   BN_ULONG aa, bb;
138 
139   aa = a[n - 1];
140   bb = b[n - 1];
141   if (aa != bb) {
142     return (aa > bb) ? 1 : -1;
143   }
144 
145   for (i = n - 2; i >= 0; i--) {
146     aa = a[i];
147     bb = b[i];
148     if (aa != bb) {
149       return (aa > bb) ? 1 : -1;
150     }
151   }
152   return 0;
153 }
154 
bn_cmp_part_words(const BN_ULONG * a,const BN_ULONG * b,int cl,int dl)155 int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) {
156   int n, i;
157   n = cl - 1;
158 
159   if (dl < 0) {
160     for (i = dl; i < 0; i++) {
161       if (b[n - i] != 0) {
162         return -1; /* a < b */
163       }
164     }
165   }
166   if (dl > 0) {
167     for (i = dl; i > 0; i--) {
168       if (a[n + i] != 0) {
169         return 1; /* a > b */
170       }
171     }
172   }
173 
174   return bn_cmp_words(a, b, cl);
175 }
176 
BN_abs_is_word(const BIGNUM * bn,BN_ULONG w)177 int BN_abs_is_word(const BIGNUM *bn, BN_ULONG w) {
178   switch (bn->top) {
179     case 1:
180       return bn->d[0] == w;
181     case 0:
182       return w == 0;
183     default:
184       return 0;
185   }
186 }
187 
BN_cmp_word(const BIGNUM * a,BN_ULONG b)188 int BN_cmp_word(const BIGNUM *a, BN_ULONG b) {
189   BIGNUM b_bn;
190   BN_init(&b_bn);
191 
192   b_bn.d = &b;
193   b_bn.top = b > 0;
194   b_bn.dmax = 1;
195   b_bn.flags = BN_FLG_STATIC_DATA;
196   return BN_cmp(a, &b_bn);
197 }
198 
BN_is_zero(const BIGNUM * bn)199 int BN_is_zero(const BIGNUM *bn) {
200   return bn->top == 0;
201 }
202 
BN_is_one(const BIGNUM * bn)203 int BN_is_one(const BIGNUM *bn) {
204   return bn->neg == 0 && BN_abs_is_word(bn, 1);
205 }
206 
BN_is_word(const BIGNUM * bn,BN_ULONG w)207 int BN_is_word(const BIGNUM *bn, BN_ULONG w) {
208   return BN_abs_is_word(bn, w) && (w == 0 || bn->neg == 0);
209 }
210 
BN_is_odd(const BIGNUM * bn)211 int BN_is_odd(const BIGNUM *bn) {
212   return bn->top > 0 && (bn->d[0] & 1) == 1;
213 }
214 
BN_is_pow2(const BIGNUM * bn)215 int BN_is_pow2(const BIGNUM *bn) {
216   if (bn->top == 0 || bn->neg) {
217     return 0;
218   }
219 
220   for (int i = 0; i < bn->top - 1; i++) {
221     if (bn->d[i] != 0) {
222       return 0;
223     }
224   }
225 
226   return 0 == (bn->d[bn->top-1] & (bn->d[bn->top-1] - 1));
227 }
228 
BN_equal_consttime(const BIGNUM * a,const BIGNUM * b)229 int BN_equal_consttime(const BIGNUM *a, const BIGNUM *b) {
230   if (a->top != b->top) {
231     return 0;
232   }
233 
234   int limbs_are_equal =
235     CRYPTO_memcmp(a->d, b->d, (size_t)a->top * sizeof(a->d[0])) == 0;
236 
237   return constant_time_select_int(constant_time_eq_int(a->neg, b->neg),
238                                   limbs_are_equal, 0);
239 }
240