@@ -1138,7 +1138,7 @@ static bool TestNegativeZero(BN_CTX *ctx) {
return false;
return false;
}
}
if (!BN_is_zero(c.get()) || BN_is_negative(c.get())) {
if (!BN_is_zero(c.get()) || BN_is_negative(c.get())) {
fprintf(stderr, "Multiplication test failed! \n");
fprintf(stderr, "Multiplication test failed. \n");
return false;
return false;
}
}
@@ -1152,7 +1152,7 @@ static bool TestNegativeZero(BN_CTX *ctx) {
return false;
return false;
}
}
if (!BN_is_zero(d.get()) || BN_is_negative(d.get())) {
if (!BN_is_zero(d.get()) || BN_is_negative(d.get())) {
fprintf(stderr, "Division test failed! \n");
fprintf(stderr, "Division test failed. \n");
return false;
return false;
}
}
@@ -1164,7 +1164,27 @@ static bool TestNegativeZero(BN_CTX *ctx) {
return false;
return false;
}
}
if (!BN_is_zero(c.get()) || BN_is_negative(c.get())) {
if (!BN_is_zero(c.get()) || BN_is_negative(c.get())) {
fprintf(stderr, "Division test failed!\n");
fprintf(stderr, "Division test failed.\n");
return false;
}
// Test that BN_set_negative will not produce a negative zero.
BN_zero(a.get());
BN_set_negative(a.get(), 1);
if (BN_is_negative(a.get())) {
fprintf(stderr, "BN_set_negative produced a negative zero.\n");
return false;
}
// Test that forcibly creating a negative zero does not break |BN_bn2hex| or
// |BN_bn2dec|.
a->neg = 1;
ScopedOpenSSLString dec(BN_bn2dec(a.get()));
ScopedOpenSSLString hex(BN_bn2hex(a.get()));
if (!dec || !hex ||
strcmp(dec.get(), "-0") != 0 ||
strcmp(hex.get(), "-0") != 0) {
fprintf(stderr, "BN_bn2dec or BN_bn2hex failed with negative zero.\n");
return false;
return false;
}
}
@@ -1183,39 +1203,43 @@ static bool TestBadModulus(BN_CTX *ctx) {
BN_zero(zero.get());
BN_zero(zero.get());
if (BN_div(a.get(), b.get(), BN_value_one(), zero.get(), ctx)) {
if (BN_div(a.get(), b.get(), BN_value_one(), zero.get(), ctx)) {
fprintf(stderr, "Division by zero succeeded! \n");
fprintf(stderr, "Division by zero unexpectedly succeeded. \n");
return false;
return false;
}
}
ERR_clear_error();
ERR_clear_error();
if (BN_mod_mul(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx)) {
if (BN_mod_mul(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx)) {
fprintf(stderr, "BN_mod_mul with zero modulus succeeded! \n");
fprintf(stderr, "BN_mod_mul with zero modulus unexpectedly succeeded. \n");
return false;
return false;
}
}
ERR_clear_error();
ERR_clear_error();
if (BN_mod_exp(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx)) {
if (BN_mod_exp(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx)) {
fprintf(stderr, "BN_mod_exp with zero modulus succeeded! \n");
fprintf(stderr, "BN_mod_exp with zero modulus unexpectedly succeeded. \n");
return 0;
return 0;
}
}
ERR_clear_error();
ERR_clear_error();
if (BN_mod_exp_mont(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx,
if (BN_mod_exp_mont(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx,
NULL)) {
NULL)) {
fprintf(stderr, "BN_mod_exp_mont with zero modulus succeeded!\n");
fprintf(stderr,
"BN_mod_exp_mont with zero modulus unexpectedly succeeded.\n");
return 0;
return 0;
}
}
ERR_clear_error();
ERR_clear_error();
if (BN_mod_exp_mont_consttime(a.get(), BN_value_one(), BN_value_one(),
if (BN_mod_exp_mont_consttime(a.get(), BN_value_one(), BN_value_one(),
zero.get(), ctx, nullptr)) {
zero.get(), ctx, nullptr)) {
fprintf(stderr, "BN_mod_exp_mont_consttime with zero modulus succeeded!\n");
fprintf(stderr,
"BN_mod_exp_mont_consttime with zero modulus unexpectedly "
"succeeded.\n");
return 0;
return 0;
}
}
ERR_clear_error();
ERR_clear_error();
if (BN_MONT_CTX_set(mont.get(), zero.get(), ctx)) {
if (BN_MONT_CTX_set(mont.get(), zero.get(), ctx)) {
fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
fprintf(stderr,
"BN_MONT_CTX_set unexpectedly succeeded for zero modulus.\n");
return false;
return false;
}
}
ERR_clear_error();
ERR_clear_error();
@@ -1227,21 +1251,25 @@ static bool TestBadModulus(BN_CTX *ctx) {
}
}
if (BN_MONT_CTX_set(mont.get(), b.get(), ctx)) {
if (BN_MONT_CTX_set(mont.get(), b.get(), ctx)) {
fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n");
fprintf(stderr,
"BN_MONT_CTX_set unexpectedly succeeded for even modulus.\n");
return false;
return false;
}
}
ERR_clear_error();
ERR_clear_error();
if (BN_mod_exp_mont(a.get(), BN_value_one(), BN_value_one(), b.get(), ctx,
if (BN_mod_exp_mont(a.get(), BN_value_one(), BN_value_one(), b.get(), ctx,
NULL)) {
NULL)) {
fprintf(stderr, "BN_mod_exp_mont with even modulus succeeded!\n");
fprintf(stderr,
"BN_mod_exp_mont with even modulus unexpectedly succeeded.\n");
return 0;
return 0;
}
}
ERR_clear_error();
ERR_clear_error();
if (BN_mod_exp_mont_consttime(a.get(), BN_value_one(), BN_value_one(),
if (BN_mod_exp_mont_consttime(a.get(), BN_value_one(), BN_value_one(),
b.get(), ctx, nullptr)) {
b.get(), ctx, nullptr)) {
fprintf(stderr, "BN_mod_exp_mont_consttime with even modulus succeeded!\n");
fprintf(stderr,
"BN_mod_exp_mont_consttime with even modulus unexpectedly "
"succeeded.\n");
return 0;
return 0;
}
}
ERR_clear_error();
ERR_clear_error();