summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorBernd Edlinger <bernd.edlinger@hotmail.de>2019-07-04 17:56:23 +0200
committerBernd Edlinger <bernd.edlinger@hotmail.de>2019-07-07 08:05:33 +0200
commit291f616ced45c924d639d97fc9ca2cbeaad096cf (patch)
tree86fbc3f0f65398c02bbad3f975f5d36bd69c2a06 /test
parent2a1e2fe145c6eb8e75aa2e1b3a8c3a49384b2852 (diff)
Fix an endless loop in BN_generate_prime_ex
Happens when trying to generate 4 or 5 bit safe primes. [extended tests] Reviewed-by: Paul Dale <paul.dale@oracle.com> (Merged from https://github.com/openssl/openssl/pull/9311)
Diffstat (limited to 'test')
-rw-r--r--test/bntest.c47
1 files changed, 40 insertions, 7 deletions
diff --git a/test/bntest.c b/test/bntest.c
index 8df6e0f34c..1e502103c0 100644
--- a/test/bntest.c
+++ b/test/bntest.c
@@ -2249,18 +2249,50 @@ static int test_expmodone(void)
return ret;
}
-static int test_smallprime(void)
+static int test_smallprime(int kBits)
{
- static const int kBits = 10;
BIGNUM *r;
int st = 0;
- if (!TEST_ptr(r = BN_new())
- || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
- NULL, NULL, NULL))
- || !TEST_int_eq(BN_num_bits(r), kBits))
+ if (!TEST_ptr(r = BN_new()))
+ goto err;
+
+ if (kBits <= 1) {
+ if (!TEST_false(BN_generate_prime_ex(r, kBits, 0,
+ NULL, NULL, NULL)))
+ goto err;
+ } else {
+ if (!TEST_true(BN_generate_prime_ex(r, kBits, 0,
+ NULL, NULL, NULL))
+ || !TEST_int_eq(BN_num_bits(r), kBits))
+ goto err;
+ }
+
+ st = 1;
+ err:
+ BN_free(r);
+ return st;
+}
+
+static int test_smallsafeprime(int kBits)
+{
+ BIGNUM *r;
+ int st = 0;
+
+ if (!TEST_ptr(r = BN_new()))
goto err;
+ if (kBits <= 5 && kBits != 3) {
+ if (!TEST_false(BN_generate_prime_ex(r, kBits, 1,
+ NULL, NULL, NULL)))
+ goto err;
+ } else {
+ if (!TEST_true(BN_generate_prime_ex(r, kBits, 1,
+ NULL, NULL, NULL))
+ || !TEST_int_eq(BN_num_bits(r), kBits))
+ goto err;
+ }
+
st = 1;
err:
BN_free(r);
@@ -2518,7 +2550,8 @@ int setup_tests(void)
ADD_TEST(test_badmod);
ADD_TEST(test_expmodzero);
ADD_TEST(test_expmodone);
- ADD_TEST(test_smallprime);
+ ADD_ALL_TESTS(test_smallprime, 16);
+ ADD_ALL_TESTS(test_smallsafeprime, 16);
ADD_TEST(test_swap);
ADD_TEST(test_ctx_consttime_flag);
#ifndef OPENSSL_NO_EC2M