| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/bn.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 |
| 20 | #include <openssl/err.h> |
| 21 |
| 22 | #include "internal.h" |
| 23 |
| 24 |
| 25 | // bn_div_words divides a double-width |h|,|l| by |d| and returns the result, |
| 26 | // which must fit in a |BN_ULONG|. |
| 27 | static inline BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) { |
| 28 | BN_ULONG dh, dl, q, ret = 0, th, tl, t; |
| 29 | int i, count = 2; |
| 30 |
| 31 | if (d == 0) { |
| 32 | return BN_MASK2; |
| 33 | } |
| 34 |
| 35 | i = BN_num_bits_word(d); |
| 36 | assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i)); |
| 37 |
| 38 | i = BN_BITS2 - i; |
| 39 | if (h >= d) { |
| 40 | h -= d; |
| 41 | } |
| 42 |
| 43 | if (i) { |
| 44 | d <<= i; |
| 45 | h = (h << i) | (l >> (BN_BITS2 - i)); |
| 46 | l <<= i; |
| 47 | } |
| 48 | dh = (d & BN_MASK2h) >> BN_BITS4; |
| 49 | dl = (d & BN_MASK2l); |
| 50 | for (;;) { |
| 51 | if ((h >> BN_BITS4) == dh) { |
| 52 | q = BN_MASK2l; |
| 53 | } else { |
| 54 | q = h / dh; |
| 55 | } |
| 56 |
| 57 | th = q * dh; |
| 58 | tl = dl * q; |
| 59 | for (;;) { |
| 60 | t = h - th; |
| 61 | if ((t & BN_MASK2h) || |
| 62 | ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4)))) { |
| 63 | break; |
| 64 | } |
| 65 | q--; |
| 66 | th -= dh; |
| 67 | tl -= dl; |
| 68 | } |
| 69 | t = (tl >> BN_BITS4); |
| 70 | tl = (tl << BN_BITS4) & BN_MASK2h; |
| 71 | th += t; |
| 72 |
| 73 | if (l < tl) { |
| 74 | th++; |
| 75 | } |
| 76 | l -= tl; |
| 77 | if (h < th) { |
| 78 | h += d; |
| 79 | q--; |
| 80 | } |
| 81 | h -= th; |
| 82 |
| 83 | if (--count == 0) { |
| 84 | break; |
| 85 | } |
| 86 |
| 87 | ret = q << BN_BITS4; |
| 88 | h = (h << BN_BITS4) | (l >> BN_BITS4); |
| 89 | l = (l & BN_MASK2l) << BN_BITS4; |
| 90 | } |
| 91 |
| 92 | ret |= q; |
| 93 | return ret; |
| 94 | } |
| 95 |
| 96 | static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out, |
| 97 | BN_ULONG n0, BN_ULONG n1, BN_ULONG d0) { |
| 98 | // GCC and Clang generate function calls to |__udivdi3| and |__umoddi3| when |
| 99 | // the |BN_ULLONG|-based C code is used. |
| 100 | // |
| 101 | // GCC bugs: |
| 102 | // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224 |
| 103 | // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43721 |
| 104 | // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54183 |
| 105 | // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58897 |
| 106 | // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65668 |
| 107 | // |
| 108 | // Clang bugs: |
| 109 | // * https://github.com/llvm/llvm-project/issues/6769 |
| 110 | // * https://github.com/llvm/llvm-project/issues/12790 |
| 111 | // |
| 112 | // These is specific to x86 and x86_64; Arm and RISC-V do not have double-wide |
| 113 | // division instructions. |
| 114 | #if defined(BN_CAN_USE_INLINE_ASM) && defined(OPENSSL_X86) |
| 115 | __asm__ volatile("divl %4" |
| 116 | : "=a"(*quotient_out), "=d"(*rem_out) |
| 117 | : "a"(n1), "d"(n0), "rm"(d0) |
| 118 | : "cc"); |
| 119 | #elif defined(BN_CAN_USE_INLINE_ASM) && defined(OPENSSL_X86_64) |
| 120 | __asm__ volatile("divq %4" |
| 121 | : "=a"(*quotient_out), "=d"(*rem_out) |
| 122 | : "a"(n1), "d"(n0), "rm"(d0) |
| 123 | : "cc"); |
| 124 | #else |
| 125 | #if defined(BN_CAN_DIVIDE_ULLONG) |
| 126 | BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1; |
| 127 | *quotient_out = (BN_ULONG)(n / d0); |
| 128 | #else |
| 129 | *quotient_out = bn_div_words(n0, n1, d0); |
| 130 | #endif |
| 131 | *rem_out = n1 - (*quotient_out * d0); |
| 132 | #endif |
| 133 | } |
| 134 |
| 135 | int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator, |
| 136 | const BIGNUM *divisor, BN_CTX *ctx) { |
| 137 | // This function implements long division, per Knuth, The Art of Computer |
| 138 | // Programming, Volume 2, Chapter 4.3.1, Algorithm D. This algorithm only |
| 139 | // divides non-negative integers, but we round towards zero, so we divide |
| 140 | // absolute values and adjust the signs separately. |
| 141 | // |
| 142 | // Inputs to this function are assumed public and may be leaked by timing and |
| 143 | // cache side channels. Division with secret inputs should use other |
| 144 | // implementation strategies such as Montgomery reduction. |
| 145 | if (BN_is_zero(divisor)) { |
| 146 | OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO); |
| 147 | return 0; |
| 148 | } |
| 149 |
| 150 | bssl::BN_CTXScope scope(ctx); |
| 151 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 152 | BIGNUM *snum = BN_CTX_get(ctx); |
| 153 | BIGNUM *sdiv = BN_CTX_get(ctx); |
| 154 | BIGNUM *res = quotient == NULL ? BN_CTX_get(ctx) : quotient; |
| 155 | int norm_shift, num_n, loop, div_n; |
| 156 | BN_ULONG d0, d1; |
| 157 | if (tmp == NULL || snum == NULL || sdiv == NULL || res == NULL) { |
| 158 | return 0; |
| 159 | } |
| 160 |
| 161 | // Knuth step D1: Normalise the numbers such that the divisor's MSB is set. |
| 162 | // This ensures, in Knuth's terminology, that v1 >= b/2, needed for the |
| 163 | // quotient estimation step. |
| 164 | norm_shift = BN_BITS2 - (BN_num_bits(divisor) % BN_BITS2); |
| 165 | if (!BN_lshift(sdiv, divisor, norm_shift) || |
| 166 | !BN_lshift(snum, numerator, norm_shift)) { |
| 167 | return 0; |
| 168 | } |
| 169 |
| 170 | // This algorithm relies on |sdiv| being minimal width. We do not use this |
| 171 | // function on secret inputs, so leaking this is fine. Also minimize |snum| to |
| 172 | // avoid looping on leading zeros, as we're not trying to be leak-free. |
| 173 | bn_set_minimal_width(sdiv); |
| 174 | bn_set_minimal_width(snum); |
| 175 | div_n = sdiv->width; |
| 176 | d0 = sdiv->d[div_n - 1]; |
| 177 | d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2]; |
| 178 | assert(d0 & (((BN_ULONG)1) << (BN_BITS2 - 1))); |
| 179 |
| 180 | // Extend |snum| with zeros to satisfy the long division invariants: |
| 181 | // - |snum| must have at least |div_n| + 1 words. |
| 182 | // - |snum|'s most significant word must be zero to guarantee the first loop |
| 183 | // iteration works with a prefix greater than |sdiv|. (This is the extra u0 |
| 184 | // digit in Knuth step D1.) |
| 185 | num_n = snum->width <= div_n ? div_n + 1 : snum->width + 1; |
| 186 | if (!bn_resize_words(snum, num_n)) { |
| 187 | return 0; |
| 188 | } |
| 189 |
| 190 | // Knuth step D2: The quotient's width is the difference between numerator and |
| 191 | // denominator. Also set up its sign and size a temporary for the loop. |
| 192 | loop = num_n - div_n; |
| 193 | res->neg = snum->neg ^ sdiv->neg; |
| 194 | if (!bn_wexpand(res, loop) || // |
| 195 | !bn_wexpand(tmp, div_n + 1)) { |
| 196 | return 0; |
| 197 | } |
| 198 | res->width = loop; |
| 199 |
| 200 | // Knuth steps D2 through D7: Compute the quotient with a word-by-word long |
| 201 | // division. Note that Knuth indexes words from most to least significant, so |
| 202 | // our index is reversed. Each loop iteration computes res->d[i] of the |
| 203 | // quotient and updates snum with the running remainder. Before each loop |
| 204 | // iteration, the div_n words beginning at snum->d[i+1] must be less than |
| 205 | // snum. |
| 206 | for (int i = loop - 1; i >= 0; i--) { |
| 207 | // The next word of the quotient, q, is floor(wnum / sdiv), where wnum is |
| 208 | // the div_n + 1 words beginning at snum->d[i]. i starts at |
| 209 | // num_n - div_n - 1, so there are at least div_n + 1 words available. |
| 210 | // |
| 211 | // Knuth step D3: Compute q', an estimate of q by looking at the top words |
| 212 | // of wnum and sdiv. We must estimate such that q' = q or q' = q + 1. |
| 213 | BN_ULONG q, rm = 0; |
| 214 | BN_ULONG *wnum = snum->d + i; |
| 215 | BN_ULONG n0 = wnum[div_n]; |
| 216 | BN_ULONG n1 = wnum[div_n - 1]; |
| 217 | if (n0 == d0) { |
| 218 | // Estimate q' = b - 1, where b is the base. |
| 219 | q = BN_MASK2; |
| 220 | // Knuth also runs the fixup routine in this case, but this would require |
| 221 | // computing rm and is unnecessary. q' is already close enough. That is, |
| 222 | // the true quotient, q is either b - 1 or b - 2. |
| 223 | // |
| 224 | // By the loop invariant, q <= b - 1, so we must show that q >= b - 2. We |
| 225 | // do this by showing wnum / sdiv >= b - 2. Suppose wnum / sdiv < b - 2. |
| 226 | // wnum and sdiv have the same most significant word, so: |
| 227 | // |
| 228 | // wnum >= n0 * b^div_n |
| 229 | // sdiv < (n0 + 1) * b^(d_div - 1) |
| 230 | // |
| 231 | // Thus: |
| 232 | // |
| 233 | // b - 2 > wnum / sdiv |
| 234 | // > (n0 * b^div_n) / (n0 + 1) * b^(div_n - 1) |
| 235 | // = (n0 * b) / (n0 + 1) |
| 236 | // |
| 237 | // (n0 + 1) * (b - 2) > n0 * b |
| 238 | // n0 * b + b - 2 * n0 - 2 > n0 * b |
| 239 | // b - 2 > 2 * n0 |
| 240 | // b/2 - 1 > n0 |
| 241 | // |
| 242 | // This contradicts the normalization condition, so q >= b - 2 and our |
| 243 | // estimate is close enough. |
| 244 | } else { |
| 245 | // Estimate q' = floor(n0n1 / d0). Per Theorem B, q' - 2 <= q <= q', which |
| 246 | // is slightly outside of our bounds. |
| 247 | assert(n0 < d0); |
| 248 | bn_div_rem_words(&q, &rm, n0, n1, d0); |
| 249 |
| 250 | // Fix the estimate by examining one more word and adjusting q' as needed. |
| 251 | // This is the second half of step D3 and is sufficient per exercises 19, |
| 252 | // 20, and 21. Although only one iteration is needed to correct q + 2 to |
| 253 | // q + 1, Knuth uses a loop. A loop will often also correct q + 1 to q, |
| 254 | // saving the slightly more expensive underflow handling below. |
| 255 | if (div_n > 1) { |
| 256 | BN_ULONG n2 = wnum[div_n - 2]; |
| 257 | #ifdef BN_ULLONG |
| 258 | BN_ULLONG t2 = (BN_ULLONG)d1 * q; |
| 259 | for (;;) { |
| 260 | if (t2 <= ((((BN_ULLONG)rm) << BN_BITS2) | n2)) { |
| 261 | break; |
| 262 | } |
| 263 | q--; |
| 264 | rm += d0; |
| 265 | if (rm < d0) { |
| 266 | // If rm overflows, the true value exceeds BN_ULONG and the next |
| 267 | // t2 comparison should exit the loop. |
| 268 | break; |
| 269 | } |
| 270 | t2 -= d1; |
| 271 | } |
| 272 | #else // !BN_ULLONG |
| 273 | BN_ULONG t2l, t2h; |
| 274 | BN_UMULT_LOHI(t2l, t2h, d1, q); |
| 275 | for (;;) { |
| 276 | if (t2h < rm || (t2h == rm && t2l <= n2)) { |
| 277 | break; |
| 278 | } |
| 279 | q--; |
| 280 | rm += d0; |
| 281 | if (rm < d0) { |
| 282 | // If rm overflows, the true value exceeds BN_ULONG and the next |
| 283 | // t2 comparison should exit the loop. |
| 284 | break; |
| 285 | } |
| 286 | if (t2l < d1) { |
| 287 | t2h--; |
| 288 | } |
| 289 | t2l -= d1; |
| 290 | } |
| 291 | #endif // !BN_ULLONG |
| 292 | } |
| 293 | } |
| 294 |
| 295 | // Knuth step D4 through D6: Now q' = q or q' = q + 1, and |
| 296 | // -sdiv < wnum - sdiv * q < sdiv. If q' = q + 1, the subtraction will |
| 297 | // underflow, and we fix it up below. |
| 298 | tmp->d[div_n] = bn_mul_words(tmp->d, sdiv->d, div_n, q); |
| 299 | if (bn_sub_words(wnum, wnum, tmp->d, div_n + 1)) { |
| 300 | q--; |
| 301 | // The final addition is expected to overflow, canceling the underflow. |
| 302 | wnum[div_n] += bn_add_words(wnum, wnum, sdiv->d, div_n); |
| 303 | } |
| 304 |
| 305 | // q is now correct, and wnum has been updated to the running remainder. |
| 306 | res->d[i] = q; |
| 307 | } |
| 308 |
| 309 | // Trim leading zeros and correct any negative zeros. |
| 310 | bn_set_minimal_width(snum); |
| 311 | bn_set_minimal_width(res); |
| 312 |
| 313 | // Knuth step D8: Unnormalize. snum now contains the remainder. |
| 314 | if (rem != NULL && !BN_rshift(rem, snum, norm_shift)) { |
| 315 | return 0; |
| 316 | } |
| 317 |
| 318 | return 1; |
| 319 | } |
| 320 |
| 321 | int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) { |
| 322 | if (!(BN_mod(r, m, d, ctx))) { |
| 323 | return 0; |
| 324 | } |
| 325 | if (!r->neg) { |
| 326 | return 1; |
| 327 | } |
| 328 |
| 329 | // now -d < r < 0, so we have to set r := r + d. Ignoring the sign bits, this |
| 330 | // is r = d - r. |
| 331 | return BN_usub(r, d, r); |
| 332 | } |
| 333 |
| 334 | BN_ULONG bn_reduce_once(BN_ULONG *r, const BN_ULONG *a, BN_ULONG carry, |
| 335 | const BN_ULONG *m, size_t num) { |
| 336 | assert(r != a); |
| 337 | // |r| = |a| - |m|. |bn_sub_words| performs the bulk of the subtraction, and |
| 338 | // then we apply the borrow to |carry|. |
| 339 | carry -= bn_sub_words(r, a, m, num); |
| 340 | // We know 0 <= |a| < 2*|m|, so -|m| <= |r| < |m|. |
| 341 | // |
| 342 | // If 0 <= |r| < |m|, |r| fits in |num| words and |carry| is zero. We then |
| 343 | // wish to select |r| as the answer. Otherwise -m <= r < 0 and we wish to |
| 344 | // return |r| + |m|, or |a|. |carry| must then be -1 or all ones. In both |
| 345 | // cases, |carry| is a suitable input to |bn_select_words|. |
| 346 | // |
| 347 | // Although |carry| may be one if it was one on input and |bn_sub_words| |
| 348 | // returns zero, this would give |r| > |m|, violating our input assumptions. |
| 349 | declassify_assert(carry + 1 <= 1); |
| 350 | bn_select_words(r, carry, a /* r < 0 */, r /* r >= 0 */, num); |
| 351 | return carry; |
| 352 | } |
| 353 |
| 354 | BN_ULONG bn_reduce_once_in_place(BN_ULONG *r, BN_ULONG carry, const BN_ULONG *m, |
| 355 | BN_ULONG *tmp, size_t num) { |
| 356 | // See |bn_reduce_once| for why this logic works. |
| 357 | carry -= bn_sub_words(tmp, r, m, num); |
| 358 | declassify_assert(carry + 1 <= 1); |
| 359 | bn_select_words(r, carry, r /* tmp < 0 */, tmp /* tmp >= 0 */, num); |
| 360 | return carry; |
| 361 | } |
| 362 |
| 363 | void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 364 | const BN_ULONG *m, BN_ULONG *tmp, size_t num) { |
| 365 | // r = a - b |
| 366 | BN_ULONG borrow = bn_sub_words(r, a, b, num); |
| 367 | // tmp = a - b + m |
| 368 | bn_add_words(tmp, r, m, num); |
| 369 | bn_select_words(r, 0 - borrow, tmp /* r < 0 */, r /* r >= 0 */, num); |
| 370 | } |
| 371 |
| 372 | void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 373 | const BN_ULONG *m, BN_ULONG *tmp, size_t num) { |
| 374 | BN_ULONG carry = bn_add_words(r, a, b, num); |
| 375 | bn_reduce_once_in_place(r, carry, m, tmp, num); |
| 376 | } |
| 377 |
| 378 | int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder, |
| 379 | const BIGNUM *numerator, const BIGNUM *divisor, |
| 380 | unsigned divisor_min_bits, BN_CTX *ctx) { |
| 381 | if (BN_is_negative(numerator) || BN_is_negative(divisor)) { |
| 382 | OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 383 | return 0; |
| 384 | } |
| 385 | if (BN_is_zero(divisor)) { |
| 386 | OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO); |
| 387 | return 0; |
| 388 | } |
| 389 |
| 390 | // This function implements long division in binary. It is not very efficient, |
| 391 | // but it is simple, easy to make constant-time, and performant enough for RSA |
| 392 | // key generation. |
| 393 |
| 394 | bssl::BN_CTXScope scope(ctx); |
| 395 | BIGNUM *q = quotient, *r = remainder; |
| 396 | if (quotient == NULL || quotient == numerator || quotient == divisor) { |
| 397 | q = BN_CTX_get(ctx); |
| 398 | } |
| 399 | if (remainder == NULL || remainder == numerator || remainder == divisor) { |
| 400 | r = BN_CTX_get(ctx); |
| 401 | } |
| 402 | BIGNUM *tmp = BN_CTX_get(ctx); |
| 403 | int initial_words; |
| 404 | if (q == NULL || r == NULL || tmp == NULL || |
| 405 | !bn_wexpand(q, numerator->width) || !bn_wexpand(r, divisor->width) || |
| 406 | !bn_wexpand(tmp, divisor->width)) { |
| 407 | return 0; |
| 408 | } |
| 409 |
| 410 | OPENSSL_memset(q->d, 0, numerator->width * sizeof(BN_ULONG)); |
| 411 | q->width = numerator->width; |
| 412 | q->neg = 0; |
| 413 |
| 414 | OPENSSL_memset(r->d, 0, divisor->width * sizeof(BN_ULONG)); |
| 415 | r->width = divisor->width; |
| 416 | r->neg = 0; |
| 417 |
| 418 | // Incorporate |numerator| into |r|, one bit at a time, reducing after each |
| 419 | // step. We maintain the invariant that |0 <= r < divisor| and |
| 420 | // |q * divisor + r = n| where |n| is the portion of |numerator| incorporated |
| 421 | // so far. |
| 422 | // |
| 423 | // First, we short-circuit the loop: if we know |divisor| has at least |
| 424 | // |divisor_min_bits| bits, the top |divisor_min_bits - 1| can be incorporated |
| 425 | // without reductions. This significantly speeds up |RSA_check_key|. For |
| 426 | // simplicity, we round down to a whole number of words. |
| 427 | declassify_assert(divisor_min_bits <= BN_num_bits(divisor)); |
| 428 | initial_words = 0; |
| 429 | if (divisor_min_bits > 0) { |
| 430 | initial_words = (divisor_min_bits - 1) / BN_BITS2; |
| 431 | if (initial_words > numerator->width) { |
| 432 | initial_words = numerator->width; |
| 433 | } |
| 434 | OPENSSL_memcpy(r->d, numerator->d + numerator->width - initial_words, |
| 435 | initial_words * sizeof(BN_ULONG)); |
| 436 | } |
| 437 |
| 438 | for (int i = numerator->width - initial_words - 1; i >= 0; i--) { |
| 439 | for (int bit = BN_BITS2 - 1; bit >= 0; bit--) { |
| 440 | // Incorporate the next bit of the numerator, by computing |
| 441 | // r = 2*r or 2*r + 1. Note the result fits in one more word. We store the |
| 442 | // extra word in |carry|. |
| 443 | BN_ULONG carry = bn_add_words(r->d, r->d, r->d, divisor->width); |
| 444 | r->d[0] |= (numerator->d[i] >> bit) & 1; |
| 445 | // |r| was previously fully-reduced, so we know: |
| 446 | // 2*0 <= r <= 2*(divisor-1) + 1 |
| 447 | // 0 <= r <= 2*divisor - 1 < 2*divisor. |
| 448 | // Thus |r| satisfies the preconditions for |bn_reduce_once_in_place|. |
| 449 | BN_ULONG subtracted = bn_reduce_once_in_place(r->d, carry, divisor->d, |
| 450 | tmp->d, divisor->width); |
| 451 | // The corresponding bit of the quotient is set iff we needed to subtract. |
| 452 | q->d[i] |= (~subtracted & 1) << bit; |
| 453 | } |
| 454 | } |
| 455 |
| 456 | if ((quotient != NULL && !BN_copy(quotient, q)) || |
| 457 | (remainder != NULL && !BN_copy(remainder, r))) { |
| 458 | return 0; |
| 459 | } |
| 460 |
| 461 | return 1; |
| 462 | } |
| 463 |
| 464 | static BIGNUM *bn_scratch_space_from_ctx(size_t width, BN_CTX *ctx) { |
| 465 | BIGNUM *ret = BN_CTX_get(ctx); |
| 466 | if (ret == NULL || !bn_wexpand(ret, width)) { |
| 467 | return NULL; |
| 468 | } |
| 469 | ret->neg = 0; |
| 470 | ret->width = (int)width; |
| 471 | return ret; |
| 472 | } |
| 473 |
| 474 | // bn_resized_from_ctx returns |bn| with width at least |width| or NULL on |
| 475 | // error. This is so it may be used with low-level "words" functions. If |
| 476 | // necessary, it allocates a new |BIGNUM| with a lifetime of the current scope |
| 477 | // in |ctx|, so the caller does not need to explicitly free it. |bn| must fit in |
| 478 | // |width| words. |
| 479 | static const BIGNUM *bn_resized_from_ctx(const BIGNUM *bn, size_t width, |
| 480 | BN_CTX *ctx) { |
| 481 | if ((size_t)bn->width >= width) { |
| 482 | // Any excess words must be zero. |
| 483 | assert(bn_fits_in_words(bn, width)); |
| 484 | return bn; |
| 485 | } |
| 486 | BIGNUM *ret = bn_scratch_space_from_ctx(width, ctx); |
| 487 | if (ret == NULL || !BN_copy(ret, bn) || !bn_resize_words(ret, width)) { |
| 488 | return NULL; |
| 489 | } |
| 490 | return ret; |
| 491 | } |
| 492 |
| 493 | int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, |
| 494 | BN_CTX *ctx) { |
| 495 | if (!BN_add(r, a, b)) { |
| 496 | return 0; |
| 497 | } |
| 498 | return BN_nnmod(r, r, m, ctx); |
| 499 | } |
| 500 |
| 501 | int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 502 | const BIGNUM *m) { |
| 503 | bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new()); |
| 504 | return ctx != nullptr && bn_mod_add_consttime(r, a, b, m, ctx.get()); |
| 505 | } |
| 506 |
| 507 | int bn_mod_add_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 508 | const BIGNUM *m, BN_CTX *ctx) { |
| 509 | bssl::BN_CTXScope scope(ctx); |
| 510 | a = bn_resized_from_ctx(a, m->width, ctx); |
| 511 | b = bn_resized_from_ctx(b, m->width, ctx); |
| 512 | BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx); |
| 513 | if (a == nullptr || b == nullptr || tmp == nullptr || |
| 514 | !bn_wexpand(r, m->width)) { |
| 515 | return 0; |
| 516 | } |
| 517 | bn_mod_add_words(r->d, a->d, b->d, m->d, tmp->d, m->width); |
| 518 | r->width = m->width; |
| 519 | r->neg = 0; |
| 520 | return 1; |
| 521 | } |
| 522 |
| 523 | int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, |
| 524 | BN_CTX *ctx) { |
| 525 | if (!BN_sub(r, a, b)) { |
| 526 | return 0; |
| 527 | } |
| 528 | return BN_nnmod(r, r, m, ctx); |
| 529 | } |
| 530 |
| 531 | int bn_mod_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 532 | const BIGNUM *m, BN_CTX *ctx) { |
| 533 | bssl::BN_CTXScope scope(ctx); |
| 534 | a = bn_resized_from_ctx(a, m->width, ctx); |
| 535 | b = bn_resized_from_ctx(b, m->width, ctx); |
| 536 | BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx); |
| 537 | if (a == nullptr || b == nullptr || tmp == nullptr || |
| 538 | !bn_wexpand(r, m->width)) { |
| 539 | return 0; |
| 540 | } |
| 541 | bn_mod_sub_words(r->d, a->d, b->d, m->d, tmp->d, m->width); |
| 542 | r->width = m->width; |
| 543 | r->neg = 0; |
| 544 | return 1; |
| 545 | } |
| 546 |
| 547 | int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 548 | const BIGNUM *m) { |
| 549 | bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new()); |
| 550 | return ctx != nullptr && bn_mod_sub_consttime(r, a, b, m, ctx.get()); |
| 551 | } |
| 552 |
| 553 | int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, |
| 554 | BN_CTX *ctx) { |
| 555 | bssl::BN_CTXScope scope(ctx); |
| 556 | BIGNUM *t = BN_CTX_get(ctx); |
| 557 | if (t == NULL) { |
| 558 | return 0; |
| 559 | } |
| 560 |
| 561 | if (a == b) { |
| 562 | if (!BN_sqr(t, a, ctx)) { |
| 563 | return 0; |
| 564 | } |
| 565 | } else { |
| 566 | if (!BN_mul(t, a, b, ctx)) { |
| 567 | return 0; |
| 568 | } |
| 569 | } |
| 570 |
| 571 | if (!BN_nnmod(r, t, m, ctx)) { |
| 572 | return 0; |
| 573 | } |
| 574 |
| 575 | return 1; |
| 576 | } |
| 577 |
| 578 | int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) { |
| 579 | if (!BN_sqr(r, a, ctx)) { |
| 580 | return 0; |
| 581 | } |
| 582 |
| 583 | // r->neg == 0, thus we don't need BN_nnmod |
| 584 | return BN_mod(r, r, m, ctx); |
| 585 | } |
| 586 |
| 587 | int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, |
| 588 | BN_CTX *ctx) { |
| 589 | if (!BN_nnmod(r, a, m, ctx)) { |
| 590 | return 0; |
| 591 | } |
| 592 |
| 593 | bssl::UniquePtr<BIGNUM> abs_m; |
| 594 | if (m->neg) { |
| 595 | abs_m.reset(BN_dup(m)); |
| 596 | if (abs_m == nullptr) { |
| 597 | return 0; |
| 598 | } |
| 599 | abs_m->neg = 0; |
| 600 | } |
| 601 |
| 602 | return bn_mod_lshift_consttime(r, r, n, (abs_m ? abs_m.get() : m), ctx); |
| 603 | } |
| 604 |
| 605 | int bn_mod_lshift_consttime(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, |
| 606 | BN_CTX *ctx) { |
| 607 | if (!BN_copy(r, a) || !bn_resize_words(r, m->width)) { |
| 608 | return 0; |
| 609 | } |
| 610 |
| 611 | bssl::BN_CTXScope scope(ctx); |
| 612 | BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx); |
| 613 | if (tmp == nullptr) { |
| 614 | return 0; |
| 615 | } |
| 616 | for (int i = 0; i < n; i++) { |
| 617 | bn_mod_add_words(r->d, r->d, r->d, m->d, tmp->d, m->width); |
| 618 | } |
| 619 | r->neg = 0; |
| 620 | return 1; |
| 621 | } |
| 622 |
| 623 | int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) { |
| 624 | bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new()); |
| 625 | return ctx != nullptr && bn_mod_lshift_consttime(r, a, n, m, ctx.get()); |
| 626 | } |
| 627 |
| 628 | int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) { |
| 629 | if (!BN_lshift1(r, a)) { |
| 630 | return 0; |
| 631 | } |
| 632 |
| 633 | return BN_nnmod(r, r, m, ctx); |
| 634 | } |
| 635 |
| 636 | int bn_mod_lshift1_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, |
| 637 | BN_CTX *ctx) { |
| 638 | return bn_mod_add_consttime(r, a, a, m, ctx); |
| 639 | } |
| 640 |
| 641 | int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) { |
| 642 | bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new()); |
| 643 | return ctx != nullptr && bn_mod_lshift1_consttime(r, a, m, ctx.get()); |
| 644 | } |
| 645 |
| 646 | BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) { |
| 647 | BN_ULONG ret = 0; |
| 648 | int i, j; |
| 649 |
| 650 | if (!w) { |
| 651 | // actually this an error (division by zero) |
| 652 | return (BN_ULONG)-1; |
| 653 | } |
| 654 |
| 655 | if (a->width == 0) { |
| 656 | return 0; |
| 657 | } |
| 658 |
| 659 | // normalize input for |bn_div_rem_words|. |
| 660 | j = BN_BITS2 - BN_num_bits_word(w); |
| 661 | w <<= j; |
| 662 | if (!BN_lshift(a, a, j)) { |
| 663 | return (BN_ULONG)-1; |
| 664 | } |
| 665 |
| 666 | for (i = a->width - 1; i >= 0; i--) { |
| 667 | BN_ULONG l = a->d[i]; |
| 668 | BN_ULONG d; |
| 669 | BN_ULONG unused_rem; |
| 670 | bn_div_rem_words(&d, &unused_rem, ret, l, w); |
| 671 | ret = l - (d * w); |
| 672 | a->d[i] = d; |
| 673 | } |
| 674 |
| 675 | bn_set_minimal_width(a); |
| 676 | ret >>= j; |
| 677 | return ret; |
| 678 | } |
| 679 |
| 680 | BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) { |
| 681 | #ifndef BN_CAN_DIVIDE_ULLONG |
| 682 | BN_ULONG ret = 0; |
| 683 | #else |
| 684 | BN_ULLONG ret = 0; |
| 685 | #endif |
| 686 | int i; |
| 687 |
| 688 | if (w == 0) { |
| 689 | return (BN_ULONG)-1; |
| 690 | } |
| 691 |
| 692 | #ifndef BN_CAN_DIVIDE_ULLONG |
| 693 | // If |w| is too long and we don't have |BN_ULLONG| division then we need to |
| 694 | // fall back to using |BN_div_word|. |
| 695 | if (w > ((BN_ULONG)1 << BN_BITS4)) { |
| 696 | BIGNUM *tmp = BN_dup(a); |
| 697 | if (tmp == NULL) { |
| 698 | return (BN_ULONG)-1; |
| 699 | } |
| 700 | ret = BN_div_word(tmp, w); |
| 701 | BN_free(tmp); |
| 702 | return ret; |
| 703 | } |
| 704 | #endif |
| 705 |
| 706 | for (i = a->width - 1; i >= 0; i--) { |
| 707 | #ifndef BN_CAN_DIVIDE_ULLONG |
| 708 | ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w; |
| 709 | ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w; |
| 710 | #else |
| 711 | ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w); |
| 712 | #endif |
| 713 | } |
| 714 | return (BN_ULONG)ret; |
| 715 | } |
| 716 |
| 717 | int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) { |
| 718 | if (e == 0 || a->width == 0) { |
| 719 | BN_zero(r); |
| 720 | return 1; |
| 721 | } |
| 722 |
| 723 | size_t num_words = 1 + ((e - 1) / BN_BITS2); |
| 724 |
| 725 | // If |a| definitely has less than |e| bits, just BN_copy. |
| 726 | if ((size_t)a->width < num_words) { |
| 727 | return BN_copy(r, a) != NULL; |
| 728 | } |
| 729 |
| 730 | // Otherwise, first make sure we have enough space in |r|. |
| 731 | // Note that this will fail if num_words > INT_MAX. |
| 732 | if (!bn_wexpand(r, num_words)) { |
| 733 | return 0; |
| 734 | } |
| 735 |
| 736 | // Copy the content of |a| into |r|. |
| 737 | OPENSSL_memcpy(r->d, a->d, num_words * sizeof(BN_ULONG)); |
| 738 |
| 739 | // If |e| isn't word-aligned, we have to mask off some of our bits. |
| 740 | size_t top_word_exponent = e % (sizeof(BN_ULONG) * 8); |
| 741 | if (top_word_exponent != 0) { |
| 742 | r->d[num_words - 1] &= (((BN_ULONG)1) << top_word_exponent) - 1; |
| 743 | } |
| 744 |
| 745 | // Fill in the remaining fields of |r|. |
| 746 | r->neg = a->neg; |
| 747 | r->width = (int)num_words; |
| 748 | bn_set_minimal_width(r); |
| 749 | return 1; |
| 750 | } |
| 751 |
| 752 | int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) { |
| 753 | if (!BN_mod_pow2(r, a, e)) { |
| 754 | return 0; |
| 755 | } |
| 756 |
| 757 | // If the returned value was non-negative, we're done. |
| 758 | if (BN_is_zero(r) || !r->neg) { |
| 759 | return 1; |
| 760 | } |
| 761 |
| 762 | size_t num_words = 1 + (e - 1) / BN_BITS2; |
| 763 |
| 764 | // Expand |r| to the size of our modulus. |
| 765 | if (!bn_wexpand(r, num_words)) { |
| 766 | return 0; |
| 767 | } |
| 768 |
| 769 | // Clear the upper words of |r|. |
| 770 | OPENSSL_memset(&r->d[r->width], 0, (num_words - r->width) * BN_BYTES); |
| 771 |
| 772 | // Set parameters of |r|. |
| 773 | r->neg = 0; |
| 774 | r->width = (int)num_words; |
| 775 |
| 776 | // Now, invert every word. The idea here is that we want to compute 2^e-|x|, |
| 777 | // which is actually equivalent to the twos-complement representation of |x| |
| 778 | // in |e| bits, which is -x = ~x + 1. |
| 779 | for (int i = 0; i < r->width; i++) { |
| 780 | r->d[i] = ~r->d[i]; |
| 781 | } |
| 782 |
| 783 | // If our exponent doesn't span the top word, we have to mask the rest. |
| 784 | size_t top_word_exponent = e % BN_BITS2; |
| 785 | if (top_word_exponent != 0) { |
| 786 | r->d[r->width - 1] &= (((BN_ULONG)1) << top_word_exponent) - 1; |
| 787 | } |
| 788 |
| 789 | // Keep the minimal-width invariant for |BIGNUM|. |
| 790 | bn_set_minimal_width(r); |
| 791 |
| 792 | // Finally, add one, for the reason described above. |
| 793 | return BN_add(r, r, BN_value_one()); |
| 794 | } |
| 795 |