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