-/* $OpenBSD: bn_test.c,v 1.11 2023/04/07 22:30:31 tb Exp $ */
+/* $OpenBSD: bn_test.c,v 1.12 2023/04/07 22:32:59 tb Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
const int num1 = 50; /* additional tests for some functions */
const int num2 = 5; /* number of tests for slow functions */
-int test_add(BIO *bp);
-int test_sub(BIO *bp);
-int test_lshift1(BIO *bp);
+int test_add(BIO *bp, BN_CTX *ctx);
+int test_sub(BIO *bp, BN_CTX *ctx);
+int test_lshift1(BIO *bp, BN_CTX *ctx);
int test_lshift(BIO *bp, BN_CTX *ctx, int use_lst);
-int test_rshift1(BIO *bp);
+int test_rshift1(BIO *bp, BN_CTX *ctx);
int test_rshift(BIO *bp, BN_CTX *ctx);
int test_div(BIO *bp, BN_CTX *ctx);
-int test_div_word(BIO *bp);
+int test_div_word(BIO *bp, BN_CTX *ctx);
int test_div_recp(BIO *bp, BN_CTX *ctx);
-int test_mul(BIO *bp);
+int test_mul(BIO *bp, BN_CTX *ctx);
int test_sqr(BIO *bp, BN_CTX *ctx);
int test_mont(BIO *bp, BN_CTX *ctx);
int test_mod(BIO *bp, BN_CTX *ctx);
int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
int test_mod_exp_sizes(BIO *bp, BN_CTX *ctx);
int test_exp(BIO *bp, BN_CTX *ctx);
-int test_gf2m_add(BIO *bp);
-int test_gf2m_mod(BIO *bp);
+int test_gf2m_add(BIO *bp, BN_CTX *ctx);
+int test_gf2m_mod(BIO *bp, BN_CTX *ctx);
int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx);
int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx);
int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx);
BIO_puts(out, "obase=16\nibase=16\n");
message(out, "BN_add");
- if (!test_add(out))
+ if (!test_add(out, ctx))
goto err;
(void)BIO_flush(out);
message(out, "BN_sub");
- if (!test_sub(out))
+ if (!test_sub(out, ctx))
goto err;
(void)BIO_flush(out);
message(out, "BN_lshift1");
- if (!test_lshift1(out))
+ if (!test_lshift1(out, ctx))
goto err;
(void)BIO_flush(out);
message(out, "BN_lshift (fixed)");
- if (!test_lshift(out, ctx, 1))
+ if (!test_lshift(out, ctx, 0))
goto err;
(void)BIO_flush(out);
message(out, "BN_lshift");
- if (!test_lshift(out, ctx, 0))
+ if (!test_lshift(out, ctx, 1))
goto err;
(void)BIO_flush(out);
message(out, "BN_rshift1");
- if (!test_rshift1(out))
+ if (!test_rshift1(out, ctx))
goto err;
(void)BIO_flush(out);
(void)BIO_flush(out);
message(out, "BN_mul");
- if (!test_mul(out))
+ if (!test_mul(out, ctx))
goto err;
(void)BIO_flush(out);
(void)BIO_flush(out);
message(out, "BN_div_word");
- if (!test_div_word(out))
+ if (!test_div_word(out, ctx))
goto err;
(void)BIO_flush(out);
#ifndef OPENSSL_NO_EC2M
message(out, "BN_GF2m_add");
- if (!test_gf2m_add(out))
+ if (!test_gf2m_add(out, ctx))
goto err;
(void)BIO_flush(out);
message(out, "BN_GF2m_mod");
- if (!test_gf2m_mod(out))
+ if (!test_gf2m_mod(out, ctx))
goto err;
(void)BIO_flush(out);
goto err;
(void)BIO_flush(out);
#endif
+
BN_CTX_free(ctx);
BIO_free(out);
}
int
-test_add(BIO *bp)
+test_add(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL;
+ BIGNUM *a, *b, *c;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_bntest_rand(a, 512, 0, 0));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
+ BN_CTX_end(ctx);
return rc;
}
int
-test_sub(BIO *bp)
+test_sub(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL;
+ BIGNUM *a, *b, *c;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
for (i = 0; i < num0 + num1; i++) {
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
+ BN_CTX_end(ctx);
return rc;
}
int
test_div(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *b, *c, *d, *e;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_one(a));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
}
int
-test_div_word(BIO *bp)
+test_div_word(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL;
+ BIGNUM *a, *b;
BN_ULONG r, rmod, s = 0;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
for (i = 0; i < num0; i++) {
rc = 1;
err:
- BN_free(a);
- BN_free(b);
+ BN_CTX_end(ctx);
return rc;
}
int
test_div_recp(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
BN_RECP_CTX *recp = NULL;
+ BIGNUM *a, *b, *c, *d, *e;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
if ((recp = BN_RECP_CTX_new()) == NULL)
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
BN_RECP_CTX_free(recp);
return rc;
}
int
-test_mul(BIO *bp)
+test_mul(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *b, *c, *d, *e;
int i;
int rc = 0;
- BN_CTX *ctx;
- ctx = BN_CTX_new();
- if (ctx == NULL)
- exit(1);
+ BN_CTX_start(ctx);
- if ((a = BN_new()) == NULL)
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
for (i = 0; i < num0 + num1; i++) {
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(e);
- BN_CTX_free(ctx);
+ BN_CTX_end(ctx);
return rc;
}
int
test_sqr(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *c, *d, *e;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
for (i = 0; i < num0; i++) {
rc = 1;
err:
- BN_free(a);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
int
test_mont(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *A = NULL, *B = NULL;
- BIGNUM *n = NULL;
+ BN_MONT_CTX *mont = NULL;
+ BIGNUM *a, *b, *c, *d, *A, *B, *n;
int i;
int rc = 0;
- BN_MONT_CTX *mont;
- mont = BN_MONT_CTX_new();
- if (mont == NULL)
- return 0;
+ BN_CTX_start(ctx);
- if ((a = BN_new()) == NULL)
+ if ((a = BN_CTX_get(ctx)) == NULL)
+ goto err;
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((A = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((A = BN_new()) == NULL)
+ if ((B = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((B = BN_new()) == NULL)
+ if ((n = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((n = BN_new()) == NULL)
+
+ if ((mont = BN_MONT_CTX_new()) == NULL)
goto err;
CHECK_GOTO(BN_zero(n));
rc = 1;
err:
+ BN_CTX_end(ctx);
BN_MONT_CTX_free(mont);
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(A);
- BN_free(B);
- BN_free(n);
return rc;
}
int
test_mod(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *b, *c, *d, *e;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_bntest_rand(a, 1024, 0, 0));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
int
test_mod_mul(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *b, *c, *d, *e;
int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_one(a));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
int
test_mod_exp(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *b, *c, *d, *e;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_one(a));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
int
test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *b, *c, *d, *e;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_one(a));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
int
test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
- BIGNUM *b = NULL, *n = NULL, *c = NULL;
+ BIGNUM *a, *p, *m, *d, *e;
+ BIGNUM *b, *n, *c;
BN_MONT_CTX *mont = NULL;
int len;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((p = BN_new()) == NULL)
+ if ((p = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((m = BN_new()) == NULL)
+ if ((m = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((n = BN_new()) == NULL)
+ if ((n = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(mont = BN_MONT_CTX_new());
rc = 1;
err:
- BN_free(a);
- BN_free(p);
- BN_free(m);
- BN_free(d);
- BN_free(e);
- BN_free(b);
- BN_free(n);
- BN_free(c);
+ BN_CTX_end(ctx);
BN_MONT_CTX_free(mont);
return rc;
int
test_exp(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *d = NULL, *e = NULL, *one = NULL;
+ BIGNUM *a, *b, *d, *e;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
- goto err;
- if ((b = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((one = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
- CHECK_GOTO(BN_one(one));
for (i = 0; i < num2; i++) {
CHECK_GOTO(BN_bntest_rand(a, 20 + i * 5, 0, 0));
BIO_puts(bp, "\n");
}
CHECK_GOTO(BN_one(e));
- for (; !BN_is_zero(b); BN_sub(b, b, one))
+ for (; !BN_is_zero(b); BN_sub_word(b, 1))
CHECK_GOTO(BN_mul(e, e, a, ctx));
CHECK_GOTO(BN_sub(e, e, d));
if (!BN_is_zero(e)) {
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(d);
- BN_free(e);
- BN_free(one);
+ BN_CTX_end(ctx);
return rc;
}
#ifndef OPENSSL_NO_EC2M
int
-test_gf2m_add(BIO *bp)
+test_gf2m_add(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL;
+ BIGNUM *a, *b, *c;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
for (i = 0; i < num0; i++) {
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
+ BN_CTX_end(ctx);
return rc;
}
int
-test_gf2m_mod(BIO *bp)
+test_gf2m_mod(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL;
- int i, j;
+ BIGNUM *a, *b[2] = { 0 }, *c, *d, *e;
int p0[] = { 163, 7, 6, 3, 0, -1 };
- int p1[] = { 193, 15, 0, -1 };
+ int p1[] = { 193, 15, 0, -1, 0 };
+ int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[0] = BN_new()) == NULL)
+ if ((b[0] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[1] = BN_new()) == NULL)
+ if ((b[1] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
rc = 1;
err:
- BN_free(a);
- BN_free(b[0]);
- BN_free(b[1]);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
int
test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
- BIGNUM *g = NULL, *h = NULL;
- int i, j;
+ BIGNUM *a, *b[2] = { 0 }, *c, *d, *e, *f, *g, *h;
int p0[] = { 163, 7, 6, 3, 0, -1 };
int p1[] = { 193, 15, 0, -1 };
+ int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[0] = BN_new()) == NULL)
+ if ((b[0] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[1] = BN_new()) == NULL)
+ if ((b[1] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((f = BN_new()) == NULL)
+ if ((f = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((g = BN_new()) == NULL)
+ if ((g = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((h = BN_new()) == NULL)
+ if ((h = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
rc = 1;
err:
- BN_free(a);
- BN_free(b[0]);
- BN_free(b[1]);
- BN_free(c);
- BN_free(d);
- BN_free(e);
- BN_free(f);
- BN_free(g);
- BN_free(h);
+ BN_CTX_end(ctx);
return rc;
}
int
test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL;
+ BIGNUM *a, *b[2] = { 0 }, *c, *d;
int p0[] = { 163, 7, 6, 3, 0, -1 };
int p1[] = { 193, 15, 0, -1 };
int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[0] = BN_new()) == NULL)
+ if ((b[0] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[1] = BN_new()) == NULL)
+ if ((b[1] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
rc = 1;
err:
- BN_free(a);
- BN_free(b[0]);
- BN_free(b[1]);
- BN_free(c);
- BN_free(d);
+ BN_CTX_end(ctx);
return rc;
}
int
test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL;
+ BIGNUM *a, *b[2] = { 0 }, *c, *d;
int p0[] = { 163, 7, 6, 3, 0, -1 };
int p1[] = { 193, 15, 0, -1 };
int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[0] = BN_new()) == NULL)
+ if ((b[0] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[1] = BN_new()) == NULL)
+ if ((b[1] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
rc = 1;
err:
- BN_free(a);
- BN_free(b[0]);
- BN_free(b[1]);
- BN_free(c);
- BN_free(d);
+ BN_CTX_end(ctx);
return rc;
}
int
test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
+ BIGNUM *a, *b[2] = { 0 }, *c, *d, *e, *f;
int p0[] = { 163, 7, 6, 3, 0, -1 };
int p1[] = { 193, 15, 0, -1 };
int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[0] = BN_new()) == NULL)
+ if ((b[0] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[1] = BN_new()) == NULL)
+ if ((b[1] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((f = BN_new()) == NULL)
+ if ((f = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
rc = 1;
err:
- BN_free(a);
- BN_free(b[0]);
- BN_free(b[1]);
- BN_free(c);
- BN_free(d);
- BN_free(e);
- BN_free(f);
+ BN_CTX_end(ctx);
return rc;
}
int
test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
+ BIGNUM *a, *b[2] = { 0 }, *c, *d, *e, *f;
int p0[] = { 163, 7, 6, 3, 0, -1 };
int p1[] = { 193, 15, 0, -1 };
int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[0] = BN_new()) == NULL)
+ if ((b[0] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[1] = BN_new()) == NULL)
+ if ((b[1] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((f = BN_new()) == NULL)
+ if ((f = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
rc = 1;
err:
- BN_free(a);
- BN_free(b[0]);
- BN_free(b[1]);
- BN_free(c);
- BN_free(d);
- BN_free(e);
- BN_free(f);
+ BN_CTX_end(ctx);
return rc;
}
int
test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL, *f = NULL;
+ BIGNUM *a, *b[2] = { 0 }, *c, *d, *e, *f;
int p0[] = { 163, 7, 6, 3, 0, -1 };
int p1[] = { 193, 15, 0, -1 };
int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[0] = BN_new()) == NULL)
+ if ((b[0] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[1] = BN_new()) == NULL)
+ if ((b[1] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((f = BN_new()) == NULL)
+ if ((f = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
rc = 1;
err:
- BN_free(a);
- BN_free(b[0]);
- BN_free(b[1]);
- BN_free(c);
- BN_free(d);
- BN_free(e);
- BN_free(f);
+ BN_CTX_end(ctx);
return rc;
}
int
test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b[2] = { 0 }, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *b[2] = { 0 }, *c, *d, *e;
int p0[] = { 163, 7, 6, 3, 0, -1 };
int p1[] = { 193, 15, 0, -1 };
int i, j, s = 0, t;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[0] = BN_new()) == NULL)
+ if ((b[0] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b[1] = BN_new()) == NULL)
+ if ((b[1] = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_GF2m_arr2poly(p0, b[0]));
rc = 1;
err:
- BN_free(a);
- BN_free(b[0]);
- BN_free(b[1]);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
#endif
+
static int
genprime_cb(int p, int n, BN_GENCB *arg)
{
int
test_kron(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
+ BIGNUM *a, *b, *r, *t;
BN_GENCB *cb = NULL;
int i;
int legendre, kronecker;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((r = BN_new()) == NULL)
+ if ((r = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((t = BN_new()) == NULL)
+ if ((t = BN_CTX_get(ctx)) == NULL)
goto err;
if ((cb = BN_GENCB_new()) == NULL)
rc = 1;
err:
BN_GENCB_free(cb);
- BN_free(a);
- BN_free(b);
- BN_free(r);
- BN_free(t);
+ BN_CTX_end(ctx);
return rc;
}
int
test_sqrt(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *p = NULL, *r = NULL;
+ BIGNUM *a, *p, *r;
BN_GENCB *cb = NULL;
int i, j;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((p = BN_new()) == NULL)
+ if ((p = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((r = BN_new()) == NULL)
+ if ((r = BN_CTX_get(ctx)) == NULL)
goto err;
if ((cb = BN_GENCB_new()) == NULL)
rc = 1;
err:
BN_GENCB_free(cb);
- BN_free(a);
- BN_free(p);
- BN_free(r);
+ BN_CTX_end(ctx);
return rc;
}
int
test_lshift(BIO *bp, BN_CTX *ctx, int use_lst)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
+ BIGNUM *a, *b, *c, *d;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_one(c));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
+ BN_CTX_end(ctx);
return rc;
}
int
-test_lshift1(BIO *bp)
+test_lshift1(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL;
+ BIGNUM *a, *b, *c;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
+ BN_CTX_end(ctx);
return rc;
}
int
test_rshift(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
+ BIGNUM *a, *b, *c, *d, *e;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((d = BN_new()) == NULL)
+ if ((d = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((e = BN_new()) == NULL)
+ if ((e = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_one(c));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_free(d);
- BN_free(e);
+ BN_CTX_end(ctx);
return rc;
}
int
-test_rshift1(BIO *bp)
+test_rshift1(BIO *bp, BN_CTX *ctx)
{
- BIGNUM *a = NULL, *b = NULL, *c = NULL;
+ BIGNUM *a, *b, *c;
int i;
int rc = 0;
- if ((a = BN_new()) == NULL)
+ BN_CTX_start(ctx);
+
+ if ((a = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((b = BN_new()) == NULL)
+ if ((b = BN_CTX_get(ctx)) == NULL)
goto err;
- if ((c = BN_new()) == NULL)
+ if ((c = BN_CTX_get(ctx)) == NULL)
goto err;
CHECK_GOTO(BN_bntest_rand(a, 200, 0, 0));
rc = 1;
err:
- BN_free(a);
- BN_free(b);
- BN_free(c);
+ BN_CTX_end(ctx);
return rc;
}
test_mod_exp_sizes(BIO *bp, BN_CTX *ctx)
{
BN_MONT_CTX *mont_ctx = NULL;
- BIGNUM *p = NULL, *x = NULL, *y = NULL, *r = NULL, *r2 = NULL;
+ BIGNUM *p, *x, *y, *r, *r2;
int size;
int rc = 0;
rc = 1;
err:
- BN_MONT_CTX_free(mont_ctx);
BN_CTX_end(ctx);
+ BN_MONT_CTX_free(mont_ctx);
return rc;
}