diff options
| author | jsing <> | 2024-03-26 04:14:45 +0000 |
|---|---|---|
| committer | jsing <> | 2024-03-26 04:14:45 +0000 |
| commit | 10fd751deb36d78d543bb76f136e3844648b633a (patch) | |
| tree | c4056fbe5431948b21d05ea1ae5c18beefbcc1d3 /src/lib/libc | |
| parent | d85783d637619b77333572a1ac0715e5739d3116 (diff) | |
| download | openbsd-10fd751deb36d78d543bb76f136e3844648b633a.tar.gz openbsd-10fd751deb36d78d543bb76f136e3844648b633a.tar.bz2 openbsd-10fd751deb36d78d543bb76f136e3844648b633a.zip | |
Move bn_montgomery_reduce() and drop prototype.
No functional change.
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_mont.c | 144 |
1 files changed, 71 insertions, 73 deletions
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c index 12fea44c5a..7fdbfbd54d 100644 --- a/src/lib/libcrypto/bn/bn_mont.c +++ b/src/lib/libcrypto/bn/bn_mont.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_mont.c,v 1.61 2023/07/08 12:21:58 beck Exp $ */ | 1 | /* $OpenBSD: bn_mont.c,v 1.62 2024/03/26 04:14:45 jsing Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -299,7 +299,76 @@ BN_MONT_CTX_set_locked(BN_MONT_CTX **pmctx, int lock, const BIGNUM *mod, | |||
| 299 | } | 299 | } |
| 300 | LCRYPTO_ALIAS(BN_MONT_CTX_set_locked); | 300 | LCRYPTO_ALIAS(BN_MONT_CTX_set_locked); |
| 301 | 301 | ||
| 302 | static int bn_montgomery_reduce(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mctx); | 302 | /* |
| 303 | * bn_montgomery_reduce() performs Montgomery reduction, reducing the input | ||
| 304 | * from its Montgomery form aR to a, returning the result in r. Note that the | ||
| 305 | * input is mutated in the process of performing the reduction, destroying its | ||
| 306 | * original value. | ||
| 307 | */ | ||
| 308 | static int | ||
| 309 | bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx) | ||
| 310 | { | ||
| 311 | BIGNUM *n; | ||
| 312 | BN_ULONG *ap, *rp, n0, v, carry, mask; | ||
| 313 | int i, max, n_len; | ||
| 314 | |||
| 315 | n = &mctx->N; | ||
| 316 | n_len = mctx->N.top; | ||
| 317 | |||
| 318 | if (n_len == 0) { | ||
| 319 | BN_zero(r); | ||
| 320 | return 1; | ||
| 321 | } | ||
| 322 | |||
| 323 | if (!bn_wexpand(r, n_len)) | ||
| 324 | return 0; | ||
| 325 | |||
| 326 | /* | ||
| 327 | * Expand a to twice the length of the modulus, zero if necessary. | ||
| 328 | * XXX - make this a requirement of the caller. | ||
| 329 | */ | ||
| 330 | if ((max = 2 * n_len) < n_len) | ||
| 331 | return 0; | ||
| 332 | if (!bn_wexpand(a, max)) | ||
| 333 | return 0; | ||
| 334 | for (i = a->top; i < max; i++) | ||
| 335 | a->d[i] = 0; | ||
| 336 | |||
| 337 | carry = 0; | ||
| 338 | n0 = mctx->n0[0]; | ||
| 339 | |||
| 340 | /* Add multiples of the modulus, so that it becomes divisible by R. */ | ||
| 341 | for (i = 0; i < n_len; i++) { | ||
| 342 | v = bn_mul_add_words(&a->d[i], n->d, n_len, a->d[i] * n0); | ||
| 343 | bn_addw_addw(v, a->d[i + n_len], carry, &carry, | ||
| 344 | &a->d[i + n_len]); | ||
| 345 | } | ||
| 346 | |||
| 347 | /* Divide by R (this is the equivalent of right shifting by n_len). */ | ||
| 348 | ap = &a->d[n_len]; | ||
| 349 | |||
| 350 | /* | ||
| 351 | * The output is now in the range of [0, 2N). Attempt to reduce once by | ||
| 352 | * subtracting the modulus. If the reduction was necessary then the | ||
| 353 | * result is already in r, otherwise copy the value prior to reduction | ||
| 354 | * from the top half of a. | ||
| 355 | */ | ||
| 356 | mask = carry - bn_sub_words(r->d, ap, n->d, n_len); | ||
| 357 | |||
| 358 | rp = r->d; | ||
| 359 | for (i = 0; i < n_len; i++) { | ||
| 360 | *rp = (*rp & ~mask) | (*ap & mask); | ||
| 361 | rp++; | ||
| 362 | ap++; | ||
| 363 | } | ||
| 364 | r->top = n_len; | ||
| 365 | |||
| 366 | bn_correct_top(r); | ||
| 367 | |||
| 368 | BN_set_negative(r, a->neg ^ n->neg); | ||
| 369 | |||
| 370 | return 1; | ||
| 371 | } | ||
| 303 | 372 | ||
| 304 | static int | 373 | static int |
| 305 | bn_mod_mul_montgomery_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, | 374 | bn_mod_mul_montgomery_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, |
| @@ -512,77 +581,6 @@ BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mctx, BN_CTX *ctx) | |||
| 512 | } | 581 | } |
| 513 | LCRYPTO_ALIAS(BN_to_montgomery); | 582 | LCRYPTO_ALIAS(BN_to_montgomery); |
| 514 | 583 | ||
| 515 | /* | ||
| 516 | * bn_montgomery_reduce() performs Montgomery reduction, reducing the input | ||
| 517 | * from its Montgomery form aR to a, returning the result in r. Note that the | ||
| 518 | * input is mutated in the process of performing the reduction, destroying its | ||
| 519 | * original value. | ||
| 520 | */ | ||
| 521 | static int | ||
| 522 | bn_montgomery_reduce(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mctx) | ||
| 523 | { | ||
| 524 | BIGNUM *n; | ||
| 525 | BN_ULONG *ap, *rp, n0, v, carry, mask; | ||
| 526 | int i, max, n_len; | ||
| 527 | |||
| 528 | n = &mctx->N; | ||
| 529 | n_len = mctx->N.top; | ||
| 530 | |||
| 531 | if (n_len == 0) { | ||
| 532 | BN_zero(r); | ||
| 533 | return 1; | ||
| 534 | } | ||
| 535 | |||
| 536 | if (!bn_wexpand(r, n_len)) | ||
| 537 | return 0; | ||
| 538 | |||
| 539 | /* | ||
| 540 | * Expand a to twice the length of the modulus, zero if necessary. | ||
| 541 | * XXX - make this a requirement of the caller. | ||
| 542 | */ | ||
| 543 | if ((max = 2 * n_len) < n_len) | ||
| 544 | return 0; | ||
| 545 | if (!bn_wexpand(a, max)) | ||
| 546 | return 0; | ||
| 547 | for (i = a->top; i < max; i++) | ||
| 548 | a->d[i] = 0; | ||
| 549 | |||
| 550 | carry = 0; | ||
| 551 | n0 = mctx->n0[0]; | ||
| 552 | |||
| 553 | /* Add multiples of the modulus, so that it becomes divisible by R. */ | ||
| 554 | for (i = 0; i < n_len; i++) { | ||
| 555 | v = bn_mul_add_words(&a->d[i], n->d, n_len, a->d[i] * n0); | ||
| 556 | bn_addw_addw(v, a->d[i + n_len], carry, &carry, | ||
| 557 | &a->d[i + n_len]); | ||
| 558 | } | ||
| 559 | |||
| 560 | /* Divide by R (this is the equivalent of right shifting by n_len). */ | ||
| 561 | ap = &a->d[n_len]; | ||
| 562 | |||
| 563 | /* | ||
| 564 | * The output is now in the range of [0, 2N). Attempt to reduce once by | ||
| 565 | * subtracting the modulus. If the reduction was necessary then the | ||
| 566 | * result is already in r, otherwise copy the value prior to reduction | ||
| 567 | * from the top half of a. | ||
| 568 | */ | ||
| 569 | mask = carry - bn_sub_words(r->d, ap, n->d, n_len); | ||
| 570 | |||
| 571 | rp = r->d; | ||
| 572 | for (i = 0; i < n_len; i++) { | ||
| 573 | *rp = (*rp & ~mask) | (*ap & mask); | ||
| 574 | rp++; | ||
| 575 | ap++; | ||
| 576 | } | ||
| 577 | r->top = n_len; | ||
| 578 | |||
| 579 | bn_correct_top(r); | ||
| 580 | |||
| 581 | BN_set_negative(r, a->neg ^ n->neg); | ||
| 582 | |||
| 583 | return 1; | ||
| 584 | } | ||
| 585 | |||
| 586 | int | 584 | int |
| 587 | BN_from_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mctx, BN_CTX *ctx) | 585 | BN_from_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mctx, BN_CTX *ctx) |
| 588 | { | 586 | { |
