Add a test for BN_cmp_word.

This also adds a missing OPENSSL_EXPORT.

Change-Id: I6c2400246280f68f51157e959438644976b1171b
Reviewed-on: https://boringssl-review.googlesource.com/9041
Reviewed-by: Adam Langley <agl@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
This commit is contained in:
David Benjamin 2016-07-29 12:11:34 -04:00 committed by CQ bot account: commit-bot@chromium.org
parent 4edca0b308
commit ccd511e499
2 changed files with 74 additions and 2 deletions

View File

@ -1291,6 +1291,77 @@ static bool TestSmallPrime(BN_CTX *ctx) {
return true; return true;
} }
static bool TestCmpWord() {
static const BN_ULONG kMaxWord = (BN_ULONG)-1;
ScopedBIGNUM r(BN_new());
if (!r ||
!BN_set_word(r.get(), 0)) {
return false;
}
if (BN_cmp_word(r.get(), 0) != 0 ||
BN_cmp_word(r.get(), 1) >= 0 ||
BN_cmp_word(r.get(), kMaxWord) >= 0) {
fprintf(stderr, "BN_cmp_word compared against 0 incorrectly.\n");
return false;
}
if (!BN_set_word(r.get(), 100)) {
return false;
}
if (BN_cmp_word(r.get(), 0) <= 0 ||
BN_cmp_word(r.get(), 99) <= 0 ||
BN_cmp_word(r.get(), 100) != 0 ||
BN_cmp_word(r.get(), 101) >= 0 ||
BN_cmp_word(r.get(), kMaxWord) >= 0) {
fprintf(stderr, "BN_cmp_word compared against 100 incorrectly.\n");
return false;
}
BN_set_negative(r.get(), 1);
if (BN_cmp_word(r.get(), 0) >= 0 ||
BN_cmp_word(r.get(), 100) >= 0 ||
BN_cmp_word(r.get(), kMaxWord) >= 0) {
fprintf(stderr, "BN_cmp_word compared against -100 incorrectly.\n");
return false;
}
if (!BN_set_word(r.get(), kMaxWord)) {
return false;
}
if (BN_cmp_word(r.get(), 0) <= 0 ||
BN_cmp_word(r.get(), kMaxWord - 1) <= 0 ||
BN_cmp_word(r.get(), kMaxWord) != 0) {
fprintf(stderr, "BN_cmp_word compared against kMaxWord incorrectly.\n");
return false;
}
if (!BN_add(r.get(), r.get(), BN_value_one())) {
return false;
}
if (BN_cmp_word(r.get(), 0) <= 0 ||
BN_cmp_word(r.get(), kMaxWord) <= 0) {
fprintf(stderr, "BN_cmp_word compared against kMaxWord + 1 incorrectly.\n");
return false;
}
BN_set_negative(r.get(), 1);
if (BN_cmp_word(r.get(), 0) >= 0 ||
BN_cmp_word(r.get(), kMaxWord) >= 0) {
fprintf(stderr,
"BN_cmp_word compared against -kMaxWord - 1 incorrectly.\n");
return false;
}
return true;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
CRYPTO_library_init(); CRYPTO_library_init();
@ -1314,7 +1385,8 @@ int main(int argc, char *argv[]) {
!TestNegativeZero(ctx.get()) || !TestNegativeZero(ctx.get()) ||
!TestBadModulus(ctx.get()) || !TestBadModulus(ctx.get()) ||
!TestExpModZero() || !TestExpModZero() ||
!TestSmallPrime(ctx.get())) { !TestSmallPrime(ctx.get()) ||
!TestCmpWord()) {
return 1; return 1;
} }

View File

@ -438,7 +438,7 @@ OPENSSL_EXPORT int BN_cmp(const BIGNUM *a, const BIGNUM *b);
/* BN_cmp_word is like |BN_cmp| except it takes its second argument as a /* BN_cmp_word is like |BN_cmp| except it takes its second argument as a
* |BN_ULONG| instead of a |BIGNUM|. */ * |BN_ULONG| instead of a |BIGNUM|. */
int BN_cmp_word(const BIGNUM *a, BN_ULONG b); OPENSSL_EXPORT int BN_cmp_word(const BIGNUM *a, BN_ULONG b);
/* BN_ucmp returns a value less than, equal to or greater than zero if the /* BN_ucmp returns a value less than, equal to or greater than zero if the
* absolute value of |a| is less than, equal to or greater than the absolute * absolute value of |a| is less than, equal to or greater than the absolute