diff options
author | Rich Salz <rsalz@openssl.org> | 2017-04-26 12:39:46 -0400 |
---|---|---|
committer | Rich Salz <rsalz@openssl.org> | 2017-04-26 12:48:24 -0400 |
commit | 30bea14be6bbf77ed60acb9bd1befeb51d4c4b10 (patch) | |
tree | 4be59c467d54f17a4a35276a65e1355d34adcc02 /test/bntest.c | |
parent | e596c68c315dc896458cd2a97ab62ec88e144e0b (diff) |
Convert bntest to TEST_ framework
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/3265)
Diffstat (limited to 'test/bntest.c')
-rw-r--r-- | test/bntest.c | 1328 |
1 files changed, 582 insertions, 746 deletions
diff --git a/test/bntest.c b/test/bntest.c index 0410e07d08..97ad97aca6 100644 --- a/test/bntest.c +++ b/test/bntest.c @@ -70,6 +70,12 @@ static const int NUM1 = 50; /* additional tests for some functions */ static FILE *fp; static BN_CTX *ctx; +/* + * Polynomial coefficients used in GFM tests. + */ +static int p0[] = { 163, 7, 6, 3, 0, -1 }; +static int p1[] = { 193, 15, 0, -1 }; + /* * Look for |key| in the stanza and return it or NULL if not found. @@ -106,13 +112,12 @@ static BIGNUM *getBN(STANZA *s, const char *attribute) BIGNUM *ret = NULL; if ((hex = findattr(s, attribute)) == NULL) { - fprintf(stderr, "Can't find %s in test at line %d\n", - attribute, s->start); + TEST_error("Can't find %s in test at line %d", attribute, s->start); return NULL; } if (parseBN(&ret, hex) != (int)strlen(hex)) { - fprintf(stderr, "Could not decode '%s'.\n", hex); + TEST_error("Could not decode '%s'", hex); return NULL; } return ret; @@ -120,14 +125,12 @@ static BIGNUM *getBN(STANZA *s, const char *attribute) static int getint(STANZA *s, int *out, const char *attribute) { - BIGNUM *ret = getBN(s, attribute); + BIGNUM *ret; BN_ULONG word; int st = 0; - if (ret == NULL) - goto err; - - if ((word = BN_get_word(ret)) > INT_MAX) + if (!TEST_ptr(ret = getBN(s, attribute)) + || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX)) goto err; *out = (int)word; @@ -153,13 +156,10 @@ static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual) actstr = OPENSSL_strdup("-0"); else actstr = BN_bn2hex(actual); - if (exstr == NULL || actstr == NULL) + if (!TEST_ptr(exstr) || !TEST_ptr(actstr)) goto err; - fprintf(stderr, "Got %s =\n", op); - fprintf(stderr, "\t%s\n", actstr); - fprintf(stderr, "wanted:\n"); - fprintf(stderr, "\t%s\n", exstr); + TEST_error("Got %s =\n\t%s\nwanted:\n\t%s", op, actstr, exstr); err: OPENSSL_free(exstr); @@ -182,19 +182,20 @@ static int rand_neg(void) static int test_sub() { - BIGNUM *a, *b, *c; - int i; + BIGNUM *a = NULL, *b = NULL, *c = NULL; + int i, st = 0; - a = BN_new(); - b = BN_new(); - c = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b = BN_new()) + || !TEST_ptr(c = BN_new())) + goto err; for (i = 0; i < NUM0 + NUM1; i++) { if (i < NUM1) { BN_bntest_rand(a, 512, 0, 0); BN_copy(b, a); - if (BN_set_bit(a, i) == 0) - return 0; + if (!TEST_int_ne(BN_set_bit(a, i), 0)) + goto err; BN_add_word(b, i); } else { BN_bntest_rand(b, 400 + i - NUM1, 0, 0); @@ -204,30 +205,31 @@ static int test_sub() BN_sub(c, a, b); BN_add(c, c, b); BN_sub(c, c, a); - if (!BN_is_zero(c)) { - printf("Subtract test failed!\n"); - return 0; - } + if (!TEST_true(BN_is_zero(c))) + goto err; } + st = 1; +err: BN_free(a); BN_free(b); BN_free(c); - return 1; + return st; } static int test_div_recip() { - BIGNUM *a, *b, *c, *d, *e; - BN_RECP_CTX *recp; - int i; + BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; + BN_RECP_CTX *recp = NULL; + int st = 0, i; - recp = BN_RECP_CTX_new(); - a = BN_new(); - b = BN_new(); - c = BN_new(); - d = BN_new(); - e = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new()) + || !TEST_ptr(recp = BN_RECP_CTX_new())) + goto err; for (i = 0; i < NUM0 + NUM1; i++) { if (i < NUM1) { @@ -244,36 +246,32 @@ static int test_div_recip() BN_mul(e, d, b, ctx); BN_add(d, e, c); BN_sub(d, d, a); - if (!BN_is_zero(d)) { - printf("Reciprocal division test failed!\n"); - printf("a="); - BN_print_fp(stdout, a); - printf("\nb="); - BN_print_fp(stdout, b); - printf("\n"); - return 0; - } + if (!TEST_true(BN_is_zero(d))) + goto err; } + st = 1; +err: BN_free(a); BN_free(b); BN_free(c); BN_free(d); BN_free(e); BN_RECP_CTX_free(recp); - return 1; + return st; } static int test_mod() { - BIGNUM *a, *b, *c, *d, *e; - int i; + BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL; + int st = 0, i; - a = BN_new(); - b = BN_new(); - c = BN_new(); - d = BN_new(); - e = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new())) + goto err; BN_bntest_rand(a, 1024, 0, 0); for (i = 0; i < NUM0; i++) { @@ -283,17 +281,17 @@ static int test_mod() BN_mod(c, a, b, ctx); BN_div(d, e, a, b, ctx); BN_sub(e, e, c); - if (!BN_is_zero(e)) { - printf("Modulo test failed!\n"); - return 0; - } + if (!TEST_true(BN_is_zero(e))) + goto err; } + st = 1; +err: BN_free(a); BN_free(b); BN_free(c); BN_free(d); BN_free(e); - return 1; + return st; } static const char *bn1strings[] = { @@ -344,11 +342,10 @@ static char *glue(const char *list[]) for (i = 0; list[i] != NULL; i++) len += strlen(list[i]); - p = save = OPENSSL_malloc(len + 1); - if (p != NULL) { - for (i = 0; list[i] != NULL; i++) - p += strlen(strcpy(p, 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; } @@ -358,30 +355,31 @@ static char *glue(const char *list[]) */ static int test_modexp_mont5() { - BIGNUM *a, *p, *m, *d, *e, *b, *n, *c; - BN_MONT_CTX *mont; + 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; - a = BN_new(); - p = BN_new(); - m = BN_new(); - d = BN_new(); - e = BN_new(); - b = BN_new(); - n = BN_new(); - c = BN_new(); - mont = BN_MONT_CTX_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(p = BN_new()) + || !TEST_ptr(m = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new()) + || !TEST_ptr(b = BN_new()) + || !TEST_ptr(n = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(mont = BN_MONT_CTX_new())) + goto err; BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */ /* Zero exponent */ BN_bntest_rand(a, 1024, 0, 0); BN_zero(p); - if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) - return 0; - if (!BN_is_one(d)) { - printf("Modular exponentiation test failed!\n"); - return 0; - } + if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))) + goto err; + if (!TEST_true(BN_is_one(d))) + goto err; /* Regression test for carry bug in mulx4x_mont */ BN_hex2bn(&a, @@ -402,11 +400,8 @@ 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 (BN_cmp(c, d)) { - fprintf(stderr, "Montgomery multiplication test failed:" - " a*b != b*a.\n"); - return 0; - } + if (!TEST_int_eq(BN_cmp(c, d), 0)) + goto err; /* Regression test for carry bug in sqr[x]8x_mont */ bigstring = glue(bn1strings); @@ -420,21 +415,16 @@ 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 (BN_cmp(c, d)) { - fprintf(stderr, "Montgomery multiplication test failed:" - " a**2 != a*a.\n"); - return 0; - } + if (!TEST_int_eq(BN_cmp(c, d), 0)) + goto err; /* Zero input */ BN_bntest_rand(p, 1024, 0, 0); BN_zero(a); - if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) - return 0; - if (!BN_is_zero(d)) { - fprintf(stderr, "Modular exponentiation test failed!\n"); - return 0; - } + if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) + || !TEST_true(BN_is_zero(d))) + goto err; + /* * Craft an input whose Montgomery representation is 1, i.e., shorter * than the modulus m, in order to test the const time precomputation @@ -442,26 +432,22 @@ static int test_modexp_mont5() */ BN_one(a); BN_MONT_CTX_set(mont, m, ctx); - if (!BN_from_montgomery(e, a, mont, ctx)) - return 0; - if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL)) - return 0; - if (!BN_mod_exp_simple(a, e, p, m, ctx)) - return 0; - if (BN_cmp(a, d) != 0) { - printf("Modular exponentiation test failed!\n"); - return 0; - } + 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)) + goto err; + /* Finally, some regular test vectors. */ BN_bntest_rand(e, 1024, 0, 0); - if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL)) - return 0; - if (!BN_mod_exp_simple(a, e, p, m, ctx)) - return 0; - if (BN_cmp(a, d) != 0) { - printf("Modular exponentiation test failed!\n"); - return 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)) + goto err; + + st = 1; + +err: BN_MONT_CTX_free(mont); BN_free(a); BN_free(p); @@ -471,18 +457,19 @@ static int test_modexp_mont5() BN_free(b); BN_free(n); BN_free(c); - return 1; + return st; } #ifndef OPENSSL_NO_EC2M static int test_gf2m_add() { - BIGNUM *a, *b, *c; + BIGNUM *a = NULL, *b = NULL, *c = NULL; int i, st = 0; - a = BN_new(); - b = BN_new(); - c = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b = BN_new()) + || !TEST_ptr(c = BN_new())) + goto err; for (i = 0; i < NUM0; i++) { BN_rand(a, 512, 0, 0); @@ -491,17 +478,13 @@ static int test_gf2m_add() b->neg = rand_neg(); BN_GF2m_add(c, a, b); /* Test that two added values have the correct parity. */ - if ((BN_is_odd(a) && BN_is_odd(c)) - || (!BN_is_odd(a) && !BN_is_odd(c))) { - printf("GF(2^m) addition test (a) failed!\n"); + if (!TEST_false((BN_is_odd(a) && BN_is_odd(c)) + || (!BN_is_odd(a) && !BN_is_odd(c)))) goto err; - } BN_GF2m_add(c, c, c); /* Test that c + c = 0. */ - if (!BN_is_zero(c)) { - printf("GF(2^m) addition test (b) failed!\n"); + if (!TEST_true(BN_is_zero(c))) goto err; - } } st = 1; err: @@ -513,17 +496,16 @@ static int test_gf2m_add() static int test_gf2m_mod() { - static int p0[] = { 163, 7, 6, 3, 0, -1 }; - static int p1[] = { 193, 15, 0, -1 }; - BIGNUM *a, *b[2], *c, *d, *e; + BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL; int i, j, st = 0; - a = BN_new(); - b[0] = BN_new(); - b[1] = BN_new(); - c = BN_new(); - d = BN_new(); - e = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b[0] = BN_new()) + || !TEST_ptr(b[1] = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new())) + goto err; BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); @@ -535,10 +517,8 @@ 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 (!BN_is_zero(e)) { - printf("GF(2^m) modulo test failed!\n"); + if (!TEST_true(BN_is_zero(e))) goto err; - } } } st = 1; @@ -554,20 +534,20 @@ static int test_gf2m_mod() static int test_gf2m_mul() { - BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h; + BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL; + BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL; int i, j, st = 0; - int p0[] = { 163, 7, 6, 3, 0, -1 }; - int p1[] = { 193, 15, 0, -1 }; - - a = BN_new(); - b[0] = BN_new(); - b[1] = BN_new(); - c = BN_new(); - d = BN_new(); - e = BN_new(); - f = BN_new(); - g = BN_new(); - h = BN_new(); + + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b[0] = BN_new()) + || !TEST_ptr(b[1] = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new()) + || !TEST_ptr(f = BN_new()) + || !TEST_ptr(g = BN_new()) + || !TEST_ptr(h = BN_new())) + goto err; BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); @@ -584,13 +564,12 @@ 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 (!BN_is_zero(f)) { - printf("GF(2^m) modular multiplication test failed!\n"); + if (!TEST_true(BN_is_zero(f))) goto err; - } } } st = 1; + err: BN_free(a); BN_free(b[0]); @@ -606,16 +585,15 @@ static int test_gf2m_mul() static int test_gf2m_sqr() { - BIGNUM *a, *b[2], *c, *d; + BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; int i, j, st = 0; - int p0[] = { 163, 7, 6, 3, 0, -1 }; - int p1[] = { 193, 15, 0, -1 }; - a = BN_new(); - b[0] = BN_new(); - b[1] = BN_new(); - c = BN_new(); - d = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b[0] = BN_new()) + || !TEST_ptr(b[1] = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new())) + goto err; BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); @@ -628,10 +606,8 @@ 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 (!BN_is_zero(d)) { - printf("GF(2^m) modular squaring test failed!\n"); + if (!TEST_true(BN_is_zero(d))) goto err; - } } } st = 1; @@ -646,16 +622,15 @@ static int test_gf2m_sqr() static int test_gf2m_modinv() { - BIGNUM *a, *b[2], *c, *d; + BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; int i, j, st = 0; - int p0[] = { 163, 7, 6, 3, 0, -1 }; - int p1[] = { 193, 15, 0, -1 }; - a = BN_new(); - b[0] = BN_new(); - b[1] = BN_new(); - c = BN_new(); - d = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b[0] = BN_new()) + || !TEST_ptr(b[1] = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new())) + goto err; BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); @@ -666,10 +641,8 @@ 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 (!BN_is_one(d)) { - printf("GF(2^m) modular inversion test failed!\n"); + if (!TEST_true(BN_is_one(d))) goto err; - } } } st = 1; @@ -684,18 +657,18 @@ static int test_gf2m_modinv() static int test_gf2m_moddiv() { - BIGNUM *a, *b[2], *c, *d, *e, *f; + BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; + BIGNUM *e = NULL, *f = NULL; int i, j, st = 0; - int p0[] = { 163, 7, 6, 3, 0, -1 }; - int p1[] = { 193, 15, 0, -1 }; - a = BN_new(); - b[0] = BN_new(); - b[1] = BN_new(); - c = BN_new(); - d = BN_new(); - e = BN_new(); - f = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b[0] = BN_new()) + || !TEST_ptr(b[1] = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new()) + || !TEST_ptr(f = BN_new())) + goto err; BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); @@ -708,10 +681,8 @@ 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 (!BN_is_one(f)) { - printf("GF(2^m) modular division test failed!\n"); + if (!TEST_true(BN_is_one(f))) goto err; - } } } st = 1; @@ -728,18 +699,18 @@ static int test_gf2m_moddiv() static int test_gf2m_modexp() { - BIGNUM *a, *b[2], *c, *d, *e, *f; + BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; + BIGNUM *e = NULL, *f = NULL; int i, j, st = 0; - int p0[] = { 163, 7, 6, 3, 0, -1 }; - int p1[] = { 193, 15, 0, -1 }; - a = BN_new(); - b[0] = BN_new(); - b[1] = BN_new(); - c = BN_new(); - d = BN_new(); - e = BN_new(); - f = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b[0] = BN_new()) + || !TEST_ptr(b[1] = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new()) + || !TEST_ptr(f = BN_new())) + goto err; BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); @@ -756,10 +727,8 @@ 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 (!BN_is_zero(f)) { - printf("GF(2^m) modular exponentiation test failed!\n"); + if (!TEST_true(BN_is_zero(f))) goto err; - } } } st = 1; @@ -776,18 +745,18 @@ static int test_gf2m_modexp() static int test_gf2m_modsqrt() { - BIGNUM *a, *b[2], *c, *d, *e, *f; + BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; + BIGNUM *e = NULL, *f = NULL; int i, j, st = 0; - int p0[] = { 163, 7, 6, 3, 0, -1 }; - int p1[] = { 193, 15, 0, -1 }; - a = BN_new(); - b[0] = BN_new(); - b[1] = BN_new(); - c = BN_new(); - d = BN_new(); - e = BN_new(); - f = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b[0] = BN_new()) + || !TEST_ptr(b[1] = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new()) + || !TEST_ptr(f = BN_new())) + goto err; BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); @@ -800,10 +769,8 @@ 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 (!BN_is_zero(f)) { - printf("GF(2^m) modular square root test failed!\n"); + if (!TEST_true(BN_is_zero(f))) goto err; - } } } st = 1; @@ -820,17 +787,17 @@ static int test_gf2m_modsqrt() static int test_gf2m_modsolvequad() { - BIGNUM *a, *b[2], *c, *d, *e; + BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL; + BIGNUM *e = NULL; int i, j, s = 0, t, st = 0; - int p0[] = { 163, 7, 6, 3, 0, -1 }; - int p1[] = { 193, 15, 0, -1 }; - a = BN_new(); - b[0] = BN_new(); - b[1] = BN_new(); - c = BN_new(); - d = BN_new(); - e = BN_new(); + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b[0] = BN_new()) + || !TEST_ptr(b[1] = BN_new()) + || !TEST_ptr(c = BN_new()) + || !TEST_ptr(d = BN_new()) + || !TEST_ptr(e = BN_new())) + goto err; BN_GF2m_arr2poly(p0, b[0]); BN_GF2m_arr2poly(p1, b[1]); @@ -848,18 +815,13 @@ static int test_gf2m_modsolvequad() /* * Test that solution of quadratic c satisfies c^2 + c = a. */ - if (!BN_is_zero(e)) { - printf("GF(2^m) modular solve quadratic test failed!\n"); + if (!TEST_true(BN_is_zero(e))) goto err; - } - } } } - if (s == 0) { - printf("All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", - NUM0); - printf("this is very unlikely and probably indicates an error.\n"); + if (!TEST_int_ge(s, 0)) { + TEST_info("%d tests found no roots; probably an error", NUM0); goto err; } st = 1; @@ -876,16 +838,13 @@ static int test_gf2m_modsolvequad() static int test_kronecker() { - BIGNUM *a, *b, *r, *t; - int i; - int legendre, kronecker; - int st = 0; + BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL; + int i, legendre, kronecker, st = 0; - a = BN_new(); - b = BN_new(); - r = BN_new(); - t = BN_new(); - if (a == NULL || b == NULL || r == NULL || t == NULL) + if (!TEST_ptr(a = BN_new()) + || !TEST_ptr(b = BN_new()) + || !TEST_ptr(r = BN_new()) + || !TEST_ptr(t = BN_new())) goto err; /* @@ -898,27 +857,27 @@ static int test_kronecker() * is prime but whether BN_kronecker works.) */ - if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)) + if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL))) goto err; b->neg = rand_neg(); for (i = 0; i < NUM0; i++) { - if (!BN_bntest_rand(a, 512, 0, 0)) + if (!TEST_true(BN_bntest_rand(a, 512, 0, 0))) goto err; a->neg = rand_neg(); /* t := (|b|-1)/2 (note that b is odd) */ - if (!BN_copy(t, b)) + if (!TEST_true(BN_copy(t, b))) goto err; t->neg = 0; - if (!BN_sub_word(t, 1)) + if (!TEST_true(BN_sub_word(t, 1))) goto err; - if (!BN_rshift1(t, t)) + if (!TEST_true(BN_rshift1(t, t))) goto err; /* r := a^t mod b */ b->neg = 0; - if (!BN_mod_exp_recp(r, a, t, b, ctx)) + if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx))) goto err; b->neg = 1; @@ -927,30 +886,23 @@ static int test_kronecker() else if (BN_is_zero(r)) legendre = 0; else { - if (!BN_add_word(r, 1)) + if (!TEST_true(BN_add_word(r, 1))) goto err; - if (0 != BN_ucmp(r, b)) { - printf("Legendre symbol computation failed\n"); + if (!TEST_int_eq(BN_ucmp(r, b), 0)) { + TEST_info("Legendre symbol computation failed"); goto err; } legendre = -1; } - kronecker = BN_kronecker(a, b, ctx); - if (kronecker < -1) + if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1)) goto err; /* we actually need BN_kronecker(a, |b|) */ if (a->neg && b->neg) kronecker = -kronecker; - if (legendre != kronecker) { - printf("legendre != kronecker; a = "); - BN_print_fp(stdout, a); - printf(", b = "); - BN_print_fp(stdout, b); - printf("\n"); + if (!TEST_int_eq(legendre, kronecker)) goto err; - } } st = 1; @@ -964,21 +916,21 @@ static int test_kronecker() static int file_sum(STANZA *s) { - BIGNUM *a = getBN(s, "A"); - BIGNUM *b = getBN(s, "B"); - BIGNUM *sum = getBN(s, "Sum"); - BIGNUM *ret = BN_new(); + BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL; BN_ULONG b_word; int st = 0; - if (a == NULL || b == NULL || sum == NULL || ret == NULL) + if (!TEST_ptr(a = getBN(s, "A")) + || !TEST_ptr(b = getBN(s, "B")) + || !TEST_ptr(sum = getBN(s, "Sum")) + || !TEST_ptr(ret = BN_new())) goto err; - if (!BN_add(ret, a, b) + if (!TEST_true(BN_add(ret, a, b)) || !equalBN("A + B", sum, ret) - || !BN_sub(ret, sum, a) + || !TEST_true(BN_sub(ret, sum, a)) || !equalBN("Sum - A", b, ret) - || !BN_sub(ret, sum, b) + || !TEST_true(BN_sub(ret, sum, b)) || !equalBN("Sum - B", a, ret)) goto err; @@ -987,23 +939,23 @@ static int file_sum(STANZA *s) * or when |r| and |b| point to the same BIGNUM. * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM. */ - if (!BN_copy(ret, a) - || !BN_add(ret, ret, b) + if (!TEST_true(BN_copy(ret, a)) + || !TEST_true(BN_add(ret, ret, b)) || !equalBN("A + B (r is a)", sum, ret) - || !BN_copy(ret, b) - || !BN_add(ret, a, ret) + || !TEST_true(BN_copy(ret, b)) + || !TEST_true(BN_add(ret, a, ret)) || !equalBN("A + B (r is b)", sum, ret) - || !BN_copy(ret, sum) - || !BN_sub(ret, ret, a) + || !TEST_true(BN_copy(ret, sum)) + || !TEST_true(BN_sub(ret, ret, a)) || !equalBN("Sum - A (r is a)", b, ret) - || !BN_copy(ret, a) - || !BN_sub(ret, sum, ret) + || !TEST_true(BN_copy(ret, a)) + || !TEST_true(BN_sub(ret, sum, ret)) || !equalBN("Sum - A (r is b)", b, ret) - || !BN_copy(ret, sum) - || !BN_sub(ret, ret, b) + || !TEST_true(BN_copy(ret, sum)) + || !TEST_true(BN_sub(ret, ret, b)) || !equalBN("Sum - B (r is a)", a, ret) - || !BN_copy(ret, b) - || !BN_sub(ret, sum, ret) + || !TEST_true(BN_copy(ret, b)) + || !TEST_true(BN_sub(ret, sum, ret)) || !equalBN("Sum - B (r is b)", a, ret)) goto err; @@ -1015,11 +967,11 @@ static int file_sum(STANZA *s) * TODO: test that. */ if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) { - if (!BN_uadd(ret, a, b) + if (!TEST_true(BN_uadd(ret, a, b)) || !equalBN("A +u B", sum, ret) - || !BN_usub(ret, sum, a) + || !TEST_true(BN_usub(ret, sum, a)) || !equalBN("Sum -u A", b, ret) - || !BN_usub(ret, sum, b) + || !TEST_true(BN_usub(ret, sum, b)) || !equalBN("Sum -u B", a, ret)) goto err; /* @@ -1027,23 +979,23 @@ static int file_sum(STANZA *s) * BIGNUM, or when |r| and |b| point to the same BIGNUM. * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM. */ - if (!BN_copy(ret, a) - || !BN_uadd(ret, ret, b) + if (!TEST_true(BN_copy(ret, a)) + || !TEST_true(BN_uadd(ret, ret, b)) || !equalBN("A +u B (r is a)", sum, ret) - || !BN_copy(ret, b) - || !BN_uadd(ret, a, ret) + || !TEST_true(BN_copy(ret, b)) + || !TEST_true(BN_uadd(ret, a, ret)) || !equalBN("A +u B (r is b)", sum, ret) - || !BN_copy(ret, sum) - || !BN_usub(ret, ret, a) + || !TEST_true(BN_copy(ret, sum)) + || !TEST_true(BN_usub(ret, ret, a)) || !equalBN("Sum -u A (r is a)", b, ret) - || !BN_copy(ret, a) - || !BN_usub(ret, sum, ret) + || !TEST_true(BN_copy(ret, a)) + || !TEST_true(BN_usub(ret, sum, ret)) || !equalBN("Sum -u A (r is b)", b, ret) - || !BN_copy(ret, sum) - || !BN_usub(ret, ret, b) + || !TEST_true(BN_copy(ret, sum)) + || !TEST_true(BN_usub(ret, ret, b)) || !equalBN("Sum -u B (r is a)", a, ret) - || !BN_copy(ret, b) - || !BN_usub(ret, sum, ret) + || !TEST_true(BN_copy(ret, b)) + || !TEST_true(BN_usub(ret, sum, ret)) || !equalBN("Sum -u B (r is b)", a, ret)) goto err; } @@ -1053,11 +1005,11 @@ static int file_sum(STANZA *s) */ b_word = BN_get_word(b); if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) { - if (!BN_copy(ret, a) - || !BN_add_word(ret, b_word) + if (!TEST_true(BN_copy(ret, a)) + || !TEST_true(BN_add_word(ret, b_word)) || !equalBN("A + B (word)", sum, ret) - || !BN_copy(ret, sum) - || !BN_sub_word(ret, b_word) + || !TEST_true(BN_copy(ret, sum)) + || !TEST_true(BN_sub_word(ret, b_word)) || !equalBN("Sum - B (word)", a, ret)) goto err; } @@ -1073,41 +1025,41 @@ err: static int file_lshift1(STANZA *s) { - BIGNUM *a = getBN(s, "A"); - BIGNUM *lshift1 = getBN(s, "LShift1"); - BIGNUM *zero = BN_new(); - BIGNUM *ret = BN_new(); - BIGNUM *two = BN_new(); - BIGNUM *remainder = BN_new(); + BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL; + BIGNUM *two = NULL, *remainder = NULL; int st = 0; - if (a == NULL || lshift1 == NULL || zero == NULL - || ret == NULL || two == NULL || remainder == NULL) + if (!TEST_ptr(a = getBN(s, "A")) + || !TEST_ptr(lshift1 = getBN(s, "LShift1")) + || !TEST_ptr(zero = BN_new()) + || !TEST_ptr(ret = BN_new()) + || !TEST_ptr(two = BN_new()) + || !TEST_ptr(remainder = BN_new())) goto err; BN_zero(zero); - if (!BN_set_word(two, 2) - || !BN_add(ret, a, a) + if (!TEST_true(BN_set_word(two, 2)) + || !TEST_true(BN_add(ret, a, a)) || !equalBN("A + A", lshift1, ret) - || !BN_mul(ret, a, two, ctx) + || !TEST_true(BN_mul(ret, a, two, ctx)) || !equalBN("A * 2", lshift1, ret) - || !BN_div(ret, remainder, lshift1, two, ctx) + || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx)) || !equalBN("LShift1 / 2", a, ret) || !equalBN("LShift1 % 2", zero, remainder) - || !BN_lshift1(ret, a) + || !TEST_true(BN_lshift1(ret, a)) || !equalBN("A << 1", lshift1, ret) - || !BN_rshift1(ret, lshift1) + || !TEST_true(BN_rshift1(ret, lshift1)) || !equalBN("LShift >> 1", a, ret) - || !BN_rshift1(ret, lshift1) + || !TEST_true(BN_rshift1(ret, lshift1)) || !equalBN("LShift >> 1", a, ret)) goto err; /* Set the LSB to 1 and test rshift1 again. */ - if (!BN_set_bit(lshift1, 0) - || !BN_div(ret, NULL /* rem */ , lshift1, two, ctx) + if (!TEST_true(BN_set_bit(lshift1, 0)) + || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx)) || !equalBN("(LShift1 | 1) / 2", a, ret) - || !BN_rshift1(ret, lshift1) + || !TEST_true(BN_rshift1(ret, lshift1)) || !equalBN("(LShift | 1) >> 1", a, ret)) goto err; @@ -1125,18 +1077,16 @@ err: static int file_lshift(STANZA *s) { - BIGNUM *a = getBN(s, "A"); - BIGNUM *lshift = getBN(s, "LShift"); - BIGNUM *ret = BN_new(); - int n = 0; - int st = 0; + BIGNUM *a = NULL, *lshift = NULL, *ret = NULL; + int n = 0, st = 0; - if (a == NULL || lshift == NULL || ret == NULL || !getint(s, &n, "N")) - goto err; + if (!TEST_ptr(a = getBN(s, "A")) + || !TEST_ptr(lshift = getBN(s, "LShift")) + || !TEST_ptr(ret = BN_new())) - if (!BN_lshift(ret, a, n) + if (!TEST_true(BN_lshift(ret, a, n)) || !equalBN("A << N", lshift, ret) - || !BN_rshift(ret, lshift, n) + || !TEST_true(BN_rshift(ret, lshift, n)) || !equalBN("A >> N", a, ret)) goto err; @@ -1150,85 +1100,78 @@ err: static int file_rshift(STANZA *s) { - BIGNUM *a = getBN(s, "A"); - BIGNUM *rshift = getBN(s, "RShift"); - BIGNUM *ret = BN_new(); - int n = 0; - int errcnt = 1; + BIGNUM *a = NULL, *rshift = NULL, *ret = NULL; + int n = 0, st = 0; - if (a == NULL || rshift == NULL || ret == NULL || !getint(s, &n, "N")) + if (!TEST_ptr(a = getBN(s, "A")) + || !TEST_ptr(rshift = getBN(s, "RShift")) + || !TEST_ptr(ret = BN_new()) + || !getint(s, &n, "N")) goto err; |