-/* $OpenBSD: bn_test.c,v 1.4 2023/04/07 22:14:20 tb Exp $ */
+/* $OpenBSD: bn_test.c,v 1.5 2023/04/07 22:18:42 tb Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
#define CHECK_GOTO(a) do { \
if (!(a)) { \
PRINT_ERROR; \
- rc = 0; \
goto err; \
} \
} while (0)
{
BIGNUM *a = NULL, *b = NULL, *c = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_add(c, c, a));
if (!BN_is_zero(c)) {
fprintf(stderr, "Add test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+ rc = 1;
err:
BN_free(a);
BN_free(b);
{
BIGNUM *a = NULL, *b = NULL, *c = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_bntest_rand(a, 512, 0, 0));
CHECK_GOTO(bn_copy(b, a));
if (BN_set_bit(a, i) == 0) {
- rc = 0;
- break;
+ goto err;
}
CHECK_GOTO(BN_add_word(b, i));
} else {
CHECK_GOTO(BN_sub(c, c, a));
if (!BN_is_zero(c)) {
fprintf(stderr, "Subtract test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_sub(d, d, a));
if (!BN_is_zero(d)) {
fprintf(stderr, "Division test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
- return (rc);
+
+ return rc;
}
static void
BIGNUM *a = NULL, *b = NULL;
BN_ULONG r, rmod, s = 0;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
do {
if (!BN_bntest_rand(a, 512, -1, 0) ||
!BN_bntest_rand(b, BN_BITS2, -1, 0)) {
- rc = 0;
- break;
+ goto err;
}
s = BN_get_word(b);
} while (!s);
if (!bn_copy(b, a)) {
- rc = 0;
- break;
+ goto err;
}
rmod = BN_mod_word(b, s);
r = BN_div_word(b, s);
if (r == (BN_ULONG)-1 || rmod == (BN_ULONG)-1) {
- rc = 0;
- break;
+ goto err;
}
if (rmod != r) {
fprintf(stderr, "Mod (word) test failed!\n");
- rc = 0;
- break;
+ goto err;
}
if (bp != NULL) {
CHECK_GOTO(BN_sub(b, a, b));
if (!BN_is_zero(b)) {
fprintf(stderr, "Division (word) test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
BN_RECP_CTX *recp = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
fprintf(stderr, "\nb=");
CHECK_GOTO(BN_print_fp(stderr, b));
fprintf(stderr, "\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(d);
BN_free(e);
BN_RECP_CTX_free(recp);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
int i;
- int rc = 1;
+ int rc = 0;
BN_CTX *ctx;
ctx = BN_CTX_new();
CHECK_GOTO(BN_sub(d, d, b));
if (!BN_is_zero(d) || !BN_is_zero(e)) {
fprintf(stderr, "Multiplication test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(d);
BN_free(e);
BN_CTX_free(ctx);
- return (rc);
+
+ return rc;
}
int
"different results!\n");
goto err;
}
+
rc = 1;
err:
BN_free(a);
BN_free(c);
BN_free(d);
BN_free(e);
+
return rc;
}
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *A = NULL, *B = NULL;
BIGNUM *n = NULL;
int i;
- int rc = 1;
+ int rc = 0;
BN_MONT_CTX *mont;
mont = BN_MONT_CTX_new();
CHECK_GOTO(BN_sub(d, d, A));
if (!BN_is_zero(d)) {
fprintf(stderr, "Montgomery multiplication test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_MONT_CTX_free(mont);
BN_free(a);
BN_free(A);
BN_free(B);
BN_free(n);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_sub(e, e, c));
if (!BN_is_zero(e)) {
fprintf(stderr, "Modulo test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
int i, j;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
if (!BN_is_zero(b)) {
fprintf(stderr, "Modulo multiply test failed!\n");
ERR_print_errors_fp(stderr);
- rc = 0;
goto err;
}
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_zero(c));
if (BN_mod_exp(d, a, b, c, ctx)) {
fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
- rc = 0;
goto err;
}
if (BN_mod_exp_ct(d, a, b, c, ctx)) {
fprintf(stderr, "BN_mod_exp_ct with zero modulus succeeded!\n");
- rc = 0;
goto err;
}
if (BN_mod_exp_nonct(d, a, b, c, ctx)) {
fprintf(stderr, "BN_mod_exp_nonct with zero modulus succeeded!\n");
- rc = 0;
goto err;
}
CHECK_GOTO(BN_bntest_rand(b, 2 + i, 0, 0));
if (!BN_mod_exp(d, a, b, c, ctx)) {
- rc = 0;
- break;
+ goto err;
}
if (bp != NULL) {
CHECK_GOTO(BN_div(a, b, e, c, ctx));
if (!BN_is_zero(b)) {
fprintf(stderr, "Modulo exponentiation test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
CHECK_GOTO(BN_bntest_rand(b, 2 + i, 0, 0));
if (!BN_mod_exp_ct(d, a, b, c, ctx)) {
- rc = 0;
- break;
+ goto err;
}
if (bp != NULL) {
CHECK_GOTO(BN_div(a, b, e, c, ctx));
if (!BN_is_zero(b)) {
fprintf(stderr, "Modulo exponentiation test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
CHECK_GOTO(BN_bntest_rand(b, 2 + i, 0, 0));
if (!BN_mod_exp_nonct(d, a, b, c, ctx)) {
- rc = 0;
- break;
+ goto err;
}
if (bp != NULL) {
CHECK_GOTO(BN_div(a, b, e, c, ctx));
if (!BN_is_zero(b)) {
fprintf(stderr, "Modulo exponentiation test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
fprintf(stderr, "BN_mod_exp_mont_consttime with zero modulus "
"succeeded\n");
- rc = 0;
goto err;
}
if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
fprintf(stderr, "BN_mod_exp_mont_consttime with even modulus "
"succeeded\n");
- rc = 0;
goto err;
}
CHECK_GOTO(BN_bntest_rand(b, 2 + i, 0, 0));
if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
- rc = 0;
- break;
+ goto err;
}
if (bp != NULL) {
CHECK_GOTO(BN_div(a, b, e, c, ctx));
if (!BN_is_zero(b)) {
fprintf(stderr, "Modulo exponentiation test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
- return (rc);
+
+ return rc;
}
/*
BIGNUM *b = NULL, *n = NULL, *c = NULL;
BN_MONT_CTX *mont = NULL;
int len;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_bntest_rand(a, 1024, 0, 0));
CHECK_GOTO(BN_zero(p));
if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) {
- rc = 0;
goto err;
}
if (!BN_is_one(d)) {
fprintf(stderr, "Modular exponentiation test failed!\n");
- rc = 0;
goto err;
}
/* Regression test for carry bug in mulx4x_mont */
if (BN_cmp(c, d)) {
fprintf(stderr, "Montgomery multiplication test failed:"
" a*b != b*a.\n");
- rc = 0;
goto err;
}
/* Regression test for carry bug in sqr[x]8x_mont */
if (BN_cmp(c, d)) {
fprintf(stderr, "Montgomery multiplication test failed:"
" a**2 != a*a.\n");
- rc = 0;
goto err;
}
/* Zero input */
CHECK_GOTO(BN_bntest_rand(p, 1024, 0, 0));
CHECK_GOTO(BN_zero(a));
if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)) {
- rc = 0;
goto err;
}
if (!BN_is_zero(d)) {
fprintf(stderr, "Modular exponentiation test failed!\n");
- rc = 0;
goto err;
}
/*
CHECK_GOTO(BN_one(a));
CHECK_GOTO(BN_MONT_CTX_set(mont, m, ctx));
if (!BN_from_montgomery(e, a, mont, ctx)) {
- rc = 0;
goto err;
}
if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL)) {
- rc = 0;
goto err;
}
if (!BN_mod_exp_simple(a, e, p, m, ctx)) {
- rc = 0;
goto err;
}
if (BN_cmp(a, d) != 0) {
fprintf(stderr, "Modular exponentiation test failed!\n");
- rc = 0;
goto err;
}
/* Finally, some regular test vectors. */
CHECK_GOTO(BN_bntest_rand(e, 1024, 0, 0));
if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL)) {
- rc = 0;
goto err;
}
if (!BN_mod_exp_simple(a, e, p, m, ctx)) {
- rc = 0;
goto err;
}
if (BN_cmp(a, d) != 0) {
fprintf(stderr, "Modular exponentiation test failed!\n");
- rc = 0;
goto err;
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(p);
BN_free(n);
BN_free(c);
BN_MONT_CTX_free(mont);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *d = NULL, *e = NULL, *one = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_bntest_rand(b, 2 + i, 0, 0));
if (BN_exp(d, a, b, ctx) <= 0) {
- rc = 0;
- break;
+ goto err;
}
if (bp != NULL) {
CHECK_GOTO(BN_sub(e, e, d));
if (!BN_is_zero(e)) {
fprintf(stderr, "Exponentiation test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(d);
BN_free(e);
BN_free(one);
- return (rc);
+
+ return rc;
}
#ifndef OPENSSL_NO_EC2M
goto err;
}
}
+
rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
+
return rc;
}
}
}
}
+
rc = 1;
err:
BN_free(a);
BN_free(c);
BN_free(d);
BN_free(e);
+
return rc;
}
}
}
}
+
rc = 1;
err:
BN_free(a);
BN_free(f);
BN_free(g);
BN_free(h);
+
return rc;
}
}
}
}
+
rc = 1;
err:
BN_free(a);
BN_free(b[1]);
BN_free(c);
BN_free(d);
+
return rc;
}
}
}
}
+
rc = 1;
err:
BN_free(a);
BN_free(b[1]);
BN_free(c);
BN_free(d);
+
return rc;
}
}
}
}
+
rc = 1;
err:
BN_free(a);
BN_free(d);
BN_free(e);
BN_free(f);
+
return rc;
}
}
}
}
+
rc = 1;
err:
BN_free(a);
BN_free(d);
BN_free(e);
BN_free(f);
+
return rc;
}
}
}
}
+
rc = 1;
err:
BN_free(a);
BN_free(d);
BN_free(e);
BN_free(f);
+
return rc;
}
fprintf(stderr, "this is very unlikely and probably indicates an error.\n");
goto err;
}
+
rc = 1;
err:
BN_free(a);
BN_free(c);
BN_free(d);
BN_free(e);
+
return rc;
}
#endif
if (p == 3)
c = '\n';
putc(c, stderr);
- return (1);
+ return 1;
}
int
}
putc('\n', stderr);
- rc = 1;
+ rc = 1;
err:
BN_GENCB_free(cb);
BN_free(a);
BN_free(b);
BN_free(r);
BN_free(t);
+
return rc;
}
putc('\n', stderr);
}
- rc = 1;
+ rc = 1;
err:
BN_GENCB_free(cb);
BN_free(a);
BN_free(p);
BN_free(r);
+
return rc;
}
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
fprintf(stderr, "\nd=");
CHECK_GOTO(BN_print_fp(stderr, d));
fprintf(stderr, "\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_sub(a, b, c));
if (!BN_is_zero(a)) {
fprintf(stderr, "Left shift one test failed!\n");
- rc = 0;
- break;
+ goto err;
}
CHECK_GOTO(bn_copy(a, b));
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_sub(d, d, b));
if (!BN_is_zero(d)) {
fprintf(stderr, "Right shift test failed!\n");
- rc = 0;
- break;
+ goto err;
}
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
BN_free(d);
BN_free(e);
- return (rc);
+
+ return rc;
}
int
{
BIGNUM *a = NULL, *b = NULL, *c = NULL;
int i;
- int rc = 1;
+ int rc = 0;
if ((a = BN_new()) == NULL)
goto err;
CHECK_GOTO(BN_sub(c, c, b));
if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
fprintf(stderr, "Right shift one test failed!\n");
- rc = 0;
- break;
+ goto err;
}
CHECK_GOTO(bn_copy(a, b));
}
+
+ rc = 1;
err:
BN_free(a);
BN_free(b);
BN_free(c);
- return (rc);
+
+ return rc;
}
int
}
rc = 1;
-
err:
BN_MONT_CTX_free(mont_ctx);
BN_CTX_end(ctx);
+
return rc;
}