[openssl-commits] [openssl] master update

Richard Levitte levitte at openssl.org
Tue May 9 19:31:02 UTC 2017


The branch master has been updated
       via  03d8e9cb43da5c524e5890a5a51e2c77f1fbd789 (commit)
       via  dc352c193755525292310c8992e3c9b81a556a31 (commit)
      from  5e3766e2f15b3a8ea696b194c32a141cbe668d4e (commit)


- Log -----------------------------------------------------------------
commit 03d8e9cb43da5c524e5890a5a51e2c77f1fbd789
Author: Pauli <paul.dale at oracle.com>
Date:   Tue May 9 07:58:55 2017 +1000

    Add test_test tests for bignums.
    Add relative tests for bignums.
    
    Reviewed-by: Rich Salz <rsalz at openssl.org>
    Reviewed-by: Richard Levitte <levitte at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/3405)

commit dc352c193755525292310c8992e3c9b81a556a31
Author: Pauli <paul.dale at oracle.com>
Date:   Mon May 8 12:09:41 2017 +1000

    Add BN support to the test infrastructure.
    
    This includes support for:
    
    - comparisions between pairs of BIGNUMs
    - comparisions between BIGNUMs and zero
    - equality comparison between BIGNUMs and one
    - equality comparisons between BIGNUMs and constants
    - parity checks for BIGNUMs
    
    Reviewed-by: Rich Salz <rsalz at openssl.org>
    Reviewed-by: Richard Levitte <levitte at openssl.org>
    (Merged from https://github.com/openssl/openssl/pull/3405)

-----------------------------------------------------------------------

Summary of changes:
 test/bntest.c         | 219 +++++++++++++++++++++++++++++---------------------
 test/ecdsatest.c      |   4 +-
 test/ectest.c         |  18 ++---
 test/exptest.c        |  14 ++--
 test/srptest.c        |  19 ++---
 test/test_test.c      |  70 ++++++++++++++++
 test/testutil.h       |  40 +++++++++
 test/testutil/tests.c | 130 ++++++++++++++++++++++++++++++
 8 files changed, 395 insertions(+), 119 deletions(-)

diff --git a/test/bntest.c b/test/bntest.c
index 97ad97a..f3669f1 100644
--- a/test/bntest.c
+++ b/test/bntest.c
@@ -142,28 +142,11 @@ err:
 
 static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
 {
-    char *exstr = NULL;
-    char *actstr = NULL;
-
     if (BN_cmp(expected, actual) == 0)
         return 1;
 
-    if (BN_is_zero(expected) && BN_is_negative(expected))
-        exstr = OPENSSL_strdup("-0");
-    else
-        exstr = BN_bn2hex(expected);
-    if (BN_is_zero(actual) && BN_is_negative(actual))
-        actstr = OPENSSL_strdup("-0");
-    else
-        actstr = BN_bn2hex(actual);
-    if (!TEST_ptr(exstr) || !TEST_ptr(actstr))
-        goto err;
-
-    TEST_error("Got %s =\n\t%s\nwanted:\n\t%s", op, actstr, exstr);
-
-err:
-    OPENSSL_free(exstr);
-    OPENSSL_free(actstr);
+    TEST_error("unexpected %s value", op);
+    TEST_BN_eq(expected, actual);
     return 0;
 }
 
@@ -205,7 +188,7 @@ static int test_sub()
         BN_sub(c, a, b);
         BN_add(c, c, b);
         BN_sub(c, c, a);
-        if (!TEST_true(BN_is_zero(c)))
+        if (!TEST_BN_eq_zero(c))
             goto err;
     }
     st = 1;
@@ -246,7 +229,7 @@ static int test_div_recip()
         BN_mul(e, d, b, ctx);
         BN_add(d, e, c);
         BN_sub(d, d, a);
-        if (!TEST_true(BN_is_zero(d)))
+        if (!TEST_BN_eq_zero(d))
             goto err;
     }
     st = 1;
@@ -281,7 +264,7 @@ static int test_mod()
         BN_mod(c, a, b, ctx);
         BN_div(d, e, a, b, ctx);
         BN_sub(e, e, c);
-        if (!TEST_true(BN_is_zero(e)))
+        if (!TEST_BN_eq_zero(e))
             goto err;
     }
     st = 1;
@@ -378,7 +361,7 @@ static int test_modexp_mont5()
     BN_zero(p);
     if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
         goto err;
-    if (!TEST_true(BN_is_one(d)))
+    if (!TEST_BN_eq_one(d))
         goto err;
 
     /* Regression test for carry bug in mulx4x_mont */
@@ -400,7 +383,7 @@ static int test_modexp_mont5()
     BN_MONT_CTX_set(mont, n, ctx);
     BN_mod_mul_montgomery(c, a, b, mont, ctx);
     BN_mod_mul_montgomery(d, b, a, mont, ctx);
-    if (!TEST_int_eq(BN_cmp(c, d), 0))
+    if (!TEST_BN_eq(c, d))
         goto err;
 
     /* Regression test for carry bug in sqr[x]8x_mont */
@@ -415,14 +398,14 @@ static int test_modexp_mont5()
     BN_MONT_CTX_set(mont, n, ctx);
     BN_mod_mul_montgomery(c, a, a, mont, ctx);
     BN_mod_mul_montgomery(d, a, b, mont, ctx);
-    if (!TEST_int_eq(BN_cmp(c, d), 0))
+    if (!TEST_BN_eq(c, d))
         goto err;
 
     /* Zero input */
     BN_bntest_rand(p, 1024, 0, 0);
     BN_zero(a);
     if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
-            || !TEST_true(BN_is_zero(d)))
+            || !TEST_BN_eq_zero(d))
         goto err;
 
     /*
@@ -435,14 +418,14 @@ static int test_modexp_mont5()
     if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
             || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
             || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
-            || !TEST_int_eq(BN_cmp(a, d), 0))
+            || !TEST_BN_eq(a, d))
         goto err;
 
     /* Finally, some regular test vectors. */
     BN_bntest_rand(e, 1024, 0, 0);
     if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
             || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
-            || !TEST_int_eq(BN_cmp(a, d), 0))
+            || !TEST_BN_eq(a, d))
         goto err;
 
     st = 1;
@@ -483,7 +466,7 @@ static int test_gf2m_add()
             goto err;
         BN_GF2m_add(c, c, c);
         /* Test that c + c = 0. */
-        if (!TEST_true(BN_is_zero(c)))
+        if (!TEST_BN_eq_zero(c))
             goto err;
     }
     st = 1;
@@ -517,7 +500,7 @@ static int test_gf2m_mod()
             BN_GF2m_add(d, a, c);
             BN_GF2m_mod(e, d, b[j]);
             /* Test that a + (a mod p) mod p == 0. */
-            if (!TEST_true(BN_is_zero(e)))
+            if (!TEST_BN_eq_zero(e))
                 goto err;
         }
     }
@@ -564,7 +547,7 @@ static int test_gf2m_mul()
             BN_GF2m_add(f, e, g);
             BN_GF2m_add(f, f, h);
             /* Test that (a+d)*c = a*c + d*c. */
-            if (!TEST_true(BN_is_zero(f)))
+            if (!TEST_BN_eq_zero(f))
                 goto err;
         }
     }
@@ -606,7 +589,7 @@ static int test_gf2m_sqr()
             BN_GF2m_mod_mul(d, a, d, b[j], ctx);
             BN_GF2m_add(d, c, d);
             /* Test that a*a = a^2. */
-            if (!TEST_true(BN_is_zero(d)))
+            if (!TEST_BN_eq_zero(d))
                 goto err;
         }
     }
@@ -641,7 +624,7 @@ static int test_gf2m_modinv()
             BN_GF2m_mod_inv(c, a, b[j], ctx);
             BN_GF2m_mod_mul(d, a, c, b[j], ctx);
             /* Test that ((1/a)*a) = 1. */
-            if (!TEST_true(BN_is_one(d)))
+            if (!TEST_BN_eq_one(d))
                 goto err;
         }
     }
@@ -681,7 +664,7 @@ static int test_gf2m_moddiv()
             BN_GF2m_mod_mul(e, d, c, b[j], ctx);
             BN_GF2m_mod_div(f, a, e, b[j], ctx);
             /* Test that ((a/c)*c)/a = 1. */
-            if (!TEST_true(BN_is_one(f)))
+            if (!TEST_BN_eq_one(f))
                 goto err;
         }
     }
@@ -727,7 +710,7 @@ static int test_gf2m_modexp()
             BN_GF2m_mod_exp(f, a, f, b[j], ctx);
             BN_GF2m_add(f, e, f);
             /* Test that a^(c+d)=a^c*a^d. */
-            if (!TEST_true(BN_is_zero(f)))
+            if (!TEST_BN_eq_zero(f))
                 goto err;
         }
     }
@@ -769,7 +752,7 @@ static int test_gf2m_modsqrt()
             BN_GF2m_mod_sqr(e, d, b[j], ctx);
             BN_GF2m_add(f, c, e);
             /* Test that d^2 = a, where d = sqrt(a). */
-            if (!TEST_true(BN_is_zero(f)))
+            if (!TEST_BN_eq_zero(f))
                 goto err;
         }
     }
@@ -815,7 +798,7 @@ static int test_gf2m_modsolvequad()
                 /*
                  * Test that solution of quadratic c satisfies c^2 + c = a.
                  */
-                if (!TEST_true(BN_is_zero(e)))
+                if (!TEST_BN_eq_zero(e))
                     goto err;
             }
         }
@@ -1158,8 +1141,9 @@ static int file_square(STANZA *s)
         goto err;
 
     /* BN_sqrt should fail on non-squares and negative numbers. */
-    if (!TEST_true(BN_is_zero(square))) {
-        if (!TEST_ptr(tmp = BN_new()) || !TEST_true(BN_copy(tmp, square)))
+    if (!TEST_BN_eq_zero(square)) {
+        if (!TEST_ptr(tmp = BN_new())
+                || !TEST_true(BN_copy(tmp, square)))
             goto err;
         BN_set_negative(tmp, 1);
 
@@ -1399,7 +1383,7 @@ static int file_modexp(STANZA *s)
         "0000000000000000000000000000000000000000000000000000000001");
     BN_mod_exp(d, a, b, c, ctx);
     BN_mul(e, a, a, ctx);
-    if (!TEST_int_eq(BN_cmp(d, e), 0))
+    if (!TEST_BN_eq(d, e))
         goto err;
 
     st = 1;
@@ -1538,32 +1522,61 @@ static int test_dec2bn()
     int st = 0;
 
     if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
-            || !TEST_true(BN_is_zero(bn))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_word(bn, 0)
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
     BN_free(bn);
+    bn = NULL;
 
     if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
-            || !TEST_true(BN_is_word(bn, 256))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_word(bn, 256)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
     BN_free(bn);
+    bn = NULL;
 
     if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
-            || !TEST_true(BN_abs_is_word(bn, 42))
-            || !TEST_true(BN_is_negative(bn)))
+            || !TEST_BN_abs_eq_word(bn, 42)
+            || !TEST_BN_lt_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
     BN_free(bn);
+    bn = NULL;
+
+    if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
+            || !TEST_BN_eq_word(bn, 1)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_eq_one(bn)
+            || !TEST_BN_odd(bn))
+        goto err;
+    BN_free(bn);
+    bn = NULL;
 
     if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
-            || !TEST_true(BN_is_zero(bn))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
     BN_free(bn);
+    bn = NULL;
 
     if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
-            || !TEST_true(BN_abs_is_word(bn, 42))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_abs_eq_word(bn, 42)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
 
     st = 1;
@@ -1578,32 +1591,58 @@ static int test_hex2bn()
     int st = 0;
 
     if (!TEST_int_eq(parseBN(&bn, "0"), 1)
-            || !TEST_true(BN_is_zero(bn))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
     BN_free(bn);
+    bn = NULL;
 
     if (!TEST_int_eq(parseBN(&bn, "256"), 3)
-            || !TEST_true(BN_is_word(bn, 0x256))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_word(bn, 0x256)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
     BN_free(bn);
+    bn = NULL;
 
     if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
-            || !TEST_true(BN_abs_is_word(bn, 0x42))
-            || !TEST_true(BN_is_negative(bn)))
+            || !TEST_BN_abs_eq_word(bn, 0x42)
+            || !TEST_BN_lt_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
+        goto err;
+    BN_free(bn);
+    bn = NULL;
+
+    if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
+            || !TEST_BN_eq_word(bn, 0xCB)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_odd(bn))
         goto err;
     BN_free(bn);
+    bn = NULL;
 
     if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
-            || !TEST_true(BN_is_zero(bn))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_le_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
     BN_free(bn);
+    bn = NULL;
 
     if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
-            || !TEST_true(BN_is_word(bn, 0xabc))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_word(bn, 0xabc)
+            || !TEST_BN_ge_zero(bn)
+            || !TEST_BN_gt_zero(bn)
+            || !TEST_BN_ne_zero(bn)
+            || !TEST_BN_even(bn))
         goto err;
     st = 1;
 
@@ -1621,43 +1660,43 @@ static int test_asc2bn()
         goto err;
 
     if (!TEST_true(BN_asc2bn(&bn, "0"))
-            || !TEST_true(BN_is_zero(bn))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn))
         goto err;
 
     if (!TEST_true(BN_asc2bn(&bn, "256"))
-            || !TEST_true(BN_is_word(bn, 256))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_word(bn, 256)
+            || !TEST_BN_ge_zero(bn))
         goto err;
 
     if (!TEST_true(BN_asc2bn(&bn, "-42"))
-            || !TEST_true(BN_abs_is_word(bn, 42))
-            || !TEST_true(BN_is_negative(bn)))
+            || !TEST_BN_abs_eq_word(bn, 42)
+            || !TEST_BN_lt_zero(bn))
         goto err;
 
     if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
-            || !TEST_true(BN_is_word(bn, 0x1234))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_word(bn, 0x1234)
+            || !TEST_BN_ge_zero(bn))
         goto err;
 
     if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
-            || !TEST_true(BN_is_word(bn, 0x1234))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_word(bn, 0x1234)
+            || !TEST_BN_ge_zero(bn))
         goto err;
 
     if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
-            || !TEST_true(BN_abs_is_word(bn, 0xabcd))
-            || !TEST_true(BN_is_negative(bn)))
+            || !TEST_BN_abs_eq_word(bn, 0xabcd)
+            || !TEST_BN_lt_zero(bn))
         goto err;
 
     if (!TEST_true(BN_asc2bn(&bn, "-0"))
-            || !TEST_true(BN_is_zero(bn))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_zero(bn)
+            || !TEST_BN_ge_zero(bn))
         goto err;
 
     if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
-            || !TEST_true(BN_is_word(bn, 123))
-            || !TEST_false(BN_is_negative(bn)))
+            || !TEST_BN_eq_word(bn, 123)
+            || !TEST_BN_ge_zero(bn))
         goto err;
 
     st = 1;
@@ -1698,7 +1737,7 @@ static int test_mpi(int i)
     if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
         goto err;
 
-    if (!TEST_int_eq(BN_cmp(bn, bn2), 0)) {
+    if (!TEST_BN_eq(bn, bn2)) {
         BN_free(bn2);
         goto err;
     }
@@ -1722,12 +1761,12 @@ static int test_rand()
     if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
             || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
             || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
-            || !TEST_true(BN_is_word(bn, 1))
+            || !TEST_BN_eq_one(bn)
             || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
             || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
-            || !TEST_true(BN_is_word(bn, 1))
+            || !TEST_BN_eq_one(bn)
             || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
-            || !TEST_true(BN_is_word(bn, 3)))
+            || !TEST_BN_eq_word(bn, 3))
         goto err;
 
     st = 1;
@@ -1755,8 +1794,8 @@ static int test_negzero()
     BN_zero(b);
     if (!TEST_true(BN_mul(c, a, b, ctx)))
         goto err;
-    if (!TEST_true(BN_is_zero(c))
-            || !TEST_false(BN_is_negative(c)))
+    if (!TEST_BN_eq_zero(c)
+            || !TEST_BN_ge_zero(c))
         goto err;
 
     for (consttime = 0; consttime < 2; consttime++) {
@@ -1773,15 +1812,15 @@ static int test_negzero()
             goto err;
         BN_set_negative(numerator, 1);
         if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
-                || !TEST_true(BN_is_zero(a))
-                || !TEST_false(BN_is_negative(a)))
+                || !TEST_BN_eq_zero(a)
+                || !TEST_BN_ge_zero(a))
             goto err;
 
         /* Test that BN_div never gives negative zero in the remainder. */
         if (!TEST_true(BN_set_word(denominator, 1))
                 || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
-                || !TEST_true(BN_is_zero(b))
-                || !TEST_false(BN_is_negative(b)))
+                || !TEST_BN_eq_zero(b)
+                || !TEST_BN_ge_zero(b))
             goto err;
         BN_free(numerator);
         BN_free(denominator);
@@ -1883,17 +1922,17 @@ static int test_expmodzero()
     BN_zero(zero);
 
     if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
-            || !TEST_true(BN_is_zero(r))
+            || !TEST_BN_eq_zero(r)
             || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
                                           NULL, NULL))
-            || !TEST_true(BN_is_zero(r))
+            || !TEST_BN_eq_zero(r)
             || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
                                                     BN_value_one(),
                                                     NULL, NULL))
-            || !TEST_true(BN_is_zero(r))
+            || !TEST_BN_eq_zero(r)
             || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
                                                BN_value_one(), NULL, NULL))
-            || !TEST_true(BN_is_zero(r)))
+            || !TEST_BN_eq_zero(r))
         goto err;
 
     st = 1;
diff --git a/test/ecdsatest.c b/test/ecdsatest.c
index 561ff0c..a323481 100644
--- a/test/ecdsatest.c
+++ b/test/ecdsatest.c
@@ -167,8 +167,8 @@ static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)
     if (!TEST_true(BN_dec2bn(&r, r_in)) || !TEST_true(BN_dec2bn(&s, s_in)))
         goto x962_int_err;
     ECDSA_SIG_get0(signature, &sig_r, &sig_s);
-    if (!TEST_int_eq(BN_cmp(sig_r, r), 0)
-            || !TEST_int_eq(BN_cmp(sig_s, s), 0))
+    if (!TEST_BN_eq(sig_r, r)
+            || !TEST_BN_eq(sig_s, s))
         goto x962_int_err;
 
     /* verify the signature */
diff --git a/test/ectest.c b/test/ectest.c
index c057bd9..8e311e5 100644
--- a/test/ectest.c
+++ b/test/ectest.c
@@ -345,7 +345,7 @@ static int prime_field_tests(void)
     /* G_y value taken from the standard: */
     if (!TEST_true(BN_hex2bn(&z,                         "23a62855"
                                  "3168947d59dcc912042351377ac5fb32"))
-        || !TEST_int_eq(0, BN_cmp(y, z))
+        || !TEST_BN_eq(y, z)
         || !TEST_int_eq(EC_GROUP_get_degree(group), 160)
         || !group_order_tests(group)
         || !TEST_ptr(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))
@@ -380,7 +380,7 @@ static int prime_field_tests(void)
     /* G_y value taken from the standard: */
     if (!TEST_true(BN_hex2bn(&z,                 "07192B95FFC8DA78"
                                  "631011ED6B24CDD573F977A11E794811"))
-        || !TEST_int_eq(0, BN_cmp(y, z))
+        || !TEST_BN_eq(y, z)
         || !TEST_true(BN_add(yplusone, y, BN_value_one()))
     /*
      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
@@ -422,7 +422,7 @@ static int prime_field_tests(void)
     /* G_y value taken from the standard: */
     if (!TEST_true(BN_hex2bn(&z,         "BD376388B5F723FB4C22DFE6"
                                  "CD4375A05A07476444D5819985007E34"))
-        || !TEST_int_eq(0, BN_cmp(y, z))
+        || !TEST_BN_eq(y, z)
         || !TEST_true(BN_add(yplusone, y, BN_value_one()))
     /*
      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
@@ -465,7 +465,7 @@ static int prime_field_tests(void)
     /* G_y value taken from the standard: */
     if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
                                  "2BCE33576B315ECECBB6406837BF51F5"))
-        || !TEST_int_eq(0, BN_cmp(y, z))
+        || !TEST_BN_eq(y, z)
         || !TEST_true(BN_add(yplusone, y, BN_value_one()))
     /*
      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
@@ -514,7 +514,7 @@ static int prime_field_tests(void)
     if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
                                  "F8F41DBD289A147CE9DA3113B5F0B8C0"
                                  "0A60B1CE1D7E819D7A431D7C90EA0E5F"))
-        || !TEST_int_eq(0, BN_cmp(y, z))
+        || !TEST_BN_eq(y, z)
         || !TEST_true(BN_add(yplusone, y, BN_value_one()))
     /*
      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
@@ -573,7 +573,7 @@ static int prime_field_tests(void)
                                  "98F54449579B446817AFBD17273E662C"
                                  "97EE72995EF42640C550B9013FAD0761"
                                  "353C7086A272C24088BE94769FD16650"))
-        || !TEST_int_eq(0, BN_cmp(y, z))
+        || !TEST_BN_eq(y, z)
         || !TEST_true(BN_add(yplusone, y, BN_value_one()))
     /*
      * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
@@ -607,7 +607,7 @@ static int prime_field_tests(void)
 
     if (!TEST_true(EC_GROUP_get_order(group, z, ctx))
         || !TEST_true(BN_add(y, z, BN_value_one()))
-        || !TEST_false(BN_is_odd(y))
+        || !TEST_BN_even(y)
         || !TEST_true(BN_rshift1(y, y)))
         goto err;
     scalars[0] = y;         /* (group order + 1)/2, so y*Q + y*Q = Q */
@@ -905,7 +905,7 @@ static int char2_curve_test(int n)
     BIO_printf(bio_out, "\n");
     /* G_y value taken from the standard: */
     if (!TEST_true(BN_hex2bn(&z, test->y))
-        || !TEST_int_eq(0, BN_cmp(y, z)))
+        || !TEST_BN_eq(y, z))
         goto err;
 # else
     /*
@@ -953,7 +953,7 @@ static int char2_curve_test(int n)
         points[2] = Q;
 
         if (!TEST_true(BN_add(y, z, BN_value_one()))
-            || !TEST_false(BN_is_odd(y))
+            || !TEST_BN_even(y)
             || !TEST_true(BN_rshift1(y, y)))
             goto err;
         scalars[0] = y;         /* (group order + 1)/2, so y*Q + y*Q = Q */
diff --git a/test/exptest.c b/test/exptest.c
index e15c051..ee1bff1 100644
--- a/test/exptest.c
+++ b/test/exptest.c
@@ -109,7 +109,7 @@ static int test_mod_exp_zero()
     if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))
         goto err;
 
-    if (!TEST_true(BN_is_zero(r))) {
+    if (!TEST_BN_eq_zero(r)) {
         fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
         fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
         BN_print_var(r);
@@ -173,15 +173,15 @@ static int test_mod_exp(int round)
         || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))
         goto err;
 
-    if (!TEST_int_eq(BN_cmp(r_simple, r_mont), 0)
-        || !TEST_int_eq(BN_cmp(r_simple, r_recp), 0)
-        || !TEST_int_eq(BN_cmp(r_simple, r_mont_const), 0)) {
+    if (!TEST_BN_eq(r_simple, r_mont)
+        || !TEST_BN_eq(r_simple, r_recp)
+        || !TEST_BN_eq(r_simple, r_mont_const)) {
         if (BN_cmp(r_simple, r_mont) != 0)
-            fprintf(stderr, "simple and mont results differ\n");
+            TEST_info("simple and mont results differ");
         if (BN_cmp(r_simple, r_mont_const) != 0)
-            fprintf(stderr, "simple and mont const time results differ\n");
+            TEST_info("simple and mont const time results differ");
         if (BN_cmp(r_simple, r_recp) != 0)
-            fprintf(stderr, "simple and recp results differ\n");
+            TEST_info("simple and recp results differ");
 
         BN_print_var(a);
         BN_print_var(b);
diff --git a/test/srptest.c b/test/srptest.c
index d28c3bc..8e027b9 100644
--- a/test/srptest.c
+++ b/test/srptest.c
@@ -70,7 +70,7 @@ static int run_srp(const char *username, const char *client_pass,
     /* Server random */
     RAND_bytes(rand_tmp, sizeof(rand_tmp));
     b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
-    if (!TEST_false(BN_is_zero(b)))
+    if (!TEST_BN_ne_zero(b))
         goto end;
     showbn("b", b);
 
@@ -84,7 +84,7 @@ static int run_srp(const char *username, const char *client_pass,
     /* Client random */
     RAND_bytes(rand_tmp, sizeof(rand_tmp));
     a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
-    if (!TEST_false(BN_is_zero(a)))
+    if (!TEST_BN_ne_zero(a))
         goto end;
     showbn("a", a);
 
@@ -107,7 +107,7 @@ static int run_srp(const char *username, const char *client_pass,
     Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
     showbn("Server's key", Kserver);
 
-    if (!TEST_int_eq(BN_cmp(Kclient, Kserver), 0))
+    if (!TEST_BN_eq(Kclient, Kserver))
         goto end;
 
     ret = 1;
@@ -130,19 +130,16 @@ end:
 static int check_bn(const char *name, const BIGNUM *bn, const char *hexbn)
 {
     BIGNUM *tmp = NULL;
+    int r;
 
     if (!TEST_true(BN_hex2bn(&tmp, hexbn)))
         return 0;
 
-    if (!TEST_int_eq(BN_cmp(bn, tmp), 0)) {
-        TEST_info("Unexpected %s value", name);
-        showbn("expecting", tmp);
-        showbn("received", bn);
-        BN_free(tmp);
-        return 0;
-    }
+    if (BN_cmp(bn, tmp) != 0)
+        TEST_error("unexpected %s value", name);
+    r = TEST_BN_eq(bn, tmp);
     BN_free(tmp);
-    return 1;
+    return r;
 }
 
 /* SRP test vectors from RFC5054 */
diff --git a/test/test_test.c b/test/test_test.c
index 7d3af2f..0094c1c 100644
--- a/test/test_test.c
+++ b/test/test_test.c
@@ -17,6 +17,7 @@
 #include <openssl/opensslconf.h>
 #include <openssl/err.h>
 #include <openssl/crypto.h>
+#include <openssl/bn.h>
 
 #include "e_os.h"
 #include "testutil.h"
@@ -287,6 +288,74 @@ static int test_memory_overflow(void)
     return TEST(0, TEST_mem_eq(p, strlen(p), q, strlen(q)));
 }
 
+static int test_bignum(void)
+{
+    BIGNUM *a = NULL, *b = NULL, *c = NULL;
+    int r = 0;
+
+    if (!TEST(1, TEST_int_eq(BN_dec2bn(&a, "0"), 1))
+        | !TEST(1, TEST_BN_eq_word(a, 0))
+        | !TEST(0, TEST_BN_eq_word(a, 30))
+        | !TEST(1, TEST_BN_abs_eq_word(a, 0))
+        | !TEST(0, TEST_BN_eq_one(a))
+        | !TEST(1, TEST_BN_eq_zero(a))
+        | !TEST(0, TEST_BN_ne_zero(a))
+        | !TEST(1, TEST_BN_le_zero(a))
+        | !TEST(0, TEST_BN_lt_zero(a))
+        | !TEST(1, TEST_BN_ge_zero(a))
+        | !TEST(0, TEST_BN_gt_zero(a))
+        | !TEST(1, TEST_BN_even(a))
+        | !TEST(0, TEST_BN_odd(a))
+        | !TEST(1, TEST_int_eq(BN_dec2bn(&b, "1"), 1))
+        | !TEST(1, TEST_BN_eq_word(b, 1))
+        | !TEST(1, TEST_BN_eq_one(b))
+        | !TEST(0, TEST_BN_abs_eq_word(b, 0))
+        | !TEST(1, TEST_BN_abs_eq_word(b, 1))
+        | !TEST(0, TEST_BN_eq_zero(b))
+        | !TEST(1, TEST_BN_ne_zero(b))
+        | !TEST(0, TEST_BN_le_zero(b))
+        | !TEST(0, TEST_BN_lt_zero(b))
+        | !TEST(1, TEST_BN_ge_zero(b))
+        | !TEST(1, TEST_BN_gt_zero(b))
+        | !TEST(0, TEST_BN_even(b))
+        | !TEST(1, TEST_BN_odd(b))
+        | !TEST(1, TEST_int_eq(BN_dec2bn(&c, "-334739439"), 10))
+        | !TEST(0, TEST_BN_eq_word(c, 334739439))
+        | !TEST(1, TEST_BN_abs_eq_word(c, 334739439))
+        | !TEST(0, TEST_BN_eq_zero(c))
+        | !TEST(1, TEST_BN_ne_zero(c))
+        | !TEST(1, TEST_BN_le_zero(c))
+        | !TEST(1, TEST_BN_lt_zero(c))
+        | !TEST(0, TEST_BN_ge_zero(c))
+        | !TEST(0, TEST_BN_gt_zero(c))
+        | !TEST(0, TEST_BN_even(c))
+        | !TEST(1, TEST_BN_odd(c))
+        | !TEST(1, TEST_BN_eq(a, a))
+        | !TEST(0, TEST_BN_ne(a, a))
+        | !TEST(0, TEST_BN_eq(a, b))
+        | !TEST(1, TEST_BN_ne(a, b))
+        | !TEST(0, TEST_BN_lt(a, c))
+        | !TEST(1, TEST_BN_lt(c, b))
+        | !TEST(0, TEST_BN_lt(b, c))
+        | !TEST(0, TEST_BN_le(a, c))
+        | !TEST(1, TEST_BN_le(c, b))
+        | !TEST(0, TEST_BN_le(b, c))
+        | !TEST(1, TEST_BN_gt(a, c))
+        | !TEST(0, TEST_BN_gt(c, b))
+        | !TEST(1, TEST_BN_gt(b, c))
+        | !TEST(1, TEST_BN_ge(a, c))
+        | !TEST(0, TEST_BN_ge(c, b))
+        | !TEST(1, TEST_BN_ge(b, c)))
+        goto err;
+
+    r = 1;
+err:
+    BN_free(a);
+    BN_free(b);
+    BN_free(c);
+    return r;
+}
+
 static int test_long_output(void)
 {
     const char *p = "1234567890123456789012345678901234567890123456789012";
@@ -381,6 +450,7 @@ void register_tests(void)
     ADD_TEST(test_string);
     ADD_TEST(test_memory);
     ADD_TEST(test_memory_overflow);
+    ADD_TEST(test_bignum);
     ADD_TEST(test_long_output);
     ADD_TEST(test_messages);
     ADD_TEST(test_single_eval);
diff --git a/test/testutil.h b/test/testutil.h
index f1c1bba..8e4481e 100644
--- a/test/testutil.h
+++ b/test/testutil.h
@@ -14,6 +14,7 @@
 
 #include <openssl/err.h>
 #include <openssl/e_os2.h>
+#include <openssl/bn.h>
 
 /*-
  * Simple unit tests should implement register_tests().
@@ -239,6 +240,27 @@ int test_true(const char *file, int line, const char *s, int b);
 int test_false(const char *file, int line, const char *s, int b);
 
 /*
+ * Comparisons between BIGNUMs.
+ * BIGNUMS can be compared against other BIGNUMs or zero.
+ * Some additional equality tests against 1 & specific values are provided.
+ * Tests for parity are included as well.
+ */
+DECLARE_COMPARISONS(BIGNUM *, BN)
+int test_BN_eq_zero(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_ne_zero(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_lt_zero(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_le_zero(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_gt_zero(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_ge_zero(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a);
+int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
+                    const BIGNUM *a, BN_ULONG w);
+int test_BN_abs_eq_word(const char *file, int line, const char *bns,
+                        const char *ws, const BIGNUM *a, BN_ULONG w);
+
+/*
  * Pretty print a failure message.
  * These should not be called directly, use the TEST_xxx macros below instead.
  */
@@ -332,6 +354,24 @@ void test_openssl_errors(void);
 # define TEST_true(a)         test_true(__FILE__, __LINE__, #a, (a) != 0)
 # define TEST_false(a)        test_false(__FILE__, __LINE__, #a, (a) != 0)
 
+# define TEST_BN_eq(a, b)     test_BN_eq(__FILE__, __LINE__, #a, #b, a, b)
+# define TEST_BN_ne(a, b)     test_BN_ne(__FILE__, __LINE__, #a, #b, a, b)
+# define TEST_BN_lt(a, b)     test_BN_lt(__FILE__, __LINE__, #a, #b, a, b)
+# define TEST_BN_gt(a, b)     test_BN_gt(__FILE__, __LINE__, #a, #b, a, b)
+# define TEST_BN_le(a, b)     test_BN_le(__FILE__, __LINE__, #a, #b, a, b)
+# define TEST_BN_ge(a, b)     test_BN_ge(__FILE__, __LINE__, #a, #b, a, b)
+# define TEST_BN_eq_zero(a)   test_BN_eq_zero(__FILE__, __LINE__, #a, a)
+# define TEST_BN_ne_zero(a)   test_BN_ne_zero(__FILE__, __LINE__, #a, a)
+# define TEST_BN_lt_zero(a)   test_BN_lt_zero(__FILE__, __LINE__, #a, a)
+# define TEST_BN_gt_zero(a)   test_BN_gt_zero(__FILE__, __LINE__, #a, a)
+# define TEST_BN_le_zero(a)   test_BN_le_zero(__FILE__, __LINE__, #a, a)
+# define TEST_BN_ge_zero(a)   test_BN_ge_zero(__FILE__, __LINE__, #a, a)
+# define TEST_BN_eq_one(a)    test_BN_eq_one(__FILE__, __LINE__, #a, a)
+# define TEST_BN_eq_word(a, w) test_BN_eq_word(__FILE__, __LINE__, #a, #w, a, w)
+# define TEST_BN_abs_eq_word(a, w) test_BN_abs_eq_word(__FILE__, __LINE__, #a, #w, a, w)
+# define TEST_BN_odd(a)       test_BN_odd(__FILE__, __LINE__, #a, a)
+# define TEST_BN_even(a)      test_BN_even(__FILE__, __LINE__, #a, a)
+
 /*
  * TEST_error(desc, ...) prints an informative error message in the standard
  * format.  |desc| is a printf format string.
diff --git a/test/testutil/tests.c b/test/testutil/tests.c
index 726d1a2..7816df3 100644
--- a/test/testutil/tests.c
+++ b/test/testutil/tests.c
@@ -19,6 +19,9 @@
 #define MEM_BUFFER_SIZE     (33)
 #define MAX_STRING_WIDTH    (80)
 
+/* Special representation of -0 */
+static char BN_minus_zero[] = "-0";
+
 /* Output a failed test first line */
 static void test_fail_message_prefix(const char *prefix, const char *file,
                                      int line, const char *type,
@@ -170,6 +173,47 @@ fin:
     test_flush_stderr();
 }
 
+static char *convertBN(const BIGNUM *b)
+{
+    if (b == NULL)
+        return NULL;
+    if (BN_is_zero(b) && BN_is_negative(b))
+        return BN_minus_zero;
+    return BN_bn2hex(b);
+}
+
+static void test_fail_bignum_message(const char *prefix, const char *file,
+                                     int line, const char *type,
+                                     const char *left, const char *right,
+                                     const char *op,
+                                     const BIGNUM *bn1, const BIGNUM *bn2)
+{
+    char *s1 = convertBN(bn1), *s2 = convertBN(bn2);
+    size_t l1 = s1 != NULL ? strlen(s1) : 0;
+    size_t l2 = s2 != NULL ? strlen(s2) : 0;
+
+    test_fail_string_message(prefix, file, line, type, left, right, op,
+                             s1, l1, s2, l2);
+    if (s1 != BN_minus_zero)
+        OPENSSL_free(s1);
+    if (s2 != BN_minus_zero)
+        OPENSSL_free(s2);
+}
+
+static void test_fail_bignum_mono_message(const char *prefix, const char *file,
+                                          int line, const char *type,
+                                          const char *left, const char *right,
+                                          const char *op, const BIGNUM *bn)
+{
+    char *s = convertBN(bn);
+    size_t l = s != NULL ? strlen(s) : 0;
+
+    test_fail_string_message(prefix, file, line, type, left, right, op,
+                             s, l, s, l);
+    if (s != BN_minus_zero)
+        OPENSSL_free(s);
+}
+
 static void hex_convert_memory(const char *m, size_t n, char *b)
 {
     size_t i;
@@ -507,3 +551,89 @@ int test_mem_ne(const char *file, int line, const char *st1, const char *st2,
     }
     return 1;
 }
+
+#define DEFINE_BN_COMPARISONS(opname, op, zero_cond)                    \
+    int test_BN_ ## opname(const char *file, int line,                  \
+                           const char *s1, const char *s2,              \
+                           const BIGNUM *t1, const BIGNUM *t2)          \
+    {                                                                   \
+        if (BN_cmp(t1, t2) op 0)                                        \
+            return 1;                                                   \
+        test_fail_bignum_message(NULL, file, line, "BIGNUM", s1, s2,    \
+                                 #op, t1, t2);                          \
+        return 0;                                                       \
+    }                                                                   \
+    int test_BN_ ## opname ## _zero(const char *file, int line,         \
+                                    const char *s, const BIGNUM *a)     \
+    {                                                                   \
+        if (a != NULL &&(zero_cond))                                    \
+            return 1;                                                   \
+        test_fail_bignum_mono_message(NULL, file, line, "BIGNUM",       \
+                                      s, "0", #op, a);                  \
+        return 0;                                                       \
+    }
+
+DEFINE_BN_COMPARISONS(eq, ==, BN_is_zero(a))
+DEFINE_BN_COMPARISONS(ne, !=, !BN_is_zero(a))
+DEFINE_BN_COMPARISONS(gt, >,  !BN_is_negative(a) && !BN_is_zero(a))
+DEFINE_BN_COMPARISONS(ge, >=, !BN_is_negative(a) || BN_is_zero(a))
+DEFINE_BN_COMPARISONS(lt, <,  BN_is_negative(a) && !BN_is_zero(a))
+DEFINE_BN_COMPARISONS(le, <=, BN_is_negative(a) || BN_is_zero(a))
+
+int test_BN_eq_one(const char *file, int line, const char *s, const BIGNUM *a)
+{
+    if (a != NULL && BN_is_one(a))
+        return 1;
+    test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", s, "1", "==", a);
+    return 0;
+}
+
+int test_BN_odd(const char *file, int line, const char *s, const BIGNUM *a)
+{
+    if (a != NULL && BN_is_odd(a))
+        return 1;
+    test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "ODD(", ")", s,
+                                  a);
+    return 0;
+}
+
+int test_BN_even(const char *file, int line, const char *s, const BIGNUM *a)
+{
+    if (a != NULL && !BN_is_odd(a))
+        return 1;
+    test_fail_bignum_mono_message(NULL, file, line, "BIGNUM", "EVEN(", ")", s,
+                                  a);
+    return 0;
+}
+
+int test_BN_eq_word(const char *file, int line, const char *bns, const char *ws,
+                    const BIGNUM *a, BN_ULONG w)
+{
+    BIGNUM *bw;
+
+    if (a != NULL && BN_is_word(a, w))
+        return 1;
+    bw = BN_new();
+    BN_set_word(bw, w);
+    test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "==", a, bw);
+    BN_free(bw);
+    return 0;
+}
+
+int test_BN_abs_eq_word(const char *file, int line, const char *bns,
+                        const char *ws, const BIGNUM *a, BN_ULONG w)
+{
+    BIGNUM *bw, *aa;
+
+    if (a != NULL && BN_abs_is_word(a, w))
+        return 1;
+    bw = BN_new();
+    aa = BN_dup(a);
+    BN_set_negative(aa, 0);
+    BN_set_word(bw, w);
+    test_fail_bignum_message(NULL, file, line, "BIGNUM", bns, ws, "abs==",
+                             aa, bw);
+    BN_free(bw);
+    BN_free(aa);
+    return 0;
+}


More information about the openssl-commits mailing list