From 24882d0aebd3074ff4e57dba8113fde1c67534b0 Mon Sep 17 00:00:00 2001 From: tb Date: Thu, 19 Oct 2023 13:38:12 +0000 Subject: [PATCH] Add a few more test cases for mod_exp aliasing --- regress/lib/libcrypto/bn/bn_mod_exp.c | 111 ++++++++++++++++++-------- 1 file changed, 78 insertions(+), 33 deletions(-) diff --git a/regress/lib/libcrypto/bn/bn_mod_exp.c b/regress/lib/libcrypto/bn/bn_mod_exp.c index 61157385bfa..98b6a5d2efe 100644 --- a/regress/lib/libcrypto/bn/bn_mod_exp.c +++ b/regress/lib/libcrypto/bn/bn_mod_exp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_mod_exp.c,v 1.39 2023/10/19 10:17:24 tb Exp $ */ +/* $OpenBSD: bn_mod_exp.c,v 1.40 2023/10/19 13:38:12 tb Exp $ */ /* * Copyright (c) 2022,2023 Theo Buehler @@ -561,6 +561,51 @@ test_bn_mod_exp2_mont_crash(void) return failed; } +const struct aliasing_test_case { + BN_ULONG a; + BN_ULONG p; + BN_ULONG m; +} aliasing_test_cases[] = { + { + .a = 1031, + .p = 1033, + .m = 1039, + }, + { + .a = 3, + .p = 4, + .m = 5, + }, + { + .a = 97, + .p = 17, + .m = 11, + }, + { + .a = 999961, + .p = 999979, + .m = 999983, + }, +}; + +#define N_ALIASING_TEST_CASES \ + (sizeof(aliasing_test_cases) / sizeof(aliasing_test_cases[0])) + +static void +test_bn_mod_exp_aliasing_setup(BIGNUM *want, BIGNUM *a, BIGNUM *p, BIGNUM *m, + BN_CTX *ctx, const struct aliasing_test_case *tc) +{ + if (!BN_set_word(a, tc->a)) + errx(1, "BN_set_word"); + if (!BN_set_word(p, tc->p)) + errx(1, "BN_set_word"); + if (!BN_set_word(m, tc->m)) + errx(1, "BN_set_word"); + + if (!BN_mod_exp_simple(want, a, p, m, ctx)) + errx(1, "BN_mod_exp"); +} + static int test_mod_exp_aliased(const char *alias, int want_ret, BIGNUM *got, 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, else mod_exp_ret = test->mod_exp_mont_fn(got, a, p, m, ctx, NULL); - if (mod_exp_ret != want_ret) - errx(1, "%s() %s aliased with result failed", test->name, alias); + if (mod_exp_ret != want_ret) { + warnx("%s() %s aliased with result failed", test->name, alias); + goto err; + } if (!mod_exp_ret) goto done; @@ -596,19 +643,34 @@ test_mod_exp_aliased(const char *alias, int want_ret, BIGNUM *got, return ret; } -static void -test_bn_mod_exp_aliasing_setup(BIGNUM *want, BIGNUM *a, BIGNUM *p, BIGNUM *m, - BN_CTX *ctx) +static int +test_bn_mod_exp_aliasing_test(const struct mod_exp_test *test, + BIGNUM *a, BIGNUM *p, BIGNUM *m, BIGNUM *want, BIGNUM *got, BN_CTX *ctx) { - if (!BN_set_word(a, 1031)) - errx(1, "BN_set_word"); - if (!BN_set_word(p, 1033)) - errx(1, "BN_set_word"); - if (!BN_set_word(m, 1039)) - errx(1, "BN_set_word"); + int modulus_alias_works = test->mod_exp_fn != BN_mod_exp_simple; + size_t i; + int failed = 0; - if (!BN_mod_exp_simple(want, a, p, m, ctx)) - errx(1, "BN_mod_exp"); + for (i = 0; i < N_ALIASING_TEST_CASES; i++) { + const struct aliasing_test_case *tc = &aliasing_test_cases[i]; + + test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx, tc); + if (!test_mod_exp_aliased("nothing", 1, got, want, a, p, m, ctx, + test)) + failed |= 1; + test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx, tc); + if (!test_mod_exp_aliased("a", 1, a, want, a, p, m, ctx, test)) + failed |= 1; + test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx, tc); + if (!test_mod_exp_aliased("p", 1, p, want, a, p, m, ctx, test)) + failed |= 1; + test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx, tc); + if (!test_mod_exp_aliased("m", modulus_alias_works, m, want, + a, p, m, ctx, test)) + failed |= 1; + } + + return failed; } static int @@ -637,25 +699,8 @@ test_bn_mod_exp_aliasing(void) for (i = 0; i < N_MOD_EXP_FN; i++) { const struct mod_exp_test *test = &mod_exp_fn[i]; - int aliasing_allowed = 1; - - test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx); - if (!test_mod_exp_aliased("nothing", 1, got, want, a, p, m, ctx, - test)) - failed |= 1; - test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx); - if (!test_mod_exp_aliased("a", 1, a, want, a, p, m, ctx, test)) - failed |= 1; - test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx); - if (!test_mod_exp_aliased("p", 1, p, want, a, p, m, ctx, test)) - failed |= 1; - - if (test->mod_exp_fn == BN_mod_exp_simple) - aliasing_allowed = 0; - test_bn_mod_exp_aliasing_setup(want, a, p, m, ctx); - if (!test_mod_exp_aliased("m", aliasing_allowed, m, want, - a, p, m, ctx, test)) - failed |= 1; + failed |= test_bn_mod_exp_aliasing_test(test, a, p, m, + want, got, ctx); } BN_CTX_end(ctx); -- 2.20.1