summaryrefslogtreecommitdiff
path: root/src/regress
diff options
context:
space:
mode:
authortb <>2023-10-19 13:38:12 +0000
committertb <>2023-10-19 13:38:12 +0000
commitc58d283fbdca7a7ec041efd5bd41531edab6610c (patch)
treecae40991ad1aae3e1641dcbd51fd898151b33656 /src/regress
parent9bd18bca4a0907dc6f0b5f7789806250aa984594 (diff)
downloadopenbsd-c58d283fbdca7a7ec041efd5bd41531edab6610c.tar.gz
openbsd-c58d283fbdca7a7ec041efd5bd41531edab6610c.tar.bz2
openbsd-c58d283fbdca7a7ec041efd5bd41531edab6610c.zip
Add a few more test cases for mod_exp aliasing
Diffstat (limited to 'src/regress')
-rw-r--r--src/regress/lib/libcrypto/bn/bn_mod_exp.c111
1 files changed, 78 insertions, 33 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_mod_exp.c b/src/regress/lib/libcrypto/bn/bn_mod_exp.c
index 61157385bf..98b6a5d2ef 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.39 2023/10/19 10:17:24 tb Exp $ */ 1/* $OpenBSD: bn_mod_exp.c,v 1.40 2023/10/19 13:38:12 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>
@@ -561,6 +561,51 @@ test_bn_mod_exp2_mont_crash(void)
561 return failed; 561 return failed;
562} 562}
563 563
564const struct aliasing_test_case {
565 BN_ULONG a;
566 BN_ULONG p;
567 BN_ULONG m;
568} aliasing_test_cases[] = {
569 {
570 .a = 1031,
571 .p = 1033,
572 .m = 1039,
573 },
574 {
575 .a = 3,
576 .p = 4,
577 .m = 5,
578 },
579 {
580 .a = 97,
581 .p = 17,
582 .m = 11,
583 },
584 {
585 .a = 999961,
586 .p = 999979,
587 .m = 999983,
588 },
589};
590
591#define N_ALIASING_TEST_CASES \
592 (sizeof(aliasing_test_cases) / sizeof(aliasing_test_cases[0]))
593
594static void
595test_bn_mod_exp_aliasing_setup(BIGNUM *want, BIGNUM *a, BIGNUM *p, BIGNUM *m,
596 BN_CTX *ctx, const struct aliasing_test_case *tc)
597{
598 if (!BN_set_word(a, tc->a))
599 errx(1, "BN_set_word");
600 if (!BN_set_word(p, tc->p))
601 errx(1, "BN_set_word");
602 if (!BN_set_word(m, tc->m))
603 errx(1, "BN_set_word");
604
605 if (!BN_mod_exp_simple(want, a, p, m, ctx))
606 errx(1, "BN_mod_exp");
607}
608
564static int 609static int
565test_mod_exp_aliased(const char *alias, int want_ret, BIGNUM *got, 610test_mod_exp_aliased(const char *alias, int want_ret, BIGNUM *got,
566 const BIGNUM *want, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, 611 const BIGNUM *want, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
@@ -576,8 +621,10 @@ test_mod_exp_aliased(const char *alias, int want_ret, BIGNUM *got,
576 else 621 else
577 mod_exp_ret = test->mod_exp_mont_fn(got, a, p, m, ctx, NULL); 622 mod_exp_ret = test->mod_exp_mont_fn(got, a, p, m, ctx, NULL);
578 623
579 if (mod_exp_ret != want_ret) 624 if (mod_exp_ret != want_ret) {
580 errx(1, "%s() %s aliased with result failed", test->name, alias); 625 warnx("%s() %s aliased with result failed", test->name, alias);
626 goto err;
627 }
581 628
582 if (!mod_exp_ret) 629 if (!mod_exp_ret)
583 goto done; 630 goto done;
@@ -596,19 +643,34 @@ test_mod_exp_aliased(const char *alias, int want_ret, BIGNUM *got,
596 return ret; 643 return ret;
597} 644}
598 645
599static void 646static int
600test_bn_mod_exp_aliasing_setup(BIGNUM *want, BIGNUM *a, BIGNUM *p, BIGNUM *m, 647test_bn_mod_exp_aliasing_test(const struct mod_exp_test *test,
601 BN_CTX *ctx) 648 BIGNUM *a, BIGNUM *p, BIGNUM *m, BIGNUM *want, BIGNUM *got, BN_CTX *ctx)
602{ 649{
603 if (!BN_set_word(a, 1031)) 650 int modulus_alias_works = test->mod_exp_fn != BN_mod_exp_simple;
604 errx(1, "BN_set_word"); 651 size_t i;
605 if (!BN_set_word(p, 1033)) 652 int failed = 0;
606 errx(1, "BN_set_word");
607 if (!BN_set_word(m, 1039))
608 errx(1, "BN_set_word");
609 653
610 if (!BN_mod_exp_simple(want, a, p, m, ctx)) 654 for (i = 0; i < N_ALIASING_TEST_CASES; i++) {
611 errx(1, "BN_mod_exp"); 655 const struct aliasing_test_case *tc = &aliasing_test_cases[i];
656
657 test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx, tc);
658 if (!test_mod_exp_aliased("nothing", 1, got, want, a, p, m, ctx,
659 test))
660 failed |= 1;
661 test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx, tc);
662 if (!test_mod_exp_aliased("a", 1, a, want, a, p, m, ctx, test))
663 failed |= 1;
664 test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx, tc);
665 if (!test_mod_exp_aliased("p", 1, p, want, a, p, m, ctx, test))
666 failed |= 1;
667 test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx, tc);
668 if (!test_mod_exp_aliased("m", modulus_alias_works, m, want,
669 a, p, m, ctx, test))
670 failed |= 1;
671 }
672
673 return failed;
612} 674}
613 675
614static int 676static int
@@ -637,25 +699,8 @@ test_bn_mod_exp_aliasing(void)
637 699
638 for (i = 0; i < N_MOD_EXP_FN; i++) { 700 for (i = 0; i < N_MOD_EXP_FN; i++) {
639 const struct mod_exp_test *test = &mod_exp_fn[i]; 701 const struct mod_exp_test *test = &mod_exp_fn[i];
640 int aliasing_allowed = 1; 702 failed |= test_bn_mod_exp_aliasing_test(test, a, p, m,
641 703 want, got, ctx);
642 test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx);
643 if (!test_mod_exp_aliased("nothing", 1, got, want, a, p, m, ctx,
644 test))
645 failed |= 1;
646 test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx);
647 if (!test_mod_exp_aliased("a", 1, a, want, a, p, m, ctx, test))
648 failed |= 1;
649 test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx);
650 if (!test_mod_exp_aliased("p", 1, p, want, a, p, m, ctx, test))
651 failed |= 1;
652
653 if (test->mod_exp_fn == BN_mod_exp_simple)
654 aliasing_allowed = 0;
655 test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx);
656 if (!test_mod_exp_aliased("m", aliasing_allowed, m, want,
657 a, p, m, ctx, test))
658 failed |= 1;
659 } 704 }
660 705
661 BN_CTX_end(ctx); 706 BN_CTX_end(ctx);