Fix lshift tests
[openssl.git] / test / bntest.c
index 97ad97aca633188145baed64a146a08e7865c946..9eda5bd825102a91cebcc14d6428d88b0422e19c 100644 (file)
@@ -1,19 +1,20 @@
 /*
- * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
  */
+#include "../e_os.h"
 #include <assert.h>
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
 
-#include "e_os.h"
-#include <internal/numbers.h>
+#include "internal/nelem.h"
+#include "internal/numbers.h"
 #include <openssl/bn.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
@@ -35,25 +36,12 @@ BIGNUM *bn_expand2(BIGNUM *b, int words);
 BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; }
 #include "../crypto/bn/bn_lcl.h"
 
-#define MAXPAIRS        20
-
 /*
  * Things in boring, not in openssl.  TODO we should add them.
  */
 #define HAVE_BN_PADDED 0
 #define HAVE_BN_SQRT 0
 
-typedef struct pair_st {
-    char *key;
-    char *value;
-} PAIR;
-
-typedef struct stanza_st {
-    int start;
-    int numpairs;
-    PAIR pairs[MAXPAIRS];
-} STANZA;
-
 typedef struct filetest_st {
     const char *name;
     int (*func)(STANZA *s);
@@ -67,15 +55,15 @@ typedef struct mpitest_st {
 
 static const int NUM0 = 100;           /* number of tests */
 static const int NUM1 = 50;            /* additional tests for some functions */
-static FILE *fp;
 static BN_CTX *ctx;
 
 /*
  * Polynomial coefficients used in GFM tests.
  */
+#ifndef OPENSSL_NO_EC2M
 static int p0[] = { 163, 7, 6, 3, 0, -1 };
 static int p1[] = { 193, 15, 0, -1 };
-
+#endif
 
 /*
  * Look for |key| in the stanza and return it or NULL if not found.
@@ -91,6 +79,18 @@ static const char *findattr(STANZA *s, const char *key)
     return NULL;
 }
 
+/*
+ * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
+ */
+static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
+{
+    char *bigstring = glue_strings(bn_strings, NULL);
+    int ret = BN_hex2bn(out, bigstring);
+
+    OPENSSL_free(bigstring);
+    return ret;
+}
+
 /*
  * Parse BIGNUM, return number of bytes parsed.
  */
@@ -112,7 +112,7 @@ static BIGNUM *getBN(STANZA *s, const char *attribute)
     BIGNUM *ret = NULL;
 
     if ((hex = findattr(s, attribute)) == NULL) {
-        TEST_error("Can't find %s in test at line %d", attribute, s->start);
+        TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
         return NULL;
     }
 
@@ -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;
 }
 
@@ -180,7 +163,7 @@ static int rand_neg(void)
 }
 
 
-static int test_sub()
+static int test_sub(void)
 {
     BIGNUM *a = NULL, *b = NULL, *c = NULL;
     int i, st = 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;
@@ -217,7 +200,7 @@ err:
 }
 
 
-static int test_div_recip()
+static int test_div_recip(void)
 {
     BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
     BN_RECP_CTX *recp = NULL;
@@ -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;
@@ -261,7 +244,7 @@ err:
 }
 
 
-static int test_mod()
+static int test_mod(void)
 {
     BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
     int st = 0, i;
@@ -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;
@@ -334,31 +317,15 @@ static const char *bn2strings[] = {
     NULL
 };
 
-static char *glue(const char *list[])
-{
-    size_t len = 0;
-    char *p, *save;
-    int i;
-
-    for (i = 0; list[i] != NULL; i++)
-        len += strlen(list[i]);
-    if (!TEST_ptr(p = save = OPENSSL_malloc(len + 1)))
-            return NULL;
-    for (i = 0; list[i] != NULL; i++)
-        p += strlen(strcpy(p, list[i]));
-    return save;
-}
-
 /*
  * Test constant-time modular exponentiation with 1024-bit inputs, which on
  * x86_64 cause a different code branch to be taken.
  */
-static int test_modexp_mont5()
+static int test_modexp_mont5(void)
 {
     BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
     BIGNUM *b = NULL, *n = NULL, *c = NULL;
     BN_MONT_CTX *mont = NULL;
-    char *bigstring;
     int st = 0;
 
     if (!TEST_ptr(a = BN_new())
@@ -378,7 +345,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,29 +367,69 @@ 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 */
-    bigstring = glue(bn1strings);
-    BN_hex2bn(&n, bigstring);
-    OPENSSL_free(bigstring);
-    bigstring = glue(bn2strings);
-    BN_hex2bn(&a, bigstring);
-    OPENSSL_free(bigstring);
+    parse_bigBN(&n, bn1strings);
+    parse_bigBN(&a, bn2strings);
+    BN_free(b);
+    b = BN_dup(a);
+    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_BN_eq(c, d))
+        goto err;
+
+    /* Regression test for carry bug in bn_sqrx8x_internal */
+    {
+        static const char *ahex[] = {
+                      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
+            "9544D954000000006C0000000000000000000000000000000000000000000000",
+            "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
+            "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
+            "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
+            "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
+            "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
+            NULL
+        };
+        static const char *nhex[] = {
+                      "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
+            "00000010000000006C0000000000000000000000000000000000000000000000",
+            "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
+            "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
+            "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
+            NULL
+        };
+
+        parse_bigBN(&a, ahex);
+        parse_bigBN(&n, nhex);
+    }
     BN_free(b);
     b = BN_dup(a);
     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 +442,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;
@@ -461,7 +468,7 @@ err:
 }
 
 #ifndef OPENSSL_NO_EC2M
-static int test_gf2m_add()
+static int test_gf2m_add(void)
 {
     BIGNUM *a = NULL, *b = NULL, *c = NULL;
     int i, st = 0;
@@ -483,7 +490,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;
@@ -494,7 +501,7 @@ static int test_gf2m_add()
     return st;
 }
 
-static int test_gf2m_mod()
+static int test_gf2m_mod(void)
 {
     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
     int i, j, st = 0;
@@ -517,7 +524,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;
         }
     }
@@ -532,7 +539,7 @@ static int test_gf2m_mod()
     return st;
 }
 
-static int test_gf2m_mul()
+static int test_gf2m_mul(void)
 {
     BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
     BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
@@ -564,7 +571,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;
         }
     }
@@ -583,7 +590,7 @@ static int test_gf2m_mul()
     return st;
 }
 
-static int test_gf2m_sqr()
+static int test_gf2m_sqr(void)
 {
     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
     int i, j, st = 0;
@@ -606,7 +613,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;
         }
     }
@@ -620,7 +627,7 @@ static int test_gf2m_sqr()
     return st;
 }
 
-static int test_gf2m_modinv()
+static int test_gf2m_modinv(void)
 {
     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
     int i, j, st = 0;
@@ -641,7 +648,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;
         }
     }
@@ -655,7 +662,7 @@ static int test_gf2m_modinv()
     return st;
 }
 
-static int test_gf2m_moddiv()
+static int test_gf2m_moddiv(void)
 {
     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
     BIGNUM *e = NULL, *f = NULL;
@@ -681,7 +688,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;
         }
     }
@@ -697,7 +704,7 @@ static int test_gf2m_moddiv()
     return st;
 }
 
-static int test_gf2m_modexp()
+static int test_gf2m_modexp(void)
 {
     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
     BIGNUM *e = NULL, *f = NULL;
@@ -727,7 +734,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;
         }
     }
@@ -743,7 +750,7 @@ static int test_gf2m_modexp()
     return st;
 }
 
-static int test_gf2m_modsqrt()
+static int test_gf2m_modsqrt(void)
 {
     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
     BIGNUM *e = NULL, *f = NULL;
@@ -769,7 +776,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;
         }
     }
@@ -785,7 +792,7 @@ static int test_gf2m_modsqrt()
     return st;
 }
 
-static int test_gf2m_modsolvequad()
+static int test_gf2m_modsolvequad(void)
 {
     BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
     BIGNUM *e = NULL;
@@ -815,7 +822,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;
             }
         }
@@ -836,7 +843,7 @@ static int test_gf2m_modsolvequad()
 }
 #endif
 
-static int test_kronecker()
+static int test_kronecker(void)
 {
     BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
     int i, legendre, kronecker, st = 0;
@@ -1082,7 +1089,9 @@ static int file_lshift(STANZA *s)
 
     if (!TEST_ptr(a = getBN(s, "A"))
             || !TEST_ptr(lshift = getBN(s, "LShift"))
-            || !TEST_ptr(ret = BN_new()))
+            || !TEST_ptr(ret = BN_new())
+            || !getint(s, &n, "N"))
+        goto err;
 
     if (!TEST_true(BN_lshift(ret, a, n))
             || !equalBN("A << N", lshift, ret)
@@ -1158,8 +1167,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);
 
@@ -1397,9 +1407,9 @@ static int file_modexp(STANZA *s)
         "0000000000000000000000000000000000000000000000000000000000000000"
         "0000000000000000000000000000000000000000000000000000000000000000"
         "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_true(BN_mod_exp(d, a, b, c, ctx))
+        || !TEST_true(BN_mul(e, a, a, ctx))
+        || !TEST_BN_eq(d, e))
         goto err;
 
     st = 1;
@@ -1471,7 +1481,7 @@ err:
     return st;
 }
 
-static int test_bn2padded()
+static int test_bn2padded(void)
 {
 #if HAVE_BN_PADDED
     uint8_t zeros[256], out[256], reference[128];
@@ -1532,38 +1542,67 @@ err:
 #endif
 }
 
-static int test_dec2bn()
+static int test_dec2bn(void)
 {
     BIGNUM *bn = NULL;
     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;
@@ -1572,38 +1611,64 @@ err:
     return st;
 }
 
-static int test_hex2bn()
+static int test_hex2bn(void)
 {
     BIGNUM *bn = NULL;
     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;
 
@@ -1612,7 +1677,7 @@ err:
     return st;
 }
 
-static int test_asc2bn()
+static int test_asc2bn(void)
 {
     BIGNUM *bn = NULL;
     int st = 0;
@@ -1621,43 +1686,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 +1763,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;
     }
@@ -1710,7 +1775,7 @@ err:
     return st;
 }
 
-static int test_rand()
+static int test_rand(void)
 {
     BIGNUM *bn = NULL;
     int st = 0;
@@ -1722,12 +1787,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;
@@ -1736,7 +1801,7 @@ err:
     return st;
 }
 
-static int test_negzero()
+static int test_negzero(void)
 {
     BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
     BIGNUM *numerator = NULL, *denominator = NULL;
@@ -1755,8 +1820,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 +1838,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);
@@ -1805,7 +1870,7 @@ err:
     return st;
 }
 
-static int test_badmod()
+static int test_badmod(void)
 {
     BIGNUM *a = NULL, *b = NULL, *zero = NULL;
     BN_MONT_CTX *mont = NULL;
@@ -1871,7 +1936,7 @@ err:
     return st;
 }
 
-static int test_expmodzero()
+static int test_expmodzero(void)
 {
     BIGNUM *a = NULL, *r = NULL, *zero = NULL;
     int st = 0;
@@ -1883,17 +1948,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;
@@ -1904,7 +1969,7 @@ err:
     return st;
 }
 
-static int test_smallprime()
+static int test_smallprime(void)
 {
     static const int kBits = 10;
     BIGNUM *r;
@@ -1922,7 +1987,7 @@ err:
     return st;
 }
 
-static int test_3_is_prime()
+static int test_3_is_prime(void)
 {
     int ret = 0;
     BIGNUM *r = NULL;
@@ -1946,79 +2011,6 @@ err:
     return ret;
 }
 
-
-/* Delete leading and trailing spaces from a string */
-static char *strip_spaces(char *p)
-{
-    char *q;
-
-    /* Skip over leading spaces */
-    while (*p && isspace(*p))
-        p++;
-    if (!*p)
-        return NULL;
-
-    for (q = p + strlen(p) - 1; q != p && isspace(*q); )
-        *q-- = '\0';
-    return *p ? p : NULL;
-}
-
-/*
- * Read next test stanza; return 1 if found, 0 on EOF or error.
- */
-static int readstanza(STANZA *s, int *linesread)
-{
-    PAIR *pp = s->pairs;
-    char *p, *equals, *key, *value;
-    char buff[1024];
-
-    while (fgets(buff, sizeof(buff), fp) != NULL) {
-        (*linesread)++;
-        if (!TEST_ptr(p = strchr(buff, '\n'))) {
-            TEST_info("Line %d too long", s->start);
-            return 0;
-        }
-        *p = '\0';
-
-        /* Blank line marks end of tests. */
-        if (buff[0] == '\0')
-            break;
-
-        /* Lines starting with a pound sign are ignored. */
-        if (buff[0] == '#')
-            continue;
-
-        if (!TEST_ptr(equals = strchr(buff, '=')))
-            return 0;
-        *equals++ = '\0';
-
-        if (!TEST_ptr(key = strip_spaces(buff))
-                || !TEST_ptr(value = strip_spaces(equals))
-                || !TEST_int_lt(s->numpairs++, MAXPAIRS)
-                || !TEST_ptr(pp->key = OPENSSL_strdup(key))
-                || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
-            return 0;
-        pp++;
-    }
-
-    /* If we read anything, return ok. */
-    return 1;
-}
-
-static void clearstanza(STANZA *s)
-{
-    PAIR *pp = s->pairs;
-    int i = s->numpairs;
-    int start = s->start;
-
-    for ( ; --i >= 0; pp++) {
-        OPENSSL_free(pp->key);
-        OPENSSL_free(pp->value);
-    }
-    memset(s, 0, sizeof(*s));
-    s->start = start;
-}
-
 static int file_test_run(STANZA *s)
 {
     static const FILETEST filetests[] = {
@@ -2040,86 +2032,89 @@ static int file_test_run(STANZA *s)
     for ( ; --numtests >= 0; tp++) {
         if (findattr(s, tp->name) != NULL) {
             if (!tp->func(s)) {
-                TEST_info("Failed %s test at %d", tp->name, s->start);
+                TEST_info("%s:%d: Failed %s test",
+                          s->test_file, s->start, tp->name);
                 return 0;
             }
             return 1;
         }
     }
-    TEST_info("Unknown test at %d", s->start);
+    TEST_info("%s:%d: Unknown test", s->test_file, s->start);
     return 0;
 }
 
-static int file_tests()
+static int run_file_tests(int i)
 {
-    STANZA s;
-    int linesread = 0, errcnt = 0;
+    STANZA *s = NULL;
+    char *testfile = test_get_argument(i);
+    int c;
+
+    if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
+        return 0;
+    if (!test_start_file(s, testfile)) {
+        OPENSSL_free(s);
+        return 0;
+    }
 
     /* Read test file. */
-    memset(&s, 0, sizeof(s));
-    while (!feof(fp) && readstanza(&s, &linesread)) {
-        if (s.numpairs == 0)
+    while (!BIO_eof(s->fp) && test_readstanza(s)) {
+        if (s->numpairs == 0)
             continue;
-        if (!file_test_run(&s)) {
-            errcnt++;
-        }
-        clearstanza(&s);
-        s.start = linesread;
+        if (!file_test_run(s))
+            s->errors++;
+        s->numtests++;
+        test_clearstanza(s);
     }
+    test_end_file(s);
+    c = s->errors;
+    OPENSSL_free(s);
 
-    return errcnt == 0;
+    return c == 0;
 }
 
-int test_main(int argc, char *argv[])
+
+int setup_tests(void)
 {
-    static const char rnd_seed[] =
-        "If not seeded, BN_generate_prime might fail";
-    int result = 0;
+    int n = test_get_argument_count();
 
-    if (argc != 2) {
-        TEST_error("%s TEST_FILE", argv[0]);
+    if (!TEST_ptr(ctx = BN_CTX_new()))
         return 0;
-    }
 
-    ADD_TEST(test_sub);
-    ADD_TEST(test_div_recip);
-    ADD_TEST(test_mod);
-    ADD_TEST(test_modexp_mont5);
-    ADD_TEST(test_kronecker);
-    ADD_TEST(test_rand);
-    ADD_TEST(test_bn2padded);
-    ADD_TEST(test_dec2bn);
-    ADD_TEST(test_hex2bn);
-    ADD_TEST(test_asc2bn);
-    ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
-    ADD_TEST(test_negzero);
-    ADD_TEST(test_badmod);
-    ADD_TEST(test_expmodzero);
-    ADD_TEST(test_smallprime);
+    if (n == 0) {
+        ADD_TEST(test_sub);
+        ADD_TEST(test_div_recip);
+        ADD_TEST(test_mod);
+        ADD_TEST(test_modexp_mont5);
+        ADD_TEST(test_kronecker);
+        ADD_TEST(test_rand);
+        ADD_TEST(test_bn2padded);
+        ADD_TEST(test_dec2bn);
+        ADD_TEST(test_hex2bn);
+        ADD_TEST(test_asc2bn);
+        ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
+        ADD_TEST(test_negzero);
+        ADD_TEST(test_badmod);
+        ADD_TEST(test_expmodzero);
+        ADD_TEST(test_smallprime);
 #ifndef OPENSSL_NO_EC2M
-    ADD_TEST(test_gf2m_add);
-    ADD_TEST(test_gf2m_mod);
-    ADD_TEST(test_gf2m_mul);
-    ADD_TEST(test_gf2m_sqr);
-    ADD_TEST(test_gf2m_modinv);
-    ADD_TEST(test_gf2m_moddiv);
-    ADD_TEST(test_gf2m_modexp);
-    ADD_TEST(test_gf2m_modsqrt);
-    ADD_TEST(test_gf2m_modsolvequad);
+        ADD_TEST(test_gf2m_add);
+        ADD_TEST(test_gf2m_mod);
+        ADD_TEST(test_gf2m_mul);
+        ADD_TEST(test_gf2m_sqr);
+        ADD_TEST(test_gf2m_modinv);
+        ADD_TEST(test_gf2m_moddiv);
+        ADD_TEST(test_gf2m_modexp);
+        ADD_TEST(test_gf2m_modsqrt);
+        ADD_TEST(test_gf2m_modsolvequad);
 #endif
-    ADD_TEST(test_3_is_prime);
-    ADD_TEST(file_tests);
-
-    RAND_seed(rnd_seed, sizeof rnd_seed);
-    ctx = BN_CTX_new();
-    TEST_check(ctx != NULL);
-
-    if (!TEST_ptr(fp = fopen(argv[1], "r")))
-        goto end;
-    result = run_tests(argv[0]);
-    fclose(fp);
+        ADD_TEST(test_3_is_prime);
+    } else {
+        ADD_ALL_TESTS(run_file_tests, n);
+    }
+    return 1;
+}
 
-end:
+void cleanup_tests(void)
+{
     BN_CTX_free(ctx);
-    return result;
 }