提交 aa20a9b3 编写于 作者: C Cesar Pereida Garcia 提交者: Nicola Tuveri

Add GCD testing infrastructure.

This commit adds testing and Known Answer Tests (KATs) to OpenSSL for
the `BN_gcd` function.

(cherry picked from commit b75d6310857bc44ef2851bde68a1979c18bb4807)
Reviewed-by: NPaul Dale <paul.dale@oracle.com>
Reviewed-by: NNicola Tuveri <nic.tuv@gmail.com>
Reviewed-by: NMatt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/10122)
上级 ef599605
...@@ -1634,6 +1634,30 @@ static int file_modsqrt(STANZA *s) ...@@ -1634,6 +1634,30 @@ static int file_modsqrt(STANZA *s)
return st; return st;
} }
static int file_gcd(STANZA *s)
{
BIGNUM *a = NULL, *b = NULL, *gcd = NULL, *ret = NULL;
int st = 0;
if (!TEST_ptr(a = getBN(s, "A"))
|| !TEST_ptr(b = getBN(s, "B"))
|| !TEST_ptr(gcd = getBN(s, "GCD"))
|| !TEST_ptr(ret = BN_new()))
goto err;
if (!TEST_true(BN_gcd(ret, a, b, ctx))
|| !equalBN("gcd(A,B)", gcd, ret))
goto err;
st = 1;
err:
BN_free(a);
BN_free(b);
BN_free(gcd);
BN_free(ret);
return st;
}
static int test_bn2padded(void) static int test_bn2padded(void)
{ {
#if HAVE_BN_PADDED #if HAVE_BN_PADDED
...@@ -2352,6 +2376,34 @@ static int test_ctx_consttime_flag(void) ...@@ -2352,6 +2376,34 @@ static int test_ctx_consttime_flag(void)
return st; return st;
} }
static int test_gcd_prime(void)
{
BIGNUM *a = NULL, *b = NULL, *gcd = NULL;
int i, st = 0;
if (!TEST_ptr(a = BN_new())
|| !TEST_ptr(b = BN_new())
|| !TEST_ptr(gcd = BN_new()))
goto err;
if (!TEST_true(BN_generate_prime_ex(a, 1024, 0, NULL, NULL, NULL)))
goto err;
for (i = 0; i < NUM0; i++) {
if (!TEST_true(BN_generate_prime_ex(b, 1024, 0,
NULL, NULL, NULL))
|| !TEST_true(BN_gcd(gcd, a, b, ctx))
|| !TEST_true(BN_is_one(gcd)))
goto err;
}
st = 1;
err:
BN_free(a);
BN_free(b);
BN_free(gcd);
return st;
}
static int file_test_run(STANZA *s) static int file_test_run(STANZA *s)
{ {
static const FILETEST filetests[] = { static const FILETEST filetests[] = {
...@@ -2366,6 +2418,7 @@ static int file_test_run(STANZA *s) ...@@ -2366,6 +2418,7 @@ static int file_test_run(STANZA *s)
{"ModExp", file_modexp}, {"ModExp", file_modexp},
{"Exp", file_exp}, {"Exp", file_exp},
{"ModSqrt", file_modsqrt}, {"ModSqrt", file_modsqrt},
{"GCD", file_gcd},
}; };
int numtests = OSSL_NELEM(filetests); int numtests = OSSL_NELEM(filetests);
const FILETEST *tp = filetests; const FILETEST *tp = filetests;
...@@ -2454,6 +2507,7 @@ int setup_tests(void) ...@@ -2454,6 +2507,7 @@ int setup_tests(void)
#endif #endif
ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes)); ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes)); ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
ADD_TEST(test_gcd_prime);
} else { } else {
ADD_ALL_TESTS(run_file_tests, n); ADD_ALL_TESTS(run_file_tests, n);
} }
......
...@@ -16,9 +16,8 @@ use OpenSSL::Test qw/:DEFAULT data_file/; ...@@ -16,9 +16,8 @@ use OpenSSL::Test qw/:DEFAULT data_file/;
setup("test_bn"); setup("test_bn");
my @files = ( my @files = qw( bnexp.txt bnmod.txt bnmul.txt bnshift.txt bnsum.txt bngcd.txt );
"bnexp.txt", "bnmod.txt", "bnmul.txt", "bnshift.txt", "bnsum.txt"
);
plan tests => 1 + scalar(@files); plan tests => 1 + scalar(@files);
foreach my $f ( @files ) { foreach my $f ( @files ) {
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册