From 28a8c2fe25abba681aae35259eb7d5181baa2252 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Sat, 2 Jul 2016 20:48:45 -0400 Subject: Fold the rest of test_sqrt into TestSquare. BUG=31 Change-Id: Ief7bda365c3d786f946caaba0ab2af03c50459c3 Reviewed-on: https://boringssl-review.googlesource.com/8609 Reviewed-by: David Benjamin --- crypto/bn/bn_test.cc | 77 +++++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 52 deletions(-) (limited to 'crypto') diff --git a/crypto/bn/bn_test.cc b/crypto/bn/bn_test.cc index 196fed6f..bdd5ff00 100644 --- a/crypto/bn/bn_test.cc +++ b/crypto/bn/bn_test.cc @@ -105,7 +105,6 @@ static bool test_mod_exp_mont_consttime(FILE *fp, BN_CTX *ctx); static bool test_exp(FILE *fp, BN_CTX *ctx); static bool test_mod_sqrt(FILE *fp, BN_CTX *ctx); static bool test_mod_exp_mont5(FILE *fp, BN_CTX *ctx); -static bool test_sqrt(FILE *fp, BN_CTX *ctx); static bool TestBN2BinPadded(BN_CTX *ctx); static bool TestDec2BN(BN_CTX *ctx); static bool TestHex2BN(BN_CTX *ctx); @@ -200,12 +199,6 @@ int main(int argc, char *argv[]) { } flush_fp(bc_file.get()); - message(bc_file.get(), "BN_sqrt"); - if (!test_sqrt(bc_file.get(), ctx.get())) { - return 1; - } - flush_fp(bc_file.get()); - if (!TestBN2BinPadded(ctx.get()) || !TestDec2BN(ctx.get()) || !TestHex2BN(ctx.get()) || @@ -484,6 +477,31 @@ static bool TestSquare(FileTest *t, BN_CTX *ctx) { return false; } + // BN_sqrt should fail on non-squares and negative numbers. + if (!BN_is_zero(square.get())) { + ScopedBIGNUM tmp(BN_new()); + if (!tmp || !BN_copy(tmp.get(), square.get())) { + return false; + } + BN_set_negative(tmp.get(), 1); + + if (BN_sqrt(ret.get(), tmp.get(), ctx)) { + t->PrintLine("BN_sqrt succeeded on a negative number"); + return false; + } + ERR_clear_error(); + + BN_set_negative(tmp.get(), 0); + if (!BN_add(tmp.get(), tmp.get(), BN_value_one())) { + return false; + } + if (BN_sqrt(ret.get(), tmp.get(), ctx)) { + t->PrintLine("BN_sqrt succeeded on a non-square"); + return false; + } + ERR_clear_error(); + } + return true; } @@ -931,51 +949,6 @@ static bool test_mod_sqrt(FILE *fp, BN_CTX *ctx) { return true; } -static bool test_sqrt(FILE *fp, BN_CTX *ctx) { - ScopedBIGNUM n(BN_new()); - ScopedBIGNUM nn(BN_new()); - ScopedBIGNUM sqrt(BN_new()); - if (!n || !nn || !sqrt) { - return false; - } - - // Test some random squares. - for (int i = 0; i < 100; i++) { - if (!BN_rand(n.get(), 1024 /* bit length */, - -1 /* no modification of top bits */, - 0 /* don't modify bottom bit */) || - !BN_mul(nn.get(), n.get(), n.get(), ctx) || - !BN_sqrt(sqrt.get(), nn.get(), ctx)) { - ERR_print_errors_fp(stderr); - return false; - } - if (BN_cmp(n.get(), sqrt.get()) != 0) { - fprintf(stderr, "Bad result from BN_sqrt.\n"); - return false; - } - } - - // Test some non-squares. - for (int i = 0; i < 100; i++) { - if (!BN_rand(n.get(), 1024 /* bit length */, - -1 /* no modification of top bits */, - 0 /* don't modify bottom bit */) || - !BN_mul(nn.get(), n.get(), n.get(), ctx) || - !BN_add(nn.get(), nn.get(), BN_value_one())) { - ERR_print_errors_fp(stderr); - return false; - } - - if (BN_sqrt(sqrt.get(), nn.get(), ctx)) { - char *nn_str = BN_bn2dec(nn.get()); - fprintf(stderr, "BIO_sqrt didn't fail on a non-square: %s\n", nn_str); - OPENSSL_free(nn_str); - } - } - - return true; -} - static bool TestBN2BinPadded(BN_CTX *ctx) { uint8_t zeros[256], out[256], reference[128]; -- cgit v1.2.3