| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/bn.h> |
| 5 |
|
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include <openssl/err.h> |
| 9 |
#include <openssl/mem.h> |
| 10 |
|
| 11 |
#include "internal.h" |
| 12 |
|
| 13 |
|
| 14 |
int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { |
| 15 |
const BIGNUM *tmp; |
| 16 |
int a_neg = a->neg, ret; |
| 17 |
|
| 18 |
// a + b a+b |
| 19 |
// a + -b a-b |
| 20 |
// -a + b b-a |
| 21 |
// -a + -b -(a+b) |
| 22 |
if (a_neg ^ b->neg) { |
| 23 |
// only one is negative |
| 24 |
if (a_neg) { |
| 25 |
tmp = a; |
| 26 |
a = b; |
| 27 |
b = tmp; |
| 28 |
} |
| 29 |
|
| 30 |
// we are now a - b |
| 31 |
if (BN_ucmp(a, b) < 0) { |
| 32 |
if (!BN_usub(r, b, a)) { |
| 33 |
return 0; |
| 34 |
} |
| 35 |
r->neg = 1; |
| 36 |
} else { |
| 37 |
if (!BN_usub(r, a, b)) { |
| 38 |
return 0; |
| 39 |
} |
| 40 |
r->neg = 0; |
| 41 |
} |
| 42 |
return 1; |
| 43 |
} |
| 44 |
|
| 45 |
ret = BN_uadd(r, a, b); |
| 46 |
r->neg = a_neg; |
| 47 |
return ret; |
| 48 |
} |
| 49 |
|
| 50 |
int bn_uadd_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { |
| 51 |
// Widths are public, so we normalize to make |a| the larger one. |
| 52 |
if (a->width < b->width) { |
| 53 |
const BIGNUM *tmp = a; |
| 54 |
a = b; |
| 55 |
b = tmp; |
| 56 |
} |
| 57 |
|
| 58 |
int max = a->width; |
| 59 |
int min = b->width; |
| 60 |
if (!bn_wexpand(r, max + 1)) { |
| 61 |
return 0; |
| 62 |
} |
| 63 |
r->width = max + 1; |
| 64 |
|
| 65 |
BN_ULONG carry = bn_add_words(r->d, a->d, b->d, min); |
| 66 |
for (int i = min; i < max; i++) { |
| 67 |
r->d[i] = CRYPTO_addc_w(a->d[i], 0, carry, &carry); |
| 68 |
} |
| 69 |
|
| 70 |
r->d[max] = carry; |
| 71 |
return 1; |
| 72 |
} |
| 73 |
|
| 74 |
int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { |
| 75 |
if (!bn_uadd_consttime(r, a, b)) { |
| 76 |
return 0; |
| 77 |
} |
| 78 |
bn_set_minimal_width(r); |
| 79 |
return 1; |
| 80 |
} |
| 81 |
|
| 82 |
int BN_add_word(BIGNUM *a, BN_ULONG w) { |
| 83 |
BN_ULONG l; |
| 84 |
int i; |
| 85 |
|
| 86 |
// degenerate case: w is zero |
| 87 |
if (!w) { |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
// degenerate case: a is zero |
| 92 |
if (BN_is_zero(a)) { |
| 93 |
return BN_set_word(a, w); |
| 94 |
} |
| 95 |
|
| 96 |
// handle 'a' when negative |
| 97 |
if (a->neg) { |
| 98 |
a->neg = 0; |
| 99 |
i = BN_sub_word(a, w); |
| 100 |
if (!BN_is_zero(a)) { |
| 101 |
a->neg = !(a->neg); |
| 102 |
} |
| 103 |
return i; |
| 104 |
} |
| 105 |
|
| 106 |
for (i = 0; w != 0 && i < a->width; i++) { |
| 107 |
a->d[i] = l = a->d[i] + w; |
| 108 |
w = (w > l) ? 1 : 0; |
| 109 |
} |
| 110 |
|
| 111 |
if (w && i == a->width) { |
| 112 |
if (!bn_wexpand(a, a->width + 1)) { |
| 113 |
return 0; |
| 114 |
} |
| 115 |
a->width++; |
| 116 |
a->d[i] = w; |
| 117 |
} |
| 118 |
|
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { |
| 123 |
int add = 0, neg = 0; |
| 124 |
const BIGNUM *tmp; |
| 125 |
|
| 126 |
// a - b a-b |
| 127 |
// a - -b a+b |
| 128 |
// -a - b -(a+b) |
| 129 |
// -a - -b b-a |
| 130 |
if (a->neg) { |
| 131 |
if (b->neg) { |
| 132 |
tmp = a; |
| 133 |
a = b; |
| 134 |
b = tmp; |
| 135 |
} else { |
| 136 |
add = 1; |
| 137 |
neg = 1; |
| 138 |
} |
| 139 |
} else { |
| 140 |
if (b->neg) { |
| 141 |
add = 1; |
| 142 |
neg = 0; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (add) { |
| 147 |
if (!BN_uadd(r, a, b)) { |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
r->neg = neg; |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
if (BN_ucmp(a, b) < 0) { |
| 156 |
if (!BN_usub(r, b, a)) { |
| 157 |
return 0; |
| 158 |
} |
| 159 |
r->neg = 1; |
| 160 |
} else { |
| 161 |
if (!BN_usub(r, a, b)) { |
| 162 |
return 0; |
| 163 |
} |
| 164 |
r->neg = 0; |
| 165 |
} |
| 166 |
|
| 167 |
return 1; |
| 168 |
} |
| 169 |
|
| 170 |
int bn_usub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { |
| 171 |
// |b| may have more words than |a| given non-minimal inputs, but all words |
| 172 |
// beyond |a->width| must then be zero. |
| 173 |
int b_width = b->width; |
| 174 |
if (b_width > a->width) { |
| 175 |
if (!bn_fits_in_words(b, a->width)) { |
| 176 |
OPENSSL_PUT_ERROR(BN, BN_R_ARG2_LT_ARG3); |
| 177 |
return 0; |
| 178 |
} |
| 179 |
b_width = a->width; |
| 180 |
} |
| 181 |
|
| 182 |
if (!bn_wexpand(r, a->width)) { |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
BN_ULONG borrow = bn_sub_words(r->d, a->d, b->d, b_width); |
| 187 |
for (int i = b_width; i < a->width; i++) { |
| 188 |
r->d[i] = CRYPTO_subc_w(a->d[i], 0, borrow, &borrow); |
| 189 |
} |
| 190 |
|
| 191 |
if (borrow) { |
| 192 |
OPENSSL_PUT_ERROR(BN, BN_R_ARG2_LT_ARG3); |
| 193 |
return 0; |
| 194 |
} |
| 195 |
|
| 196 |
r->width = a->width; |
| 197 |
r->neg = 0; |
| 198 |
return 1; |
| 199 |
} |
| 200 |
|
| 201 |
int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b) { |
| 202 |
if (!bn_usub_consttime(r, a, b)) { |
| 203 |
return 0; |
| 204 |
} |
| 205 |
bn_set_minimal_width(r); |
| 206 |
return 1; |
| 207 |
} |
| 208 |
|
| 209 |
int BN_sub_word(BIGNUM *a, BN_ULONG w) { |
| 210 |
int i; |
| 211 |
|
| 212 |
// degenerate case: w is zero |
| 213 |
if (!w) { |
| 214 |
return 1; |
| 215 |
} |
| 216 |
|
| 217 |
// degenerate case: a is zero |
| 218 |
if (BN_is_zero(a)) { |
| 219 |
i = BN_set_word(a, w); |
| 220 |
if (i != 0) { |
| 221 |
BN_set_negative(a, 1); |
| 222 |
} |
| 223 |
return i; |
| 224 |
} |
| 225 |
|
| 226 |
// handle 'a' when negative |
| 227 |
if (a->neg) { |
| 228 |
a->neg = 0; |
| 229 |
i = BN_add_word(a, w); |
| 230 |
a->neg = 1; |
| 231 |
return i; |
| 232 |
} |
| 233 |
|
| 234 |
if ((bn_minimal_width(a) == 1) && (a->d[0] < w)) { |
| 235 |
a->d[0] = w - a->d[0]; |
| 236 |
a->neg = 1; |
| 237 |
return 1; |
| 238 |
} |
| 239 |
|
| 240 |
i = 0; |
| 241 |
for (;;) { |
| 242 |
if (a->d[i] >= w) { |
| 243 |
a->d[i] -= w; |
| 244 |
break; |
| 245 |
} else { |
| 246 |
a->d[i] -= w; |
| 247 |
i++; |
| 248 |
w = 1; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if ((a->d[i] == 0) && (i == (a->width - 1))) { |
| 253 |
a->width--; |
| 254 |
} |
| 255 |
|
| 256 |
return 1; |
| 257 |
} |