Align RSA key generation with FIPS 186-4.
FIPS prescribes a slightly different key generation algorithm than we
use. Specifically:
- Rather than using BN_RAND_TOP_TWO (so using 1.5 as an upper bound for
sqrt(2)), it prescribes using sqrt(2) itself. To avoid unnecessary
squaring, we do a comparison against a hard-coded approximation for
sqrt(2) good enough for the largest FIPS key size. I went ahead and
made it constant-time since it was easy, but all this is far from
constant-time.
- FIPS requires a check that |p-q| is sufficiently large.
- FIPS requires a check that d is sufficiently large.
- BN_generate_prime_ex adds some delta to clear a table of prime
numbers. FIPS does not specify any of that, so implement a separate
routine here.
The primality test itself will be aligned in a follow-up. For now, it is
left unchanged, except that trial division is turned back on. That makes
things faster and is analogous the original algorithm's delta-munging
logic.
Change-Id: If32f0635bfb67a8c4740dedd7781d00647bbf60b
Reviewed-on: https://boringssl-review.googlesource.com/14948
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/rsa/internal.h b/crypto/rsa/internal.h
index faa1373..589fd3a 100644
--- a/crypto/rsa/internal.h
+++ b/crypto/rsa/internal.h
@@ -59,6 +59,8 @@
#include <openssl/base.h>
+#include <openssl/bn.h>
+
#if defined(__cplusplus)
extern "C" {
@@ -135,6 +137,20 @@
void RSA_additional_prime_free(RSA_additional_prime *ap);
+/* The following utility functions are exported for test purposes. */
+
+extern const BN_ULONG kBoringSSLRSASqrtTwo[];
+extern const size_t kBoringSSLRSASqrtTwoLen;
+
+/* rsa_less_than_words returns one if |a| < |b| and zero otherwise, where |a|
+ * and |b| both are |len| words long. It runs in constant time. */
+int rsa_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len);
+
+/* rsa_greater_than_pow2 returns one if |b| is greater than 2^|n| and zero
+ * otherwise. */
+int rsa_greater_than_pow2(const BIGNUM *b, int n);
+
+
#if defined(__cplusplus)
} /* extern C */
#endif
diff --git a/crypto/rsa/rsa_impl.c b/crypto/rsa/rsa_impl.c
index 325ccc7..650f6f3 100644
--- a/crypto/rsa/rsa_impl.c
+++ b/crypto/rsa/rsa_impl.c
@@ -57,12 +57,14 @@
#include <openssl/rsa.h>
#include <assert.h>
+#include <limits.h>
#include <string.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/mem.h>
#include <openssl/thread.h>
+#include <openssl/type_check.h>
#include "internal.h"
#include "../bn/internal.h"
@@ -770,7 +772,171 @@
return *out != NULL;
}
+/* kBoringSSLRSASqrtTwo is the BIGNUM representation of ⌊2¹⁵³⁵×√2⌋. This is
+ * chosen to give enough precision for 3072-bit RSA, the largest key size FIPS
+ * specifies. Key sizes beyond this will round up.
+ *
+ * To verify this number, check that n² < 2³⁰⁷¹ < (n+1)², where n is value
+ * represented here. Note the components are listed in little-endian order. Here
+ * is some sample Python code to check:
+ *
+ * >>> TOBN = lambda a, b: a << 32 | b
+ * >>> l = [ <paste the contents of kSqrtTwo> ]
+ * >>> n = sum(a * 2**(64*i) for i, a in enumerate(l))
+ * >>> n**2 < 2**3071 < (n+1)**2
+ * True
+ */
+const BN_ULONG kBoringSSLRSASqrtTwo[] = {
+ TOBN(0xdea06241, 0xf7aa81c2), TOBN(0xf6a1be3f, 0xca221307),
+ TOBN(0x332a5e9f, 0x7bda1ebf), TOBN(0x0104dc01, 0xfe32352f),
+ TOBN(0xb8cf341b, 0x6f8236c7), TOBN(0x4264dabc, 0xd528b651),
+ TOBN(0xf4d3a02c, 0xebc93e0c), TOBN(0x81394ab6, 0xd8fd0efd),
+ TOBN(0xeaa4a089, 0x9040ca4a), TOBN(0xf52f120f, 0x836e582e),
+ TOBN(0xcb2a6343, 0x31f3c84d), TOBN(0xc6d5a8a3, 0x8bb7e9dc),
+ TOBN(0x460abc72, 0x2f7c4e33), TOBN(0xcab1bc91, 0x1688458a),
+ TOBN(0x53059c60, 0x11bc337b), TOBN(0xd2202e87, 0x42af1f4e),
+ TOBN(0x78048736, 0x3dfa2768), TOBN(0x0f74a85e, 0x439c7b4a),
+ TOBN(0xa8b1fe6f, 0xdc83db39), TOBN(0x4afc8304, 0x3ab8a2c3),
+ TOBN(0xed17ac85, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c),
+ TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484),
+};
+const size_t kBoringSSLRSASqrtTwoLen = OPENSSL_ARRAY_SIZE(kBoringSSLRSASqrtTwo);
+
+int rsa_less_than_words(const BN_ULONG *a, const BN_ULONG *b, size_t len) {
+ OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(size_t),
+ size_t_constant_time_functions_too_small);
+ int ret = 0;
+ /* Process the words in little-endian order. */
+ for (size_t i = 0; i < len; i++) {
+ size_t eq = constant_time_eq_s(a[i], b[i]);
+ size_t lt = constant_time_lt_s(a[i], b[i]);
+ ret = constant_time_select_int(eq, ret, constant_time_select_int(lt, 1, 0));
+ }
+ return ret;
+}
+
+int rsa_greater_than_pow2(const BIGNUM *b, int n) {
+ if (BN_is_negative(b) || n == INT_MAX) {
+ return 0;
+ }
+
+ int b_bits = BN_num_bits(b);
+ return b_bits > n + 1 || (b_bits == n + 1 && !BN_is_pow2(b));
+}
+
+/* generate_prime sets |out| to a prime with length |bits| such that |out|-1 is
+ * relatively prime to |e|. If |p| is non-NULL, |out| will also not be close to
+ * |p|. */
+static int generate_prime(BIGNUM *out, int bits, const BIGNUM *e,
+ const BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb) {
+ if (bits < 128 || (bits % BN_BITS2) != 0) {
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
+ return 0;
+ }
+
+ /* Ensure the bound on |tries| does not overflow. */
+ if (bits >= INT_MAX/5) {
+ OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
+ return 0;
+ }
+
+ int ret = 0, tries = 0, rand_tries = 0;
+ BN_CTX_start(ctx);
+ BIGNUM *tmp = BN_CTX_get(ctx);
+ if (tmp == NULL) {
+ goto err;
+ }
+
+ /* See FIPS 186-4 appendix B.3.3, steps 4 and 5. Note |bits| here is
+ * nlen/2. */
+ for (;;) {
+ /* Generate a random number of length |bits| where the bottom bit is set
+ * (steps 4.2, 4.3, 5.2 and 5.3) and the top bit is set (implied by the
+ * bound checked below in steps 4.4 and 5.5). */
+ if (!BN_rand(out, bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD) ||
+ !BN_GENCB_call(cb, BN_GENCB_GENERATED, rand_tries++)) {
+ goto err;
+ }
+
+ if (p != NULL) {
+ /* If |p| and |out| are too close, try again (step 5.4). */
+ if (!BN_sub(tmp, out, p)) {
+ goto err;
+ }
+ BN_set_negative(tmp, 0);
+ if (!rsa_greater_than_pow2(tmp, bits - 100)) {
+ continue;
+ }
+ }
+
+ /* If out < 2^(bits-1)×√2, try again (steps 4.4 and 5.5).
+ *
+ * We check the most significant words, so we retry if ⌊out/2^k⌋ <= ⌊b/2^k⌋,
+ * where b = 2^(bits-1)×√2 and k = max(0, bits - 1536). For key sizes up to
+ * 3072 (bits = 1536), k = 0, so we are testing that ⌊out⌋ <= ⌊b⌋. out is an
+ * integer and b is not, so this is equivalent to out < b. That is, the
+ * comparison is exact for FIPS key sizes.
+ *
+ * For larger keys, the comparison is approximate, leaning towards
+ * retrying. That is, we reject a negligible fraction of primes that are
+ * within the FIPS bound, but we will never accept a prime outside the
+ * bound, ensuring the resulting RSA key is the right size. Specifically, if
+ * the FIPS bound holds, we have ⌊out/2^k⌋ < out/2^k < b/2^k. This implies
+ * ⌊out/2^k⌋ <= ⌊b/2^k⌋. That is, the FIPS bound implies our bound and so we
+ * are slightly tighter. */
+ size_t out_len = (size_t)out->top;
+ assert(out_len == (size_t)bits / BN_BITS2);
+ size_t to_check = kBoringSSLRSASqrtTwoLen;
+ if (to_check > out_len) {
+ to_check = out_len;
+ }
+ if (!rsa_less_than_words(
+ kBoringSSLRSASqrtTwo + kBoringSSLRSASqrtTwoLen - to_check,
+ out->d + out_len - to_check, to_check)) {
+ continue;
+ }
+
+ /* Check gcd(out-1, e) is one (steps 4.5 and 5.6). */
+ if (!BN_sub(tmp, out, BN_value_one()) ||
+ !BN_gcd(tmp, tmp, e, ctx)) {
+ goto err;
+ }
+ if (BN_is_one(tmp)) {
+ /* Test |out| for primality (steps 4.5.1 and 5.6.1).
+ * TODO(davidben): Align the primality test with FIPS 186-4. */
+ int is_probable_prime;
+ if (!BN_primality_test(&is_probable_prime, out, BN_prime_checks, ctx, 1,
+ cb)) {
+ goto err;
+ }
+ if (is_probable_prime) {
+ ret = 1;
+ goto err;
+ }
+ }
+
+ /* If we've tried too many times to find a prime, abort (steps 4.7 and
+ * 5.8). */
+ tries++;
+ if (tries >= bits * 5) {
+ OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_MANY_ITERATIONS);
+ goto err;
+ }
+ if (!BN_GENCB_call(cb, 2, tries)) {
+ goto err;
+ }
+ }
+
+err:
+ BN_CTX_end(ctx);
+ return ret;
+}
+
int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
+ /* See FIPS 186-4 appendix B.3. This function implements a generalized version
+ * of the FIPS algorithm. For FIPS compliance, the caller is responsible for
+ * passing in 2048 or 3072 to |bits| and 65537 for |e_value|. */
+
/* Always generate RSA keys which are a multiple of 128 bits. Round |bits|
* down as needed. */
bits &= ~127;
@@ -781,24 +947,21 @@
return 0;
}
- BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
- int ok = -1, n = 0;
- BN_CTX *ctx = NULL;
-
- ctx = BN_CTX_new();
+ int ret = 0;
+ BN_CTX *ctx = BN_CTX_new();
if (ctx == NULL) {
- goto err;
+ goto bn_err;
}
BN_CTX_start(ctx);
- r0 = BN_CTX_get(ctx);
- r1 = BN_CTX_get(ctx);
- r2 = BN_CTX_get(ctx);
- r3 = BN_CTX_get(ctx);
+ BIGNUM *r0 = BN_CTX_get(ctx);
+ BIGNUM *r1 = BN_CTX_get(ctx);
+ BIGNUM *r2 = BN_CTX_get(ctx);
+ BIGNUM *r3 = BN_CTX_get(ctx);
if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
- goto err;
+ goto bn_err;
}
- /* We need the RSA components non-NULL */
+ /* We need the RSA components non-NULL. */
if (!ensure_bignum(&rsa->n) ||
!ensure_bignum(&rsa->d) ||
!ensure_bignum(&rsa->e) ||
@@ -807,91 +970,55 @@
!ensure_bignum(&rsa->dmp1) ||
!ensure_bignum(&rsa->dmq1) ||
!ensure_bignum(&rsa->iqmp)) {
- goto err;
+ goto bn_err;
}
if (!BN_copy(rsa->e, e_value)) {
- goto err;
+ goto bn_err;
}
- /* generate p and q */
- int p_bits = (bits + 1) / 2;
- int q_bits = bits - p_bits;
- for (;;) {
- if (!BN_generate_prime_ex(rsa->p, p_bits, 0, NULL, NULL, cb) ||
- !BN_sub(r2, rsa->p, BN_value_one()) ||
- !BN_gcd(r1, r2, rsa->e, ctx)) {
- goto err;
+ int prime_bits = bits / 2;
+ do {
+ /* Generate p and q, each of size |prime_bits|, using the steps outlined in
+ * appendix FIPS 186-4 appendix B.3.3. */
+ if (!generate_prime(rsa->p, prime_bits, rsa->e, NULL, ctx, cb) ||
+ !BN_GENCB_call(cb, 3, 0) ||
+ !generate_prime(rsa->q, prime_bits, rsa->e, rsa->p, ctx, cb) ||
+ !BN_GENCB_call(cb, 3, 1)) {
+ goto bn_err;
}
- if (BN_is_one(r1)) {
- break;
+
+ if (BN_cmp(rsa->p, rsa->q) < 0) {
+ BIGNUM *tmp = rsa->p;
+ rsa->p = rsa->q;
+ rsa->q = tmp;
}
- if (!BN_GENCB_call(cb, 2, n++)) {
- goto err;
+
+ /* Calculate d. */
+ if (!BN_sub(r1 /* p-1 */, rsa->p, BN_value_one()) ||
+ !BN_sub(r2 /* q-1 */, rsa->q, BN_value_one()) ||
+ !BN_mul(r0 /* (p-1)(q-1) */, r1, r2, ctx) ||
+ !BN_mod_inverse(rsa->d, rsa->e, r0, ctx)) {
+ goto bn_err;
}
- }
- if (!BN_GENCB_call(cb, 3, 0)) {
- goto err;
- }
- for (;;) {
- /* When generating ridiculously small keys, we can get stuck
- * continually regenerating the same prime values. Check for
- * this and bail if it happens 3 times. */
- unsigned int degenerate = 0;
- do {
- if (!BN_generate_prime_ex(rsa->q, q_bits, 0, NULL, NULL, cb)) {
- goto err;
- }
- } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
- if (degenerate == 3) {
- ok = 0; /* we set our own err */
- OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
- goto err;
- }
- if (!BN_sub(r2, rsa->q, BN_value_one()) ||
- !BN_gcd(r1, r2, rsa->e, ctx)) {
- goto err;
- }
- if (BN_is_one(r1)) {
- break;
- }
- if (!BN_GENCB_call(cb, 2, n++)) {
- goto err;
- }
+
+ /* Check that |rsa->d| > 2^|prime_bits| and try again if it fails. See
+ * appendix B.3.1's guidance on values for d. */
+ } while (!rsa_greater_than_pow2(rsa->d, prime_bits));
+
+ if (/* Calculate n. */
+ !BN_mul(rsa->n, rsa->p, rsa->q, ctx) ||
+ /* Calculate d mod (p-1). */
+ !BN_mod(rsa->dmp1, rsa->d, r1, ctx) ||
+ /* Calculate d mod (q-1) */
+ !BN_mod(rsa->dmq1, rsa->d, r2, ctx)) {
+ goto bn_err;
}
- if (!BN_GENCB_call(cb, 3, 1) ||
- !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
- goto err;
- }
-
- if (BN_cmp(rsa->p, rsa->q) < 0) {
- tmp = rsa->p;
- rsa->p = rsa->q;
- rsa->q = tmp;
- }
-
- /* calculate d */
- if (!BN_sub(r1, rsa->p, BN_value_one())) {
- goto err; /* p-1 */
- }
- if (!BN_sub(r2, rsa->q, BN_value_one())) {
- goto err; /* q-1 */
- }
- if (!BN_mul(r0, r1, r2, ctx)) {
- goto err; /* (p-1)(q-1) */
- }
- if (!BN_mod_inverse(rsa->d, rsa->e, r0, ctx)) {
- goto err; /* d */
- }
-
- /* calculate d mod (p-1) */
- if (!BN_mod(rsa->dmp1, rsa->d, r1, ctx)) {
- goto err;
- }
-
- /* calculate d mod (q-1) */
- if (!BN_mod(rsa->dmq1, rsa->d, r2, ctx)) {
+ /* Sanity-check that |rsa->n| has the specified size. This is implied by
+ * |generate_prime|'s bounds. */
+ if (BN_num_bits(rsa->n) != (unsigned)bits) {
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
goto err;
}
@@ -902,7 +1029,7 @@
if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, rsa->p, ctx) ||
!bn_mod_inverse_secret_prime(rsa->iqmp, rsa->q, rsa->p, ctx,
rsa->mont_p)) {
- goto err;
+ goto bn_err;
}
sk_RSA_additional_prime_pop_free(rsa->additional_primes,
@@ -912,21 +1039,23 @@
/* The key generation process is complex and thus error-prone. It could be
* disastrous to generate and then use a bad key so double-check that the key
* makes sense. */
- ok = RSA_check_key(rsa);
- if (!ok) {
+ if (!RSA_check_key(rsa)) {
OPENSSL_PUT_ERROR(RSA, RSA_R_INTERNAL_ERROR);
+ goto err;
}
-err:
- if (ok == -1) {
+ ret = 1;
+
+bn_err:
+ if (!ret) {
OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
- ok = 0;
}
+err:
if (ctx != NULL) {
BN_CTX_end(ctx);
BN_CTX_free(ctx);
}
- return ok;
+ return ret;
}
/* All of the methods are NULL to make it easier for the compiler/linker to drop
diff --git a/crypto/rsa/rsa_test.cc b/crypto/rsa/rsa_test.cc
index 7f4197b..e84a727 100644
--- a/crypto/rsa/rsa_test.cc
+++ b/crypto/rsa/rsa_test.cc
@@ -67,8 +67,10 @@
#include <openssl/err.h>
#include <openssl/nid.h>
+#include "../bn/internal.h"
#include "../internal.h"
#include "../test/test_util.h"
+#include "internal.h"
// kPlaintext is a sample plaintext.
@@ -846,3 +848,135 @@
EXPECT_TRUE(RSA_generate_key_ex(rsa.get(), 1152, e.get(), nullptr));
EXPECT_EQ(1152u, BN_num_bits(rsa->n));
}
+
+#if !defined(BORINGSSL_SHARED_LIBRARY)
+TEST(RSATest, SqrtTwo) {
+ bssl::UniquePtr<BIGNUM> sqrt(BN_new()), pow2(BN_new());
+ bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
+ ASSERT_TRUE(sqrt);
+ ASSERT_TRUE(pow2);
+ ASSERT_TRUE(ctx);
+
+ size_t bits = kBoringSSLRSASqrtTwoLen * BN_BITS2;
+ ASSERT_TRUE(BN_one(pow2.get()));
+ ASSERT_TRUE(BN_lshift(pow2.get(), pow2.get(), 2 * bits - 1));
+
+ // Check that sqrt² < pow2.
+ ASSERT_TRUE(
+ bn_set_words(sqrt.get(), kBoringSSLRSASqrtTwo, kBoringSSLRSASqrtTwoLen));
+ ASSERT_TRUE(BN_sqr(sqrt.get(), sqrt.get(), ctx.get()));
+ EXPECT_LT(BN_cmp(sqrt.get(), pow2.get()), 0);
+
+ // Check that pow2 < (sqrt + 1)².
+ ASSERT_TRUE(
+ bn_set_words(sqrt.get(), kBoringSSLRSASqrtTwo, kBoringSSLRSASqrtTwoLen));
+ ASSERT_TRUE(BN_add_word(sqrt.get(), 1));
+ ASSERT_TRUE(BN_sqr(sqrt.get(), sqrt.get(), ctx.get()));
+ EXPECT_LT(BN_cmp(pow2.get(), sqrt.get()), 0);
+
+ // Check the kBoringSSLRSASqrtTwo is sized for a 3072-bit RSA key.
+ EXPECT_EQ(3072u / 2u, bits);
+}
+
+TEST(RSATest, LessThanWords) {
+ // kTestVectors is an array of 256-bit values in sorted order.
+ static const BN_ULONG kTestVectors[][256 / BN_BITS2] = {
+ {TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000),
+ TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
+ {TOBN(0x00000000, 0x00000001), TOBN(0x00000000, 0x00000000),
+ TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
+ {TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0x00000000),
+ TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
+ {TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff),
+ TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
+ {TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff),
+ TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0x00000000)},
+ {TOBN(0x00000000, 0x00000000), TOBN(0x1d6f60ba, 0x893ba84c),
+ TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
+ {TOBN(0x00000000, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c),
+ TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
+ {TOBN(0xed17ac85, 0x00000000), TOBN(0x1d6f60ba, 0x893ba84c),
+ TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
+ {TOBN(0xed17ac85, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c),
+ TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
+ {TOBN(0xed17ac85, 0xffffffff), TOBN(0x1d6f60ba, 0x893ba84c),
+ TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
+ {TOBN(0xffffffff, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c),
+ TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
+ {TOBN(0xffffffff, 0xffffffff), TOBN(0x1d6f60ba, 0x893ba84c),
+ TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
+ {TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000),
+ TOBN(0x00000000, 0x00000000), TOBN(0xffffffff, 0xffffffff)},
+ {TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000),
+ TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff)},
+ {TOBN(0x00000000, 0x00000001), TOBN(0x00000000, 0x00000000),
+ TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff)},
+ {TOBN(0x00000000, 0x00000000), TOBN(0xffffffff, 0xffffffff),
+ TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff)},
+ {TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff),
+ TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff)},
+ };
+
+ for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTestVectors); i++) {
+ SCOPED_TRACE(i);
+ for (size_t j = 0; j < OPENSSL_ARRAY_SIZE(kTestVectors); j++) {
+ SCOPED_TRACE(j);
+ EXPECT_EQ(i < j ? 1 : 0,
+ rsa_less_than_words(kTestVectors[i], kTestVectors[j],
+ OPENSSL_ARRAY_SIZE(kTestVectors[i])));
+ }
+ }
+
+ EXPECT_EQ(0, rsa_less_than_words(NULL, NULL, 0));
+}
+
+TEST(RSATest, GreaterThanPow2) {
+ bssl::UniquePtr<BIGNUM> b(BN_new());
+ BN_zero(b.get());
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 0));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 1));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 20));
+
+ ASSERT_TRUE(BN_set_word(b.get(), 1));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 0));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 1));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 20));
+
+ ASSERT_TRUE(BN_set_word(b.get(), 2));
+ EXPECT_TRUE(rsa_greater_than_pow2(b.get(), 0));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 1));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 20));
+
+ ASSERT_TRUE(BN_set_word(b.get(), 3));
+ EXPECT_TRUE(rsa_greater_than_pow2(b.get(), 0));
+ EXPECT_TRUE(rsa_greater_than_pow2(b.get(), 1));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 2));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 20));
+
+ BN_set_negative(b.get(), 1);
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 0));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 1));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 2));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), 20));
+
+ // Check all bit lengths mod 64.
+ for (int n = 1024; n < 1024 + 64; n++) {
+ SCOPED_TRACE(n);
+ ASSERT_TRUE(BN_set_word(b.get(), 1));
+ ASSERT_TRUE(BN_lshift(b.get(), b.get(), n));
+ EXPECT_TRUE(rsa_greater_than_pow2(b.get(), n - 1));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), n));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), n + 1));
+
+ ASSERT_TRUE(BN_sub_word(b.get(), 1));
+ EXPECT_TRUE(rsa_greater_than_pow2(b.get(), n - 1));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), n));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), n + 1));
+
+ ASSERT_TRUE(BN_add_word(b.get(), 2));
+ EXPECT_TRUE(rsa_greater_than_pow2(b.get(), n - 1));
+ EXPECT_TRUE(rsa_greater_than_pow2(b.get(), n));
+ EXPECT_FALSE(rsa_greater_than_pow2(b.get(), n + 1));
+ }
+}
+#endif // !BORINGSSL_SHARED_LIBRARY