Blame SOURCES/openssl-1.0.2k-cve-2018-0737.patch

aa8173
diff --git a/crypto/rsa/rsa_gen.c b/crypto/rsa/rsa_gen.c
aa8173
index c7f1dc3..aa8a7c0 100644
aa8173
--- a/crypto/rsa/rsa_gen.c
aa8173
+++ b/crypto/rsa/rsa_gen.c
aa8173
@@ -177,6 +177,17 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value,
aa8173
     BIGNUM *pr0, *d, *p;
aa8173
     int bitsp, bitsq, ok = -1, n = 0;
aa8173
     BN_CTX *ctx = NULL;
aa8173
+    unsigned long error = 0;
aa8173
+
aa8173
+    /*
aa8173
+     * When generating ridiculously small keys, we can get stuck
aa8173
+     * continually regenerating the same prime values.
aa8173
+     */
aa8173
+    if (bits < 16) {
aa8173
+        ok = 0;             /* we set our own err */
aa8173
+        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);
aa8173
+        goto err;
aa8173
+    }
aa8173
 
aa8173
 #ifdef OPENSSL_FIPS
aa8173
     if (FIPS_module_mode()) {
aa8173
@@ -233,45 +244,55 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value,
aa8173
     if (BN_copy(rsa->e, e_value) == NULL)
aa8173
         goto err;
aa8173
 
aa8173
+    BN_set_flags(rsa->p, BN_FLG_CONSTTIME);
aa8173
+    BN_set_flags(rsa->q, BN_FLG_CONSTTIME);
aa8173
+    BN_set_flags(r2, BN_FLG_CONSTTIME);
aa8173
     /* generate p and q */
aa8173
     for (;;) {
aa8173
         if (!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb))
aa8173
             goto err;
aa8173
         if (!BN_sub(r2, rsa->p, BN_value_one()))
aa8173
             goto err;
aa8173
-        if (!BN_gcd(r1, r2, rsa->e, ctx))
aa8173
-            goto err;
aa8173
-        if (BN_is_one(r1))
aa8173
+        ERR_set_mark();
aa8173
+        if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {
aa8173
+            /* GCD == 1 since inverse exists */
aa8173
             break;
aa8173
+        }
aa8173
+        error = ERR_peek_last_error();
aa8173
+        if (ERR_GET_LIB(error) == ERR_LIB_BN
aa8173
+            && ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
aa8173
+            /* GCD != 1 */
aa8173
+            ERR_pop_to_mark();
aa8173
+        } else {
aa8173
+            goto err;
aa8173
+        }
aa8173
         if (!BN_GENCB_call(cb, 2, n++))
aa8173
             goto err;
aa8173
     }
aa8173
     if (!BN_GENCB_call(cb, 3, 0))
aa8173
         goto err;
aa8173
     for (;;) {
aa8173
-        /*
aa8173
-         * When generating ridiculously small keys, we can get stuck
aa8173
-         * continually regenerating the same prime values. Check for this and
aa8173
-         * bail if it happens 3 times.
aa8173
-         */
aa8173
-        unsigned int degenerate = 0;
aa8173
         do {
aa8173
             if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))
aa8173
                 goto err;
aa8173
             if (!BN_sub(r2, rsa->q, rsa->p))
aa8173
                 goto err;
aa8173
-        } while ((BN_ucmp(r2, r3) <= 0) && (++degenerate < 3));
aa8173
-        if (degenerate == 3) {
aa8173
-            ok = 0;             /* we set our own err */
aa8173
-            RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);
aa8173
-            goto err;
aa8173
-        }
aa8173
+        } while (BN_ucmp(r2, r3) <= 0);
aa8173
         if (!BN_sub(r2, rsa->q, BN_value_one()))
aa8173
             goto err;
aa8173
-        if (!BN_gcd(r1, r2, rsa->e, ctx))
aa8173
-            goto err;
aa8173
-        if (BN_is_one(r1))
aa8173
+        ERR_set_mark();
aa8173
+        if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {
aa8173
+            /* GCD == 1 since inverse exists */
aa8173
             break;
aa8173
+        }
aa8173
+        error = ERR_peek_last_error();
aa8173
+        if (ERR_GET_LIB(error) == ERR_LIB_BN
aa8173
+            && ERR_GET_REASON(error) == BN_R_NO_INVERSE) {
aa8173
+            /* GCD != 1 */
aa8173
+            ERR_pop_to_mark();
aa8173
+        } else {
aa8173
+            goto err;
aa8173
+        }
aa8173
         if (!BN_GENCB_call(cb, 2, n++))
aa8173
             goto err;
aa8173
     }