| 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 <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
|
| 9 |
#include <openssl/err.h> |
| 10 |
|
| 11 |
#include "internal.h" |
| 12 |
|
| 13 |
|
| 14 |
// bn_div_words divides a double-width |h|,|l| by |d| and returns the result, |
| 15 |
// which must fit in a |BN_ULONG|. |
| 16 |
OPENSSL_UNUSED static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, |
| 17 |
BN_ULONG d) { |
| 18 |
BN_ULONG dh, dl, q, ret = 0, th, tl, t; |
| 19 |
int i, count = 2; |
| 20 |
|
| 21 |
if (d == 0) { |
| 22 |
return BN_MASK2; |
| 23 |
} |
| 24 |
|
| 25 |
i = BN_num_bits_word(d); |
| 26 |
assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i)); |
| 27 |
|
| 28 |
i = BN_BITS2 - i; |
| 29 |
if (h >= d) { |
| 30 |
h -= d; |
| 31 |
} |
| 32 |
|
| 33 |
if (i) { |
| 34 |
d <<= i; |
| 35 |
h = (h << i) | (l >> (BN_BITS2 - i)); |
| 36 |
l <<= i; |
| 37 |
} |
| 38 |
dh = (d & BN_MASK2h) >> BN_BITS4; |
| 39 |
dl = (d & BN_MASK2l); |
| 40 |
for (;;) { |
| 41 |
if ((h >> BN_BITS4) == dh) { |
| 42 |
q = BN_MASK2l; |
| 43 |
} else { |
| 44 |
q = h / dh; |
| 45 |
} |
| 46 |
|
| 47 |
th = q * dh; |
| 48 |
tl = dl * q; |
| 49 |
for (;;) { |
| 50 |
t = h - th; |
| 51 |
if ((t & BN_MASK2h) || |
| 52 |
((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4)))) { |
| 53 |
break; |
| 54 |
} |
| 55 |
q--; |
| 56 |
th -= dh; |
| 57 |
tl -= dl; |
| 58 |
} |
| 59 |
t = (tl >> BN_BITS4); |
| 60 |
tl = (tl << BN_BITS4) & BN_MASK2h; |
| 61 |
th += t; |
| 62 |
|
| 63 |
if (l < tl) { |
| 64 |
th++; |
| 65 |
} |
| 66 |
l -= tl; |
| 67 |
if (h < th) { |
| 68 |
h += d; |
| 69 |
q--; |
| 70 |
} |
| 71 |
h -= th; |
| 72 |
|
| 73 |
if (--count == 0) { |
| 74 |
break; |
| 75 |
} |
| 76 |
|
| 77 |
ret = q << BN_BITS4; |
| 78 |
h = (h << BN_BITS4) | (l >> BN_BITS4); |
| 79 |
l = (l & BN_MASK2l) << BN_BITS4; |
| 80 |
} |
| 81 |
|
| 82 |
ret |= q; |
| 83 |
return ret; |
| 84 |
} |
| 85 |
|
| 86 |
static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out, |
| 87 |
BN_ULONG n0, BN_ULONG n1, BN_ULONG d0) { |
| 88 |
// GCC and Clang generate function calls to |__udivdi3| and |__umoddi3| when |
| 89 |
// the |BN_ULLONG|-based C code is used. |
| 90 |
// |
| 91 |
// GCC bugs: |
| 92 |
// * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224 |
| 93 |
// * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43721 |
| 94 |
// * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54183 |
| 95 |
// * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58897 |
| 96 |
// * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65668 |
| 97 |
// |
| 98 |
// Clang bugs: |
| 99 |
// * https://llvm.org/bugs/show_bug.cgi?id=6397 |
| 100 |
// * https://llvm.org/bugs/show_bug.cgi?id=12418 |
| 101 |
// |
| 102 |
// These issues aren't specific to x86 and x86_64, so it might be worthwhile |
| 103 |
// to add more assembly language implementations. |
| 104 |
#if defined(BN_CAN_USE_INLINE_ASM) && defined(OPENSSL_X86) |
| 105 |
__asm__ volatile("divl %4" |
| 106 |
: "=a"(*quotient_out), "=d"(*rem_out) |
| 107 |
: "a"(n1), "d"(n0), "rm"(d0) |
| 108 |
: "cc"); |
| 109 |
#elif defined(BN_CAN_USE_INLINE_ASM) && defined(OPENSSL_X86_64) |
| 110 |
__asm__ volatile("divq %4" |
| 111 |
: "=a"(*quotient_out), "=d"(*rem_out) |
| 112 |
: "a"(n1), "d"(n0), "rm"(d0) |
| 113 |
: "cc"); |
| 114 |
#else |
| 115 |
#if defined(BN_CAN_DIVIDE_ULLONG) |
| 116 |
BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1; |
| 117 |
*quotient_out = (BN_ULONG)(n / d0); |
| 118 |
#else |
| 119 |
*quotient_out = bn_div_words(n0, n1, d0); |
| 120 |
#endif |
| 121 |
*rem_out = n1 - (*quotient_out * d0); |
| 122 |
#endif |
| 123 |
} |
| 124 |
|
| 125 |
// BN_div computes "quotient := numerator / divisor", rounding towards zero, |
| 126 |
// and sets up |rem| such that "quotient * divisor + rem = numerator" holds. |
| 127 |
// |
| 128 |
// Thus: |
| 129 |
// |
| 130 |
// quotient->neg == numerator->neg ^ divisor->neg |
| 131 |
// (unless the result is zero) |
| 132 |
// rem->neg == numerator->neg |
| 133 |
// (unless the remainder is zero) |
| 134 |
// |
| 135 |
// If |quotient| or |rem| is NULL, the respective value is not returned. |
| 136 |
// |
| 137 |
// This was specifically designed to contain fewer branches that may leak |
| 138 |
// sensitive information; see "New Branch Prediction Vulnerabilities in OpenSSL |
| 139 |
// and Necessary Software Countermeasures" by Onur Acıçmez, Shay Gueron, and |
| 140 |
// Jean-Pierre Seifert. |
| 141 |
int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator, |
| 142 |
const BIGNUM *divisor, BN_CTX *ctx) { |
| 143 |
int norm_shift, loop; |
| 144 |
BIGNUM wnum; |
| 145 |
BN_ULONG *resp, *wnump; |
| 146 |
BN_ULONG d0, d1; |
| 147 |
int num_n, div_n; |
| 148 |
|
| 149 |
// This function relies on the historical minimal-width |BIGNUM| invariant. |
| 150 |
// It is already not constant-time (constant-time reductions should use |
| 151 |
// Montgomery logic), so we shrink all inputs and intermediate values to |
| 152 |
// retain the previous behavior. |
| 153 |
|
| 154 |
// Invalid zero-padding would have particularly bad consequences. |
| 155 |
int numerator_width = bn_minimal_width(numerator); |
| 156 |
int divisor_width = bn_minimal_width(divisor); |
| 157 |
if ((numerator_width > 0 && numerator->d[numerator_width - 1] == 0) || |
| 158 |
(divisor_width > 0 && divisor->d[divisor_width - 1] == 0)) { |
| 159 |
OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
if (BN_is_zero(divisor)) { |
| 164 |
OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO); |
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
BN_CTX_start(ctx); |
| 169 |
BIGNUM *tmp = BN_CTX_get(ctx); |
| 170 |
BIGNUM *snum = BN_CTX_get(ctx); |
| 171 |
BIGNUM *sdiv = BN_CTX_get(ctx); |
| 172 |
BIGNUM *res = NULL; |
| 173 |
if (quotient == NULL) { |
| 174 |
res = BN_CTX_get(ctx); |
| 175 |
} else { |
| 176 |
res = quotient; |
| 177 |
} |
| 178 |
if (sdiv == NULL || res == NULL) { |
| 179 |
goto err; |
| 180 |
} |
| 181 |
|
| 182 |
// First we normalise the numbers |
| 183 |
norm_shift = BN_BITS2 - (BN_num_bits(divisor) % BN_BITS2); |
| 184 |
if (!BN_lshift(sdiv, divisor, norm_shift)) { |
| 185 |
goto err; |
| 186 |
} |
| 187 |
bn_set_minimal_width(sdiv); |
| 188 |
sdiv->neg = 0; |
| 189 |
norm_shift += BN_BITS2; |
| 190 |
if (!BN_lshift(snum, numerator, norm_shift)) { |
| 191 |
goto err; |
| 192 |
} |
| 193 |
bn_set_minimal_width(snum); |
| 194 |
snum->neg = 0; |
| 195 |
|
| 196 |
// Since we don't want to have special-case logic for the case where snum is |
| 197 |
// larger than sdiv, we pad snum with enough zeroes without changing its |
| 198 |
// value. |
| 199 |
if (snum->width <= sdiv->width + 1) { |
| 200 |
if (!bn_wexpand(snum, sdiv->width + 2)) { |
| 201 |
goto err; |
| 202 |
} |
| 203 |
for (int i = snum->width; i < sdiv->width + 2; i++) { |
| 204 |
snum->d[i] = 0; |
| 205 |
} |
| 206 |
snum->width = sdiv->width + 2; |
| 207 |
} else { |
| 208 |
if (!bn_wexpand(snum, snum->width + 1)) { |
| 209 |
goto err; |
| 210 |
} |
| 211 |
snum->d[snum->width] = 0; |
| 212 |
snum->width++; |
| 213 |
} |
| 214 |
|
| 215 |
div_n = sdiv->width; |
| 216 |
num_n = snum->width; |
| 217 |
loop = num_n - div_n; |
| 218 |
// Lets setup a 'window' into snum |
| 219 |
// This is the part that corresponds to the current |
| 220 |
// 'area' being divided |
| 221 |
wnum.neg = 0; |
| 222 |
wnum.d = &(snum->d[loop]); |
| 223 |
wnum.width = div_n; |
| 224 |
// only needed when BN_ucmp messes up the values between width and max |
| 225 |
wnum.dmax = snum->dmax - loop; // so we don't step out of bounds |
| 226 |
|
| 227 |
// Get the top 2 words of sdiv |
| 228 |
// div_n=sdiv->width; |
| 229 |
d0 = sdiv->d[div_n - 1]; |
| 230 |
d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2]; |
| 231 |
|
| 232 |
// pointer to the 'top' of snum |
| 233 |
wnump = &(snum->d[num_n - 1]); |
| 234 |
|
| 235 |
// Setup |res|. |numerator| and |res| may alias, so we save |numerator->neg| |
| 236 |
// for later. |
| 237 |
const int numerator_neg = numerator->neg; |
| 238 |
res->neg = (numerator_neg ^ divisor->neg); |
| 239 |
if (!bn_wexpand(res, loop + 1)) { |
| 240 |
goto err; |
| 241 |
} |
| 242 |
res->width = loop - 1; |
| 243 |
resp = &(res->d[loop - 1]); |
| 244 |
|
| 245 |
// space for temp |
| 246 |
if (!bn_wexpand(tmp, div_n + 1)) { |
| 247 |
goto err; |
| 248 |
} |
| 249 |
|
| 250 |
// if res->width == 0 then clear the neg value otherwise decrease |
| 251 |
// the resp pointer |
| 252 |
if (res->width == 0) { |
| 253 |
res->neg = 0; |
| 254 |
} else { |
| 255 |
resp--; |
| 256 |
} |
| 257 |
|
| 258 |
for (int i = 0; i < loop - 1; i++, wnump--, resp--) { |
| 259 |
BN_ULONG q, l0; |
| 260 |
// the first part of the loop uses the top two words of snum and sdiv to |
| 261 |
// calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv |
| 262 |
BN_ULONG n0, n1, rm = 0; |
| 263 |
|
| 264 |
n0 = wnump[0]; |
| 265 |
n1 = wnump[-1]; |
| 266 |
if (n0 == d0) { |
| 267 |
q = BN_MASK2; |
| 268 |
} else { |
| 269 |
// n0 < d0 |
| 270 |
bn_div_rem_words(&q, &rm, n0, n1, d0); |
| 271 |
|
| 272 |
#ifdef BN_ULLONG |
| 273 |
BN_ULLONG t2 = (BN_ULLONG)d1 * q; |
| 274 |
for (;;) { |
| 275 |
if (t2 <= ((((BN_ULLONG)rm) << BN_BITS2) | wnump[-2])) { |
| 276 |
break; |
| 277 |
} |
| 278 |
q--; |
| 279 |
rm += d0; |
| 280 |
if (rm < d0) { |
| 281 |
break; // don't let rm overflow |
| 282 |
} |
| 283 |
t2 -= d1; |
| 284 |
} |
| 285 |
#else // !BN_ULLONG |
| 286 |
BN_ULONG t2l, t2h; |
| 287 |
BN_UMULT_LOHI(t2l, t2h, d1, q); |
| 288 |
for (;;) { |
| 289 |
if (t2h < rm || |
| 290 |
(t2h == rm && t2l <= wnump[-2])) { |
| 291 |
break; |
| 292 |
} |
| 293 |
q--; |
| 294 |
rm += d0; |
| 295 |
if (rm < d0) { |
| 296 |
break; // don't let rm overflow |
| 297 |
} |
| 298 |
if (t2l < d1) { |
| 299 |
t2h--; |
| 300 |
} |
| 301 |
t2l -= d1; |
| 302 |
} |
| 303 |
#endif // !BN_ULLONG |
| 304 |
} |
| 305 |
|
| 306 |
l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q); |
| 307 |
tmp->d[div_n] = l0; |
| 308 |
wnum.d--; |
| 309 |
// ingore top values of the bignums just sub the two |
| 310 |
// BN_ULONG arrays with bn_sub_words |
| 311 |
if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) { |
| 312 |
// Note: As we have considered only the leading |
| 313 |
// two BN_ULONGs in the calculation of q, sdiv * q |
| 314 |
// might be greater than wnum (but then (q-1) * sdiv |
| 315 |
// is less or equal than wnum) |
| 316 |
q--; |
| 317 |
if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n)) { |
| 318 |
// we can't have an overflow here (assuming |
| 319 |
// that q != 0, but if q == 0 then tmp is |
| 320 |
// zero anyway) |
| 321 |
(*wnump)++; |
| 322 |
} |
| 323 |
} |
| 324 |
// store part of the result |
| 325 |
*resp = q; |
| 326 |
} |
| 327 |
|
| 328 |
bn_set_minimal_width(snum); |
| 329 |
|
| 330 |
if (rem != NULL) { |
| 331 |
if (!BN_rshift(rem, snum, norm_shift)) { |
| 332 |
goto err; |
| 333 |
} |
| 334 |
if (!BN_is_zero(rem)) { |
| 335 |
rem->neg = numerator_neg; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
bn_set_minimal_width(res); |
| 340 |
BN_CTX_end(ctx); |
| 341 |
return 1; |
| 342 |
|
| 343 |
err: |
| 344 |
BN_CTX_end(ctx); |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) { |
| 349 |
if (!(BN_mod(r, m, d, ctx))) { |
| 350 |
return 0; |
| 351 |
} |
| 352 |
if (!r->neg) { |
| 353 |
return 1; |
| 354 |
} |
| 355 |
|
| 356 |
// now -|d| < r < 0, so we have to set r := r + |d|. |
| 357 |
return (d->neg ? BN_sub : BN_add)(r, r, d); |
| 358 |
} |
| 359 |
|
| 360 |
BN_ULONG bn_reduce_once(BN_ULONG *r, const BN_ULONG *a, BN_ULONG carry, |
| 361 |
const BN_ULONG *m, size_t num) { |
| 362 |
assert(r != a); |
| 363 |
// |r| = |a| - |m|. |bn_sub_words| performs the bulk of the subtraction, and |
| 364 |
// then we apply the borrow to |carry|. |
| 365 |
carry -= bn_sub_words(r, a, m, num); |
| 366 |
// We know 0 <= |a| < 2*|m|, so -|m| <= |r| < |m|. |
| 367 |
// |
| 368 |
// If 0 <= |r| < |m|, |r| fits in |num| words and |carry| is zero. We then |
| 369 |
// wish to select |r| as the answer. Otherwise -m <= r < 0 and we wish to |
| 370 |
// return |r| + |m|, or |a|. |carry| must then be -1 or all ones. In both |
| 371 |
// cases, |carry| is a suitable input to |bn_select_words|. |
| 372 |
// |
| 373 |
// Although |carry| may be one if it was one on input and |bn_sub_words| |
| 374 |
// returns zero, this would give |r| > |m|, violating our input assumptions. |
| 375 |
declassify_assert(carry + 1 <= 1); |
| 376 |
bn_select_words(r, carry, a /* r < 0 */, r /* r >= 0 */, num); |
| 377 |
return carry; |
| 378 |
} |
| 379 |
|
| 380 |
BN_ULONG bn_reduce_once_in_place(BN_ULONG *r, BN_ULONG carry, const BN_ULONG *m, |
| 381 |
BN_ULONG *tmp, size_t num) { |
| 382 |
// See |bn_reduce_once| for why this logic works. |
| 383 |
carry -= bn_sub_words(tmp, r, m, num); |
| 384 |
declassify_assert(carry + 1 <= 1); |
| 385 |
bn_select_words(r, carry, r /* tmp < 0 */, tmp /* tmp >= 0 */, num); |
| 386 |
return carry; |
| 387 |
} |
| 388 |
|
| 389 |
void bn_mod_sub_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 390 |
const BN_ULONG *m, BN_ULONG *tmp, size_t num) { |
| 391 |
// r = a - b |
| 392 |
BN_ULONG borrow = bn_sub_words(r, a, b, num); |
| 393 |
// tmp = a - b + m |
| 394 |
bn_add_words(tmp, r, m, num); |
| 395 |
bn_select_words(r, 0 - borrow, tmp /* r < 0 */, r /* r >= 0 */, num); |
| 396 |
} |
| 397 |
|
| 398 |
void bn_mod_add_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, |
| 399 |
const BN_ULONG *m, BN_ULONG *tmp, size_t num) { |
| 400 |
BN_ULONG carry = bn_add_words(r, a, b, num); |
| 401 |
bn_reduce_once_in_place(r, carry, m, tmp, num); |
| 402 |
} |
| 403 |
|
| 404 |
int bn_div_consttime(BIGNUM *quotient, BIGNUM *remainder, |
| 405 |
const BIGNUM *numerator, const BIGNUM *divisor, |
| 406 |
unsigned divisor_min_bits, BN_CTX *ctx) { |
| 407 |
if (BN_is_negative(numerator) || BN_is_negative(divisor)) { |
| 408 |
OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER); |
| 409 |
return 0; |
| 410 |
} |
| 411 |
if (BN_is_zero(divisor)) { |
| 412 |
OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO); |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
// This function implements long division in binary. It is not very efficient, |
| 417 |
// but it is simple, easy to make constant-time, and performant enough for RSA |
| 418 |
// key generation. |
| 419 |
|
| 420 |
int ret = 0; |
| 421 |
BN_CTX_start(ctx); |
| 422 |
BIGNUM *q = quotient, *r = remainder; |
| 423 |
if (quotient == NULL || quotient == numerator || quotient == divisor) { |
| 424 |
q = BN_CTX_get(ctx); |
| 425 |
} |
| 426 |
if (remainder == NULL || remainder == numerator || remainder == divisor) { |
| 427 |
r = BN_CTX_get(ctx); |
| 428 |
} |
| 429 |
BIGNUM *tmp = BN_CTX_get(ctx); |
| 430 |
if (q == NULL || r == NULL || tmp == NULL || |
| 431 |
!bn_wexpand(q, numerator->width) || |
| 432 |
!bn_wexpand(r, divisor->width) || |
| 433 |
!bn_wexpand(tmp, divisor->width)) { |
| 434 |
goto err; |
| 435 |
} |
| 436 |
|
| 437 |
OPENSSL_memset(q->d, 0, numerator->width * sizeof(BN_ULONG)); |
| 438 |
q->width = numerator->width; |
| 439 |
q->neg = 0; |
| 440 |
|
| 441 |
OPENSSL_memset(r->d, 0, divisor->width * sizeof(BN_ULONG)); |
| 442 |
r->width = divisor->width; |
| 443 |
r->neg = 0; |
| 444 |
|
| 445 |
// Incorporate |numerator| into |r|, one bit at a time, reducing after each |
| 446 |
// step. We maintain the invariant that |0 <= r < divisor| and |
| 447 |
// |q * divisor + r = n| where |n| is the portion of |numerator| incorporated |
| 448 |
// so far. |
| 449 |
// |
| 450 |
// First, we short-circuit the loop: if we know |divisor| has at least |
| 451 |
// |divisor_min_bits| bits, the top |divisor_min_bits - 1| can be incorporated |
| 452 |
// without reductions. This significantly speeds up |RSA_check_key|. For |
| 453 |
// simplicity, we round down to a whole number of words. |
| 454 |
declassify_assert(divisor_min_bits <= BN_num_bits(divisor)); |
| 455 |
int initial_words = 0; |
| 456 |
if (divisor_min_bits > 0) { |
| 457 |
initial_words = (divisor_min_bits - 1) / BN_BITS2; |
| 458 |
if (initial_words > numerator->width) { |
| 459 |
initial_words = numerator->width; |
| 460 |
} |
| 461 |
OPENSSL_memcpy(r->d, numerator->d + numerator->width - initial_words, |
| 462 |
initial_words * sizeof(BN_ULONG)); |
| 463 |
} |
| 464 |
|
| 465 |
for (int i = numerator->width - initial_words - 1; i >= 0; i--) { |
| 466 |
for (int bit = BN_BITS2 - 1; bit >= 0; bit--) { |
| 467 |
// Incorporate the next bit of the numerator, by computing |
| 468 |
// r = 2*r or 2*r + 1. Note the result fits in one more word. We store the |
| 469 |
// extra word in |carry|. |
| 470 |
BN_ULONG carry = bn_add_words(r->d, r->d, r->d, divisor->width); |
| 471 |
r->d[0] |= (numerator->d[i] >> bit) & 1; |
| 472 |
// |r| was previously fully-reduced, so we know: |
| 473 |
// 2*0 <= r <= 2*(divisor-1) + 1 |
| 474 |
// 0 <= r <= 2*divisor - 1 < 2*divisor. |
| 475 |
// Thus |r| satisfies the preconditions for |bn_reduce_once_in_place|. |
| 476 |
BN_ULONG subtracted = bn_reduce_once_in_place(r->d, carry, divisor->d, |
| 477 |
tmp->d, divisor->width); |
| 478 |
// The corresponding bit of the quotient is set iff we needed to subtract. |
| 479 |
q->d[i] |= (~subtracted & 1) << bit; |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
if ((quotient != NULL && !BN_copy(quotient, q)) || |
| 484 |
(remainder != NULL && !BN_copy(remainder, r))) { |
| 485 |
goto err; |
| 486 |
} |
| 487 |
|
| 488 |
ret = 1; |
| 489 |
|
| 490 |
err: |
| 491 |
BN_CTX_end(ctx); |
| 492 |
return ret; |
| 493 |
} |
| 494 |
|
| 495 |
static BIGNUM *bn_scratch_space_from_ctx(size_t width, BN_CTX *ctx) { |
| 496 |
BIGNUM *ret = BN_CTX_get(ctx); |
| 497 |
if (ret == NULL || |
| 498 |
!bn_wexpand(ret, width)) { |
| 499 |
return NULL; |
| 500 |
} |
| 501 |
ret->neg = 0; |
| 502 |
ret->width = (int)width; |
| 503 |
return ret; |
| 504 |
} |
| 505 |
|
| 506 |
// bn_resized_from_ctx returns |bn| with width at least |width| or NULL on |
| 507 |
// error. This is so it may be used with low-level "words" functions. If |
| 508 |
// necessary, it allocates a new |BIGNUM| with a lifetime of the current scope |
| 509 |
// in |ctx|, so the caller does not need to explicitly free it. |bn| must fit in |
| 510 |
// |width| words. |
| 511 |
static const BIGNUM *bn_resized_from_ctx(const BIGNUM *bn, size_t width, |
| 512 |
BN_CTX *ctx) { |
| 513 |
if ((size_t)bn->width >= width) { |
| 514 |
// Any excess words must be zero. |
| 515 |
assert(bn_fits_in_words(bn, width)); |
| 516 |
return bn; |
| 517 |
} |
| 518 |
BIGNUM *ret = bn_scratch_space_from_ctx(width, ctx); |
| 519 |
if (ret == NULL || |
| 520 |
!BN_copy(ret, bn) || |
| 521 |
!bn_resize_words(ret, width)) { |
| 522 |
return NULL; |
| 523 |
} |
| 524 |
return ret; |
| 525 |
} |
| 526 |
|
| 527 |
int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, |
| 528 |
BN_CTX *ctx) { |
| 529 |
if (!BN_add(r, a, b)) { |
| 530 |
return 0; |
| 531 |
} |
| 532 |
return BN_nnmod(r, r, m, ctx); |
| 533 |
} |
| 534 |
|
| 535 |
int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 536 |
const BIGNUM *m) { |
| 537 |
BN_CTX *ctx = BN_CTX_new(); |
| 538 |
int ok = ctx != NULL && |
| 539 |
bn_mod_add_consttime(r, a, b, m, ctx); |
| 540 |
BN_CTX_free(ctx); |
| 541 |
return ok; |
| 542 |
} |
| 543 |
|
| 544 |
int bn_mod_add_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 545 |
const BIGNUM *m, BN_CTX *ctx) { |
| 546 |
BN_CTX_start(ctx); |
| 547 |
a = bn_resized_from_ctx(a, m->width, ctx); |
| 548 |
b = bn_resized_from_ctx(b, m->width, ctx); |
| 549 |
BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx); |
| 550 |
int ok = a != NULL && b != NULL && tmp != NULL && |
| 551 |
bn_wexpand(r, m->width); |
| 552 |
if (ok) { |
| 553 |
bn_mod_add_words(r->d, a->d, b->d, m->d, tmp->d, m->width); |
| 554 |
r->width = m->width; |
| 555 |
r->neg = 0; |
| 556 |
} |
| 557 |
BN_CTX_end(ctx); |
| 558 |
return ok; |
| 559 |
} |
| 560 |
|
| 561 |
int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, |
| 562 |
BN_CTX *ctx) { |
| 563 |
if (!BN_sub(r, a, b)) { |
| 564 |
return 0; |
| 565 |
} |
| 566 |
return BN_nnmod(r, r, m, ctx); |
| 567 |
} |
| 568 |
|
| 569 |
int bn_mod_sub_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 570 |
const BIGNUM *m, BN_CTX *ctx) { |
| 571 |
BN_CTX_start(ctx); |
| 572 |
a = bn_resized_from_ctx(a, m->width, ctx); |
| 573 |
b = bn_resized_from_ctx(b, m->width, ctx); |
| 574 |
BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx); |
| 575 |
int ok = a != NULL && b != NULL && tmp != NULL && |
| 576 |
bn_wexpand(r, m->width); |
| 577 |
if (ok) { |
| 578 |
bn_mod_sub_words(r->d, a->d, b->d, m->d, tmp->d, m->width); |
| 579 |
r->width = m->width; |
| 580 |
r->neg = 0; |
| 581 |
} |
| 582 |
BN_CTX_end(ctx); |
| 583 |
return ok; |
| 584 |
} |
| 585 |
|
| 586 |
int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| 587 |
const BIGNUM *m) { |
| 588 |
BN_CTX *ctx = BN_CTX_new(); |
| 589 |
int ok = ctx != NULL && |
| 590 |
bn_mod_sub_consttime(r, a, b, m, ctx); |
| 591 |
BN_CTX_free(ctx); |
| 592 |
return ok; |
| 593 |
} |
| 594 |
|
| 595 |
int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, |
| 596 |
BN_CTX *ctx) { |
| 597 |
BIGNUM *t; |
| 598 |
int ret = 0; |
| 599 |
|
| 600 |
BN_CTX_start(ctx); |
| 601 |
t = BN_CTX_get(ctx); |
| 602 |
if (t == NULL) { |
| 603 |
goto err; |
| 604 |
} |
| 605 |
|
| 606 |
if (a == b) { |
| 607 |
if (!BN_sqr(t, a, ctx)) { |
| 608 |
goto err; |
| 609 |
} |
| 610 |
} else { |
| 611 |
if (!BN_mul(t, a, b, ctx)) { |
| 612 |
goto err; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
if (!BN_nnmod(r, t, m, ctx)) { |
| 617 |
goto err; |
| 618 |
} |
| 619 |
|
| 620 |
ret = 1; |
| 621 |
|
| 622 |
err: |
| 623 |
BN_CTX_end(ctx); |
| 624 |
return ret; |
| 625 |
} |
| 626 |
|
| 627 |
int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) { |
| 628 |
if (!BN_sqr(r, a, ctx)) { |
| 629 |
return 0; |
| 630 |
} |
| 631 |
|
| 632 |
// r->neg == 0, thus we don't need BN_nnmod |
| 633 |
return BN_mod(r, r, m, ctx); |
| 634 |
} |
| 635 |
|
| 636 |
int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, |
| 637 |
BN_CTX *ctx) { |
| 638 |
BIGNUM *abs_m = NULL; |
| 639 |
int ret; |
| 640 |
|
| 641 |
if (!BN_nnmod(r, a, m, ctx)) { |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
if (m->neg) { |
| 646 |
abs_m = BN_dup(m); |
| 647 |
if (abs_m == NULL) { |
| 648 |
return 0; |
| 649 |
} |
| 650 |
abs_m->neg = 0; |
| 651 |
} |
| 652 |
|
| 653 |
ret = bn_mod_lshift_consttime(r, r, n, (abs_m ? abs_m : m), ctx); |
| 654 |
|
| 655 |
BN_free(abs_m); |
| 656 |
return ret; |
| 657 |
} |
| 658 |
|
| 659 |
int bn_mod_lshift_consttime(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, |
| 660 |
BN_CTX *ctx) { |
| 661 |
if (!BN_copy(r, a) || |
| 662 |
!bn_resize_words(r, m->width)) { |
| 663 |
return 0; |
| 664 |
} |
| 665 |
|
| 666 |
BN_CTX_start(ctx); |
| 667 |
BIGNUM *tmp = bn_scratch_space_from_ctx(m->width, ctx); |
| 668 |
int ok = tmp != NULL; |
| 669 |
if (ok) { |
| 670 |
for (int i = 0; i < n; i++) { |
| 671 |
bn_mod_add_words(r->d, r->d, r->d, m->d, tmp->d, m->width); |
| 672 |
} |
| 673 |
r->neg = 0; |
| 674 |
} |
| 675 |
BN_CTX_end(ctx); |
| 676 |
return ok; |
| 677 |
} |
| 678 |
|
| 679 |
int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) { |
| 680 |
BN_CTX *ctx = BN_CTX_new(); |
| 681 |
int ok = ctx != NULL && |
| 682 |
bn_mod_lshift_consttime(r, a, n, m, ctx); |
| 683 |
BN_CTX_free(ctx); |
| 684 |
return ok; |
| 685 |
} |
| 686 |
|
| 687 |
int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) { |
| 688 |
if (!BN_lshift1(r, a)) { |
| 689 |
return 0; |
| 690 |
} |
| 691 |
|
| 692 |
return BN_nnmod(r, r, m, ctx); |
| 693 |
} |
| 694 |
|
| 695 |
int bn_mod_lshift1_consttime(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, |
| 696 |
BN_CTX *ctx) { |
| 697 |
return bn_mod_add_consttime(r, a, a, m, ctx); |
| 698 |
} |
| 699 |
|
| 700 |
int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) { |
| 701 |
BN_CTX *ctx = BN_CTX_new(); |
| 702 |
int ok = ctx != NULL && |
| 703 |
bn_mod_lshift1_consttime(r, a, m, ctx); |
| 704 |
BN_CTX_free(ctx); |
| 705 |
return ok; |
| 706 |
} |
| 707 |
|
| 708 |
BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) { |
| 709 |
BN_ULONG ret = 0; |
| 710 |
int i, j; |
| 711 |
|
| 712 |
if (!w) { |
| 713 |
// actually this an error (division by zero) |
| 714 |
return (BN_ULONG) - 1; |
| 715 |
} |
| 716 |
|
| 717 |
if (a->width == 0) { |
| 718 |
return 0; |
| 719 |
} |
| 720 |
|
| 721 |
// normalize input for |bn_div_rem_words|. |
| 722 |
j = BN_BITS2 - BN_num_bits_word(w); |
| 723 |
w <<= j; |
| 724 |
if (!BN_lshift(a, a, j)) { |
| 725 |
return (BN_ULONG) - 1; |
| 726 |
} |
| 727 |
|
| 728 |
for (i = a->width - 1; i >= 0; i--) { |
| 729 |
BN_ULONG l = a->d[i]; |
| 730 |
BN_ULONG d; |
| 731 |
BN_ULONG unused_rem; |
| 732 |
bn_div_rem_words(&d, &unused_rem, ret, l, w); |
| 733 |
ret = l - (d * w); |
| 734 |
a->d[i] = d; |
| 735 |
} |
| 736 |
|
| 737 |
bn_set_minimal_width(a); |
| 738 |
ret >>= j; |
| 739 |
return ret; |
| 740 |
} |
| 741 |
|
| 742 |
BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) { |
| 743 |
#ifndef BN_CAN_DIVIDE_ULLONG |
| 744 |
BN_ULONG ret = 0; |
| 745 |
#else |
| 746 |
BN_ULLONG ret = 0; |
| 747 |
#endif |
| 748 |
int i; |
| 749 |
|
| 750 |
if (w == 0) { |
| 751 |
return (BN_ULONG) -1; |
| 752 |
} |
| 753 |
|
| 754 |
#ifndef BN_CAN_DIVIDE_ULLONG |
| 755 |
// If |w| is too long and we don't have |BN_ULLONG| division then we need to |
| 756 |
// fall back to using |BN_div_word|. |
| 757 |
if (w > ((BN_ULONG)1 << BN_BITS4)) { |
| 758 |
BIGNUM *tmp = BN_dup(a); |
| 759 |
if (tmp == NULL) { |
| 760 |
return (BN_ULONG)-1; |
| 761 |
} |
| 762 |
ret = BN_div_word(tmp, w); |
| 763 |
BN_free(tmp); |
| 764 |
return ret; |
| 765 |
} |
| 766 |
#endif |
| 767 |
|
| 768 |
for (i = a->width - 1; i >= 0; i--) { |
| 769 |
#ifndef BN_CAN_DIVIDE_ULLONG |
| 770 |
ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w; |
| 771 |
ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w; |
| 772 |
#else |
| 773 |
ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w); |
| 774 |
#endif |
| 775 |
} |
| 776 |
return (BN_ULONG)ret; |
| 777 |
} |
| 778 |
|
| 779 |
int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) { |
| 780 |
if (e == 0 || a->width == 0) { |
| 781 |
BN_zero(r); |
| 782 |
return 1; |
| 783 |
} |
| 784 |
|
| 785 |
size_t num_words = 1 + ((e - 1) / BN_BITS2); |
| 786 |
|
| 787 |
// If |a| definitely has less than |e| bits, just BN_copy. |
| 788 |
if ((size_t) a->width < num_words) { |
| 789 |
return BN_copy(r, a) != NULL; |
| 790 |
} |
| 791 |
|
| 792 |
// Otherwise, first make sure we have enough space in |r|. |
| 793 |
// Note that this will fail if num_words > INT_MAX. |
| 794 |
if (!bn_wexpand(r, num_words)) { |
| 795 |
return 0; |
| 796 |
} |
| 797 |
|
| 798 |
// Copy the content of |a| into |r|. |
| 799 |
OPENSSL_memcpy(r->d, a->d, num_words * sizeof(BN_ULONG)); |
| 800 |
|
| 801 |
// If |e| isn't word-aligned, we have to mask off some of our bits. |
| 802 |
size_t top_word_exponent = e % (sizeof(BN_ULONG) * 8); |
| 803 |
if (top_word_exponent != 0) { |
| 804 |
r->d[num_words - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1; |
| 805 |
} |
| 806 |
|
| 807 |
// Fill in the remaining fields of |r|. |
| 808 |
r->neg = a->neg; |
| 809 |
r->width = (int) num_words; |
| 810 |
bn_set_minimal_width(r); |
| 811 |
return 1; |
| 812 |
} |
| 813 |
|
| 814 |
int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) { |
| 815 |
if (!BN_mod_pow2(r, a, e)) { |
| 816 |
return 0; |
| 817 |
} |
| 818 |
|
| 819 |
// If the returned value was non-negative, we're done. |
| 820 |
if (BN_is_zero(r) || !r->neg) { |
| 821 |
return 1; |
| 822 |
} |
| 823 |
|
| 824 |
size_t num_words = 1 + (e - 1) / BN_BITS2; |
| 825 |
|
| 826 |
// Expand |r| to the size of our modulus. |
| 827 |
if (!bn_wexpand(r, num_words)) { |
| 828 |
return 0; |
| 829 |
} |
| 830 |
|
| 831 |
// Clear the upper words of |r|. |
| 832 |
OPENSSL_memset(&r->d[r->width], 0, (num_words - r->width) * BN_BYTES); |
| 833 |
|
| 834 |
// Set parameters of |r|. |
| 835 |
r->neg = 0; |
| 836 |
r->width = (int) num_words; |
| 837 |
|
| 838 |
// Now, invert every word. The idea here is that we want to compute 2^e-|x|, |
| 839 |
// which is actually equivalent to the twos-complement representation of |x| |
| 840 |
// in |e| bits, which is -x = ~x + 1. |
| 841 |
for (int i = 0; i < r->width; i++) { |
| 842 |
r->d[i] = ~r->d[i]; |
| 843 |
} |
| 844 |
|
| 845 |
// If our exponent doesn't span the top word, we have to mask the rest. |
| 846 |
size_t top_word_exponent = e % BN_BITS2; |
| 847 |
if (top_word_exponent != 0) { |
| 848 |
r->d[r->width - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1; |
| 849 |
} |
| 850 |
|
| 851 |
// Keep the minimal-width invariant for |BIGNUM|. |
| 852 |
bn_set_minimal_width(r); |
| 853 |
|
| 854 |
// Finally, add one, for the reason described above. |
| 855 |
return BN_add(r, r, BN_value_one()); |
| 856 |
} |