diff options
author | David Benjamin <davidben@google.com> | 2016-08-24 06:53:12 +0300 |
---|---|---|
committer | CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org> | 2016-09-18 23:12:25 +0300 |
commit | 40a63113e4b1d1878bbbec74fbd8d1684ce79db0 (patch) | |
tree | bd82749ffc4121406828f8bace62f6955207a789 /crypto | |
parent | c446ce52944cb2fd9073a9072fc6e151af16919b (diff) |
Add BN_set_u64.
Android currently implements this manually (see NativeBN_putULongInt) by
reaching into BIGNUM's internals. BN_ULONG is a somewhat unfortunate API
anyway as the size is platform-dependent, so add a platform-independent
way to do this.
The other things Android needs are going to need more work, but this
one's easy.
BUG=97
Change-Id: I4af4dc29f9845bdce0f0663c379b4b5d3e1dc46e
Reviewed-on: https://boringssl-review.googlesource.com/11088
Commit-Queue: David Benjamin <davidben@google.com>
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/bn/bn.c | 22 | ||||
-rw-r--r-- | crypto/bn/bn_test.cc | 30 |
2 files changed, 51 insertions, 1 deletions
diff --git a/crypto/bn/bn.c b/crypto/bn/bn.c index 496266fb..87d81d26 100644 --- a/crypto/bn/bn.c +++ b/crypto/bn/bn.c @@ -266,6 +266,28 @@ int BN_set_word(BIGNUM *bn, BN_ULONG value) { return 1; } +int BN_set_u64(BIGNUM *bn, uint64_t value) { +#if BN_BITS2 == 64 + return BN_set_word(bn, value); +#elif BN_BITS2 == 32 + if (value <= BN_MASK2) { + return BN_set_word(bn, (BN_ULONG)value); + } + + if (bn_wexpand(bn, 2) == NULL) { + return 0; + } + + bn->neg = 0; + bn->d[0] = (BN_ULONG)value; + bn->d[1] = (BN_ULONG)(value >> 32); + bn->top = 2; + return 1; +#else +#error "BN_BITS2 must be 32 or 64." +#endif +} + int bn_set_words(BIGNUM *bn, const BN_ULONG *words, size_t num) { if (bn_wexpand(bn, num) == NULL) { return 0; diff --git a/crypto/bn/bn_test.cc b/crypto/bn/bn_test.cc index 0867dec2..67a5a319 100644 --- a/crypto/bn/bn_test.cc +++ b/crypto/bn/bn_test.cc @@ -1437,6 +1437,33 @@ static bool TestBN2Dec() { return true; } +static bool TestBNSetU64() { + static const struct { + const char *hex; + uint64_t value; + } kU64Tests[] = { + {"0", UINT64_C(0x0)}, + {"1", UINT64_C(0x1)}, + {"ffffffff", UINT64_C(0xffffffff)}, + {"100000000", UINT64_C(0x100000000)}, + {"ffffffffffffffff", UINT64_C(0xffffffffffffffff)}, + }; + + for (const auto& test : kU64Tests) { + bssl::UniquePtr<BIGNUM> bn(BN_new()), expected; + if (!bn || + !BN_set_u64(bn.get(), test.value) || + !HexToBIGNUM(&expected, test.hex) || + BN_cmp(bn.get(), expected.get()) != 0) { + fprintf(stderr, "BN_set_u64 test failed for 0x%s.\n", test.hex); + ERR_print_errors_fp(stderr); + return false; + } + } + + return true; +} + int main(int argc, char *argv[]) { CRYPTO_library_init(); @@ -1462,7 +1489,8 @@ int main(int argc, char *argv[]) { !TestExpModZero() || !TestSmallPrime(ctx.get()) || !TestCmpWord() || - !TestBN2Dec()) { + !TestBN2Dec() || + !TestBNSetU64()) { return 1; } |