Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/boringssl.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorDavid Benjamin <davidben@google.com>2016-07-03 03:48:45 +0300
committerDavid Benjamin <davidben@google.com>2016-07-07 02:12:37 +0300
commit28a8c2fe25abba681aae35259eb7d5181baa2252 (patch)
tree20b9405489e6f3c1f71bac042744b2460f4d0f64 /crypto
parent5a13e40ab6d8e0320f7257374fbefc05488156e4 (diff)
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 <davidben@google.com>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/bn/bn_test.cc77
1 files changed, 25 insertions, 52 deletions
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];