diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/bn/bn_mod_exp.c | 88 |
1 files changed, 43 insertions, 45 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_mod_exp.c b/src/regress/lib/libcrypto/bn/bn_mod_exp.c index f381df3734..38e77f3ad9 100644 --- a/src/regress/lib/libcrypto/bn/bn_mod_exp.c +++ b/src/regress/lib/libcrypto/bn/bn_mod_exp.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_mod_exp.c,v 1.27 2023/03/27 09:01:08 tb Exp $ */ | 1 | /* $OpenBSD: bn_mod_exp.c,v 1.28 2023/03/29 06:32:53 tb Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 2022,2023 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2022,2023 Theo Buehler <tb@openbsd.org> |
| @@ -280,34 +280,34 @@ generate_test_triple(int reduce, BIGNUM *a, BIGNUM *p, BIGNUM *m, BN_CTX *ctx) | |||
| 280 | } | 280 | } |
| 281 | 281 | ||
| 282 | static int | 282 | static int |
| 283 | generate_test_quintuple(int reduce, BIGNUM *a1, BIGNUM *p1, | 283 | generate_test_quintuple(int reduce, BIGNUM *a, BIGNUM *p, |
| 284 | BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx) | 284 | BIGNUM *b, BIGNUM *q, BIGNUM *m, BN_CTX *ctx) |
| 285 | { | 285 | { |
| 286 | BIGNUM *mmodified; | 286 | BIGNUM *mmodified; |
| 287 | BN_ULONG multiple; | 287 | BN_ULONG multiple; |
| 288 | int avg = 2 * BN_BITS, deviate = BN_BITS / 2; | 288 | int avg = 2 * BN_BITS, deviate = BN_BITS / 2; |
| 289 | int ret = 0; | 289 | int ret = 0; |
| 290 | 290 | ||
| 291 | if (!generate_bn(a1, avg, deviate, 0)) | 291 | if (!generate_bn(a, avg, deviate, 0)) |
| 292 | return 0; | 292 | return 0; |
| 293 | 293 | ||
| 294 | if (!generate_bn(p1, avg, deviate, 0)) | 294 | if (!generate_bn(p, avg, deviate, 0)) |
| 295 | return 0; | 295 | return 0; |
| 296 | 296 | ||
| 297 | if (!generate_bn(a2, avg, deviate, 0)) | 297 | if (!generate_bn(b, avg, deviate, 0)) |
| 298 | return 0; | 298 | return 0; |
| 299 | 299 | ||
| 300 | if (!generate_bn(p2, avg, deviate, 0)) | 300 | if (!generate_bn(q, avg, deviate, 0)) |
| 301 | return 0; | 301 | return 0; |
| 302 | 302 | ||
| 303 | if (!generate_bn(m, avg, deviate, 1)) | 303 | if (!generate_bn(m, avg, deviate, 1)) |
| 304 | return 0; | 304 | return 0; |
| 305 | 305 | ||
| 306 | if (reduce) { | 306 | if (reduce) { |
| 307 | if (!BN_mod(a1, a1, m, ctx)) | 307 | if (!BN_mod(a, a, m, ctx)) |
| 308 | return 0; | 308 | return 0; |
| 309 | 309 | ||
| 310 | return BN_mod(a2, a2, m, ctx); | 310 | return BN_mod(b, b, m, ctx); |
| 311 | } | 311 | } |
| 312 | 312 | ||
| 313 | /* | 313 | /* |
| @@ -327,10 +327,10 @@ generate_test_quintuple(int reduce, BIGNUM *a1, BIGNUM *p1, | |||
| 327 | if (!BN_mul_word(mmodified, multiple)) | 327 | if (!BN_mul_word(mmodified, multiple)) |
| 328 | goto err; | 328 | goto err; |
| 329 | 329 | ||
| 330 | if (!BN_add(a1, a1, mmodified)) | 330 | if (!BN_add(a, a, mmodified)) |
| 331 | goto err; | 331 | goto err; |
| 332 | 332 | ||
| 333 | if (!BN_add(a2, a2, mmodified)) | 333 | if (!BN_add(b, b, mmodified)) |
| 334 | goto err; | 334 | goto err; |
| 335 | 335 | ||
| 336 | ret = 1; | 336 | ret = 1; |
| @@ -405,7 +405,7 @@ bn_mod_exp_test(int reduce, BIGNUM *want, BIGNUM *a, BIGNUM *p, BIGNUM *m, | |||
| 405 | errx(1, "generate_test_triple"); | 405 | errx(1, "generate_test_triple"); |
| 406 | 406 | ||
| 407 | for (i = 0; i < 8 && !failed; i++) { | 407 | for (i = 0; i < 8 && !failed; i++) { |
| 408 | BN_set_negative(a, i & 1); | 408 | BN_set_negative(a, (i >> 0) & 1); |
| 409 | BN_set_negative(p, (i >> 1) & 1); | 409 | BN_set_negative(p, (i >> 1) & 1); |
| 410 | BN_set_negative(m, (i >> 2) & 1); | 410 | BN_set_negative(m, (i >> 2) & 1); |
| 411 | 411 | ||
| @@ -461,8 +461,8 @@ run_bn_mod_exp_tests(void) | |||
| 461 | } | 461 | } |
| 462 | 462 | ||
| 463 | static void | 463 | static void |
| 464 | dump_exp2_results(const BIGNUM *a1, const BIGNUM *p1, const BIGNUM *a2, | 464 | dump_exp2_results(const BIGNUM *a, const BIGNUM *p, const BIGNUM *b, |
| 465 | const BIGNUM *p2, const BIGNUM *m, const BIGNUM *want, const BIGNUM *got) | 465 | const BIGNUM *q, const BIGNUM *m, const BIGNUM *want, const BIGNUM *got) |
| 466 | { | 466 | { |
| 467 | printf("BN_mod_exp_simple() and BN_mod_exp2_mont() disagree"); | 467 | printf("BN_mod_exp_simple() and BN_mod_exp2_mont() disagree"); |
| 468 | 468 | ||
| @@ -472,21 +472,21 @@ dump_exp2_results(const BIGNUM *a1, const BIGNUM *p1, const BIGNUM *a2, | |||
| 472 | BN_print_fp(stdout, got); | 472 | BN_print_fp(stdout, got); |
| 473 | 473 | ||
| 474 | printf("\na1: "); | 474 | printf("\na1: "); |
| 475 | BN_print_fp(stdout, a1); | 475 | BN_print_fp(stdout, a); |
| 476 | printf("\np1: "); | 476 | printf("\np1: "); |
| 477 | BN_print_fp(stdout, p1); | 477 | BN_print_fp(stdout, p); |
| 478 | printf("\na2: "); | 478 | printf("\na2: "); |
| 479 | BN_print_fp(stdout, a2); | 479 | BN_print_fp(stdout, b); |
| 480 | printf("\np2: "); | 480 | printf("\np2: "); |
| 481 | BN_print_fp(stdout, p2); | 481 | BN_print_fp(stdout, q); |
| 482 | printf("\nm: "); | 482 | printf("\nm: "); |
| 483 | BN_print_fp(stdout, m); | 483 | BN_print_fp(stdout, m); |
| 484 | printf("\n\n"); | 484 | printf("\n\n"); |
| 485 | } | 485 | } |
| 486 | 486 | ||
| 487 | static int | 487 | static int |
| 488 | bn_mod_exp2_simple(BIGNUM *out, const BIGNUM *a1, const BIGNUM *p1, | 488 | bn_mod_exp2_simple(BIGNUM *out, const BIGNUM *a, const BIGNUM *p, |
| 489 | const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m, BN_CTX *ctx) | 489 | const BIGNUM *b, const BIGNUM *q, const BIGNUM *m, BN_CTX *ctx) |
| 490 | { | 490 | { |
| 491 | BIGNUM *fact1, *fact2; | 491 | BIGNUM *fact1, *fact2; |
| 492 | int ret = 0; | 492 | int ret = 0; |
| @@ -498,9 +498,9 @@ bn_mod_exp2_simple(BIGNUM *out, const BIGNUM *a1, const BIGNUM *p1, | |||
| 498 | if ((fact2 = BN_CTX_get(ctx)) == NULL) | 498 | if ((fact2 = BN_CTX_get(ctx)) == NULL) |
| 499 | goto err; | 499 | goto err; |
| 500 | 500 | ||
| 501 | if (!BN_mod_exp_simple(fact1, a1, p1, m, ctx)) | 501 | if (!BN_mod_exp_simple(fact1, a, p, m, ctx)) |
| 502 | goto err; | 502 | goto err; |
| 503 | if (!BN_mod_exp_simple(fact2, a2, p2, m, ctx)) | 503 | if (!BN_mod_exp_simple(fact2, b, q, m, ctx)) |
| 504 | goto err; | 504 | goto err; |
| 505 | if (!BN_mod_mul(out, fact1, fact2, m, ctx)) | 505 | if (!BN_mod_mul(out, fact1, fact2, m, ctx)) |
| 506 | goto err; | 506 | goto err; |
| @@ -513,30 +513,30 @@ bn_mod_exp2_simple(BIGNUM *out, const BIGNUM *a1, const BIGNUM *p1, | |||
| 513 | } | 513 | } |
| 514 | 514 | ||
| 515 | static int | 515 | static int |
| 516 | bn_mod_exp2_test(int reduce, BIGNUM *want, BIGNUM *got, BIGNUM *a1, BIGNUM *p1, | 516 | bn_mod_exp2_test(int reduce, BIGNUM *want, BIGNUM *got, BIGNUM *a, BIGNUM *p, |
| 517 | BIGNUM *a2, BIGNUM *p2, BIGNUM *m, BN_CTX *ctx) | 517 | BIGNUM *b, BIGNUM *q, BIGNUM *m, BN_CTX *ctx) |
| 518 | { | 518 | { |
| 519 | size_t i; | 519 | size_t i; |
| 520 | int failed = 0; | 520 | int failed = 0; |
| 521 | 521 | ||
| 522 | if (!generate_test_quintuple(reduce, a1, p1, a2, p2, m, ctx)) | 522 | if (!generate_test_quintuple(reduce, a, p, b, q, m, ctx)) |
| 523 | errx(1, "generate_test_quintuple"); | 523 | errx(1, "generate_test_quintuple"); |
| 524 | 524 | ||
| 525 | for (i = 0; i < 32 && !failed; i++) { | 525 | for (i = 0; i < 32 && !failed; i++) { |
| 526 | BN_set_negative(a1, i & 1); | 526 | BN_set_negative(a, (i >> 0) & 1); |
| 527 | BN_set_negative(p1, (i >> 1) & 1); | 527 | BN_set_negative(p, (i >> 1) & 1); |
| 528 | BN_set_negative(a2, (i >> 2) & 1); | 528 | BN_set_negative(b, (i >> 2) & 1); |
| 529 | BN_set_negative(p2, (i >> 3) & 1); | 529 | BN_set_negative(q, (i >> 3) & 1); |
| 530 | BN_set_negative(m, (i >> 4) & 1); | 530 | BN_set_negative(m, (i >> 4) & 1); |
| 531 | 531 | ||
| 532 | if (!bn_mod_exp2_simple(want, a1, p1, a2, p2, m, ctx)) | 532 | if (!bn_mod_exp2_simple(want, a, p, b, q, m, ctx)) |
| 533 | errx(1, "BN_mod_exp_simple"); | 533 | errx(1, "BN_mod_exp_simple"); |
| 534 | 534 | ||
| 535 | if (!BN_mod_exp2_mont(got, a1, p1, a2, p2, m, ctx, NULL)) | 535 | if (!BN_mod_exp2_mont(got, a, p, b, q, m, ctx, NULL)) |
| 536 | errx(1, "BN_mod_exp2_mont"); | 536 | errx(1, "BN_mod_exp2_mont"); |
| 537 | 537 | ||
| 538 | if (BN_cmp(want, got) != 0) { | 538 | if (BN_cmp(want, got) != 0) { |
| 539 | dump_exp2_results(a1, p1, a2, p2, m, want, got); | 539 | dump_exp2_results(a, p, b, q, m, want, got); |
| 540 | failed |= 1; | 540 | failed |= 1; |
| 541 | } | 541 | } |
| 542 | } | 542 | } |
| @@ -547,7 +547,7 @@ bn_mod_exp2_test(int reduce, BIGNUM *want, BIGNUM *got, BIGNUM *a1, BIGNUM *p1, | |||
| 547 | static int | 547 | static int |
| 548 | run_bn_mod_exp2_tests(void) | 548 | run_bn_mod_exp2_tests(void) |
| 549 | { | 549 | { |
| 550 | BIGNUM *a1, *p1, *a2, *p2, *m, *want, *got; | 550 | BIGNUM *a, *p, *b, *q, *m, *want, *got; |
| 551 | BN_CTX *ctx; | 551 | BN_CTX *ctx; |
| 552 | int i; | 552 | int i; |
| 553 | int reduce; | 553 | int reduce; |
| @@ -558,14 +558,14 @@ run_bn_mod_exp2_tests(void) | |||
| 558 | 558 | ||
| 559 | BN_CTX_start(ctx); | 559 | BN_CTX_start(ctx); |
| 560 | 560 | ||
| 561 | if ((a1 = BN_CTX_get(ctx)) == NULL) | 561 | if ((a = BN_CTX_get(ctx)) == NULL) |
| 562 | errx(1, "a1 = BN_CTX_get()"); | 562 | errx(1, "a = BN_CTX_get()"); |
| 563 | if ((p1 = BN_CTX_get(ctx)) == NULL) | 563 | if ((p = BN_CTX_get(ctx)) == NULL) |
| 564 | errx(1, "p1 = BN_CTX_get()"); | 564 | errx(1, "p = BN_CTX_get()"); |
| 565 | if ((a2 = BN_CTX_get(ctx)) == NULL) | 565 | if ((b = BN_CTX_get(ctx)) == NULL) |
| 566 | errx(1, "a2 = BN_CTX_get()"); | 566 | errx(1, "b = BN_CTX_get()"); |
| 567 | if ((p2 = BN_CTX_get(ctx)) == NULL) | 567 | if ((q = BN_CTX_get(ctx)) == NULL) |
| 568 | errx(1, "p2 = BN_CTX_get()"); | 568 | errx(1, "q = BN_CTX_get()"); |
| 569 | if ((m = BN_CTX_get(ctx)) == NULL) | 569 | if ((m = BN_CTX_get(ctx)) == NULL) |
| 570 | errx(1, "m = BN_CTX_get()"); | 570 | errx(1, "m = BN_CTX_get()"); |
| 571 | if ((want = BN_CTX_get(ctx)) == NULL) | 571 | if ((want = BN_CTX_get(ctx)) == NULL) |
| @@ -575,13 +575,11 @@ run_bn_mod_exp2_tests(void) | |||
| 575 | 575 | ||
| 576 | reduce = 0; | 576 | reduce = 0; |
| 577 | for (i = 0; i < N_MOD_EXP_TESTS && !failed; i++) | 577 | for (i = 0; i < N_MOD_EXP_TESTS && !failed; i++) |
| 578 | failed |= bn_mod_exp2_test(reduce, want, got, a1, p1, a2, p2, m, | 578 | failed |= bn_mod_exp2_test(reduce, want, got, a, p, b, q, m, ctx); |
| 579 | ctx); | ||
| 580 | 579 | ||
| 581 | reduce = 1; | 580 | reduce = 1; |
| 582 | for (i = 0; i < N_MOD_EXP_TESTS && !failed; i++) | 581 | for (i = 0; i < N_MOD_EXP_TESTS && !failed; i++) |
| 583 | failed |= bn_mod_exp2_test(reduce, want, got, a1, p1, a2, p2, m, | 582 | failed |= bn_mod_exp2_test(reduce, want, got, a, p, b, q, m, ctx); |
| 584 | ctx); | ||
| 585 | 583 | ||
| 586 | BN_CTX_end(ctx); | 584 | BN_CTX_end(ctx); |
| 587 | BN_CTX_free(ctx); | 585 | BN_CTX_free(ctx); |
